aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
parent6d338602aa98cc9dfab39b83702af0e548286596 (diff)
downloadsldj-ffd314a245632a85ba5f310e8d86937091886d4d.tar.gz
sldj-ffd314a245632a85ba5f310e8d86937091886d4d.tar.bz2
sldj-ffd314a245632a85ba5f310e8d86937091886d4d.zip
Slow adhd work
Diffstat (limited to 'src')
-rw-r--r--src/Makefile4
-rw-r--r--src/ffmpeg.c70
-rw-r--r--src/ffmpeg.h13
-rw-r--r--src/libsldj/Makefile4
4 files changed, 75 insertions, 16 deletions
diff --git a/src/Makefile b/src/Makefile
index 4d2ca1f..10581e7 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,4 +1,4 @@
-CFLAGS = -Isrc -I../include -std=c99
+CFLAGS = -Isrc -I../include -std=gnu99
LDFLAGS = -lm -lraylib -ltcc
TARGET = sldj
@@ -12,6 +12,8 @@ $(TARGET): $(OBJECTS) $(LIBSLDJ)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(wildcard "libsldj/*.o") $(LDFLAGS)
$(LIBSLDJ): $(wildcard libsldj/*.c libsldj/*.h)
@$(MAKE) -C libsldj
+
+.PHONY: clean
clean:
rm *.o
rm $(TARGET)
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;
}
diff --git a/src/ffmpeg.h b/src/ffmpeg.h
index 71277a5..ea1f3c6 100644
--- a/src/ffmpeg.h
+++ b/src/ffmpeg.h
@@ -1,19 +1,12 @@
#include <assert.h>
-#include <stdio.h>
#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <raylib.h>
-#include "sldjConfig.h"
-
// stolen code: https://github.com/tsoding/musializer/blob/master/src/ffmpeg_posix.c
#ifndef FFMPEG_H
@@ -27,9 +20,9 @@ typedef struct FFMPEG {
pid_t pid;
} FFMPEG;
-FFMPEG* ffmpegStart(const char* filename, int width, int height, int fps);
-int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, int widtth, int height);
-int ffmpegEnd(FFMPEG* ffmpeg);
+FFMPEG* ffmpegStart(const char* filename, uint32_t width, uint32_t height, uint32_t fps);
+int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, uint32_t width, uint32_t height);
+int ffmpegEnd(FFMPEG* ffmpeg, bool cancel);
#endif
diff --git a/src/libsldj/Makefile b/src/libsldj/Makefile
index afaf181..a9a05de 100644
--- a/src/libsldj/Makefile
+++ b/src/libsldj/Makefile
@@ -1,4 +1,4 @@
-CFLAGS = -std=c99
+CFLAGS = -std=gnu99
TARGET = libsldj.so
SOURCES = $(shell find -name "*.c")
OBJECTS = $(SOURCES:.c=.o)
@@ -7,6 +7,8 @@ OBJECTS = $(SOURCES:.c=.o)
$(CC) $(CFLAGS) -c -fPIC -o $@ $<
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) -shared -o $(TARGET) $(OBJECTS)
+
+.PHONY: clean
clean:
rm *.o
rm $(TARGET)