]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/hls.c
Merge commit 'de44dfc7c0ec02bda7d846ef713145c890bfae3f'
[ffmpeg] / libavformat / hls.c
index 290f12e5bbbff179079bfd71ffb1c1a02c34b23d..471a62dc9c46b1d55387c6c3bf128fcb0cab215b 100644 (file)
@@ -2,20 +2,20 @@
  * Apple HTTP Live Streaming demuxer
  * Copyright (c) 2010 Martin Storsjo
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -103,6 +103,9 @@ typedef struct HLSContext {
     int64_t seek_timestamp;
     int seek_flags;
     AVIOInterruptCB *interrupt_callback;
+    char *user_agent;                    ///< holds HTTP user agent set as an AVOption to the HTTP protocol context
+    char *cookies;                       ///< holds HTTP cookie values set in either the initial response or as an AVOption to the HTTP protocol context
+    char *headers;                       ///< holds HTTP headers set as an AVOption to the HTTP protocol context
 } HLSContext;
 
 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
@@ -139,6 +142,8 @@ static void free_variant_list(HLSContext *c)
         av_free(var);
     }
     av_freep(&c->variants);
+    av_freep(&c->cookies);
+    av_freep(&c->user_agent);
     c->n_variants = 0;
 }
 
@@ -208,15 +213,26 @@ static int parse_playlist(HLSContext *c, const char *url,
     uint8_t iv[16] = "";
     int has_iv = 0;
     char key[MAX_URL_SIZE] = "";
-    char line[1024];
+    char line[MAX_URL_SIZE];
     const char *ptr;
     int close_in = 0;
     uint8_t *new_url = NULL;
 
     if (!in) {
+        AVDictionary *opts = NULL;
         close_in = 1;
-        if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,
-                              c->interrupt_callback, NULL)) < 0)
+        /* Some HLS servers don't like being sent the range header */
+        av_dict_set(&opts, "seekable", "0", 0);
+
+        // broker prior HTTP options that should be consistent across requests
+        av_dict_set(&opts, "user-agent", c->user_agent, 0);
+        av_dict_set(&opts, "cookies", c->cookies, 0);
+        av_dict_set(&opts, "headers", c->headers, 0);
+
+        ret = avio_open2(&in, url, AVIO_FLAG_READ,
+                         c->interrupt_callback, &opts);
+        av_dict_free(&opts);
+        if (ret < 0)
             return ret;
     }
 
@@ -233,7 +249,7 @@ static int parse_playlist(HLSContext *c, const char *url,
         free_segment_list(var);
         var->finished = 0;
     }
