[FFmpeg-devel,5/7] avformat/hls: migrate to AVFormatContext->url

Submitted by Marton Balint on Jan. 6, 2018, 8:50 p.m.

Details

Message ID 20180106205032.15186-5-cus@passwd.hu
State New
Headers show

Commit Message

Marton Balint Jan. 6, 2018, 8:50 p.m.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/hls.c    |   4 +-
 libavformat/hlsenc.c | 166 +++++++++++++++++++++++++++++----------------------
 2 files changed, 96 insertions(+), 74 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavformat/hls.c b/libavformat/hls.c
index 950cc4c3bd..559b8c3391 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1676,7 +1676,7 @@  static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
     av_log(s, AV_LOG_ERROR,
            "A HLS playlist item '%s' referred to an external file '%s'. "
            "Opening this file was forbidden for security reasons\n",
-           s->filename, url);
+           s->url, url);
     return AVERROR(EPERM);
 }
 
@@ -1814,7 +1814,7 @@  static int hls_read_header(AVFormatContext *s)
         update_options(&c->http_proxy, "http_proxy", u);
     }
 
-    if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
+    if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
         goto fail;
 
     if ((ret = save_avio_options(s)) < 0)
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 20a3ab5758..c5a5681605 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -275,7 +275,7 @@  static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename
 
 static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
 {
-    int http_base_proto = ff_is_http_proto(s->filename);
+    int http_base_proto = ff_is_http_proto(s->url);
 
     if (c->method) {
         av_dict_set(options, "method", c->method, 0);
@@ -405,7 +405,7 @@  static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
         if (hls->segment_filename) {
             dirname = av_strdup(hls->segment_filename);
         } else {
-            dirname = av_strdup(vs->avf->filename);
+            dirname = av_strdup(vs->avf->url);
         }
         if (!dirname) {
             ret = AVERROR(ENOMEM);
@@ -432,7 +432,7 @@  static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
             av_strlcat(path, segment->filename, path_size);
         }
 
-        proto = avio_find_protocol_name(s->filename);
+        proto = avio_find_protocol_name(s->url);
         if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
             av_dict_set(&options, "method", "DELETE", 0);
             if ((ret = vs->avf->io_open(vs->avf, &out, path, AVIO_FLAG_WRITE, &options)) < 0)
@@ -502,12 +502,12 @@  static int do_encrypt(AVFormatContext *s, VariantStream *vs)
     AVIOContext *pb;
     uint8_t key[KEYSIZE];
 
-    len = strlen(s->filename) + 4 + 1;
+    len = strlen(s->url) + 4 + 1;
     hls->key_basename = av_mallocz(len);
     if (!hls->key_basename)
         return AVERROR(ENOMEM);
 
-    av_strlcpy(hls->key_basename, s->filename, len);
+    av_strlcpy(hls->key_basename, s->url, len);
     av_strlcat(hls->key_basename, ".key", len);
 
     if (hls->key_url) {
@@ -637,7 +637,10 @@  static int hls_mux_init(AVFormatContext *s, VariantStream *vs)
         return ret;
     oc = vs->avf;
 
-    oc->filename[0]        = '\0';
+    oc->url                = av_strdup("");
+    if (!oc->url)
+        return AVERROR(ENOMEM);
+
     oc->oformat            = vs->oformat;
     oc->interrupt_callback = s->interrupt_callback;
     oc->max_delay          = s->max_delay;
@@ -746,35 +749,38 @@  static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls
 {
     if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
         strlen(vs->current_segment_final_filename_fmt)) {
-        av_strlcpy(vs->avf->filename, vs->current_segment_final_filename_fmt, sizeof(vs->avf->filename));
+        char * new_url = av_strdup(vs->current_segment_final_filename_fmt);
+        if (!new_url) {
+            av_free(en);
+            return AVERROR(ENOMEM);
+        }
+        ff_format_set_url(vs->avf, new_url);
         if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
             char *filename = NULL;
-            if (replace_int_data_in_filename(&filename, vs->avf->filename, 's', pos + size) < 1) {
+            if (replace_int_data_in_filename(&filename, vs->avf->url, 's', pos + size) < 1) {
                 av_log(hls, AV_LOG_ERROR,
                        "Invalid second level segment filename template '%s', "
                         "you can try to remove second_level_segment_size flag\n",
-                       filename);
+                       vs->avf->url);
                 av_free(filename);
                 av_free(en);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(vs->avf->filename, filename, sizeof(vs->avf->filename));
-            av_free(filename);
+            ff_format_set_url(vs->avf, filename);
         }
         if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
             char *filename = NULL;
-            if (replace_int_data_in_filename(&filename, vs->avf->filename,
+            if (replace_int_data_in_filename(&filename, vs->avf->url,
                 't',  (int64_t)round(duration * HLS_MICROSECOND_UNIT)) < 1) {
                 av_log(hls, AV_LOG_ERROR,
                        "Invalid second level segment filename template '%s', "
                         "you can try to remove second_level_segment_time flag\n",
-                       filename);
+                       vs->avf->url);
                 av_free(filename);
                 av_free(en);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(vs->avf->filename, filename, sizeof(vs->avf->filename));
-            av_free(filename);
+            ff_format_set_url(vs->avf, filename);
         }
     }
     return 0;
@@ -826,7 +832,7 @@  static int sls_flag_check_duration_size(HLSContext *hls, VariantStream *vs)
 static void sls_flag_file_rename(HLSContext *hls, VariantStream *vs, char *old_filename) {
     if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
         strlen(vs->current_segment_final_filename_fmt)) {
-        ff_rename(old_filename, vs->avf->filename, hls);
+        ff_rename(old_filename, vs->avf->url, hls);
     }
 }
 
@@ -836,45 +842,42 @@  static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
         char *filename = NULL;
         if (replace_int_data_in_filename(&filename,
 #if FF_API_HLS_WRAP
-            oc->filename, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) {
+            oc->url, 'd', c->wrap ? vs->sequence % c->wrap : vs->sequence) < 1) {
 #else
-            oc->filename, 'd', vs->sequence) < 1) {
+            oc->url, 'd', vs->sequence) < 1) {
 #endif
             av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
                     "you can try to remove second_level_segment_index flag\n",
-                   filename);
+                   oc->url);
             av_free(filename);
             return AVERROR(EINVAL);
         }
