Browse code

libavformat/hlsenc: Persistent HTTP connections supported as an option

Jeyapal, Karthick authored on 2017/11/29 15:33:07
Showing 2 changed files
... ...
@@ -854,6 +854,9 @@ ffmpeg -re -i in.ts -f hls -master_pl_name master.m3u8 \
854 854
 This example creates HLS master playlist with name master.m3u8 and keep
855 855
 publishing it repeatedly every after 30 segments i.e. every after 60s.
856 856
 
857
+@item http_persistent
858
+Use persistent HTTP connections. Applicable only for HTTP output.
859
+
857 860
 @end table
858 861
 
859 862
 @anchor{ico}
... ...
@@ -45,6 +45,7 @@
45 45
 
46 46
 #include "avformat.h"
47 47
 #include "avio_internal.h"
48
+#include "http.h"
48 49
 #include "internal.h"
49 50
 #include "os_support.h"
50 51
 
... ...
@@ -205,6 +206,7 @@ typedef struct HLSContext {
205 205
     char *var_stream_map; /* user specified variant stream map string */
206 206
     char *master_pl_name;
207 207
     unsigned int master_publish_rate;
208
+    int http_persistent;
208 209
 } HLSContext;
209 210
 
210 211
 static int get_int_from_double(double val)
... ...
@@ -245,10 +247,38 @@ static int mkdir_p(const char *path) {
245 245
     return ret;
246 246
 }
247 247
 
248
+static int is_http_proto(char *filename) {
249
+    const char *proto = avio_find_protocol_name(filename);
250
+    return proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
251
+}
252
+
253
+static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename,
254
+                          AVDictionary **options) {
255
+    HLSContext *hls = s->priv_data;
256
+    int http_base_proto = is_http_proto(filename);
257
+    int err;
258
+    if (!*pb || !http_base_proto || !hls->http_persistent) {
259
+        err = s->io_open(s, pb, filename, AVIO_FLAG_WRITE, options);
260
+    } else {
261
+        URLContext *http_url_context = ffio_geturlcontext(*pb);
262
+        av_assert0(http_url_context);
263
+        err = ff_http_do_new_request(http_url_context, filename);
264
+    }
265
+    return err;
266
+}
267
+
268
+static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename) {
269
+    HLSContext *hls = s->priv_data;
270
+    int http_base_proto = is_http_proto(filename);
271
+
272
+    if (!http_base_proto || !hls->http_persistent || hls->key_info_file || hls->encrypt) {
273
+        ff_format_io_close(s, pb);
274
+    }
275
+}
276
+
248 277
 static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
249 278
 {
250
-    const char *proto = avio_find_protocol_name(s->filename);
251
-    int http_base_proto = proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
279
+    int http_base_proto = is_http_proto(s->filename);
252 280
 
253 281
     if (c->method) {
254 282
         av_dict_set(options, "method", c->method, 0);
... ...
@@ -258,6 +288,8 @@ static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSCont
258 258
     }
259 259
     if (c->user_agent)
260 260
         av_dict_set(options, "user_agent", c->user_agent, 0);
261
+    if (c->http_persistent)
262
+        av_dict_set_int(options, "multiple_requests", 1, 0);
261 263
 
262 264
 }
263 265
 
... ...
@@ -1437,17 +1469,17 @@ static int hls_start(AVFormatContext *s, VariantStream *vs)
1437 1437
             err = AVERROR(ENOMEM);
1438 1438
             goto fail;
1439 1439
         }
1440
-        err = s->io_open(s, &oc->pb, filename, AVIO_FLAG_WRITE, &options);
1440
+        err = hlsenc_io_open(s, &oc->pb, filename, &options);
1441 1441
         av_free(filename);
1442 1442
         av_dict_free(&options);
1443 1443
         if (err < 0)
1444 1444
             return err;
1445 1445
     } else
1446
-        if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, &options)) < 0)
1446
+        if ((err = hlsenc_io_open(s, &oc->pb, oc->filename, &options)) < 0)
1447 1447
             goto fail;
1448 1448
     if (vs->vtt_basename) {
1449 1449
         set_http_options(s, &options, c);
1450
-        if ((err = s->io_open(s, &vtt_oc->pb, vtt_oc->filename, AVIO_FLAG_WRITE, &options)) < 0)
1450
+        if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->filename, &options)) < 0)
1451 1451
             goto fail;
1452 1452
     }
1453 1453
     av_dict_free(&options);
... ...
@@ -2165,11 +2197,12 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
2165 2165
                 avio_open_dyn_buf(&oc->pb);
2166 2166
                 vs->packets_written = 0;
2167 2167
                 ff_format_io_close(s, &vs->out);
2168
+                hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
2168 2169
             } else {
2169
-                ff_format_io_close(s, &oc->pb);
2170
+                hlsenc_io_close(s, &oc->pb, oc->filename);
2170 2171
             }
2171 2172
             if (vs->vtt_avf) {
2172
-                ff_format_io_close(s, &vs->vtt_avf->pb);
2173
+                hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->filename);
2173 2174
             }
2174 2175
         }
2175 2176
         if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
... ...
@@ -2355,6 +2388,7 @@ static const AVOption options[] = {
2355 2355
     {"var_stream_map", "Variant stream map string", OFFSET(var_stream_map), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,    E},
2356 2356
     {"master_pl_name", "Create HLS master playlist with this name", OFFSET(master_pl_name), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,    E},
2357 2357
     {"master_pl_publish_rate", "Publish master play list every after this many segment intervals", OFFSET(master_publish_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT_MAX, E},
2358
+    {"http_persistent", "Use persistent HTTP connections", OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
2358 2359
     { NULL },
2359 2360
 };
2360 2361