From 8d86a8b75d678eabb4a2124b8473b17674d9e298 Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Wed, 14 Oct 2015 17:38:39 +0200 Subject: [PATCH] hlsdemux: Avoid negative sequence numbers 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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ext/hls/gsthlsdemux.c b/ext/hls/gsthlsdemux.c index 91178e2379..1c05fa860a 100644 --- a/ext/hls/gsthlsdemux.c +++ b/ext/hls/gsthlsdemux.c @@ -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)) {