-    while (!in->eof_reached) {
+    while (!url_feof(in)) {
         read_chomp_line(in, line, sizeof(line));
         if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
             struct variant_info info = {{0}};
@@ -329,19 +345,28 @@ fail:
     return ret;
 }
 
-static int open_input(struct variant *var)
+static int open_input(HLSContext *c, struct variant *var)
 {
+    AVDictionary *opts = NULL;
+    int ret;
     struct segment *seg = var->segments[var->cur_seq_no - var->start_seq_no];
+
+    // broker prior HTTP options that should be consistent across requests
+    av_dict_set(&opts, "user-agent", c->user_agent, 0);
+    av_dict_set(&opts, "cookies", c->cookies, 0);
+    av_dict_set(&opts, "headers", c->headers, 0);
+    av_dict_set(&opts, "seekable", "0", 0);
+
     if (seg->key_type == KEY_NONE) {
-        return ffurl_open(&var->input, seg->url, AVIO_FLAG_READ,
-                          &var->parent->interrupt_callback, NULL);
+        ret = ffurl_open(&var->input, seg->url, AVIO_FLAG_READ,
+                          &var->parent->interrupt_callback, &opts);
+        goto cleanup;
     } else if (seg->key_type == KEY_AES_128) {
         char iv[33], key[33], url[MAX_URL_SIZE];
-        int ret;
         if (strcmp(seg->key, var->key_url)) {
             URLContext *uc;
             if (ffurl_open(&uc, seg->key, AVIO_FLAG_READ,
-                           &var->parent->interrupt_callback, NULL) == 0) {
+                           &var->parent->interrupt_callback, &opts) == 0) {
                 if (ffurl_read_complete(uc, var->key, sizeof(var->key))
                     != sizeof(var->key)) {
                     av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
@@ -363,17 +388,25 @@ static int open_input(struct variant *var)
             snprintf(url, sizeof(url), "crypto:%s", seg->url);
         if ((ret = ffurl_alloc(&var->input, url, AVIO_FLAG_READ,
                                &var->parent->interrupt_callback)) < 0)
-            return ret;
+            goto cleanup;
         av_opt_set(var->input->priv_data, "key", key, 0);
         av_opt_set(var->input->priv_data, "iv", iv, 0);
-        if ((ret = ffurl_connect(var->input, NULL)) < 0) {
+        /* Need to repopulate options */
+        av_dict_free(&opts);
+        av_dict_set(&opts, "seekable", "0", 0);
+        if ((ret = ffurl_connect(var->input, &opts)) < 0) {
             ffurl_close(var->input);
             var->input = NULL;
-            return ret;
+            goto cleanup;
         }
-        return 0;
+        ret = 0;
     }
-    return AVERROR(ENOSYS);
+    else
+      ret = AVERROR(ENOSYS);
+
+cleanup:
+    av_dict_free(&opts);
+    return ret;
 }
 
 static int read_data(void *opaque, uint8_t *buf, int buf_size)
@@ -418,7 +451,7 @@ reload:
             goto reload;
         }
 
-        ret = open_input(v);
+        ret = open_input(c, v);
         if (ret < 0)
             return ret;
     }
@@ -451,11 +484,33 @@ reload:
 
 static int hls_read_header(AVFormatContext *s)
 {
+    URLContext *u = (s->flags & AVFMT_FLAG_CUSTOM_IO) ? NULL : s->pb->opaque;
     HLSContext *c = s->priv_data;
     int ret = 0, i, j, stream_offset = 0;
 
     c->interrupt_callback = &s->interrupt_callback;
 
+    // if the URL context is good, read important options we must broker later
+    if (u && u->prot->priv_data_class) {
+        // get the previous user agent & set back to null if string size is zero
+        av_freep(&c->user_agent);
+        av_opt_get(u->priv_data, "user-agent", 0, (uint8_t**)&(c->user_agent));
+        if (c->user_agent && !strlen(c->user_agent))
+            av_freep(&c->user_agent);
+
+        // get the previous cookies & set back to null if string size is zero
+        av_freep(&c->cookies);
+        av_opt_get(u->priv_data, "cookies", 0, (uint8_t**)&(c->cookies));
+        if (c->cookies && !strlen(c->cookies))
+            av_freep(&c->cookies);
+
+        // get the previous headers & set back to null if string size is zero
+        av_freep(&c->headers);
+        av_opt_get(u->priv_data, "headers", 0, (uint8_t**)&(c->headers));
+        if (c->headers && !strlen(c->headers))
+            av_freep(&c->headers);
+    }
+
     if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
         goto fail;
 
@@ -525,6 +580,7 @@ static int hls_read_header(AVFormatContext *s)
              * so avformat_close_input shouldn't be called. If
              * avformat_open_input fails below, it frees and zeros the
              * context, so it doesn't need any special treatment like this. */
+            av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", v->segments[0]->url);
             avformat_free_context(v->ctx);
             v->ctx = NULL;
             goto fail;
@@ -582,7 +638,7 @@ static int recheck_discard_flags(AVFormatContext *s, int first)
 
     /* Check if any new streams are needed */
     for (i = 0; i < c->n_variants; i++)
-        c->variants[i]->cur_needed = 0;;
+        c->variants[i]->cur_needed = 0;
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
@@ -632,7 +688,7 @@ start:
                 AVStream *st;
                 ret = av_read_frame(var->ctx, &var->pkt);
                 if (ret < 0) {
-                    if (!var->pb.eof_reached)
+                    if (!url_feof(&var->pb) && ret != AVERROR_EOF)
                         return ret;
                     reset_packet(&var->pkt);
                     break;