From 83116fc19984bbed9da0200e341d140626fee3fd Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 16 May 2021 15:32:31 +0200 Subject: [PATCH] Notify end-of-stream via callback Do not send a SDL event directly, to make stream independent of SDL. --- app/src/scrcpy.c | 15 ++++++++++++++- app/src/stream.c | 19 +++++++++---------- app/src/stream.h | 11 +++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index e847037e..1cb66bc1 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -241,6 +241,16 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) { free(local_fmt); } +static void +stream_on_eos(struct stream *stream, void *userdata) { + (void) stream; + (void) userdata; + + SDL_Event stop_event; + stop_event.type = EVENT_STREAM_STOPPED; + SDL_PushEvent(&stop_event); +} + bool scrcpy(const struct scrcpy_options *options) { if (!server_init(&server)) { @@ -343,7 +353,10 @@ scrcpy(const struct scrcpy_options *options) { av_log_set_callback(av_log_callback); - stream_init(&stream, server.video_socket); + const struct stream_callbacks stream_cbs = { + .on_eos = stream_on_eos, + }; + stream_init(&stream, server.video_socket, &stream_cbs, NULL); if (dec) { stream_add_sink(&stream, &dec->packet_sink); diff --git a/app/src/stream.c b/app/src/stream.c index a11218e3..2d9a0ab4 100644 --- a/app/src/stream.c +++ b/app/src/stream.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include "decoder.h" @@ -58,13 +57,6 @@ stream_recv_packet(struct stream *stream, AVPacket *packet) { return true; } -static void -notify_stopped(void) { - SDL_Event stop_event; - stop_event.type = EVENT_STREAM_STOPPED; - SDL_PushEvent(&stop_event); -} - static bool push_packet_to_sinks(struct stream *stream, const AVPacket *packet) { for (unsigned i = 0; i < stream->sink_count; ++i) { @@ -251,15 +243,22 @@ finally_close_sinks: finally_free_codec_ctx: avcodec_free_context(&stream->codec_ctx); end: - notify_stopped(); + stream->cbs->on_eos(stream, stream->cbs_userdata); + return 0; } void -stream_init(struct stream *stream, socket_t socket) { +stream_init(struct stream *stream, socket_t socket, + const struct stream_callbacks *cbs, void *cbs_userdata) { stream->socket = socket; stream->has_pending = false; stream->sink_count = 0; + + assert(cbs && cbs->on_eos); + + stream->cbs = cbs; + stream->cbs_userdata = cbs_userdata; } void diff --git a/app/src/stream.h b/app/src/stream.h index 81175420..9fc4d1e1 100644 --- a/app/src/stream.h +++ b/app/src/stream.h @@ -6,7 +6,6 @@ #include #include #include -#include #include "trait/packet_sink.h" #include "util/net.h" @@ -27,10 +26,18 @@ struct stream { // packet is available bool has_pending; AVPacket pending; + + const struct stream_callbacks *cbs; + void *cbs_userdata; +}; + +struct stream_callbacks { + void (*on_eos)(struct stream *stream, void *userdata); }; void -stream_init(struct stream *stream, socket_t socket); +stream_init(struct stream *stream, socket_t socket, + const struct stream_callbacks *cbs, void *cbs_userdata); void stream_add_sink(struct stream *stream, struct sc_packet_sink *sink);