/**
* @file
* Apple HTTP Live Streaming demuxer
- * http://tools.ietf.org/html/draft-pantos-http-live-streaming
+ * https://www.rfc-editor.org/rfc/rfc8216.txt
*/
#include "libavformat/http.h"
AVFormatContext *parent;
int index;
AVFormatContext *ctx;
- AVPacket pkt;
+ AVPacket *pkt;
int has_noheader_flag;
/* main demuxer streams associated with this playlist
int finished;
enum PlaylistType type;
int64_t target_duration;
- int start_seq_no;
+ int64_t start_seq_no;
int n_segments;
struct segment **segments;
int needed;
int broken;
- int cur_seq_no;
- int last_seq_no;
+ int64_t cur_seq_no;
+ int64_t last_seq_no;
int m3u8_hold_counters;
int64_t cur_seg_offset;
int64_t last_load_time;
int n_renditions;
struct rendition **renditions;
- int cur_seq_no;
+ int64_t cur_seq_no;
int m3u8_hold_counters;
int live_start_index;
int first_packet;
av_dict_free(&pls->id3_initial);
ff_id3v2_free_extra_meta(&pls->id3_deferred_extra);
av_freep(&pls->init_sec_buf);
- av_packet_unref(&pls->pkt);
+ av_packet_free(&pls->pkt);
av_freep(&pls->pb.buffer);
ff_format_io_close(c->ctx, &pls->input);
pls->input_read_done = 0;
c->n_renditions = 0;
}
-/*
- * Used to reset a statically allocated AVPacket to a clean state,
- * containing no data.
- */
-static void reset_packet(AVPacket *pkt)
-{
- av_init_packet(pkt);
- pkt->data = NULL;
-}
-
static struct playlist *new_playlist(HLSContext *c, const char *url,
const char *base)
{
struct playlist *pls = av_mallocz(sizeof(struct playlist));
if (!pls)
return NULL;
- reset_packet(&pls->pkt);
+ pls->pkt = av_packet_alloc();
+ if (!pls->pkt) {
+ av_free(pls);
+ return NULL;
+ }
ff_make_absolute_url(pls->url, sizeof(pls->url), base, url);
- if (!pls->url[0])
+ if (!pls->url[0]) {
+ av_packet_free(&pls->pkt);
+ av_free(pls);
return NULL;
+ }
pls->seek_timestamp = AV_NOPTS_VALUE;
pls->is_id3_timestamped = -1;
}
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
- AVDictionary *opts, AVDictionary *opts2, int *is_http_out)
+ AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
{
HLSContext *c = s->priv_data;
AVDictionary *tmp = NULL;
else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
return AVERROR_INVALIDDATA;
- av_dict_copy(&tmp, opts, 0);
+ av_dict_copy(&tmp, *opts, 0);
av_dict_copy(&tmp, opts2, 0);
if (is_http && c->http_persistent && *pb) {
av_log(s, AV_LOG_WARNING,
"keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n",
url, av_err2str(ret));
+ av_dict_copy(&tmp, *opts, 0);
+ av_dict_copy(&tmp, opts2, 0);
ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
}
} else {
av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
if (new_cookies)
- av_dict_set(&opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
+ av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
}
av_dict_free(&tmp);
int is_http = av_strstart(url, "http", NULL);
struct segment **prev_segments = NULL;
int prev_n_segments = 0;
- int prev_start_seq_no = -1;
+ int64_t prev_start_seq_no = -1;
if (is_http && !in && c->http_persistent && c->playlist_pb) {
in = c->playlist_pb;
goto fail;
pls->target_duration = strtoll(ptr, NULL, 10) * AV_TIME_BASE;
} else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
+ uint64_t seq_no;
ret = ensure_playlist(c, &pls, url);
if (ret < 0)
goto fail;
- pls->start_seq_no = atoi(ptr);
+ seq_no = strtoull(ptr, NULL, 10);
+ if (seq_no > INT64_MAX) {
+ av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than "
+ "INT64_MAX, mask out the highest bit\n");
+ seq_no &= INT64_MAX;
+ }
+ pls->start_seq_no = seq_no;
} else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
ret = ensure_playlist(c, &pls, url);
if (ret < 0)
ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_init_section_args,
&info);
cur_init_section = new_init_section(pls, &info, url);
+ if (!cur_init_section) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
cur_init_section->key_type = key_type;
if (has_iv) {
memcpy(cur_init_section->iv, iv, sizeof(iv));
} else {
- int seq = pls->start_seq_no + pls->n_segments;
+ int64_t seq = pls->start_seq_no + pls->n_segments;
memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv));
- AV_WB32(cur_init_section->iv + 12, seq);
+ AV_WB64(cur_init_section->iv + 8, seq);
}
if (key_type != KEY_NONE) {
if (has_iv) {
memcpy(seg->iv, iv, sizeof(iv));
} else {
- int seq = pls->start_seq_no + pls->n_segments;
+ int64_t seq = pls->start_seq_no + pls->n_segments;
memset(seg->iv, 0, sizeof(seg->iv));
- AV_WB32(seg->iv + 12, seq);
+ AV_WB64(seg->iv + 8, seq);
}
if (key_type != KEY_NONE) {
if (prev_segments) {
if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE) {
int64_t prev_timestamp = c->first_timestamp;
- int i, diff = pls->start_seq_no - prev_start_seq_no;
+ int i;
+ int64_t diff = pls->start_seq_no - prev_start_seq_no;
for (i = 0; i < prev_n_segments && i < diff; i++) {
c->first_timestamp += prev_segments[i]->duration;
}
- av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%d -> %d)"
+ av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")"
" reflected in first_timestamp: %"PRId64" -> %"PRId64"\n",
prev_start_seq_no, pls->start_seq_no,
prev_timestamp, c->first_timestamp);
} else if (pls->start_seq_no < prev_start_seq_no) {
- av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %d -> %d\n",
+ av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n",
prev_start_seq_no, pls->start_seq_no);
}
free_segment_dynarray(prev_segments, prev_n_segments);
static struct segment *next_segment(struct playlist *pls)
{
- int n = pls->cur_seq_no - pls->start_seq_no + 1;
+ int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
if (n >= pls->n_segments)
return NULL;
return pls->segments[n];
seg->url, seg->url_offset, pls->index);
if (seg->key_type == KEY_NONE) {
- ret = open_url(pls->parent, in, seg->url, c->avio_opts, opts, &is_http);
+ ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http);
} else if (seg->key_type == KEY_AES_128) {
char iv[33], key[33], url[MAX_URL_SIZE];
if (strcmp(seg->key, pls->key_url)) {
AVIOContext *pb = NULL;
- if (open_url(pls->parent, &pb, seg->key, c->avio_opts, opts, NULL) == 0) {
+ if (open_url(pls->parent, &pb, seg->key, &c->avio_opts, opts, NULL) == 0) {
ret = avio_read(pb, pls->key, sizeof(pls->key));
if (ret != sizeof(pls->key)) {
av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s\n",
av_dict_set(&opts, "key", key, 0);
av_dict_set(&opts, "iv", iv, 0);
- ret = open_url(pls->parent, in, url, c->avio_opts, opts, &is_http);
+ ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http);
if (ret < 0) {
goto cleanup;
}
* as would be expected. Wrong offset received from the server will not be
* noticed without the call, though.
*/
- if (ret == 0 && !is_http && seg->key_type == KEY_NONE && seg->url_offset) {
+ if (ret == 0 && !is_http && seg->url_offset) {
int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET);
if (seekret < 0) {
av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
}
if (v->cur_seq_no < v->start_seq_no) {
av_log(v->parent, AV_LOG_WARNING,
- "skipping %d segments ahead, expired from playlists\n",
+ "skipping %"PRId64" segments ahead, expired from playlists\n",
v->start_seq_no - v->cur_seq_no);
v->cur_seq_no = v->start_seq_no;
}
if (ret < 0) {
if (ff_check_interrupt(c->interrupt_callback))
return AVERROR_EXIT;
- av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %d of playlist %d\n",
+ av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
v->cur_seq_no,
v->index);
v->cur_seq_no += 1;
if (ret < 0) {
if (ff_check_interrupt(c->interrupt_callback))
return AVERROR_EXIT;
- av_log(v->parent, AV_LOG_WARNING, "Failed to open next segment %d of playlist %d\n",
+ av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
v->cur_seq_no + 1,
v->index);
} else {
/* if timestamp was in valid range: returns 1 and sets seq_no
* if not: returns 0 and sets seq_no to closest segment */
static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls,
- int64_t timestamp, int *seq_no)
+ int64_t timestamp, int64_t *seq_no)
{
int i;
int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
return 0;
}
-static int select_cur_seq_no(HLSContext *c, struct playlist *pls)
+static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
{
- int seq_no;
+ int64_t seq_no;
if (!pls->finished && !c->first_packet &&
av_gettime_relative() - pls->last_load_time >= default_reload_interval(pls))
{
HLSContext *c = s->priv_data;
int ret = 0, i;
- int highest_cur_seq_no = 0;
+ int64_t highest_cur_seq_no = 0;
c->ctx = s;
c->interrupt_callback = &s->interrupt_callback;
/* Open the demuxer for each playlist */
for (i = 0; i < c->n_playlists; i++) {
struct playlist *pls = c->playlists[i];
+ const AVInputFormat *in_fmt = NULL;
char *url;
- ff_const59 AVInputFormat *in_fmt = NULL;
if (!(pls->ctx = avformat_alloc_context())) {
ret = AVERROR(ENOMEM);
read_data, NULL, NULL);
pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
+ pls->ctx->interrupt_callback = s->interrupt_callback;
url = av_strdup(pls->segments[0]->url);
ret = av_probe_input_buffer(&pls->pb, &in_fmt, url, NULL, 0, 0);
- av_free(url);
if (ret < 0) {
/* Free the ctx - it isn't initialized properly at this point,
* so avformat_close_input shouldn't be called. If
* avformat_open_input fails below, it frees and zeros the
* context, so it doesn't need any special treatment like this. */
- av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", pls->segments[0]->url);
+ av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url);
avformat_free_context(pls->ctx);
pls->ctx = NULL;
+ av_free(url);
goto fail;
}
+ av_free(url);
pls->ctx->pb = &pls->pb;
pls->ctx->io_open = nested_io_open;
pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
pls->seek_flags = AVSEEK_FLAG_ANY;
pls->seek_stream_index = -1;
}
- av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %d\n", i, pls->cur_seq_no);
+ av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no);
} else if (first && !cur_needed && pls->needed) {
ff_format_io_close(pls->parent, &pls->input);
pls->input_read_done = 0;
static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
{
if (pls->id3_offset >= 0) {
- pls->pkt.dts = pls->id3_mpegts_timestamp +
+ pls->pkt->dts = pls->id3_mpegts_timestamp +
av_rescale_q(pls->id3_offset,
- pls->ctx->streams[pls->pkt.stream_index]->time_base,
+ pls->ctx->streams[pls->pkt->stream_index]->time_base,
MPEG_TIME_BASE_Q);
- if (pls->pkt.duration)
- pls->id3_offset += pls->pkt.duration;
+ if (pls->pkt->duration)
+ pls->id3_offset += pls->pkt->duration;
else
pls->id3_offset = -1;
} else {
/* there have been packets with unknown duration
* since the last id3 tag, should not normally happen */
- pls->pkt.dts = AV_NOPTS_VALUE;
+ pls->pkt->dts = AV_NOPTS_VALUE;
}
- if (pls->pkt.duration)
- pls->pkt.duration = av_rescale_q(pls->pkt.duration,
- pls->ctx->streams[pls->pkt.stream_index]->time_base,
+ if (pls->pkt->duration)
+ pls->pkt->duration = av_rescale_q(pls->pkt->duration,
+ pls->ctx->streams[pls->pkt->stream_index]->time_base,
MPEG_TIME_BASE_Q);
- pls->pkt.pts = AV_NOPTS_VALUE;
+ pls->pkt->pts = AV_NOPTS_VALUE;
}
static AVRational get_timebase(struct playlist *pls)
if (pls->is_id3_timestamped)
return MPEG_TIME_BASE_Q;
- return pls->ctx->streams[pls->pkt.stream_index]->time_base;
+ return pls->ctx->streams[pls->pkt->stream_index]->time_base;
}
static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a,
struct playlist *pls = c->playlists[i];
/* Make sure we've got one buffered packet from each open playlist
* stream */
- if (pls->needed && !pls->pkt.data) {
+ if (pls->needed && !pls->pkt->data) {
while (1) {
int64_t ts_diff;
AVRational tb;
- ret = av_read_frame(pls->ctx, &pls->pkt);
+ ret = av_read_frame(pls->ctx, pls->pkt);
if (ret < 0) {
if (!avio_feof(&pls->pb) && ret != AVERROR_EOF)
return ret;
- reset_packet(&pls->pkt);
break;
} else {
/* stream_index check prevents matching picture attachments etc. */
- if (pls->is_id3_timestamped && pls->pkt.stream_index == 0) {
+ if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) {
/* audio elementary streams are id3 timestamped */
fill_timing_for_id3_timestamped_stream(pls);
}
if (c->first_timestamp == AV_NOPTS_VALUE &&
- pls->pkt.dts != AV_NOPTS_VALUE)
- c->first_timestamp = av_rescale_q(pls->pkt.dts,
+ pls->pkt->dts != AV_NOPTS_VALUE)
+ c->first_timestamp = av_rescale_q(pls->pkt->dts,
get_timebase(pls), AV_TIME_BASE_Q);
}
break;
if (pls->seek_stream_index < 0 ||
- pls->seek_stream_index == pls->pkt.stream_index) {
+ pls->seek_stream_index == pls->pkt->stream_index) {
- if (pls->pkt.dts == AV_NOPTS_VALUE) {
+ if (pls->pkt->dts == AV_NOPTS_VALUE) {
pls->seek_timestamp = AV_NOPTS_VALUE;
break;
}
tb = get_timebase(pls);
- ts_diff = av_rescale_rnd(pls->pkt.dts, AV_TIME_BASE,
+ ts_diff = av_rescale_rnd(pls->pkt->dts, AV_TIME_BASE,
tb.den, AV_ROUND_DOWN) -
pls->seek_timestamp;
if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
- pls->pkt.flags & AV_PKT_FLAG_KEY)) {
+ pls->pkt->flags & AV_PKT_FLAG_KEY)) {
pls->seek_timestamp = AV_NOPTS_VALUE;
break;
}
}
- av_packet_unref(&pls->pkt);
+ av_packet_unref(pls->pkt);
}
}
/* Check if this stream has the packet with the lowest dts */
- if (pls->pkt.data) {
+ if (pls->pkt->data) {
struct playlist *minpls = minplaylist < 0 ?
NULL : c->playlists[minplaylist];
if (minplaylist < 0) {
minplaylist = i;
} else {
- int64_t dts = pls->pkt.dts;
- int64_t mindts = minpls->pkt.dts;
+ int64_t dts = pls->pkt->dts;
+ int64_t mindts = minpls->pkt->dts;
if (dts == AV_NOPTS_VALUE ||
(mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
ret = update_streams_from_subdemuxer(s, pls);
if (ret < 0) {
- av_packet_unref(&pls->pkt);
+ av_packet_unref(pls->pkt);
return ret;
}
update_noheader_flag(s);
}
- if (pls->pkt.stream_index >= pls->n_main_streams) {
+ if (pls->pkt->stream_index >= pls->n_main_streams) {
av_log(s, AV_LOG_ERROR, "stream index inconsistency: index %d, %d main streams, %d subdemuxer streams\n",
- pls->pkt.stream_index, pls->n_main_streams, pls->ctx->nb_streams);
- av_packet_unref(&pls->pkt);
+ pls->pkt->stream_index, pls->n_main_streams, pls->ctx->nb_streams);
+ av_packet_unref(pls->pkt);
return AVERROR_BUG;
}
- ist = pls->ctx->streams[pls->pkt.stream_index];
- st = pls->main_streams[pls->pkt.stream_index];
+ ist = pls->ctx->streams[pls->pkt->stream_index];
+ st = pls->main_streams[pls->pkt->stream_index];
- av_packet_move_ref(pkt, &pls->pkt);
+ av_packet_move_ref(pkt, pls->pkt);
pkt->stream_index = st->index;
if (pkt->dts != AV_NOPTS_VALUE)
{
HLSContext *c = s->priv_data;
struct playlist *seek_pls = NULL;
- int i, seq_no;
- int j;
+ int i, j;
int stream_subdemuxer_index;
int64_t first_timestamp, seek_timestamp, duration;
+ int64_t seq_no;
if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
return AVERROR(ENOSYS);
pls->input_read_done = 0;
ff_format_io_close(pls->parent, &pls->input_next);
pls->input_next_requested = 0;
- av_packet_unref(&pls->pkt);
+ av_packet_unref(pls->pkt);
pls->pb.eof_reached = 0;
/* Clear any buffered data */
pls->pb.buf_end = pls->pb.buf_ptr = pls->pb.buffer;
OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
{"allowed_extensions", "List of file extensions that hls is allowed to access",
OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
- {.str = "3gp,aac,avi,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,wav"},
+ {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,wav"},
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},