From 3b4366e5bfa062d80df9ad455b68c98f7f7482c9 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 30 May 2019 11:57:40 +0200 Subject: [PATCH] Stop stream immediately on quit If the stream is stopped, av_read_frame() will be woken up and yield a corrupted packet. Do not try to decode or record it. --- app/src/stream.c | 9 +++++++++ app/src/stream.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/app/src/stream.c b/app/src/stream.c index 0e751eb3..9a946375 100644 --- a/app/src/stream.c +++ b/app/src/stream.c @@ -207,6 +207,13 @@ run_stream(void *data) { packet.size = 0; while (!av_read_frame(format_ctx, &packet)) { + if (SDL_AtomicGet(&stream->stopped)) { + // if the stream is stopped, the socket had been shutdown, so the + // last packet is probably corrupted (but not detected as such by + // FFmpeg) and will not be decoded correctly + av_packet_unref(&packet); + goto quit; + } if (stream->decoder && !decoder_push(stream->decoder, &packet)) { av_packet_unref(&packet); goto quit; @@ -259,6 +266,7 @@ stream_init(struct stream *stream, socket_t socket, stream->socket = socket; stream->decoder = decoder, stream->recorder = recorder; + SDL_AtomicSet(&stream->stopped, 0); } bool @@ -275,6 +283,7 @@ stream_start(struct stream *stream) { void stream_stop(struct stream *stream) { + SDL_AtomicSet(&stream->stopped, 1); if (stream->decoder) { decoder_interrupt(stream->decoder); } diff --git a/app/src/stream.h b/app/src/stream.h index d5eda0ac..1ebff1a0 100644 --- a/app/src/stream.h +++ b/app/src/stream.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "net.h" @@ -18,6 +19,7 @@ struct stream { socket_t socket; struct video_buffer *video_buffer; SDL_Thread *thread; + SDL_atomic_t stopped; struct decoder *decoder; struct recorder *recorder; struct receiver_state {