From patchwork Wed Oct 4 22:03:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 5415 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.76 with SMTP id m12csp1347964jah; Wed, 4 Oct 2017 15:09:54 -0700 (PDT) X-Received: by 10.223.178.199 with SMTP id g65mr11343998wrd.191.1507154993911; Wed, 04 Oct 2017 15:09:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507154993; cv=none; d=google.com; s=arc-20160816; b=eer83YLMMotFVjH0httLHFlJLW5Zk6DCWfNDqNSFbpM/du31jZDbLAflGzmmkZLCMJ MNUh3wozORvyBUPpbNru5UvxLs/c8kjG+FbCcfddNoNu88nORkeA/+qYuNF3CRa7s9XG 8foUADdursajXkI4AIJWsoz3OcEgdw+RWel2Q654znzKs8E5yJvFWNcLT8LyVMINKF4L 5xhv9TsdgaIZCHNKTPIjTILeCspeXGtum24k/UZY4j9tWQurk4WuZsKn28VEWjSfv7Cz FkQ11mkzE8CRQ1ibbkWUaNtMzgHvofF864/zviPiVRQwL6tXyNldEXYYwl1pZHMRvmbs RcDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=yMmpSyD0GARtMdP8AQw9rih72O4Mz+aj0Gic9pvi8fw=; b=nGTErRxltss9ewZvV1Jm99Op0OqZyE2ucRisCCpn3vZ2A6qvhea7vY09FjmAY6jnji Iqt1m1MHJfw5vQaIa3qQ5SKW3YQ0/AqZQT8wunSDJ/k2YoOqhcNuykrClx3kaQpni+eZ /PBBsrOxoASAVWcTCS94JTDOgAmyMeXDIsJTFWQntqYnBIh31pGL17yzz9myX0JBi8t9 W8E+Popewq5nCLcthBWdlBuzZYBMkWdzy7l/VogDdddiPpNpgZQRENo/fvRPlRyd9MDX MqpRhS+K2YKMmIv+apEt0Grsbz1NZRKDg5a+XslVDio1D3iaOaDt9GuQR9JabyTv2ogv p9HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=n1tadG8I; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v41si1374455wrb.54.2017.10.04.15.09.53; Wed, 04 Oct 2017 15:09:53 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=n1tadG8I; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 73B4F68A3B5; Thu, 5 Oct 2017 01:09:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f67.google.com (mail-pg0-f67.google.com [74.125.83.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D287368A3BD for ; Thu, 5 Oct 2017 01:09:30 +0300 (EEST) Received: by mail-pg0-f67.google.com with SMTP id i195so5901853pgd.3 for ; Wed, 04 Oct 2017 15:09:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=F8gEyPalSHH0k08TcHUK0x/HXdczENftO1/JAMOCS38=; b=n1tadG8I8kcRwGUY98WQjcXvJh55Ib6uzO/mA+gUZjojuEQKYl8mY2GDvX2ZEXbhTO VNHxQ+DafW+K2q+kt4Pvds6XOdhMLDoisWeA7nQWi+HyOMgcG5IYikopReNOOs4N6RtK qElg2xutFHiO/W/ztN5ODcNLmKVBpd0Aldk2ANNKiZT9QC7zJj4U6Ab13K8A4MwfrcT7 LPk/jvnwZdMqEI/7i6QaTvxOd97mxOHbr+NJj5TWVrBcr0fYGoQTKMSPkNYYEk/s4/hu HJM9VI/Im5p9GSpeqO0DIKUrjhewtihY3L0Y1RqcriWaK3TPw9cW5+L+oHUjWLcW2a4z tgow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=F8gEyPalSHH0k08TcHUK0x/HXdczENftO1/JAMOCS38=; b=iGD3jiqvAFtdEbpAoJmsRdp7JnuihaVrlxudFQKIgQ9vFFFz0k1U82oyZSc5W7U2YA xIoGiM3pD8i5HrfStV5qWC9J3G85Y80gs9LdF3rrqrFpZo/rTcndiX1JXo0Gd9DNyRRj tj6OfusgMsyA7Xbehj03cu9yggNWecaFQHpRP+CC+wtMnz4I0LbysoAeJNxUIFfJTFXF 4Gotp5hHcdZzxamcWntnvExwvAEK8wp89AV1V2ujBniU4XLXSjvWuiWPOaMfWD43G3Ba KkN9UIFdwet8r3SLr1RWMtckJVUSShyvEva6aK6sclf975icgj45I0WhIPKjDivs9hH2 HLzQ== X-Gm-Message-State: AMCzsaWriuV9udXVBZq/dtfxfe3JGvf5glfSMu1MxL4H8lWO5q5KOtTZ 5hN6jC8nSk+n1PL/G6fSR2dbcnKB X-Google-Smtp-Source: AOwi7QDktU8LaiG1bUx/0UVSLTcYMbhx79ZwhqZDT1Wk/5Kc0aer/GR7tyUAr8A01i1u5NuLjRZj+A== X-Received: by 10.159.249.4 with SMTP id bf4mr8994848plb.283.1507154620460; Wed, 04 Oct 2017 15:03:40 -0700 (PDT) Received: from tmm1-macbook.local.net (c-73-252-174-83.hsd1.ca.comcast.net. [73.252.174.83]) by smtp.gmail.com with ESMTPSA id u20sm26404120pfh.171.2017.10.04.15.03.39 (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 04 Oct 2017 15:03:39 -0700 (PDT) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Wed, 4 Oct 2017 15:03:34 -0700 Message-Id: <20171004220334.79264-3-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.13.5 (Apple Git-94) In-Reply-To: <20171004220334.79264-1-ffmpeg@tmm1.net> References: <20171004220334.79264-1-ffmpeg@tmm1.net> Subject: [FFmpeg-devel] [PATCH 3/3] avformat/hls: add http_keepalive option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Aman Gupta MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta This teaches the HLS demuxer to use the HTTP protocols multiple_requests=1 option, to take advantage of "Connection: Keep-Alive" when downloading playlists and segments from the HLS server. With the new option, you can avoid TCP connection and TLS negotiation overhead, which is particularly beneficial when streaming via a high-latency internet connection. --- libavformat/hls.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 786934af03..bf90582755 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -26,6 +26,7 @@ * http://tools.ietf.org/html/draft-pantos-http-live-streaming */ +#include "libavformat/http.h" #include "libavutil/avstring.h" #include "libavutil/avassert.h" #include "libavutil/intreadwrite.h" @@ -94,6 +95,7 @@ struct playlist { AVIOContext pb; uint8_t* read_buffer; AVIOContext *input; + int input_done; AVFormatContext *parent; int index; AVFormatContext *ctx; @@ -206,6 +208,8 @@ typedef struct HLSContext { int strict_std_compliance; char *allowed_extensions; int max_reload; + int http_keepalive; + AVIOContext *playlist_pb; } HLSContext; static int read_chomp_line(AVIOContext *s, char *buf, int maxlen) @@ -640,7 +644,17 @@ static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5)) return AVERROR_INVALIDDATA; - ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); + if (c->http_keepalive && *pb && av_strstart(proto_name, "http", NULL)) { + URLContext *uc = (URLContext *)av_opt_child_next(*pb, NULL); + (*pb)->eof_reached = 0; + ret = ff_http_do_new_request(uc, url); + if (ret < 0) { + ff_format_io_close(c->ctx, pb); + ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); + } + } else { + ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); + } if (ret >= 0) { // update cookies on http response with setcookies. char *new_cookies = NULL; @@ -683,10 +697,20 @@ static int parse_playlist(HLSContext *c, const char *url, char tmp_str[MAX_URL_SIZE]; struct segment *cur_init_section = NULL; + if (!in && c->http_keepalive && c->playlist_pb) { + in = c->playlist_pb; + URLContext *uc = (URLContext *)av_opt_child_next(in, NULL); + in->eof_reached = 0; + ret = ff_http_do_new_request(uc, url); + if (ret < 0) { + ff_format_io_close(c->ctx, &c->playlist_pb); + in = NULL; + } + } + if (!in) { #if 1 AVDictionary *opts = NULL; - close_in = 1; /* Some HLS servers don't like being sent the range header */ av_dict_set(&opts, "seekable", "0", 0); @@ -696,10 +720,18 @@ static int parse_playlist(HLSContext *c, const char *url, av_dict_set(&opts, "headers", c->headers, 0); av_dict_set(&opts, "http_proxy", c->http_proxy, 0); + if (c->http_keepalive) + av_dict_set(&opts, "multiple_requests", "1", 0); + ret = c->ctx->io_open(c->ctx, &in, url, AVIO_FLAG_READ, &opts); av_dict_free(&opts); if (ret < 0) return ret; + + if (c->http_keepalive) + c->playlist_pb = in; + else + close_in = 1; #else ret = open_in(c, &in, url); if (ret < 0) @@ -1111,6 +1143,9 @@ static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg) av_dict_set(&opts, "http_proxy", c->http_proxy, 0); av_dict_set(&opts, "seekable", "0", 0); + if (c->http_keepalive) + av_dict_set(&opts, "multiple_requests", "1", 0); + if (seg->size >= 0) { /* try to restrict the HTTP request to the part we want * (if this is in fact a HTTP request) */ @@ -1270,7 +1305,7 @@ restart: if (!v->needed) return AVERROR_EOF; - if (!v->input) { + if (!v->input || (c->http_keepalive && v->input_done)) { int64_t reload_interval; struct segment *seg; @@ -1329,6 +1364,7 @@ reload: goto reload; } + v->input_done = 0; seg = current_segment(v); /* load/update Media Initialization Section, if any */ @@ -1366,7 +1402,11 @@ reload: return ret; } - ff_format_io_close(v->parent, &v->input); + if (c->http_keepalive) { + v->input_done = 1; + } else { + ff_format_io_close(v->parent, &v->input); + } v->cur_seq_no++; c->cur_seq_no = v->cur_seq_no; @@ -1627,6 +1667,7 @@ static int hls_close(AVFormatContext *s) free_rendition_list(c); av_dict_free(&c->avio_opts); + ff_format_io_close(c->ctx, &c->playlist_pb); return 0; } @@ -2157,6 +2198,8 @@ static const AVOption hls_options[] = { INT_MIN, INT_MAX, FLAGS}, {"max_reload", "Maximum number of times a insufficient list is attempted to be reloaded", OFFSET(max_reload), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS}, + {"http_keepalive", "re-use http connections when reloading playlists and fetching new segments", + OFFSET(http_keepalive), AV_OPT_TYPE_BOOL, {.i64 = 0}, INT_MIN, 1, FLAGS}, {NULL} };