hlsdemux: Avoid negative sequence numbers
authorEdward Hervey <edward@centricular.com>
Wed, 14 Oct 2015 15:38:39 +0000 (17:38 +0200)
committerEdward Hervey <bilboed@bilboed.com>
Thu, 15 Oct 2015 08:34:19 +0000 (10:34 +0200)
For live streams, we want to make sure there's a certain distance
between the sequence to play and the last (earliest) fragment.

The problem is that it assumes there are at least 3 fragments in
the playlist, which might not always be the case (like in the case
of a server restarting and gradually adding fragments).

In order to avoid ending up with negative sequence numbers (which
will just loop forever), limit the new target sequence number to
the highest of:
* either the first sequence number of the playlist (fallback)
* or 3 fragments from the last one (standard behaviour)

ext/hls/gsthlsdemux.c

index 91178e2379e52e21fd55344e35e5b0be464bd2b1..1c05fa860af04df7373fa03708ee2262c481fa53 100644 (file)
@@ -955,18 +955,26 @@ retry:
    * three fragments before the end of the list */
   if (update == FALSE && demux->client->current &&
       gst_m3u8_client_is_live (demux->client)) {
-    gint64 last_sequence;
+    gint64 last_sequence, first_sequence;
 
     GST_M3U8_CLIENT_LOCK (demux->client);
     last_sequence =
         GST_M3U8_MEDIA_FILE (g_list_last (demux->client->current->
             files)->data)->sequence;
+    first_sequence =
+        GST_M3U8_MEDIA_FILE (demux->client->current->files->data)->sequence;
 
+    GST_DEBUG_OBJECT (demux,
+        "sequence:%" G_GINT64_FORMAT " , first_sequence:%" G_GINT64_FORMAT
+        " , last_sequence:%" G_GINT64_FORMAT, demux->client->sequence,
+        first_sequence, last_sequence);
     if (demux->client->sequence >= last_sequence - 3) {
-      GST_DEBUG_OBJECT (demux, "Sequence is beyond playlist. Moving back to %u",
-          (guint) (last_sequence - 3));
       //demux->need_segment = TRUE;
-      demux->client->sequence = last_sequence - 3;
+      /* Make sure we never go below the minimum sequence number */
+      demux->client->sequence = MAX (first_sequence, last_sequence - 3);
+      GST_DEBUG_OBJECT (demux,
+          "Sequence is beyond playlist. Moving back to %" G_GINT64_FORMAT,
+          demux->client->sequence);
     }
     GST_M3U8_CLIENT_UNLOCK (demux->client);
   } else if (demux->client->current && !gst_m3u8_client_is_live (demux->client)) {