2010-07-22 10:30:15 +03:00
|
|
|
/*
|
|
|
|
* Apple HTTP Live Streaming Protocol Handler
|
|
|
|
* Copyright (c) 2010 Martin Storsjo
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Apple HTTP Live Streaming Protocol Handler
|
|
|
|
* http://tools.ietf.org/html/draft-pantos-http-live-streaming
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avstring.h"
|
2012-06-21 22:31:44 +03:00
|
|
|
#include "libavutil/time.h"
|
2010-07-22 10:30:15 +03:00
|
|
|
#include "avformat.h"
|
2016-01-30 03:17:51 +02:00
|
|
|
#include "avio_internal.h"
|
2010-07-22 10:30:15 +03:00
|
|
|
#include "internal.h"
|
2011-03-31 17:25:10 +03:00
|
|
|
#include "url.h"
|
2012-02-14 13:00:49 +03:00
|
|
|
#include "version.h"
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* An apple http stream consists of a playlist with media segment files,
|
|
|
|
* played sequentially. There may be several playlists with the same
|
|
|
|
* video content, in different bandwidth variants, that are played in
|
2012-07-06 02:38:53 +03:00
|
|
|
* parallel (preferably only one bandwidth variant at a time). In this case,
|
2010-07-22 10:30:15 +03:00
|
|
|
* the user supplied the url to a main playlist that only lists the variant
|
|
|
|
* playlists.
|
|
|
|
*
|
|
|
|
* If the main playlist doesn't point at any variants, we still create
|
|
|
|
* one anonymous toplevel variant for this, to maintain the structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct segment {
|
2013-07-29 10:27:09 +03:00
|
|
|
int64_t duration;
|
2010-07-22 10:30:15 +03:00
|
|
|
char url[MAX_URL_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct variant {
|
|
|
|
int bandwidth;
|
|
|
|
char url[MAX_URL_SIZE];
|
|
|
|
};
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
typedef struct HLSContext {
|
2010-07-22 10:30:15 +03:00
|
|
|
char playlisturl[MAX_URL_SIZE];
|
2013-07-29 10:27:09 +03:00
|
|
|
int64_t target_duration;
|
2010-07-22 10:30:15 +03:00
|
|
|
int start_seq_no;
|
|
|
|
int finished;
|
|
|
|
int n_segments;
|
|
|
|
struct segment **segments;
|
|
|
|
int n_variants;
|
|
|
|
struct variant **variants;
|
|
|
|
int cur_seq_no;
|
|
|
|
URLContext *seg_hd;
|
|
|
|
int64_t last_load_time;
|
2012-02-14 13:16:31 +03:00
|
|
|
} HLSContext;
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
|
|
|
|
{
|
|
|
|
int len = ff_get_line(s, buf, maxlen);
|
2013-03-03 13:17:50 +03:00
|
|
|
while (len > 0 && av_isspace(buf[len - 1]))
|
2010-07-22 10:30:15 +03:00
|
|
|
buf[--len] = '\0';
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
static void free_segment_list(HLSContext *s)
|
2010-07-22 10:30:15 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s->n_segments; i++)
|
2015-01-06 13:52:55 +02:00
|
|
|
av_freep(&s->segments[i]);
|
2010-07-22 10:30:15 +03:00
|
|
|
av_freep(&s->segments);
|
|
|
|
s->n_segments = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
static void free_variant_list(HLSContext *s)
|
2010-07-22 10:30:15 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s->n_variants; i++)
|
2015-01-06 13:52:55 +02:00
|
|
|
av_freep(&s->variants[i]);
|
2010-07-22 10:30:15 +03:00
|
|
|
av_freep(&s->variants);
|
|
|
|
s->n_variants = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct variant_info {
|
|
|
|
char bandwidth[20];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_variant_args(struct variant_info *info, const char *key,
|
|
|
|
int key_len, char **dest, int *dest_len)
|
|
|
|
{
|
|
|
|
if (!strncmp(key, "BANDWIDTH=", key_len)) {
|
|
|
|
*dest = info->bandwidth;
|
|
|
|
*dest_len = sizeof(info->bandwidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_playlist(URLContext *h, const char *url)
|
|
|
|
{
|
2012-02-14 13:16:31 +03:00
|
|
|
HLSContext *s = h->priv_data;
|
2010-07-22 10:30:15 +03:00
|
|
|
AVIOContext *in;
|
2013-07-29 10:27:09 +03:00
|
|
|
int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
|
|
|
|
int64_t duration = 0;
|
2010-07-22 10:30:15 +03:00
|
|
|
char line[1024];
|
|
|
|
const char *ptr;
|
|
|
|
|
2016-01-30 03:17:51 +02:00
|
|
|
if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
|
|
|
|
&h->interrupt_callback, NULL,
|
2016-03-03 19:14:26 +02:00
|
|
|
h->protocol_whitelist, h->protocol_blacklist)) < 0)
|
2010-07-22 10:30:15 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
read_chomp_line(in, line, sizeof(line));
|
2015-01-22 22:13:44 +02:00
|
|
|
if (strcmp(line, "#EXTM3U")) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
free_segment_list(s);
|
|
|
|
s->finished = 0;
|
2014-08-07 23:12:41 +03:00
|
|
|
while (!avio_feof(in)) {
|
2010-07-22 10:30:15 +03:00
|
|
|
read_chomp_line(in, line, sizeof(line));
|
|
|
|
if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
|
|
|
|
struct variant_info info = {{0}};
|
|
|
|
is_variant = 1;
|
|
|
|
ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
|
|
|
|
&info);
|
|
|
|
bandwidth = atoi(info.bandwidth);
|
|
|
|
} else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
|
2013-07-29 10:27:09 +03:00
|
|
|
s->target_duration = atoi(ptr) * AV_TIME_BASE;
|
2010-07-22 10:30:15 +03:00
|
|
|
} else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
|
|
|
|
s->start_seq_no = atoi(ptr);
|
|
|
|
} else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
|
|
|
|
s->finished = 1;
|
|
|
|
} else if (av_strstart(line, "#EXTINF:", &ptr)) {
|
|
|
|
is_segment = 1;
|
2013-07-29 10:27:09 +03:00
|
|
|
duration = atof(ptr) * AV_TIME_BASE;
|
2010-07-22 10:30:15 +03:00
|
|
|
} else if (av_strstart(line, "#", NULL)) {
|
|
|
|
continue;
|
|
|
|
} else if (line[0]) {
|
|
|
|
if (is_segment) {
|
|
|
|
struct segment *seg = av_malloc(sizeof(struct segment));
|
|
|
|
if (!seg) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
seg->duration = duration;
|
2011-03-17 12:24:23 +02:00
|
|
|
ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
|
2010-07-22 10:30:15 +03:00
|
|
|
dynarray_add(&s->segments, &s->n_segments, seg);
|
|
|
|
is_segment = 0;
|
|
|
|
} else if (is_variant) {
|
|
|
|
struct variant *var = av_malloc(sizeof(struct variant));
|
|
|
|
if (!var) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
var->bandwidth = bandwidth;
|
2011-03-17 12:24:23 +02:00
|
|
|
ff_make_absolute_url(var->url, sizeof(var->url), url, line);
|
2010-07-22 10:30:15 +03:00
|
|
|
dynarray_add(&s->variants, &s->n_variants, var);
|
|
|
|
is_variant = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 12:40:46 +03:00
|
|
|
s->last_load_time = av_gettime_relative();
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
avio_close(in);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
static int hls_close(URLContext *h)
|
2011-12-01 12:07:35 +03:00
|
|
|
{
|
2012-02-14 13:16:31 +03:00
|
|
|
HLSContext *s = h->priv_data;
|
2011-12-01 12:07:35 +03:00
|
|
|
|
|
|
|
free_segment_list(s);
|
|
|
|
free_variant_list(s);
|
|
|
|
ffurl_close(s->seg_hd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
static int hls_open(URLContext *h, const char *uri, int flags)
|
2010-07-22 10:30:15 +03:00
|
|
|
{
|
2012-02-14 13:16:31 +03:00
|
|
|
HLSContext *s = h->priv_data;
|
2010-07-22 10:30:15 +03:00
|
|
|
int ret, i;
|
|
|
|
const char *nested_url;
|
|
|
|
|
2011-04-15 17:42:09 +03:00
|
|
|
if (flags & AVIO_FLAG_WRITE)
|
2011-04-06 09:16:06 +03:00
|
|
|
return AVERROR(ENOSYS);
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
h->is_streamed = 1;
|
|
|
|
|
2012-02-14 13:00:49 +03:00
|
|
|
if (av_strstart(uri, "hls+", &nested_url)) {
|
2010-07-22 10:30:15 +03:00
|
|
|
av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
|
2012-02-14 13:00:49 +03:00
|
|
|
} else if (av_strstart(uri, "hls://", &nested_url)) {
|
|
|
|
av_log(h, AV_LOG_ERROR,
|
|
|
|
"No nested protocol specified. Specify e.g. hls+http://%s\n",
|
|
|
|
nested_url);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2010-07-22 10:30:15 +03:00
|
|
|
} else {
|
2011-04-20 12:40:29 +03:00
|
|
|
av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
|
2010-07-22 10:30:15 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-02-14 13:09:09 +03:00
|
|
|
av_log(h, AV_LOG_WARNING,
|
|
|
|
"Using the hls protocol is discouraged, please try using the "
|
|
|
|
"hls demuxer instead. The hls demuxer should be more complete "
|
|
|
|
"and work as well as the protocol implementation. (If not, "
|
|
|
|
"please report it.) To use the demuxer, simply use %s as url.\n",
|
|
|
|
s->playlisturl);
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
if ((ret = parse_playlist(h, s->playlisturl)) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (s->n_segments == 0 && s->n_variants > 0) {
|
|
|
|
int max_bandwidth = 0, maxvar = -1;
|
|
|
|
for (i = 0; i < s->n_variants; i++) {
|
|
|
|
if (s->variants[i]->bandwidth > max_bandwidth || i == 0) {
|
|
|
|
max_bandwidth = s->variants[i]->bandwidth;
|
|
|
|
maxvar = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_strlcpy(s->playlisturl, s->variants[maxvar]->url,
|
|
|
|
sizeof(s->playlisturl));
|
|
|
|
if ((ret = parse_playlist(h, s->playlisturl)) < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->n_segments == 0) {
|
2011-04-20 12:40:29 +03:00
|
|
|
av_log(h, AV_LOG_WARNING, "Empty playlist\n");
|
2010-07-22 10:30:15 +03:00
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->cur_seq_no = s->start_seq_no;
|
|
|
|
if (!s->finished && s->n_segments >= 3)
|
|
|
|
s->cur_seq_no = s->start_seq_no + s->n_segments - 3;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2012-02-14 13:16:31 +03:00
|
|
|
hls_close(h);
|
2010-07-22 10:30:15 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-14 13:16:31 +03:00
|
|
|
static int hls_read(URLContext *h, uint8_t *buf, int size)
|
2010-07-22 10:30:15 +03:00
|
|
|
{
|
2012-02-14 13:16:31 +03:00
|
|
|
HLSContext *s = h->priv_data;
|
2010-07-22 10:30:15 +03:00
|
|
|
const char *url;
|
|
|
|
int ret;
|
2011-12-28 17:55:46 +03:00
|
|
|
int64_t reload_interval;
|
2010-07-22 10:30:15 +03:00
|
|
|
|
|
|
|
start:
|
|
|
|
if (s->seg_hd) {
|
2011-03-31 17:31:43 +03:00
|
|
|
ret = ffurl_read(s->seg_hd, buf, size);
|
2010-07-22 10:30:15 +03:00
|
|
|
if (ret > 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (s->seg_hd) {
|
2011-03-31 18:36:06 +03:00
|
|
|
ffurl_close(s->seg_hd);
|
2010-07-22 10:30:15 +03:00
|
|
|
s->seg_hd = NULL;
|
|
|
|
s->cur_seq_no++;
|
|
|
|
}
|
2011-12-28 17:55:46 +03:00
|
|
|
reload_interval = s->n_segments > 0 ?
|
|
|
|
s->segments[s->n_segments - 1]->duration :
|
|
|
|
s->target_duration;
|
2010-07-22 10:30:15 +03:00
|
|
|
retry:
|
|
|
|
if (!s->finished) {
|
2014-10-22 12:40:46 +03:00
|
|
|
int64_t now = av_gettime_relative();
|
2011-12-28 17:55:46 +03:00
|
|
|
if (now - s->last_load_time >= reload_interval) {
|
2010-07-22 10:30:15 +03:00
|
|
|
if ((ret = parse_playlist(h, s->playlisturl)) < 0)
|
|
|
|
return ret;
|
2011-12-28 17:55:46 +03:00
|
|
|
/* If we need to reload the playlist again below (if
|
|
|
|
* there's still no more segments), switch to a reload
|
|
|
|
* interval of half the target duration. */
|
2013-07-29 10:27:09 +03:00
|
|
|
reload_interval = s->target_duration / 2;
|
2011-12-28 17:55:46 +03:00
|
|
|
}
|
2010-07-22 10:30:15 +03:00
|
|
|
}
|
|
|
|
if (s->cur_seq_no < s->start_seq_no) {
|
2011-04-20 12:40:29 +03:00
|
|
|
av_log(h, AV_LOG_WARNING,
|
2010-07-22 10:30:15 +03:00
|
|
|
"skipping %d segments ahead, expired from playlist\n",
|
|
|
|
s->start_seq_no - s->cur_seq_no);
|
|
|
|
s->cur_seq_no = s->start_seq_no;
|
|
|
|
}
|
|
|
|
if (s->cur_seq_no - s->start_seq_no >= s->n_segments) {
|
|
|
|
if (s->finished)
|
|
|
|
return AVERROR_EOF;
|
2014-10-22 12:40:46 +03:00
|
|
|
while (av_gettime_relative() - s->last_load_time < reload_interval) {
|
2011-11-06 23:34:24 +03:00
|
|
|
if (ff_check_interrupt(&h->interrupt_callback))
|
2011-03-13 01:42:27 +02:00
|
|
|
return AVERROR_EXIT;
|
2012-06-21 22:31:44 +03:00
|
|
|
av_usleep(100*1000);
|
2010-07-22 10:30:15 +03:00
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
url = s->segments[s->cur_seq_no - s->start_seq_no]->url,
|
2011-04-20 12:40:29 +03:00
|
|
|
av_log(h, AV_LOG_DEBUG, "opening %s\n", url);
|
2016-01-30 03:17:51 +02:00
|
|
|
ret = ffurl_open_whitelist(&s->seg_hd, url, AVIO_FLAG_READ,
|
|
|
|
&h->interrupt_callback, NULL,
|
2016-04-21 16:55:09 +02:00
|
|
|
h->protocol_whitelist, h->protocol_blacklist, h);
|
2010-07-22 10:30:15 +03:00
|
|
|
if (ret < 0) {
|
2011-11-06 23:34:24 +03:00
|
|
|
if (ff_check_interrupt(&h->interrupt_callback))
|
2011-03-13 01:42:27 +02:00
|
|
|
return AVERROR_EXIT;
|
2011-04-20 12:40:29 +03:00
|
|
|
av_log(h, AV_LOG_WARNING, "Unable to open %s\n", url);
|
2010-07-22 10:30:15 +03:00
|
|
|
s->cur_seq_no++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
2016-02-19 11:39:29 +02:00
|
|
|
const URLProtocol ff_hls_protocol = {
|
2012-02-14 13:00:49 +03:00
|
|
|
.name = "hls",
|
2012-02-14 13:16:31 +03:00
|
|
|
.url_open = hls_open,
|
|
|
|
.url_read = hls_read,
|
|
|
|
.url_close = hls_close,
|
2011-12-01 12:53:57 +03:00
|
|
|
.flags = URL_PROTOCOL_FLAG_NESTED_SCHEME,
|
2012-02-14 13:16:31 +03:00
|
|
|
.priv_data_size = sizeof(HLSContext),
|
2010-07-22 10:30:15 +03:00
|
|
|
};
|