From 33cd2495b87e7120a545bbd15e7e604d5ca3abd2 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 4 Jun 2020 10:10:08 +0200 Subject: [PATCH] examples: add signal handler for clean exit --- src/examples/video-src-alloc.c | 16 +++++++++++++--- src/examples/video-src.c | 11 ++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/examples/video-src-alloc.c b/src/examples/video-src-alloc.c index 77014bbc7..223e739d2 100644 --- a/src/examples/video-src-alloc.c +++ b/src/examples/video-src-alloc.c @@ -24,14 +24,13 @@ #include #include -#include +#include #include #include #include #include #include -#include #include @@ -349,6 +348,12 @@ static const struct pw_stream_events stream_events = { .remove_buffer = on_stream_remove_buffer, }; +static void do_quit(void *userdata, int signal_number) +{ + struct data *data = userdata; + pw_main_loop_quit(data->loop); +} + int main(int argc, char *argv[]) { struct data data = { 0, }; @@ -361,6 +366,10 @@ int main(int argc, char *argv[]) /* create a main loop */ data.loop = pw_main_loop_new(NULL); + /* install some handlers to exit nicely */ + pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGINT, do_quit, &data); + pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data); + /* create a simple stream, the simple stream manages the core * object for you if you don't want to deal with them. * @@ -409,7 +418,7 @@ int main(int argc, char *argv[]) * the server. */ pw_stream_connect(data.stream, PW_DIRECTION_OUTPUT, - PW_ID_ANY, + PW_ID_ANY, /* link to any node */ PW_STREAM_FLAG_DRIVER | PW_STREAM_FLAG_ALLOC_BUFFERS, params, 1); @@ -419,6 +428,7 @@ int main(int argc, char *argv[]) pw_stream_destroy(data.stream); pw_main_loop_destroy(data.loop); + pw_deinit(); return 0; } diff --git a/src/examples/video-src.c b/src/examples/video-src.c index 922c84ad7..f9aa2fe6a 100644 --- a/src/examples/video-src.c +++ b/src/examples/video-src.c @@ -61,6 +61,7 @@ struct data { double crop; double accumulator; + int res; }; static void draw_elipse(uint32_t *dst, int width, int height, uint32_t color) @@ -296,8 +297,11 @@ int main(int argc, char *argv[]) data.timer = pw_loop_add_timer(pw_main_loop_get_loop(data.loop), on_timeout, &data); data.core = pw_context_connect(data.context, NULL, 0); - if (data.core == NULL) - return -1; + if (data.core == NULL) { + fprintf(stderr, "can't connect: %m\n"); + data.res = -errno; + goto cleanup; + } data.stream = pw_stream_new(data.core, "video-src", pw_properties_new( @@ -329,9 +333,10 @@ int main(int argc, char *argv[]) pw_main_loop_run(data.loop); +cleanup: pw_context_destroy(data.context); pw_main_loop_destroy(data.loop); pw_deinit(); - return 0; + return data.res; }