From 0312887452e25c75124f38126c2bd704d88caaa9 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Thu, 20 May 2021 10:09:57 +0200 Subject: mpegtsmux: Fixup program array indices after stream removal Each stream stores the `program_array_index` of its position in its program's `streams` array. When we remove a stream from this array, we need to correct the `program_array_index` of all streams that were backshifted by the removal. Also extract the removal into a new function and add some more safety checks. Part-of: --- gst/mpegtsmux/tsmux/tsmux.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'gst/mpegtsmux') diff --git a/gst/mpegtsmux/tsmux/tsmux.c b/gst/mpegtsmux/tsmux/tsmux.c index 566111bf8..632f9f8f4 100644 --- a/gst/mpegtsmux/tsmux/tsmux.c +++ b/gst/mpegtsmux/tsmux/tsmux.c @@ -757,6 +757,31 @@ tsmux_find_stream (TsMux * mux, guint16 pid) return found; } +static gboolean +tsmux_program_remove_stream (TsMuxProgram * program, TsMuxStream * stream) +{ + GArray *streams = program->streams; + TsMuxStream *s; + gint i; + + i = stream->program_array_index; + g_return_val_if_fail (i >= 0, FALSE); + + s = g_array_index (streams, TsMuxStream *, i); + g_return_val_if_fail (s == stream, FALSE); + + g_array_remove_index (streams, i); + + /* Correct indices of remaining streams, if any */ + for (; i < streams->len; i++) { + s = g_array_index (streams, TsMuxStream *, i); + s->program_array_index -= 1; + } + + return streams->len == 0; +} + + gboolean tsmux_remove_stream (TsMux * mux, guint16 pid, TsMuxProgram * program) { @@ -769,20 +794,16 @@ tsmux_remove_stream (TsMux * mux, guint16 pid, TsMuxProgram * program) TsMuxStream *stream = (TsMuxStream *) cur->data; if (tsmux_stream_get_pid (stream) == pid) { - if (program->streams->len == 1) { - tsmux_program_delete (mux, program); - ret = TRUE; - } else { - program->streams = - g_array_remove_index (program->streams, - stream->program_array_index); - } - + ret = tsmux_program_remove_stream (program, stream); mux->streams = g_list_remove (mux->streams, stream); tsmux_stream_free (stream); - return ret; + break; } } + + if (ret) + tsmux_program_delete (mux, program); + return ret; } -- cgit v1.2.1