hlsdemux2: Handle negative time mappings
authorEdward Hervey <edward@centricular.com>
Thu, 15 Sep 2022 07:04:10 +0000 (09:04 +0200)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Tue, 20 Sep 2022 14:34:34 +0000 (14:34 +0000)
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: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3054>

subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c

index 9995ccd..eebafdc 100644 (file)
@@ -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);
 }