summaryrefslogtreecommitdiff
path: root/soundbox/pipewire-binding.c
diff options
context:
space:
mode:
authorxegineering <me@xegineering.eu>2024-12-11 20:03:29 +0100
committerxegineering <me@xegineering.eu>2024-12-15 12:36:51 +0100
commitca5ebc8a795114a72f4410f05b2270f24ead1d60 (patch)
tree5ca901ebc8365909e4fd2ec11a4cf1ff114f149d /soundbox/pipewire-binding.c
parent62d3045df3283872628b0b8b8a8caeef1c226dfa (diff)
downloadsoundbox-go-ca5ebc8a795114a72f4410f05b2270f24ead1d60.tar
soundbox-go-ca5ebc8a795114a72f4410f05b2270f24ead1d60.tar.zst
soundbox-go-ca5ebc8a795114a72f4410f05b2270f24ead1d60.zip
pipewire: Implement capture tear-down
This closes the PipeWire process properly to not leak memory and remove the PipeWire capture node of soundbox as soon as the context passed to StreamPipewireContext() is closed.
Diffstat (limited to 'soundbox/pipewire-binding.c')
-rw-r--r--soundbox/pipewire-binding.c48
1 files changed, 34 insertions, 14 deletions
diff --git a/soundbox/pipewire-binding.c b/soundbox/pipewire-binding.c
index bd23ff0..0a31843 100644
--- a/soundbox/pipewire-binding.c
+++ b/soundbox/pipewire-binding.c
@@ -2,6 +2,7 @@
#include <spa/param/audio/format-utils.h>
#include "pipewire-binding.h"
+#include "pipewire/main-loop.h"
#define SAMPLING_RATE 48000
@@ -11,15 +12,21 @@
#define STRIDE sizeof(int16_t) * CHANNELS
+struct pw_go_capture {
+ struct pw_main_loop *loop;
+ struct pw_stream *stream;
+};
+
+
static void on_process(void *userdata)
{
- struct pw_stream *stream = *(struct pw_stream **)userdata;
+ struct pw_go_capture *capture = (struct pw_go_capture *)userdata;
struct pw_buffer *pw_buf;
struct spa_buffer *spa_buf;
int n_frames;
int16_t *src;
- if ((pw_buf = pw_stream_dequeue_buffer(stream)) == NULL) {
+ if ((pw_buf = pw_stream_dequeue_buffer(capture->stream)) == NULL) {
return;
}
@@ -41,7 +48,7 @@ static void on_process(void *userdata)
spa_buf->datas[0].chunk->stride = STRIDE;
spa_buf->datas[0].chunk->size = n_frames * STRIDE;
- pw_stream_queue_buffer(stream, pw_buf);
+ pw_stream_queue_buffer(capture->stream, pw_buf);
}
@@ -51,15 +58,14 @@ static const struct pw_stream_events stream_events = {
};
-void pw_stdout(void)
+void *pw_go_capture_init(void)
{
pw_init(NULL, NULL);
- struct pw_main_loop *loop = pw_main_loop_new(NULL);
-
- struct pw_stream *stream = NULL;
- stream = pw_stream_new_simple(
- pw_main_loop_get_loop(loop),
+ struct pw_go_capture *capture = malloc(sizeof(struct pw_go_capture));
+ capture->loop = pw_main_loop_new(NULL);
+ capture->stream = pw_stream_new_simple(
+ pw_main_loop_get_loop(capture->loop),
NODE_NAME,
pw_properties_new(
PW_KEY_MEDIA_TYPE, "Audio",
@@ -69,7 +75,7 @@ void pw_stdout(void)
NULL
),
&stream_events,
- &stream
+ capture
);
uint8_t buffer[1024];
@@ -87,7 +93,7 @@ void pw_stdout(void)
};
pw_stream_connect(
- stream,
+ capture->stream,
PW_DIRECTION_INPUT,
PW_ID_ANY,
PW_STREAM_FLAG_MAP_BUFFERS |
@@ -96,9 +102,23 @@ void pw_stdout(void)
sizeof(params) / sizeof(params[0])
);
- pw_main_loop_run(loop);
+ return (void *)capture;
+}
+
- pw_stream_destroy(stream);
- pw_main_loop_destroy(loop);
+void pw_go_capture_run(void *cdata)
+{
+ struct pw_go_capture *capture = cdata;
+ pw_main_loop_run(capture->loop);
+ pw_stream_destroy(capture->stream);
+ pw_main_loop_destroy(capture->loop);
+ free(capture);
pw_deinit();
}
+
+
+void pw_go_capture_deinit(void *cdata)
+{
+ struct pw_go_capture *capture = cdata;
+ pw_main_loop_quit(capture->loop);
+}