[FFmpeg-devel] [PATCH 16/18] avformat/hls: avoid unnecessary segment retrievals

Anssi Hannula anssi.hannula at iki.fi
Mon Dec 30 12:14:30 CET 2013


Check if the playlist is still needed just before requesting the next
segment instead of after exhausting the previous segment.

Signed-off-by: Anssi Hannula <anssi.hannula at iki.fi>
---
 libavformat/hls.c | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/libavformat/hls.c b/libavformat/hls.c
index bb75071..c4e6dac 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -761,14 +761,33 @@ static int read_data(void *opaque, uint8_t *buf, int buf_size)
     int just_opened = 0;
     struct segment *seg;
 
+restart:
     if (!v->needed || v->reopen_subtitle)
         return AVERROR_EOF;
 
-restart:
     if (!v->input) {
+        int64_t reload_interval;
+
+        /* Check that the playlist is still needed before opening a new
+         * segment. */
+        if (v->ctx && v->ctx->nb_streams &&
+            v->parent->nb_streams >= v->stream_offset + v->ctx->nb_streams) {
+            v->needed = 0;
+            for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
+                i++) {
+                if (v->parent->streams[i]->discard < AVDISCARD_ALL)
+                    v->needed = 1;
+            }
+        }
+        if (!v->needed) {
+            av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d\n",
+                v->index);
+            return AVERROR_EOF;
+        }
+
         /* If this is a live stream and the reload interval has elapsed since
          * the last playlist reload, reload the playlists now. */
-        int64_t reload_interval = default_reload_interval(v);
+        reload_interval = default_reload_interval(v);
 
 reload:
         if (!v->finished &&
@@ -847,20 +866,6 @@ reload:
     c->cur_seq_no = v->cur_seq_no;
     after_segment_switch(v);
 
-    if (v->ctx && v->ctx->nb_streams &&
-        v->parent->nb_streams >= v->stream_offset + v->ctx->nb_streams) {
-        v->needed = 0;
-        for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
-             i++) {
-            if (v->parent->streams[i]->discard < AVDISCARD_ALL)
-                v->needed = 1;
-        }
-    }
-    if (!v->needed) {
-        av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d\n",
-               v->index);
-        return AVERROR_EOF;
-    }
     goto restart;
 }
 
-- 
1.8.1.5



More information about the ffmpeg-devel mailing list