2 * Apple HTTP Live Streaming demuxer
3 * Copyright (c) 2010 Martin Storsjo
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Apple HTTP Live Streaming demuxer
25 * http://tools.ietf.org/html/draft-pantos-http-live-streaming
28 #include "libavutil/avstring.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/dict.h"
33 #include "libavutil/time.h"
36 #include "avio_internal.h"
38 #define INITIAL_BUFFER_SIZE 32768
41 * An apple http stream consists of a playlist with media segment files,
42 * played sequentially. There may be several playlists with the same
43 * video content, in different bandwidth variants, that are played in
44 * parallel (preferably only one bandwidth variant at a time). In this case,
45 * the user supplied the url to a main playlist that only lists the variant
48 * If the main playlist doesn't point at any variants, we still create
49 * one anonymous toplevel variant for this, to maintain the structure.
59 char url[MAX_URL_SIZE];
60 char key[MAX_URL_SIZE];
61 enum KeyType key_type;
66 * Each variant has its own demuxer. If it currently is active,
67 * it has an open AVIOContext too, and potentially an AVPacket
68 * containing the next packet from this stream.
72 char url[MAX_URL_SIZE];
76 AVFormatContext *parent;
83 int64_t target_duration;
86 struct segment **segments;
87 int needed, cur_needed;
89 int64_t last_load_time;
91 char key_url[MAX_URL_SIZE];
95 typedef struct HLSContext {
98 struct variant **variants;
102 int64_t first_timestamp;
103 int64_t seek_timestamp;
105 AVIOInterruptCB *interrupt_callback;
106 AVDictionary *avio_opts;
109 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
111 int len = ff_get_line(s, buf, maxlen);
112 while (len > 0 && av_isspace(buf[len - 1]))
117 static void free_segment_list(struct variant *var)
120 for (i = 0; i < var->n_segments; i++)
121 av_free(var->segments[i]);
122 av_freep(&var->segments);
126 static void free_variant_list(HLSContext *c)
129 for (i = 0; i < c->n_variants; i++) {
130 struct variant *var = c->variants[i];
131 free_segment_list(var);
132 av_packet_unref(&var->pkt);
133 av_free(var->pb.buffer);
135 ff_format_io_close(c->ctx, &var->input);
138 avformat_close_input(&var->ctx);
142 av_freep(&c->variants);
147 * Used to reset a statically allocated AVPacket to a clean slate,
148 * containing no data.
150 static void reset_packet(AVPacket *pkt)
156 static struct variant *new_variant(HLSContext *c, int bandwidth,
157 const char *url, const char *base)
159 struct variant *var = av_mallocz(sizeof(struct variant));
162 reset_packet(&var->pkt);
163 var->bandwidth = bandwidth;
164 ff_make_absolute_url(var->url, sizeof(var->url), base, url);
165 dynarray_add(&c->variants, &c->n_variants, var);
169 struct variant_info {
173 static void handle_variant_args(struct variant_info *info, const char *key,
174 int key_len, char **dest, int *dest_len)
176 if (!strncmp(key, "BANDWIDTH=", key_len)) {
177 *dest = info->bandwidth;
178 *dest_len = sizeof(info->bandwidth);
183 char uri[MAX_URL_SIZE];
188 static void handle_key_args(struct key_info *info, const char *key,
189 int key_len, char **dest, int *dest_len)
191 if (!strncmp(key, "METHOD=", key_len)) {
192 *dest = info->method;
193 *dest_len = sizeof(info->method);
194 } else if (!strncmp(key, "URI=", key_len)) {
196 *dest_len = sizeof(info->uri);
197 } else if (!strncmp(key, "IV=", key_len)) {
199 *dest_len = sizeof(info->iv);
203 static int open_in(HLSContext *c, AVIOContext **in, const char *url)
205 AVDictionary *tmp = NULL;
208 av_dict_copy(&tmp, c->avio_opts, 0);
210 ret = c->ctx->io_open(c->ctx, in, url, AVIO_FLAG_READ, &tmp);
216 static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
217 const AVDictionary *opts)
219 AVDictionary *tmp = NULL;
222 av_dict_copy(&tmp, opts, 0);
224 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
231 static int parse_playlist(HLSContext *c, const char *url,
232 struct variant *var, AVIOContext *in)
234 int ret = 0, is_segment = 0, is_variant = 0, bandwidth = 0;
235 int64_t duration = 0;
236 enum KeyType key_type = KEY_NONE;
239 char key[MAX_URL_SIZE] = "";
243 uint8_t *new_url = NULL;
246 ret = open_in(c, &in, url);
252 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
255 read_chomp_line(in, line, sizeof(line));
256 if (strcmp(line, "#EXTM3U")) {
257 ret = AVERROR_INVALIDDATA;
262 free_segment_list(var);
265 while (!in->eof_reached) {
266 read_chomp_line(in, line, sizeof(line));
267 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
268 struct variant_info info = {{0}};
270 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,
272 bandwidth = atoi(info.bandwidth);
273 } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
274 struct key_info info = {{0}};
275 ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,
279 if (!strcmp(info.method, "AES-128"))
280 key_type = KEY_AES_128;
281 if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {
282 ff_hex_to_data(iv, info.iv + 2);
285 av_strlcpy(key, info.uri, sizeof(key));
286 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
288 var = new_variant(c, 0, url, NULL);
290 ret = AVERROR(ENOMEM);
294 var->target_duration = atoi(ptr) * AV_TIME_BASE;
295 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
297 var = new_variant(c, 0, url, NULL);
299 ret = AVERROR(ENOMEM);
303 var->start_seq_no = atoi(ptr);
304 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
307 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
309 duration = atof(ptr) * AV_TIME_BASE;
310 } else if (av_strstart(line, "#", NULL)) {
312 } else if (line[0]) {
314 if (!new_variant(c, bandwidth, line, url)) {
315 ret = AVERROR(ENOMEM);
324 var = new_variant(c, 0, url, NULL);
326 ret = AVERROR(ENOMEM);
330 seg = av_malloc(sizeof(struct segment));
332 ret = AVERROR(ENOMEM);
335 seg->duration = duration;
336 seg->key_type = key_type;
338 memcpy(seg->iv, iv, sizeof(iv));
340 int seq = var->start_seq_no + var->n_segments;
341 memset(seg->iv, 0, sizeof(seg->iv));
342 AV_WB32(seg->iv + 12, seq);
344 ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);
345 ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);
346 dynarray_add(&var->segments, &var->n_segments, seg);
352 var->last_load_time = av_gettime_relative();
357 ff_format_io_close(c->ctx, &in);
361 static int open_input(struct variant *var)
363 HLSContext *c = var->parent->priv_data;
364 struct segment *seg = var->segments[var->cur_seq_no - var->start_seq_no];
365 if (seg->key_type == KEY_NONE) {
366 return open_url(var->parent, &var->input, seg->url, c->avio_opts);
367 } else if (seg->key_type == KEY_AES_128) {
368 AVDictionary *opts = NULL;
369 char iv[33], key[33], url[MAX_URL_SIZE];
371 if (strcmp(seg->key, var->key_url)) {
373 if (open_url(var->parent, &pb, seg->key, c->avio_opts) == 0) {
374 ret = avio_read(pb, var->key, sizeof(var->key));
375 if (ret != sizeof(var->key)) {
376 av_log(NULL, AV_LOG_ERROR, "Unable to read key file %s\n",
379 ff_format_io_close(var->parent, &pb);
381 av_log(NULL, AV_LOG_ERROR, "Unable to open key file %s\n",
384 av_strlcpy(var->key_url, seg->key, sizeof(var->key_url));
386 ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
387 ff_data_to_hex(key, var->key, sizeof(var->key), 0);
388 iv[32] = key[32] = '\0';
389 if (strstr(seg->url, "://"))
390 snprintf(url, sizeof(url), "crypto+%s", seg->url);
392 snprintf(url, sizeof(url), "crypto:%s", seg->url);
394 av_dict_copy(&opts, c->avio_opts, 0);
395 av_dict_set(&opts, "key", key, 0);
396 av_dict_set(&opts, "iv", iv, 0);
398 ret = open_url(var->parent, &var->input, url, opts);
402 return AVERROR(ENOSYS);
405 static int read_data(void *opaque, uint8_t *buf, int buf_size)
407 struct variant *v = opaque;
408 HLSContext *c = v->parent->priv_data;
413 /* If this is a live stream and the reload interval has elapsed since
414 * the last playlist reload, reload the variant playlists now. */
415 int64_t reload_interval = v->n_segments > 0 ?
416 v->segments[v->n_segments - 1]->duration :
421 av_gettime_relative() - v->last_load_time >= reload_interval) {
422 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
424 /* If we need to reload the playlist again below (if
425 * there's still no more segments), switch to a reload
426 * interval of half the target duration. */
427 reload_interval = v->target_duration / 2;
429 if (v->cur_seq_no < v->start_seq_no) {
430 av_log(NULL, AV_LOG_WARNING,
431 "skipping %d segments ahead, expired from playlists\n",
432 v->start_seq_no - v->cur_seq_no);
433 v->cur_seq_no = v->start_seq_no;
435 if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
438 while (av_gettime_relative() - v->last_load_time < reload_interval) {
439 if (ff_check_interrupt(c->interrupt_callback))
443 /* Enough time has elapsed since the last reload */
451 ret = avio_read(v->input, buf, buf_size);
454 ff_format_io_close(c->ctx, &v->input);
457 c->end_of_segment = 1;
458 c->cur_seq_no = v->cur_seq_no;
460 if (v->ctx && v->ctx->nb_streams &&
461 v->parent->nb_streams >= v->stream_offset + v->ctx->nb_streams) {
463 for (i = v->stream_offset; i < v->stream_offset + v->ctx->nb_streams;
465 if (v->parent->streams[i]->discard < AVDISCARD_ALL)
470 av_log(v->parent, AV_LOG_INFO, "No longer receiving variant %d\n",
477 static int save_avio_options(AVFormatContext *s)
479 HLSContext *c = s->priv_data;
480 static const char *opts[] = { "headers", "user_agent", NULL };
481 const char **opt = opts;
486 if (av_opt_get(s->pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) {
487 ret = av_dict_set(&c->avio_opts, *opt, buf,
488 AV_DICT_DONT_STRDUP_VAL);
498 static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
499 int flags, AVDictionary **opts)
501 av_log(s, AV_LOG_ERROR,
502 "A HLS playlist item '%s' referred to an external file '%s'. "
503 "Opening this file was forbidden for security reasons\n",
505 return AVERROR(EPERM);
508 static int hls_read_header(AVFormatContext *s)
510 HLSContext *c = s->priv_data;
511 int ret = 0, i, j, stream_offset = 0;
514 c->interrupt_callback = &s->interrupt_callback;
516 if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
519 if ((ret = save_avio_options(s)) < 0)
522 if (c->n_variants == 0) {
523 av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
527 /* If the playlist only contained variants, parse each individual
528 * variant playlist. */
529 if (c->n_variants > 1 || c->variants[0]->n_segments == 0) {
530 for (i = 0; i < c->n_variants; i++) {
531 struct variant *v = c->variants[i];
532 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0)
537 if (c->variants[0]->n_segments == 0) {
538 av_log(NULL, AV_LOG_WARNING, "Empty playlist\n");
543 /* If this isn't a live stream, calculate the total duration of the
545 if (c->variants[0]->finished) {
546 int64_t duration = 0;
547 for (i = 0; i < c->variants[0]->n_segments; i++)
548 duration += c->variants[0]->segments[i]->duration;
549 s->duration = duration;
552 /* Open the demuxer for each variant */
553 for (i = 0; i < c->n_variants; i++) {
554 struct variant *v = c->variants[i];
555 AVInputFormat *in_fmt = NULL;
556 char bitrate_str[20];
559 if (v->n_segments == 0)
562 if (!(v->ctx = avformat_alloc_context())) {
563 ret = AVERROR(ENOMEM);
571 /* If this is a live stream with more than 3 segments, start at the
572 * third last segment. */
573 v->cur_seq_no = v->start_seq_no;
574 if (!v->finished && v->n_segments > 3)
575 v->cur_seq_no = v->start_seq_no + v->n_segments - 3;
577 v->read_buffer = av_malloc(INITIAL_BUFFER_SIZE);
578 ffio_init_context(&v->pb, v->read_buffer, INITIAL_BUFFER_SIZE, 0, v,
579 read_data, NULL, NULL);
581 ret = av_probe_input_buffer(&v->pb, &in_fmt, v->segments[0]->url,
584 /* Free the ctx - it isn't initialized properly at this point,
585 * so avformat_close_input shouldn't be called. If
586 * avformat_open_input fails below, it frees and zeros the
587 * context, so it doesn't need any special treatment like this. */
588 avformat_free_context(v->ctx);
593 v->ctx->io_open = nested_io_open;
594 v->stream_offset = stream_offset;
595 ret = avformat_open_input(&v->ctx, v->segments[0]->url, in_fmt, NULL);
599 v->ctx->ctx_flags &= ~AVFMTCTX_NOHEADER;
600 ret = avformat_find_stream_info(v->ctx, NULL);
603 snprintf(bitrate_str, sizeof(bitrate_str), "%d", v->bandwidth);
605 program = av_new_program(s, i);
608 av_dict_set(&program->metadata, "variant_bitrate", bitrate_str, 0);
610 /* Create new AVStreams for each stream in this variant */
611 for (j = 0; j < v->ctx->nb_streams; j++) {
612 AVStream *st = avformat_new_stream(s, NULL);
613 AVStream *ist = v->ctx->streams[j];
615 ret = AVERROR(ENOMEM);
618 ff_program_add_stream_index(s, i, stream_offset + j);
620 avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
621 avcodec_parameters_copy(st->codecpar, v->ctx->streams[j]->codecpar);
623 av_dict_set(&st->metadata, "variant_bitrate", bitrate_str,
626 stream_offset += v->ctx->nb_streams;
630 c->first_timestamp = AV_NOPTS_VALUE;
631 c->seek_timestamp = AV_NOPTS_VALUE;
635 free_variant_list(c);
639 static int recheck_discard_flags(AVFormatContext *s, int first)
641 HLSContext *c = s->priv_data;
644 /* Check if any new streams are needed */
645 for (i = 0; i < c->n_variants; i++)
646 c->variants[i]->cur_needed = 0;;
648 for (i = 0; i < s->nb_streams; i++) {
649 AVStream *st = s->streams[i];
650 struct variant *var = c->variants[s->streams[i]->id];
651 if (st->discard < AVDISCARD_ALL)
654 for (i = 0; i < c->n_variants; i++) {
655 struct variant *v = c->variants[i];
656 if (v->cur_needed && !v->needed) {
659 v->cur_seq_no = c->cur_seq_no;
660 v->pb.eof_reached = 0;
661 av_log(s, AV_LOG_INFO, "Now receiving variant %d\n", i);
662 } else if (first && !v->cur_needed && v->needed) {
664 ff_format_io_close(s, &v->input);
667 av_log(s, AV_LOG_INFO, "No longer receiving variant %d\n", i);
673 static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
675 HLSContext *c = s->priv_data;
676 int ret, i, minvariant = -1;
678 if (c->first_packet) {
679 recheck_discard_flags(s, 1);
684 c->end_of_segment = 0;
685 for (i = 0; i < c->n_variants; i++) {
686 struct variant *var = c->variants[i];
687 /* Make sure we've got one buffered packet from each open variant
689 if (var->needed && !var->pkt.data) {
693 ret = av_read_frame(var->ctx, &var->pkt);
695 if (!var->pb.eof_reached)
697 reset_packet(&var->pkt);
700 if (c->first_timestamp == AV_NOPTS_VALUE &&
701 var->pkt.dts != AV_NOPTS_VALUE)
702 c->first_timestamp = av_rescale_q(var->pkt.dts,
703 var->ctx->streams[var->pkt.stream_index]->time_base,
707 if (c->seek_timestamp == AV_NOPTS_VALUE)
710 if (var->pkt.dts == AV_NOPTS_VALUE) {
711 c->seek_timestamp = AV_NOPTS_VALUE;
715 st = var->ctx->streams[var->pkt.stream_index];
716 ts_diff = av_rescale_rnd(var->pkt.dts, AV_TIME_BASE,
717 st->time_base.den, AV_ROUND_DOWN) -
719 if (ts_diff >= 0 && (c->seek_flags & AVSEEK_FLAG_ANY ||
720 var->pkt.flags & AV_PKT_FLAG_KEY)) {
721 c->seek_timestamp = AV_NOPTS_VALUE;
724 av_packet_unref(&var->pkt);
725 reset_packet(&var->pkt);
728 /* Check if this stream still is on an earlier segment number, or
729 * has the packet with the lowest dts */
731 struct variant *minvar = minvariant < 0 ?
732 NULL : c->variants[minvariant];
733 if (minvariant < 0 || var->cur_seq_no < minvar->cur_seq_no) {
735 } else if (var->cur_seq_no == minvar->cur_seq_no) {
736 int64_t dts = var->pkt.dts;
737 int64_t mindts = minvar->pkt.dts;
738 AVStream *st = var->ctx->streams[var->pkt.stream_index];
739 AVStream *minst = minvar->ctx->streams[minvar->pkt.stream_index];
741 if (dts == AV_NOPTS_VALUE) {
743 } else if (mindts != AV_NOPTS_VALUE) {
744 if (st->start_time != AV_NOPTS_VALUE)
745 dts -= st->start_time;
746 if (minst->start_time != AV_NOPTS_VALUE)
747 mindts -= minst->start_time;
749 if (av_compare_ts(dts, st->time_base,
750 mindts, minst->time_base) < 0)
756 if (c->end_of_segment) {
757 if (recheck_discard_flags(s, 0))
760 /* If we got a packet, return it */
761 if (minvariant >= 0) {
762 *pkt = c->variants[minvariant]->pkt;
763 pkt->stream_index += c->variants[minvariant]->stream_offset;
764 reset_packet(&c->variants[minvariant]->pkt);
770 static int hls_close(AVFormatContext *s)
772 HLSContext *c = s->priv_data;
774 free_variant_list(c);
776 av_dict_free(&c->avio_opts);
781 static int hls_read_seek(AVFormatContext *s, int stream_index,
782 int64_t timestamp, int flags)
784 HLSContext *c = s->priv_data;
787 if ((flags & AVSEEK_FLAG_BYTE) || !c->variants[0]->finished)
788 return AVERROR(ENOSYS);
790 c->seek_flags = flags;
791 c->seek_timestamp = stream_index < 0 ? timestamp :
792 av_rescale_rnd(timestamp, AV_TIME_BASE,
793 s->streams[stream_index]->time_base.den,
794 flags & AVSEEK_FLAG_BACKWARD ?
795 AV_ROUND_DOWN : AV_ROUND_UP);
796 timestamp = av_rescale_rnd(timestamp, AV_TIME_BASE, stream_index >= 0 ?
797 s->streams[stream_index]->time_base.den :
798 AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
799 AV_ROUND_DOWN : AV_ROUND_UP);
800 if (s->duration < c->seek_timestamp) {
801 c->seek_timestamp = AV_NOPTS_VALUE;
806 for (i = 0; i < c->n_variants; i++) {
808 struct variant *var = c->variants[i];
809 int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
810 0 : c->first_timestamp;
812 ff_format_io_close(s, &var->input);
813 av_packet_unref(&var->pkt);
814 reset_packet(&var->pkt);
815 var->pb.eof_reached = 0;
816 /* Clear any buffered data */
817 var->pb.buf_end = var->pb.buf_ptr = var->pb.buffer;
818 /* Reset the pos, to let the mpegts demuxer know we've seeked. */
821 /* Locate the segment that contains the target timestamp */
822 for (j = 0; j < var->n_segments; j++) {
823 if (timestamp >= pos &&
824 timestamp < pos + var->segments[j]->duration) {
825 var->cur_seq_no = var->start_seq_no + j;
829 pos += var->segments[j]->duration;
832 c->seek_timestamp = AV_NOPTS_VALUE;
837 static int hls_probe(AVProbeData *p)
839 /* Require #EXTM3U at the start, and either one of the ones below
840 * somewhere for a proper match. */
841 if (strncmp(p->buf, "#EXTM3U", 7))
843 if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
844 strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
845 strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:"))
846 return AVPROBE_SCORE_MAX;
850 AVInputFormat ff_hls_demuxer = {
851 .name = "hls,applehttp",
852 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
853 .priv_data_size = sizeof(HLSContext),
854 .read_probe = hls_probe,
855 .read_header = hls_read_header,
856 .read_packet = hls_read_packet,
857 .read_close = hls_close,
858 .read_seek = hls_read_seek,