-        av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-        av_free(filename);
+        ff_format_set_url(oc, filename);
     }
     if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
-        av_strlcpy(vs->current_segment_final_filename_fmt, oc->filename,
+        av_strlcpy(vs->current_segment_final_filename_fmt, oc->url,
                    sizeof(vs->current_segment_final_filename_fmt));
         if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
             char *filename = NULL;
-            if (replace_int_data_in_filename(&filename, oc->filename, 's', 0) < 1) {
+            if (replace_int_data_in_filename(&filename, oc->url, 's', 0) < 1) {
                 av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
                         "you can try to remove second_level_segment_size flag\n",
-                       filename);
+                       oc->url);
                 av_free(filename);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-            av_free(filename);
+            ff_format_set_url(oc, filename);
         }
         if (c->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
             char *filename = NULL;
-            if (replace_int_data_in_filename(&filename, oc->filename, 't', 0) < 1) {
+            if (replace_int_data_in_filename(&filename, oc->url, 't', 0) < 1) {
                 av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
                         "you can try to remove second_level_segment_time flag\n",
-                       filename);
+                       oc->url);
                 av_free(filename);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-            av_free(filename);
+            ff_format_set_url(oc, filename);
         }
     }
     return 0;
@@ -898,10 +901,10 @@  static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
         return ret;
     }
 
-    filename = av_basename(vs->avf->filename);
+    filename = av_basename(vs->avf->url);
 
     if (hls->use_localtime_mkdir) {
-        filename = vs->avf->filename;
+        filename = vs->avf->url;
     }
     if ((find_segment_by_filename(vs->segments, filename) || find_segment_by_filename(vs->old_segments, filename))
         && !byterange_mode) {
@@ -910,7 +913,7 @@  static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
     av_strlcpy(en->filename, filename, sizeof(en->filename));
 
     if(vs->has_subtitle)
-        av_strlcpy(en->sub_filename, av_basename(vs->vtt_avf->filename), sizeof(en->sub_filename));
+        av_strlcpy(en->sub_filename, av_basename(vs->vtt_avf->url), sizeof(en->sub_filename));
     else
         en->sub_filename[0] = '\0';
 
@@ -1036,10 +1039,15 @@  static int parse_playlist(AVFormatContext *s, const char *url, VariantStream *vs
             continue;
         } else if (line[0]) {
             if (is_segment) {
+                char *new_file = av_strdup(line);
+                if (!new_file) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+                ff_format_set_url(vs->avf, new_file);
                 is_segment = 0;
                 new_start_pos = avio_tell(vs->avf->pb);
                 vs->size = new_start_pos - vs->start_pos;
-                av_strlcpy(vs->avf->filename, line, sizeof(line));
                 ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);
                 if (ret < 0)
                     goto fail;
@@ -1064,15 +1072,19 @@  static void hls_free_segments(HLSSegment *p)
     }
 }
 
