From ffd314a245632a85ba5f310e8d86937091886d4d Mon Sep 17 00:00:00 2001 From: nathansmith Date: Tue, 29 Apr 2025 08:04:51 -0600 Subject: Slow adhd work --- src/ffmpeg.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 4 deletions(-) (limited to 'src/ffmpeg.c') 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 +#include +#include +#include + +#include + #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; } -- cgit v1.2.3