aboutsummaryrefslogtreecommitdiffstats
path: root/src/ffmpeg.c
diff options
context:
space:
mode:
authornathansmith <nathansmith@posteo.com>2025-04-29 14:04:51 +0000
committernathansmith <nathansmith@posteo.com>2025-04-29 14:04:51 +0000
commitffd314a245632a85ba5f310e8d86937091886d4d (patch)
tree2816cb5158e1e3d414cdce9f9273f426a9410f63 /src/ffmpeg.c
parent6d338602aa98cc9dfab39b83702af0e548286596 (diff)
downloadsldj-ffd314a245632a85ba5f310e8d86937091886d4d.tar.gz
sldj-ffd314a245632a85ba5f310e8d86937091886d4d.tar.bz2
sldj-ffd314a245632a85ba5f310e8d86937091886d4d.zip
Slow adhd work
Diffstat (limited to 'src/ffmpeg.c')
-rw-r--r--src/ffmpeg.c70
1 files changed, 66 insertions, 4 deletions
diff --git a/src/ffmpeg.c b/src/ffmpeg.c
index bbc94f7..37d5185 100644
--- a/src/ffmpeg.c
+++ b/src/ffmpeg.c
@@ -1,6 +1,13 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <signal.h>
+
#include "ffmpeg.h"
-FFMPEG* ffmpegStart(const char* filename, int width, int height, int fps)
+FFMPEG* ffmpegStart(const char* filename, uint32_t width, uint32_t height, uint32_t fps)
{
int pipefd[2];
@@ -67,20 +74,75 @@ FFMPEG* ffmpegStart(const char* filename, int width, int height, int fps)
TraceLog(LOG_WARNING, "FFMPEG: could not close read end of the pipe on the parent's end: %s", strerror(errno));
}
- FFMPEG *ffmpeg = malloc(sizeof(FFMPEG));
+ FFMPEG* ffmpeg = malloc(sizeof(FFMPEG));
assert(ffmpeg != NULL && "Buy MORE RAM lol!!");
ffmpeg->pid = child;
ffmpeg->pipe = pipefd[WRITE_END];
return ffmpeg;
}
-int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, int widtth, int height)
+int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, uint32_t width, uint32_t height)
{
+ for (uint32_t y = height; y > 0; --y)
+ {
+ if (write(ffmpeg->pipe, (uint32_t*)data + (y - 1) * width, sizeof(uint32_t) * width) < 0)
+ {
+ TraceLog(LOG_ERROR, "FFMPEG: failed to write into ffmpeg pipe: %s", strerror(errno));
+ return -1;
+ }
+ }
+
return 0;
}
-int ffmpegEnd(FFMPEG* ffmpeg)
+int ffmpegEnd(FFMPEG* ffmpeg, bool cancel)
{
+ int pipe = ffmpeg->pipe;
+ pid_t pid = ffmpeg->pid;
+
+ free(ffmpeg);
+
+ if (close(pipe) < 0)
+ {
+ TraceLog(LOG_WARNING, "FFMPEG: could not close write end of the pipe on the parent's end: %s", strerror(errno));
+ }
+
+ if (cancel)
+ {
+ kill(pid, SIGKILL);
+ }
+
+ while (true)
+ {
+ int wstatus = 0;
+
+ if (waitpid(pid, &wstatus, 0) < 0)
+ {
+ TraceLog(LOG_ERROR, "FFMPEG: could not wait for ffmpeg child process to finish: %s", strerror(errno));
+ return -1;
+ }
+
+ if (WIFEXITED(wstatus))
+ {
+ int exit_status = WEXITSTATUS(wstatus);
+
+ if (exit_status != 0)
+ {
+ TraceLog(LOG_ERROR, "FFMPEG: ffmpeg exited with code %d", exit_status);
+ return -1;
+ }
+
+ return 0;
+ }
+
+ if (WIFSIGNALED(wstatus))
+ {
+ TraceLog(LOG_ERROR, "FFMPEG: ffmpeg got terminated by %s", strsignal(WTERMSIG(wstatus)));
+ return -1;
+ }
+ }
+
+ assert(0 && "unreachable");
return 0;
}