]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/hlsenc.c
avformat/hlsenc: avformat/hlsenc: reopen new http session for http_persistent
[ffmpeg] / libavformat / hlsenc.c
index 18173cdce1c2a5a3e7dddc29086ff1168b5b8b82..52ee8941a1e032734a3668e90023cd5018dca14f 100644 (file)
@@ -118,6 +118,7 @@ typedef struct VariantStream {
     AVIOContext *out;
     int packets_written;
     int init_range_length;
+    uint8_t *temp_buffer;
 
     AVFormatContext *avf;
     AVFormatContext *vtt_avf;
@@ -262,11 +263,12 @@ static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename,
     return err;
 }
 
-static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename) {
+static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename) {
     HLSContext *hls = s->priv_data;
     int http_base_proto = filename ? ff_is_http_proto(filename) : 0;
+    int ret = 0;
     if (!*pb)
-        return;
+        return ret;
     if (!http_base_proto || !hls->http_persistent || hls->key_info_file || hls->encrypt) {
         ff_format_io_close(s, pb);
 #if CONFIG_HTTP_PROTOCOL
@@ -275,8 +277,10 @@ static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename
         av_assert0(http_url_context);
         avio_flush(*pb);
         ffurl_shutdown(http_url_context, AVIO_FLAG_WRITE);
+        ret = ff_http_get_shutdown_status(http_url_context);
 #endif
     }
+    return ret;
 }
 
 static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
@@ -447,7 +451,6 @@ static void write_styp(AVIOContext *pb)
 static int flush_dynbuf(VariantStream *vs, int *range_length)
 {
     AVFormatContext *ctx = vs->avf;
-    uint8_t *buffer;
 
     if (!ctx->pb) {
         return AVERROR(EINVAL);
@@ -458,15 +461,20 @@ static int flush_dynbuf(VariantStream *vs, int *range_length)
     avio_flush(ctx->pb);
 
     // write out to file
-    *range_length = avio_close_dyn_buf(ctx->pb, &buffer);
+    *range_length = avio_close_dyn_buf(ctx->pb, &vs->temp_buffer);
     ctx->pb = NULL;
-    avio_write(vs->out, buffer, *range_length);
-    av_free(buffer);
+    avio_write(vs->out, vs->temp_buffer, *range_length);
 
     // re-open buffer
     return avio_open_dyn_buf(&ctx->pb);
 }
 
+static void reflush_dynbuf(VariantStream *vs, int *range_length)
+{
+    // re-open buffer
+    avio_write(vs->out, vs->temp_buffer, *range_length);;
+}
+
 static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
                                    VariantStream *vs) {
 
@@ -1224,22 +1232,22 @@ static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
     return ret;
 }
 
-static int get_relative_url(const char *master_url, const char *media_url,
-                            char *rel_url, int rel_url_buf_size)
+static const char* get_relative_url(const char *master_url, const char *media_url)
 {
-    char *p = NULL;
-    int base_len = -1;
-    p = strrchr(master_url, '/') ? strrchr(master_url, '/') :\
-            strrchr(master_url, '\\');
+    const char *p = strrchr(master_url, '/');
+    size_t base_len = 0;
+
+    if (!p) p = strrchr(master_url, '\\');
+
     if (p) {
-        base_len = FFABS(p - master_url);
+        base_len = p + 1 - master_url;
         if (av_strncasecmp(master_url, media_url, base_len)) {
             av_log(NULL, AV_LOG_WARNING, "Unable to find relative url\n");
-            return AVERROR(EINVAL);
+            return NULL;
         }
     }
-    av_strlcpy(rel_url, &(media_url[base_len + 1]), rel_url_buf_size);
-    return 0;
+
+    return media_url + base_len;
 }
 
 static int64_t get_stream_bit_rate(AVStream *stream) {
@@ -1265,8 +1273,9 @@ static int create_master_playlist(AVFormatContext *s,
     AVStream *vid_st, *aud_st;
     AVDictionary *options = NULL;
     unsigned int i, j;
-    int m3u8_name_size, ret, bandwidth;
-    char *m3u8_rel_name = NULL, *ccgroup;
+    int ret, bandwidth;
+    const char *m3u8_rel_name = NULL;
+    char *ccgroup;
     ClosedCaptionsStream *ccs;
     const char *proto = avio_find_protocol_name(hls->master_m3u8_url);
     int is_file_proto = proto && !strcmp(proto, "file");
@@ -1315,39 +1324,21 @@ static int create_master_playlist(AVFormatContext *s,
         if (vs->has_video || vs->has_subtitle || !vs->agroup)
             continue;
 
-        m3u8_name_size = strlen(vs->m3u8_name) + 1;
-        m3u8_rel_name = av_malloc(m3u8_name_size);
+        m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
         if (!m3u8_rel_name) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-        av_strlcpy(m3u8_rel_name, vs->m3u8_name, m3u8_name_size);
-        ret = get_relative_url(hls->master_m3u8_url, vs->m3u8_name,
-                               m3u8_rel_name, m3u8_name_size);
-        if (ret < 0) {
             av_log(s, AV_LOG_ERROR, "Unable to find relative URL\n");
             goto fail;
         }
 
         ff_hls_write_audio_rendition(hls->m3u8_out, vs->agroup, m3u8_rel_name, vs->language, i, hls->has_default_key ? vs->is_default : 1);
-
-        av_freep(&m3u8_rel_name);
     }
 
     /* For variant streams with video add #EXT-X-STREAM-INF tag with attributes*/
     for (i = 0; i < hls->nb_varstreams; i++) {
         vs = &(hls->var_streams[i]);
 
-        m3u8_name_size = strlen(vs->m3u8_name) + 1;
-        m3u8_rel_name = av_malloc(m3u8_name_size);
+        m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
         if (!m3u8_rel_name) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-        av_strlcpy(m3u8_rel_name, vs->m3u8_name, m3u8_name_size);
-        ret = get_relative_url(hls->master_m3u8_url, vs->m3u8_name,
-                               m3u8_rel_name, m3u8_name_size);
-        if (ret < 0) {
             av_log(NULL, AV_LOG_ERROR, "Unable to find relative URL\n");
             goto fail;
         }
@@ -1416,13 +1407,10 @@ static int create_master_playlist(AVFormatContext *s,
                                          aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup);
             }
         }
-
-        av_freep(&m3u8_rel_name);
     }
 fail:
     if(ret >=0)
         hls->master_m3u8_created = 1;
