From fdfa70997b162c9bafe4aecb0ebd2456b1c67320 Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Thu, 15 Sep 2022 09:04:10 +0200 Subject: [PATCH] hlsdemux2: Handle negative time mappings Some servers can return playlists with "old" media playlists and different Discont Sequence. In those cases, the segment stream times would be negative when creating a new time mapping. In order to properly handle such scenarios, shift the values to stored accordingly to end up with non-negative reference stream time. Part-of: --- .../ext/adaptivedemux2/hls/gsthlsdemux.c | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c b/subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c index 9995ccd49a..eebafdcded 100644 --- a/subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c +++ b/subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c @@ -1948,8 +1948,7 @@ gst_hls_demux_add_time_mapping (GstHLSDemux * demux, gint64 dsn, #endif GstHLSTimeMap *map; GList *tmp; - - g_assert (stream_time >= 0); + GstClockTime offset = 0; /* Check if we don't already have a mapping for the given dsn */ for (tmp = demux->mappings; tmp; tmp = tmp->next) { @@ -1979,11 +1978,28 @@ gst_hls_demux_add_time_mapping (GstHLSDemux * demux, gint64 dsn, g_free (datestring); #endif + if (stream_time < 0) { + offset = -stream_time; + stream_time = 0; + /* Handle negative stream times. This can happen for example when the server + * returns an older playlist. + * + * Shift the values accordingly to end up with non-negative reference stream + * time */ + GST_DEBUG_OBJECT (demux, + "Shifting values before storage (offset : %" GST_TIME_FORMAT ")", + GST_TIME_ARGS (offset)); + } + map = gst_hls_time_map_new (); map->dsn = dsn; map->stream_time = stream_time; - if (pdt) - map->pdt = g_date_time_ref (pdt); + if (pdt) { + if (offset) + map->pdt = g_date_time_add (pdt, offset / GST_USECOND); + else + map->pdt = g_date_time_ref (pdt); + } demux->mappings = g_list_append (demux->mappings, map); }