-static void hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
+static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
 {
-    size_t len = strlen(oc->filename);
-    char final_filename[sizeof(oc->filename)];
+    size_t len = strlen(oc->url);
+    char *final_filename = av_strdup(oc->url);
+    int ret;
 
-    av_strlcpy(final_filename, oc->filename, len);
+    if (!final_filename)
+        return AVERROR(ENOMEM);
     final_filename[len-4] = '\0';
-    ff_rename(oc->filename, final_filename, s);
-    oc->filename[len-4] = '\0';
+    ret = ff_rename(oc->url, final_filename, s);
+    oc->url[len-4] = '\0';
+    av_freep(&final_filename);
+    return ret;
 }
 
 static int get_relative_url(const char *master_url, const char *media_url,
@@ -1234,7 +1246,7 @@  static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
     int ret = 0;
     char temp_filename[1024];
     int64_t sequence = FFMAX(hls->start_sequence, vs->sequence - vs->nb_entries);
-    const char *proto = avio_find_protocol_name(s->filename);
+    const char *proto = avio_find_protocol_name(s->url);
     int use_rename = proto && !strcmp(proto, "file");
     static unsigned warned_non_file;
     char *key_uri = NULL;
@@ -1353,11 +1365,16 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
     int err = 0;
 
     if (c->flags & HLS_SINGLE_FILE) {
-        av_strlcpy(oc->filename, vs->basename,
-                   sizeof(oc->filename));
-        if (vs->vtt_basename)
-            av_strlcpy(vtt_oc->filename, vs->vtt_basename,
-                  sizeof(vtt_oc->filename));
+        char *new_name = av_strdup(vs->basename);
+        if (!new_name)
+            return AVERROR(ENOMEM);
+        ff_format_set_url(oc, new_name);
+        if (vs->vtt_basename) {
+            new_name = av_strdup(vs->vtt_basename);
+            if (!new_name)
+                return AVERROR(ENOMEM);
+            ff_format_set_url(vtt_oc, new_name);
+        }
     } else if (c->max_seg_size > 0) {
         char *filename = NULL;
         if (replace_int_data_in_filename(&filename,
@@ -1370,15 +1387,19 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
                 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -use_localtime 1 with it\n", vs->basename);
                 return AVERROR(EINVAL);
         }
-        av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-        av_free(filename);
+        ff_format_set_url(oc, filename);
     } else {
         if (c->use_localtime) {
             time_t now0;
             struct tm *tm, tmpbuf;
+            int bufsize = strlen(vs->basename) + 1024;
+            char *buf = av_mallocz(bufsize);
+            if (!buf)
+                return AVERROR(ENOMEM);
             time(&now0);
             tm = localtime_r(&now0, &tmpbuf);
-            if (!strftime(oc->filename, sizeof(oc->filename), vs->basename, tm)) {
+            ff_format_set_url(oc, buf);
+            if (!strftime(oc->url, bufsize, vs->basename, tm)) {
                 av_log(oc, AV_LOG_ERROR, "Could not get segment filename with use_localtime\n");
                 return AVERROR(EINVAL);
             }
@@ -1390,7 +1411,7 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
 
             if (c->use_localtime_mkdir) {
                 const char *dir;
-                char *fn_copy = av_strdup(oc->filename);
+                char *fn_copy = av_strdup(oc->url);
                 if (!fn_copy) {
                     return AVERROR(ENOMEM);
                 }
@@ -1414,8 +1435,7 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
                 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -use_localtime 1 with it\n", vs->basename);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(oc->filename, filename, sizeof(oc->filename));
-            av_free(filename);
+            ff_format_set_url(oc, filename);
         }
         if( vs->vtt_basename) {
             char *filename = NULL;
@@ -1429,8 +1449,7 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
                 av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", vs->vtt_basename);
                 return AVERROR(EINVAL);
             }
-            av_strlcpy(vtt_oc->filename, filename, sizeof(vtt_oc->filename));
-            av_free(filename);
+            ff_format_set_url(vtt_oc, filename);
        }
     }
     vs->number++;
@@ -1438,7 +1457,10 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
     set_http_options(s, &options, c);
 
     if (c->flags & HLS_TEMP_FILE) {
-        av_strlcat(oc->filename, ".tmp", sizeof(oc->filename));
+        char *new_name = av_asprintf("%s.tmp", oc->url);
+        if (!new_name)
+            return AVERROR(ENOMEM);
+        ff_format_set_url(oc, new_name);
     }
 
     if (c->key_info_file || c->encrypt) {
@@ -1466,7 +1488,7 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
         if ((err = av_dict_set(&options, "encryption_iv", iv_string, 0)) < 0)
            goto fail;
 
-        filename = av_asprintf("crypto:%s", oc->filename);
+        filename = av_asprintf("crypto:%s", oc->url);
         if (!filename) {
             err = AVERROR(ENOMEM);
             goto fail;
@@ -1477,12 +1499,12 @@  static int hls_start(AVFormatContext *s, VariantStream *vs)
         if (err < 0)
             return err;
     } else if (c->segment_type != SEGMENT_TYPE_FMP4) {
-        if ((err = hlsenc_io_open(s, &oc->pb, oc->filename, &options)) < 0)
+        if ((err = hlsenc_io_open(s, &oc->pb, oc->url, &options)) < 0)
             goto fail;
     }
     if (vs->vtt_basename) {
         set_http_options(s, &options, c);
-        if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->filename, &options)) < 0)
+        if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->url, &options)) < 0)
             goto fail;
     }
     av_dict_free(&options);
