From 7ddd67baefbd8a8e1a974c3da18cf01a6eb1bcc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 9 Mar 2014 18:41:28 +0100 Subject: [PATCH] hlsdemux: Always calculate the current download rate Also take into account the last download rate when calculating it. --- ext/hls/gsthlsdemux.c | 32 ++++++++++++++++++++++++-------- ext/hls/gsthlsdemux.h | 3 +++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/ext/hls/gsthlsdemux.c b/ext/hls/gsthlsdemux.c index 4c373ff9fc..78fc828084 100644 --- a/ext/hls/gsthlsdemux.c +++ b/ext/hls/gsthlsdemux.c @@ -935,6 +935,8 @@ gst_hls_demux_reset (GstHLSDemux * demux, gboolean dispose) gst_element_remove_pad (GST_ELEMENT_CAST (demux), demux->srcpad); demux->srcpad = NULL; } + + demux->current_download_rate = -1; } static gboolean @@ -1227,15 +1229,11 @@ gst_hls_demux_switch_playlist (GstHLSDemux * demux, GstFragment * fragment) { GstClockTime diff; gsize size; - gint bitrate; + gint64 bitrate; GstBuffer *buffer; - GST_M3U8_CLIENT_LOCK (demux->client); - if (!demux->client->main->lists || !fragment) { - GST_M3U8_CLIENT_UNLOCK (demux->client); + if (!fragment) return TRUE; - } - GST_M3U8_CLIENT_UNLOCK (demux->client); /* compare the time when the fragment was downloaded with the time when it was * scheduled */ @@ -1244,10 +1242,28 @@ gst_hls_demux_switch_playlist (GstHLSDemux * demux, GstFragment * fragment) size = gst_buffer_get_size (buffer); bitrate = (size * 8) / ((double) diff / GST_SECOND); - GST_DEBUG ("Downloaded %d bytes in %" GST_TIME_FORMAT ". Bitrate is : %d", - (guint) size, GST_TIME_ARGS (diff), bitrate); + GST_DEBUG_OBJECT (demux, + "Downloaded %d bytes in %" GST_TIME_FORMAT ". Bitrate is : %d", + (guint) size, GST_TIME_ARGS (diff), (gint) bitrate); + + /* Take old rate into account too */ + if (demux->current_download_rate != -1) + bitrate = (demux->current_download_rate + bitrate * 3) / 4; + if (bitrate > G_MAXINT) + bitrate = G_MAXINT; + demux->current_download_rate = bitrate; + + GST_DEBUG_OBJECT (demux, "Using current download rate: %d", (gint) bitrate); gst_buffer_unref (buffer); + + GST_M3U8_CLIENT_LOCK (demux->client); + if (!demux->client->main->lists) { + GST_M3U8_CLIENT_UNLOCK (demux->client); + return TRUE; + } + GST_M3U8_CLIENT_UNLOCK (demux->client); + return gst_hls_demux_change_playlist (demux, bitrate * demux->bitrate_limit); } diff --git a/ext/hls/gsthlsdemux.h b/ext/hls/gsthlsdemux.h index 14817fe62d..9aa5ecf1d0 100644 --- a/ext/hls/gsthlsdemux.h +++ b/ext/hls/gsthlsdemux.h @@ -99,6 +99,9 @@ struct _GstHLSDemux /* Cache for the last key */ gchar *key_url; GstFragment *key_fragment; + + /* Current download rate (bps) */ + gint current_download_rate; }; struct _GstHLSDemuxClass