-    av_freep(&m3u8_rel_name);
     hlsenc_io_close(s, &hls->m3u8_out, temp_filename);
     if (use_temp_file)
         ff_rename(temp_filename, hls->master_m3u8_url, s);
@@ -1544,7 +1532,10 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
 
 fail:
     av_dict_free(&options);
-    hlsenc_io_close(s, (byterange_mode || hls->segment_type == SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename);
+    ret = hlsenc_io_close(s, (byterange_mode || hls->segment_type == SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename);
+    if (ret < 0) {
+        return ret;
+    }
     hlsenc_io_close(s, &hls->sub_m3u8_out, vs->vtt_m3u8_name);
     if (use_temp_file) {
         ff_rename(temp_filename, vs->m3u8_name, s);
@@ -2399,7 +2390,16 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
                 if (ret < 0) {
                     return ret;
                 }
-                hlsenc_io_close(s, &vs->out, filename);
+                ret = hlsenc_io_close(s, &vs->out, filename);
+                if (ret < 0) {
+                    av_log(s, AV_LOG_WARNING, "upload segment failed,"
+                           " will retry with a new http session.\n");
+                    ff_format_io_close(s, &vs->out);
+                    ret = hlsenc_io_open(s, &vs->out, filename, &options);
+                    reflush_dynbuf(vs, &range_length);
+                    ret = hlsenc_io_close(s, &vs->out, filename);
+                }
+                av_free(vs->temp_buffer);
                 av_free(filename);
             }
         }
@@ -2426,8 +2426,13 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         // if we're building a VOD playlist, skip writing the manifest multiple times, and just wait until the end
         if (hls->pl_type != PLAYLIST_TYPE_VOD) {
             if ((ret = hls_window(s, 0, vs)) < 0) {
-                av_free(old_filename);
-                return ret;
+                av_log(s, AV_LOG_WARNING, "upload playlist failed, will retry with a new http session.\n");
+                ff_format_io_close(s, &vs->out);
+                vs->out = NULL;
+                if ((ret = hls_window(s, 0, vs)) < 0) {
+                    av_free(old_filename);
+                    return ret;
+                }
             }
         }
 
@@ -2538,6 +2543,7 @@ static int hls_write_trailer(struct AVFormatContext *s)
             filename = av_asprintf("%s", vs->avf->url);
         }
         if (!filename) {
+            av_free(old_filename);
             return AVERROR(ENOMEM);
         }
 
@@ -2578,9 +2584,22 @@ static int hls_write_trailer(struct AVFormatContext *s)
 
         vs->size = range_length;
         hlsenc_io_close(s, &vs->out, filename);
-        av_free(filename);
+        ret = hlsenc_io_close(s, &vs->out, filename);
+        if (ret < 0) {
+            av_log(s, AV_LOG_WARNING, "upload segment failed, will retry with a new http session.\n");
+            ff_format_io_close(s, &vs->out);
+            ret = hlsenc_io_open(s, &vs->out, filename, &options);
+            if (ret < 0) {
+                av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->url);
+                goto failed;
+            }
+            reflush_dynbuf(vs, &range_length);
+            ret = hlsenc_io_close(s, &vs->out, filename);
+        }
+        av_free(vs->temp_buffer);
 
 failed:
+        av_free(filename);
         av_write_trailer(oc);
         if (oc->url[0]) {
             proto = avio_find_protocol_name(oc->url);
@@ -2610,7 +2629,12 @@ failed:
             ff_format_io_close(s, &vtt_oc->pb);
             avformat_free_context(vtt_oc);
         }
-        hls_window(s, 1, vs);
+        ret = hls_window(s, 1, vs);
+        if (ret < 0) {
+            av_log(s, AV_LOG_WARNING, "upload playlist failed, will retry with a new http session.\n");
+            ff_format_io_close(s, &vs->out);
+            hls_window(s, 1, vs);
+        }
         avformat_free_context(oc);
 
         vs->avf = NULL;
@@ -2785,7 +2809,6 @@ static int hls_init(AVFormatContext *s)
             ret = format_name(hls->segment_filename, &vs->basename, i, vs->varname);
             if (ret < 0)
                 goto fail;
-            basename_size = strlen(vs->basename) + 1;
         } else {
             if (hls->flags & HLS_SINGLE_FILE) {
                 if (hls->segment_type == SEGMENT_TYPE_FMP4) {