@@ -1779,7 +1801,7 @@  static int update_master_pl_info(AVFormatContext *s) {
     char *fn1= NULL, *fn2 = NULL;
     int ret = 0;
 
-    fn1 = av_strdup(s->filename);
+    fn1 = av_strdup(s->url);
     if (!fn1) {
         ret = AVERROR(ENOMEM);
         goto fail;
@@ -1948,7 +1970,7 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (vs->packets_written && can_split && av_compare_ts(pkt->pts - vs->start_pts, st->time_base,
                                    end_pts, AV_TIME_BASE_Q) >= 0) {
         int64_t new_start_pos;
-        char *old_filename = av_strdup(vs->avf->filename);
+        char *old_filename = av_strdup(vs->avf->url);
         int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
 
         if (!old_filename) {
@@ -1973,13 +1995,13 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
                     hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
                 }
             } else {
-                hlsenc_io_close(s, &oc->pb, oc->filename);
+                hlsenc_io_close(s, &oc->pb, oc->url);
             }
             if (vs->vtt_avf) {
-                hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->filename);
+                hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->url);
             }
         }
-        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
+        if ((hls->flags & HLS_TEMP_FILE) && oc->url[0]) {
             if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))
                 if ((vs->avf->oformat->priv_class && vs->avf->priv_data) && hls->segment_type != SEGMENT_TYPE_FMP4)
                     av_opt_set(vs->avf->priv_data, "mpegts_flags", "resend_headers", 0);
@@ -1991,10 +2013,10 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         }
 
         if (hls->segment_type == SEGMENT_TYPE_FMP4) {
-            ret = hlsenc_io_open(s, &vs->out, vs->avf->filename, NULL);
+            ret = hlsenc_io_open(s, &vs->out, vs->avf->url, NULL);
             if (ret < 0) {
                 av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n",
-                    vs->avf->filename);
+                    vs->avf->url);
                 av_free(old_filename);
                 return ret;
             }
@@ -2067,16 +2089,16 @@  static int hls_write_trailer(struct AVFormatContext *s)
 
     oc = vs->avf;
     vtt_oc = vs->vtt_avf;
-    old_filename = av_strdup(vs->avf->filename);
+    old_filename = av_strdup(vs->avf->url);
 
     if (!old_filename) {
         return AVERROR(ENOMEM);
     }
     if ( hls->segment_type == SEGMENT_TYPE_FMP4) {
         int range_length = 0;
-        ret = hlsenc_io_open(s, &vs->out, vs->avf->filename, NULL);
+        ret = hlsenc_io_open(s, &vs->out, vs->avf->url, NULL);
         if (ret < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->filename);
+            av_log(NULL, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->url);
             goto failed;
         }
         write_styp(vs->out);
@@ -2094,7 +2116,7 @@  failed:
         if (hls->segment_type != SEGMENT_TYPE_FMP4)
             ff_format_io_close(s, &oc->pb);
 
-        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
+        if ((hls->flags & HLS_TEMP_FILE) && oc->url[0]) {
             hls_rename_temp_file(s, oc);
         }
 
@@ -2170,7 +2192,7 @@  static int hls_init(AVFormatContext *s)
         goto fail;
     }
 
-    ret = validate_name(hls->nb_varstreams, s->filename);
+    ret = validate_name(hls->nb_varstreams, s->url);
     if (ret < 0)
         goto fail;
 
@@ -2225,12 +2247,12 @@  static int hls_init(AVFormatContext *s)
     for (i = 0; i < hls->nb_varstreams; i++) {
         vs = &hls->var_streams[i];
 
-        vs->m3u8_name = av_strdup(s->filename);
+        vs->m3u8_name = av_strdup(s->url);
         if (!vs->m3u8_name ) {
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        ret = format_name(vs->m3u8_name, strlen(s->filename) + 1, i);
+        ret = format_name(vs->m3u8_name, strlen(s->url) + 1, i);
         if (ret < 0)
             goto fail;