FFmpeg
Loading...
Searching...
No Matches
hls.c
Go to the documentation of this file.
1/*
2 * Apple HTTP Live Streaming demuxer
3 * Copyright (c) 2010 Martin Storsjo
4 * Copyright (c) 2013 Anssi Hannula
5 * Copyright (c) 2021 Nachiket Tarate
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/**
25 * @file
26 * Apple HTTP Live Streaming demuxer
27 * https://www.rfc-editor.org/rfc/rfc8216.txt
28 */
29
30#include "config_components.h"
31
32#include "libavformat/http.h"
33#include "libavutil/aes.h"
34#include "libavutil/avstring.h"
35#include "libavutil/avassert.h"
38#include "libavutil/mem.h"
39#include "libavutil/opt.h"
40#include "libavutil/dict.h"
41#include "libavutil/time.h"
42#include "avformat.h"
43#include "demux.h"
44#include "internal.h"
45#include "avio_internal.h"
46#include "id3v2.h"
47#include "url.h"
48
50
51#define INITIAL_BUFFER_SIZE 32768
52
53#define MAX_FIELD_LEN 64
54#define MAX_CHARACTERISTICS_LEN 512
55
56#define MPEG_TIME_BASE 90000
57#define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
58
59#define PACKED_AUDIO_FORMATS "aac,ac3,eac3,mp3"
60
61/*
62 * An apple http stream consists of a playlist with media segment files,
63 * played sequentially. There may be several playlists with the same
64 * video content, in different bandwidth variants, that are played in
65 * parallel (preferably only one bandwidth variant at a time). In this case,
66 * the user supplied the url to a main playlist that only lists the variant
67 * playlists.
68 *
69 * If the main playlist doesn't point at any variants, we still create
70 * one anonymous toplevel variant for this, to maintain the structure.
71 */
72
78
79struct segment {
83 char *url;
84 char *key;
86 uint8_t iv[16];
87 /* associated Media Initialization Section, treated as a segment */
89};
90
91struct rendition;
92
98
99/*
100 * Each playlist has its own demuxer. If it currently is active,
101 * it has an open AVIOContext too, and potentially an AVPacket
102 * containing the next packet from this stream.
103 */
104struct playlist {
109 int64_t input_end; /* end of the byte range requested on input, or -1 */
114 int index;
118
119 /* main demuxer streams associated with this playlist
120 * indexed by the subdemuxer stream indexes */
123
140
141 /* Currently active Media Initialization Section */
143 uint8_t *init_sec_buf;
144 unsigned int init_sec_buf_size;
145 unsigned int init_sec_data_len;
147
149 uint8_t key[16];
150
151 /* ID3 timestamp handling (elementary audio streams have ID3 timestamps
152 * (and possibly other ID3 tags) in the beginning of each segment) */
153 int is_id3_timestamped; /* -1: not yet known */
154 int64_t id3_mpegts_timestamp; /* in mpegts tb */
155 int64_t id3_offset; /* in stream original tb */
156 uint8_t* id3_buf; /* temp buffer for id3 parsing */
157 unsigned int id3_buf_size;
158 AVDictionary *id3_initial; /* data from first id3 tag */
159 int id3_found; /* ID3 tag found at some point */
160 int id3_changed; /* ID3 tag data has changed at some point */
161 ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */
162
164
165 /* Constant offset of this playlist's DTS baseline relative to
166 * c->first_timestamp. */
170 int seek_stream_index; /* into subdemuxer stream array */
171
172 /* Renditions associated with this playlist, if any.
173 * Alternative rendition playlists have a single rendition associated
174 * with them, and variant main Media Playlists may have
175 * multiple (playlist-less) renditions associated with them. */
178
179 /* Media Initialization Sections (EXT-X-MAP) associated with this
180 * playlist, if any. */
183 int is_subtitle; /* Indicates if it's a subtitle playlist */
184
185 /* Some HLS streams repeat the current timed-ID3 metadata at every segment
186 * boundary so late-joining clients get a fresh copy. Track the last seen
187 * value to avoid raising AVSTREAM_EVENT_FLAG_METADATA_UPDATED spuriously
188 * on those duplicates. timed_id3_stream_index is the subdemuxer stream
189 * index of the first timed-ID3 stream seen; -1 if none. */
192};
193
194/*
195 * Renditions are e.g. alternative subtitle or audio streams.
196 * The rendition may either be an external playlist or it may be
197 * contained in the main Media Playlist of the variant (in which case
198 * playlist is NULL).
199 */
208
209struct variant {
211
212 /* every variant contains at least the main Media Playlist in index 0 */
215
219};
220
254
256{
257 int i;
258 for (i = 0; i < n_segments; i++) {
262 }
263}
264
265static void free_segment_list(struct playlist *pls)
266{
268 av_freep(&pls->segments);
269 pls->n_segments = 0;
270}
271
272static void free_init_section_list(struct playlist *pls)
273{
274 int i;
275 for (i = 0; i < pls->n_init_sections; i++) {
276 av_freep(&pls->init_sections[i]->key);
277 av_freep(&pls->init_sections[i]->url);
278 av_freep(&pls->init_sections[i]);
279 }
280 av_freep(&pls->init_sections);
281 pls->n_init_sections = 0;
282}
283
285{
286 int i;
287 for (i = 0; i < c->n_playlists; i++) {
288 struct playlist *pls = c->playlists[i];
291 av_freep(&pls->main_streams);
292 av_freep(&pls->renditions);
293 av_freep(&pls->id3_buf);
297 av_freep(&pls->init_sec_buf);
298 av_packet_free(&pls->pkt);
299 av_freep(&pls->pb.pub.buffer);
300 ff_format_io_close(c->ctx, &pls->input);
301 pls->input_read_done = 0;
302 ff_format_io_close(c->ctx, &pls->input_next);
303 pls->input_next_requested = 0;
304 if (pls->ctx) {
305 pls->ctx->pb = NULL;
307 }
308 av_free(pls);
309 }
310 av_freep(&c->playlists);
311 c->n_playlists = 0;
312}
313
315{
316 int i;
317 for (i = 0; i < c->n_variants; i++) {
318 struct variant *var = c->variants[i];
319 av_freep(&var->playlists);
320 av_free(var);
321 }
322 av_freep(&c->variants);
323 c->n_variants = 0;
324}
325
327{
328 int i;
329 for (i = 0; i < c->n_renditions; i++)
330 av_freep(&c->renditions[i]);
331 av_freep(&c->renditions);
332 c->n_renditions = 0;
333}
334
335static struct playlist *new_playlist(HLSContext *c, const char *url,
336 const char *base)
337{
338 struct playlist *pls;
339 char abs_url[MAX_URL_SIZE];
340
341 ff_make_absolute_url(abs_url, sizeof(abs_url), base, url);
342 if (!abs_url[0])
343 return NULL;
344
345 for (int i = 0; i < c->n_playlists; i++) {
346 if (!strcmp(c->playlists[i]->url, abs_url))
347 return c->playlists[i];
348 }
349
350 pls = av_mallocz(sizeof(struct playlist));
351 if (!pls)
352 return NULL;
353 pls->pkt = av_packet_alloc();
354 if (!pls->pkt) {
355 av_free(pls);
356 return NULL;
357 }
358 av_strlcpy(pls->url, abs_url, sizeof(pls->url));
361 pls->first_read_seq_no = -1;
362
363 pls->is_id3_timestamped = -1;
365 pls->timed_id3_stream_index = -1;
366
367 dynarray_add(&c->playlists, &c->n_playlists, pls);
368 return pls;
369}
370
372 char bandwidth[20];
373 /* variant group ids: */
377};
378
379static struct variant *new_variant(HLSContext *c, struct variant_info *info,
380 const char *url, const char *base)
381{
382 struct variant *var;
383 struct playlist *pls;
384
385 pls = new_playlist(c, url, base);
386 if (!pls)
387 return NULL;
388
389 var = av_mallocz(sizeof(struct variant));
390 if (!var)
391 return NULL;
392
393 if (info) {
394 var->bandwidth = atoi(info->bandwidth);
395 strcpy(var->audio_group, info->audio);
396 strcpy(var->video_group, info->video);
397 strcpy(var->subtitles_group, info->subtitles);
398 }
399
400 dynarray_add(&c->variants, &c->n_variants, var);
401 dynarray_add(&var->playlists, &var->n_playlists, pls);
402 return var;
403}
404
405static void handle_variant_args(void *context, const char *key,
406 int key_len, char **dest, int *dest_len)
407{
408 struct variant_info *info = context;
409 if (!strncmp(key, "BANDWIDTH=", key_len)) {
410 *dest = info->bandwidth;
411 *dest_len = sizeof(info->bandwidth);
412 } else if (!strncmp(key, "AUDIO=", key_len)) {
413 *dest = info->audio;
414 *dest_len = sizeof(info->audio);
415 } else if (!strncmp(key, "VIDEO=", key_len)) {
416 *dest = info->video;
417 *dest_len = sizeof(info->video);
418 } else if (!strncmp(key, "SUBTITLES=", key_len)) {
419 *dest = info->subtitles;
420 *dest_len = sizeof(info->subtitles);
421 }
422}
423
424struct key_info {
426 char method[11];
427 char iv[35];
428};
429
430static void handle_key_args(void *context, const char *key,
431 int key_len, char **dest, int *dest_len)
432{
433 struct key_info *info = context;
434 if (!strncmp(key, "METHOD=", key_len)) {
435 *dest = info->method;
436 *dest_len = sizeof(info->method);
437 } else if (!strncmp(key, "URI=", key_len)) {
438 *dest = info->uri;
439 *dest_len = sizeof(info->uri);
440 } else if (!strncmp(key, "IV=", key_len)) {
441 *dest = info->iv;
442 *dest_len = sizeof(info->iv);
443 }
444}
445
448 char byterange[32];
449};
450
451static struct segment *new_init_section(struct playlist *pls,
452 struct init_section_info *info,
453 const char *url_base)
454{
455 struct segment *sec;
456 char tmp_str[MAX_URL_SIZE], *ptr = tmp_str;
457
458 if (!info->uri[0])
459 return NULL;
460
461 sec = av_mallocz(sizeof(*sec));
462 if (!sec)
463 return NULL;
464
465 if (!av_strncasecmp(info->uri, "data:", 5)) {
466 ptr = info->uri;
467 } else {
468 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url_base, info->uri);
469 if (!tmp_str[0]) {
470 av_free(sec);
471 return NULL;
472 }
473 }
474 sec->url = av_strdup(ptr);
475 if (!sec->url) {
476 av_free(sec);
477 return NULL;
478 }
479
480 if (info->byterange[0]) {
481 sec->size = strtoll(info->byterange, NULL, 10);
482 ptr = strchr(info->byterange, '@');
483 if (ptr)
484 sec->url_offset = strtoll(ptr+1, NULL, 10);
485 if (sec->size < 0 || sec->url_offset < 0 || sec->size > INT64_MAX - sec->url_offset) {
486 av_freep(&sec->url);
487 av_free(sec);
488 return NULL;
489 }
490 } else {
491 /* the entire file is the init section */
492 sec->size = -1;
493 }
494
495 dynarray_add(&pls->init_sections, &pls->n_init_sections, sec);
496
497 return sec;
498}
499
500static void handle_init_section_args(void *context, const char *key,
501 int key_len, char **dest, int *dest_len)
502{
503 struct init_section_info *info = context;
504 if (!strncmp(key, "URI=", key_len)) {
505 *dest = info->uri;
506 *dest_len = sizeof(info->uri);
507 } else if (!strncmp(key, "BYTERANGE=", key_len)) {
508 *dest = info->byterange;
509 *dest_len = sizeof(info->byterange);
510 }
511}
512
524
525static struct rendition *new_rendition(HLSContext *c, struct rendition_info *info,
526 const char *url_base)
527{
528 struct rendition *rend;
530 char *characteristic;
531 char *chr_ptr;
532 char *saveptr;
533
534 if (!strcmp(info->type, "AUDIO"))
536 else if (!strcmp(info->type, "VIDEO"))
538 else if (!strcmp(info->type, "SUBTITLES"))
540 else if (!strcmp(info->type, "CLOSED-CAPTIONS"))
541 /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in
542 * AVC SEI RBSP anyway */
543 return NULL;
544
545 if (type == AVMEDIA_TYPE_UNKNOWN) {
546 av_log(c->ctx, AV_LOG_WARNING, "Can't support the type: %s\n", info->type);
547 return NULL;
548 }
549
550 /* URI is mandatory for subtitles as per spec */
551 if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0]) {
552 av_log(c->ctx, AV_LOG_ERROR, "The URI tag is REQUIRED for subtitle.\n");
553 return NULL;
554 }
555
556 rend = av_mallocz(sizeof(struct rendition));
557 if (!rend)
558 return NULL;
559
560 dynarray_add(&c->renditions, &c->n_renditions, rend);
561
562 rend->type = type;
563 strcpy(rend->group_id, info->group_id);
564 strcpy(rend->language, info->language);
565 strcpy(rend->name, info->name);
566
567 /* add the playlist if this is an external rendition */
568 if (info->uri[0]) {
569 rend->playlist = new_playlist(c, info->uri, url_base);
570 if (rend->playlist) {
572 rend->playlist->is_subtitle = 1;
573 rend->playlist->is_id3_timestamped = 0;
574 }
576 &rend->playlist->n_renditions, rend);
577 }
578 }
579
580 if (info->assoc_language[0]) {
581 size_t langlen = strlen(rend->language);
582 if (langlen < sizeof(rend->language) - 3) {
583 size_t assoc_len;
584 rend->language[langlen] = ',';
585 assoc_len = av_strlcpy(rend->language + langlen + 1,
586 info->assoc_language,
587 sizeof(rend->language) - langlen - 1);
588 if (langlen + assoc_len + 2 > sizeof(rend->language)) // truncation occurred
589 av_log(c->ctx, AV_LOG_WARNING, "Truncated rendition language: %s\n",
590 info->assoc_language);
591 }
592 }
593
594 if (!strcmp(info->defaultr, "YES"))
596 if (!strcmp(info->forced, "YES"))
598
599 chr_ptr = info->characteristics;
600 while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) {
601 if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound"))
603 else if (!strcmp(characteristic, "public.accessibility.describes-video"))
605
606 chr_ptr = NULL;
607 }
608
609 return rend;
610}
611
612static void handle_rendition_args(void *vinfo, const char *key,
613 int key_len, char **dest, int *dest_len)
614{
615 struct rendition_info *info = vinfo;
616
617 if (!strncmp(key, "TYPE=", key_len)) {
618 *dest = info->type;
619 *dest_len = sizeof(info->type);
620 } else if (!strncmp(key, "URI=", key_len)) {
621 *dest = info->uri;
622 *dest_len = sizeof(info->uri);
623 } else if (!strncmp(key, "GROUP-ID=", key_len)) {
624 *dest = info->group_id;
625 *dest_len = sizeof(info->group_id);
626 } else if (!strncmp(key, "LANGUAGE=", key_len)) {
627 *dest = info->language;
628 *dest_len = sizeof(info->language);
629 } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) {
630 *dest = info->assoc_language;
631 *dest_len = sizeof(info->assoc_language);
632 } else if (!strncmp(key, "NAME=", key_len)) {
633 *dest = info->name;
634 *dest_len = sizeof(info->name);
635 } else if (!strncmp(key, "DEFAULT=", key_len)) {
636 *dest = info->defaultr;
637 *dest_len = sizeof(info->defaultr);
638 } else if (!strncmp(key, "FORCED=", key_len)) {
639 *dest = info->forced;
640 *dest_len = sizeof(info->forced);
641 } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) {
642 *dest = info->characteristics;
643 *dest_len = sizeof(info->characteristics);
644 }
645 /*
646 * ignored:
647 * - AUTOSELECT: client may autoselect based on e.g. system language
648 * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4")
649 */
650}
651
652/* used by parse_playlist to allocate a new variant+playlist when the
653 * playlist is detected to be a Media Playlist (not Master Playlist)
654 * and we have no parent Master Playlist (parsing of which would have
655 * allocated the variant and playlist already)
656 * *pls == NULL => Master Playlist or parentless Media Playlist
657 * *pls != NULL => parented Media Playlist, playlist+variant allocated */
658static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
659{
660 if (*pls)
661 return 0;
662 struct variant *var = new_variant(c, NULL, url, NULL);
663 if (!var)
664 return AVERROR(ENOMEM);
665 *pls = var->playlists[0];
666 return 0;
667}
668
670{
671#if !CONFIG_HTTP_PROTOCOL
672 return 0;
673#else
675 return uc && !strncmp(uc->prot->name, "http", 4);
676#endif
677}
678
680 const char *url, AVDictionary **options)
681{
682#if !CONFIG_HTTP_PROTOCOL
684#else
685 int ret;
687 av_assert0(uc);
688 (*pb)->eof_reached = 0;
689 ret = ff_http_do_new_request2(uc, url, options);
690 if (ret < 0) {
692 }
693 return ret;
694#endif
695}
696
697static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
698 AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
699{
700 HLSContext *c = s->priv_data;
702 const char *proto_name = NULL;
703 int ret;
704 int is_http = 0;
705
706 if (av_strstart(url, "crypto", NULL)) {
707 if (url[6] == '+' || url[6] == ':')
708 proto_name = avio_find_protocol_name(url + 7);
709 } else if (av_strstart(url, "data", NULL)) {
710 if (url[4] == '+' || url[4] == ':')
711 proto_name = avio_find_protocol_name(url + 5);
712 }
713
714 if (!proto_name)
715 proto_name = avio_find_protocol_name(url);
716
717 if (!proto_name)
718 return AVERROR_INVALIDDATA;
719
720 // only http(s) & file are allowed
721 if (av_strstart(proto_name, "file", NULL)) {
722 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
724 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
725 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
726 url);
727 return AVERROR_INVALIDDATA;
728 }
729 } else if (av_strstart(proto_name, "http", NULL)) {
730 is_http = 1;
731 } else if (av_strstart(proto_name, "data", NULL)) {
732 ;
733 } else
734 return AVERROR_INVALIDDATA;
735
736 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
737 ;
738 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
739 ;
740 else if (av_strstart(url, "data", NULL) && !strncmp(proto_name, url + 5, strlen(proto_name)) && url[5 + strlen(proto_name)] == ':')
741 ;
742 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
743 return AVERROR_INVALIDDATA;
744
745 av_dict_copy(&tmp, *opts, 0);
746 av_dict_copy(&tmp, opts2, 0);
747
748 if (is_http && c->http_persistent && is_native_http(*pb)) {
749 ret = open_url_keepalive(c->ctx, pb, url, &tmp);
750 if (ret == AVERROR_EXIT) {
752 return ret;
753 } else if (ret < 0) {
754 if (ret != AVERROR_EOF)
756 "keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n",
757 url, av_err2str(ret));
758 av_dict_copy(&tmp, *opts, 0);
759 av_dict_copy(&tmp, opts2, 0);
760 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
761 }
762 } else {
763 av_assert0(!*pb);
764 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
765 }
766 if (ret >= 0) {
767 // update cookies on http response with setcookies.
768 char *new_cookies = NULL;
769
770 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
771 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
772
773 if (new_cookies)
774 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
775 }
776
778
779 if (is_http_out)
780 *is_http_out = is_http;
781
782 return ret;
783}
784
785static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
786{
787 HLSContext *c = s->priv_data;
788 int matchA = 3;
789 int matchF = 0;
790
791 if (!c->extension_picky)
792 return 0;
793
794 if (strcmp(c->allowed_segment_extensions, "ALL"))
795 matchA = av_match_ext (seg->url, c->allowed_segment_extensions)
796 + 2*(ff_match_url_ext(seg->url, c->allowed_segment_extensions) > 0);
797
798 if (!matchA) {
799 av_log(s, AV_LOG_ERROR, "URL %s is not in allowed_segment_extensions, consider updating hls.c and submitting a patch to ffmpeg-devel, if this should be added\n", seg->url);
800 return AVERROR_INVALIDDATA;
801 }
802
803 if (in_fmt) {
804 if (in_fmt->extensions) {
805 matchF = av_match_ext( seg->url, in_fmt->extensions)
806 + 2*(ff_match_url_ext(seg->url, in_fmt->extensions) > 0);
807 // Youtube uses aac files with .ts extension
808 if(av_match_name("mp4", in_fmt->name) || av_match_name("aac", in_fmt->name)) {
809 matchF |= av_match_ext( seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa")
810 + 2*(ff_match_url_ext(seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa") > 0);
811 }
812 } else if (!strcmp(in_fmt->name, "mpegts")) {
813 const char *str = "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts"
814 ",html" // https://flash1.bogulus.cfd/
815 ;
816 matchF = av_match_ext( seg->url, str)
817 + 2*(ff_match_url_ext(seg->url, str) > 0);
818 } else if (!strcmp(in_fmt->name, "webvtt")) {
819 matchF = av_match_ext( seg->url, "vtt,webvtt")
820 + 2*(ff_match_url_ext(seg->url, "vtt,webvtt") > 0);
821 }
822
823 if (!(matchA & matchF)) {
824 av_log(s, AV_LOG_ERROR, "detected format %s extension %s mismatches allowed extensions in url %s\n", in_fmt->name, in_fmt->extensions ? in_fmt->extensions : "none", seg->url);
825 return AVERROR_INVALIDDATA;
826 }
827 }
828
829 return 0;
830}
831
832static int parse_playlist(HLSContext *c, const char *url,
833 struct playlist *pls, AVIOContext *in)
834{
835 int ret = 0, is_segment = 0, is_variant = 0;
836 int64_t duration = 0;
837 enum KeyType key_type = KEY_NONE;
838 uint8_t iv[16] = "";
839 int has_iv = 0;
840 char key[MAX_URL_SIZE] = "";
841 char line[MAX_URL_SIZE];
842 const char *ptr;
843 int close_in = 0;
844 int64_t seg_offset = 0;
845 int64_t seg_size = -1;
846 uint8_t *new_url = NULL;
848 char tmp_str[MAX_URL_SIZE];
849 struct segment *cur_init_section = NULL;
850 int is_http = av_strstart(url, "http", NULL);
851 struct segment **prev_segments = NULL;
852 int prev_n_segments = 0;
853 int64_t prev_start_seq_no = -1;
854 int64_t load_start = av_gettime_relative();
855
856 if (is_http && !in && c->http_persistent && c->playlist_pb) {
857 in = c->playlist_pb;
858 ret = open_url_keepalive(c->ctx, &c->playlist_pb, url, NULL);
859 if (ret == AVERROR_EXIT) {
860 return ret;
861 } else if (ret < 0) {
862 if (ret != AVERROR_EOF)
863 av_log(c->ctx, AV_LOG_WARNING,
864 "keepalive request failed for '%s' with error: '%s' when parsing playlist\n",
865 url, av_err2str(ret));
866 in = NULL;
867 }
868 }
869
870 if (!in) {
872 av_dict_copy(&opts, c->avio_opts, 0);
873
874 if (c->http_persistent)
875 av_dict_set(&opts, "multiple_requests", "1", 0);
876
877 ret = open_url(c->ctx, &in, url, &opts, NULL, NULL);
879 if (ret < 0)
880 return ret;
881
882 if (is_http && c->http_persistent && is_native_http(in))
883 c->playlist_pb = in;
884 else
885 close_in = 1;
886 }
887
888 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
889 url = new_url;
890
891 ff_get_chomp_line(in, line, sizeof(line));
892 if (strcmp(line, "#EXTM3U")) {
894 goto fail;
895 }
896
897 if (pls) {
898 prev_start_seq_no = pls->start_seq_no;
899 prev_segments = pls->segments;
900 prev_n_segments = pls->n_segments;
901 pls->segments = NULL;
902 pls->n_segments = 0;
903
904 pls->finished = 0;
906 }
907 while (!avio_feof(in)) {
908 ff_get_chomp_line(in, line, sizeof(line));
909 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
910 if (pls) {
911 av_log(c->ctx, AV_LOG_ERROR,
912 "Master Playlist tag found in a Media Playlist\n");
914 goto fail;
915 }
916 is_variant = 1;
917 memset(&variant_info, 0, sizeof(variant_info));
919 } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
920 struct key_info info = {{0}};
922 key_type = KEY_NONE;
923 has_iv = 0;
924 if (!strcmp(info.method, "AES-128"))
925 key_type = KEY_AES_128;
926 if (!strcmp(info.method, "SAMPLE-AES"))
927 key_type = KEY_SAMPLE_AES;
928 if (!av_strncasecmp(info.iv, "0x", 2)) {
929 ff_hex_to_data(iv, info.iv + 2);
930 has_iv = 1;
931 }
932 av_strlcpy(key, info.uri, sizeof(key));
933 } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
934 struct rendition_info info = {{0}};
935 if (pls) {
936 av_log(c->ctx, AV_LOG_ERROR,
937 "Master Playlist tag found in a Media Playlist\n");
939 goto fail;
940 }
942 new_rendition(c, &info, url);
943 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
944 int64_t t;
945 ret = ensure_playlist(c, &pls, url);
946 if (ret < 0)
947 goto fail;
948 t = strtoll(ptr, NULL, 10);
949 if (t < 0 || t >= INT64_MAX / AV_TIME_BASE) {
951 goto fail;
952 }
953 pls->target_duration = t * AV_TIME_BASE;
954 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
955 uint64_t seq_no;
956 ret = ensure_playlist(c, &pls, url);
957 if (ret < 0)
958 goto fail;
959 seq_no = strtoull(ptr, NULL, 10);
960 if (seq_no > INT64_MAX/2) {
961 av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than "
962 "INT64_MAX/2, mask out the highest bit\n");
963 seq_no &= INT64_MAX/2;
964 }
965 pls->start_seq_no = seq_no;
966 } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
967 ret = ensure_playlist(c, &pls, url);
968 if (ret < 0)
969 goto fail;
970 if (!strcmp(ptr, "EVENT"))
971 pls->type = PLS_TYPE_EVENT;
972 else if (!strcmp(ptr, "VOD"))
973 pls->type = PLS_TYPE_VOD;
974 } else if (av_strstart(line, "#EXT-X-MAP:", &ptr)) {
975 struct init_section_info info = {{0}};
976 ret = ensure_playlist(c, &pls, url);
977 if (ret < 0)
978 goto fail;
980 cur_init_section = new_init_section(pls, &info, url);
981 if (!cur_init_section) {
982 ret = AVERROR(ENOMEM);
983 goto fail;
984 }
985 cur_init_section->key_type = key_type;
986 if (has_iv) {
987 memcpy(cur_init_section->iv, iv, sizeof(iv));
988 } else {
989 int64_t seq = pls->start_seq_no + pls->n_segments;
990 memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv));
991 AV_WB64(cur_init_section->iv + 8, seq);
992 }
993
994 if (key_type != KEY_NONE) {
995 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
996 if (!tmp_str[0]) {
997 av_free(cur_init_section);
999 goto fail;
1000 }
1001 cur_init_section->key = av_strdup(tmp_str);
1002 if (!cur_init_section->key) {
1003 av_free(cur_init_section);
1004 ret = AVERROR(ENOMEM);
1005 goto fail;
1006 }
1007 } else {
1008 cur_init_section->key = NULL;
1009 }
1010
1011 } else if (av_strstart(line, "#EXT-X-START:", &ptr)) {
1012 const char *time_offset_value = NULL;
1013 ret = ensure_playlist(c, &pls, url);
1014 if (ret < 0) {
1015 goto fail;
1016 }
1017 if (av_strstart(ptr, "TIME-OFFSET=", &time_offset_value)) {
1018 double offset = strtod(time_offset_value, NULL) * AV_TIME_BASE;
1019 if (offset >= -0x1p63 && offset < 0x1p63) {
1021 pls->time_offset_flag = 1;
1022 } else {
1023 av_log(c->ctx, AV_LOG_WARNING, "TIME-OFFSET value is"
1024 "invalid, it will be ignored");
1025 }
1026 } else {
1027 av_log(c->ctx, AV_LOG_WARNING, "#EXT-X-START value is"
1028 "invalid, it will be ignored");
1029 continue;
1030 }
1031 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
1032 if (pls)
1033 pls->finished = 1;
1034 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
1035 double d = atof(ptr) * AV_TIME_BASE;
1036 if (d < 0 || d > INT64_MAX || isnan(d)) {
1037 av_log(c->ctx, AV_LOG_WARNING, "EXTINF %f unsupported\n", d / AV_TIME_BASE);
1038 d = 0;
1039 }
1040 duration = d;
1041 is_segment = 1;
1042 } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
1043 seg_size = strtoll(ptr, NULL, 10);
1044 ptr = strchr(ptr, '@');
1045 if (ptr)
1046 seg_offset = strtoll(ptr+1, NULL, 10);
1047 if (seg_size < 0 || seg_offset > INT64_MAX - seg_size) {
1048 ret = AVERROR_INVALIDDATA;
1049 goto fail;
1050 }
1051 } else if (av_strstart(line, "#", NULL)) {
1052 av_log(c->ctx, AV_LOG_VERBOSE, "Skip ('%s')\n", line);
1053 continue;
1054 } else if (line[0]) {
1055 if (is_variant) {
1056 if (!new_variant(c, &variant_info, line, url)) {
1057 ret = AVERROR(ENOMEM);
1058 goto fail;
1059 }
1060 is_variant = 0;
1061 }
1062 if (is_segment) {
1063 struct segment *seg;
1064 ret = ensure_playlist(c, &pls, url);
1065 if (ret < 0)
1066 goto fail;
1067 seg = av_malloc(sizeof(struct segment));
1068 if (!seg) {
1069 ret = AVERROR(ENOMEM);
1070 goto fail;
1071 }
1072 if (has_iv) {
1073 memcpy(seg->iv, iv, sizeof(iv));
1074 } else {
1075 uint64_t seq = pls->start_seq_no + (uint64_t)pls->n_segments;
1076 memset(seg->iv, 0, sizeof(seg->iv));
1077 AV_WB64(seg->iv + 8, seq);
1078 }
1079
1080 if (key_type != KEY_NONE) {
1081 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
1082 if (!tmp_str[0]) {
1083 ret = AVERROR_INVALIDDATA;
1084 av_free(seg);
1085 goto fail;
1086 }
1087 seg->key = av_strdup(tmp_str);
1088 if (!seg->key) {
1089 av_free(seg);
1090 ret = AVERROR(ENOMEM);
1091 goto fail;
1092 }
1093 } else {
1094 seg->key = NULL;
1095 }
1096
1097 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line);
1098 if (!tmp_str[0]) {
1099 ret = AVERROR_INVALIDDATA;
1100 if (seg->key)
1101 av_free(seg->key);
1102 av_free(seg);
1103 goto fail;
1104 }
1105 seg->url = av_strdup(tmp_str);
1106 if (!seg->url) {
1107 av_free(seg->key);
1108 av_free(seg);
1109 ret = AVERROR(ENOMEM);
1110 goto fail;
1111 }
1112
1113 ret = test_segment(c->ctx, pls->ctx ? pls->ctx->iformat : NULL, pls, seg);
1114 if (ret < 0) {
1115 av_free(seg->url);
1116 av_free(seg->key);
1117 av_free(seg);
1118 goto fail;
1119 }
1120
1121 if (duration < 0.001 * AV_TIME_BASE) {
1122 av_log(c->ctx, AV_LOG_WARNING, "Cannot get correct #EXTINF value of segment %s,"
1123 " set to default value to 1ms.\n", seg->url);
1124 duration = 0.001 * AV_TIME_BASE;
1125 }
1126 seg->duration = duration;
1127 seg->key_type = key_type;
1128 dynarray_add(&pls->segments, &pls->n_segments, seg);
1129 is_segment = 0;
1130
1131 seg->size = seg_size;
1132 if (seg_size >= 0) {
1133 seg->url_offset = seg_offset;
1134 seg_offset += seg_size;
1135 seg_size = -1;
1136 } else {
1137 seg->url_offset = 0;
1138 seg_offset = 0;
1139 }
1140
1141 seg->init_section = cur_init_section;
1142 }
1143 }
1144 }
1145 if (prev_segments) {
1146 if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE &&
1147 c->first_timestamp_pls == pls) {
1148 int64_t prev_timestamp = c->first_timestamp;
1149 int i;
1150 int64_t diff = pls->start_seq_no - prev_start_seq_no;
1151 for (i = 0; i < prev_n_segments && i < diff; i++) {
1152 c->first_timestamp += prev_segments[i]->duration;
1153 }
1154 av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")"
1155 " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n",
1156 prev_start_seq_no, pls->start_seq_no,
1157 prev_timestamp, c->first_timestamp);
1158 } else if (pls->start_seq_no < prev_start_seq_no) {
1159 av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n",
1160 prev_start_seq_no, pls->start_seq_no);
1161 }
1162 }
1163
1164fail:
1165 free_segment_dynarray(prev_segments, prev_n_segments);
1166 av_freep(&prev_segments);
1167 if (pls)
1168 pls->last_load_time = load_start;
1169 av_free(new_url);
1170 if (close_in)
1171 ff_format_io_close(c->ctx, &in);
1172 c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE;
1173 if (!c->n_variants || !c->variants[0]->n_playlists ||
1174 !c->variants[0]->playlists[0]->n_segments)
1175 c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE;
1176
1177 if (c->n_variants && c->variants[0]->n_playlists &&
1178 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT &&
1179 !c->variants[0]->playlists[0]->finished) {
1180 struct playlist *p = c->variants[0]->playlists[0];
1181 int64_t duration = 0;
1182 for (int i = 0; i < p->n_segments; i++)
1183 duration += p->segments[i]->duration;
1184 c->ctx->duration = duration;
1185 }
1186
1187 return ret;
1188}
1189
1190static struct segment *current_segment(struct playlist *pls)
1191{
1192 int64_t n = pls->cur_seq_no - pls->start_seq_no;
1193 if (n >= pls->n_segments)
1194 return NULL;
1195 return pls->segments[n];
1196}
1197
1198static struct segment *next_segment(struct playlist *pls)
1199{
1200 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1201 if (n >= pls->n_segments)
1202 return NULL;
1203 return pls->segments[n];
1204}
1205
1206/* True if 'next' is an unencrypted byte range that directly follows 'cur' in
1207 * the same resource and ends within the range requested up to 'end', so that
1208 * it is read on from the input of 'cur' (i.e. consecutive EXT-X-BYTERANGE
1209 * segments). */
1210static int segment_continues(const struct segment *cur,
1211 const struct segment *next, int64_t end)
1212{
1213 return cur && next &&
1214 cur->size >= 0 && next->size >= 0 &&
1215 next->url_offset == cur->url_offset + cur->size &&
1216 next->url_offset + next->size <= end &&
1217 cur->key_type == KEY_NONE && next->key_type == KEY_NONE &&
1218 next->init_section == cur->init_section &&
1219 !strcmp(next->url, cur->url);
1220}
1221
1222static int64_t range_end(struct playlist *pls, const struct segment *seg)
1223{
1224 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1225
1226 if (seg == next_segment(pls))
1227 n++;
1228 else if (seg != current_segment(pls))
1229 return seg->url_offset + seg->size;
1230 for (; n < pls->n_segments &&
1231 segment_continues(seg, pls->segments[n], INT64_MAX); n++)
1232 seg = pls->segments[n];
1233 return seg->url_offset + seg->size;
1234}
1235
1236static int read_from_url(struct playlist *pls, struct segment *seg,
1237 uint8_t *buf, int buf_size)
1238{
1239 int ret;
1240
1241 /* limit read if the segment was only a part of a file */
1242 if (seg->size >= 0)
1243 buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
1244
1245 ret = avio_read(pls->input, buf, buf_size);
1246 if (ret > 0)
1247 pls->cur_seg_offset += ret;
1248
1249 return ret;
1250}
1251
1252/* Parse the raw ID3 data and pass contents to caller */
1254 AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info,
1255 ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
1256{
1257 static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp";
1258 static const char id3_priv_owner_audio_setup[] = "com.apple.streaming.audioDescription";
1259 ID3v2ExtraMeta *meta;
1260
1262 for (meta = *extra_meta; meta; meta = meta->next) {
1263 if (!strcmp(meta->tag, "PRIV")) {
1264 ID3v2ExtraMetaPRIV *priv = &meta->data.priv;
1265 if (priv->datasize == 8 && !av_strncasecmp(priv->owner, id3_priv_owner_ts, 44)) {
1266 /* 33-bit MPEG timestamp */
1267 int64_t ts = AV_RB64(priv->data);
1268 av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts);
1269 if ((ts & ~((1ULL << 33) - 1)) == 0)
1270 *dts = ts;
1271 else
1272 av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts);
1273 } else if (priv->datasize >= 8 && !av_strncasecmp(priv->owner, id3_priv_owner_audio_setup, 36)) {
1274 ff_hls_senc_read_audio_setup_info(audio_setup_info, priv->data, priv->datasize);
1275 }
1276 } else if (!strcmp(meta->tag, "APIC") && apic)
1277 *apic = &meta->data.apic;
1278 }
1279}
1280
1281/* Check if the ID3 metadata contents have changed */
1283 ID3v2ExtraMetaAPIC *apic)
1284{
1285 const AVDictionaryEntry *entry = NULL;
1286 const AVDictionaryEntry *oldentry;
1287 /* check that no keys have changed values */
1288 while ((entry = av_dict_iterate(metadata, entry))) {
1289 oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE);
1290 if (!oldentry || strcmp(oldentry->value, entry->value) != 0)
1291 return 1;
1292 }
1293
1294 /* check if apic appeared */
1295 if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
1296 return 1;
1297
1298 if (apic) {
1299 int size = pls->ctx->streams[1]->attached_pic.size;
1300 if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE)
1301 return 1;
1302
1303 if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
1304 return 1;
1305 }
1306
1307 return 0;
1308}
1309
1310/* Parse ID3 data and handle the found data */
1311static void handle_id3(AVIOContext *pb, struct playlist *pls)
1312{
1314 ID3v2ExtraMetaAPIC *apic = NULL;
1315 ID3v2ExtraMeta *extra_meta = NULL;
1316 int64_t timestamp = AV_NOPTS_VALUE;
1317
1318 parse_id3(pls->ctx, pb, &metadata, &timestamp, &pls->audio_setup_info, &apic, &extra_meta);
1319
1320 if (timestamp != AV_NOPTS_VALUE) {
1321 pls->id3_mpegts_timestamp = timestamp;
1322 pls->id3_offset = 0;
1323 }
1324
1325 if (!pls->id3_found) {
1326 /* initial ID3 tags */
1328 pls->id3_found = 1;
1329
1330 /* get picture attachment and set text metadata */
1331 if (pls->ctx->nb_streams) {
1333 ff_id3v2_parse_apic(pls->ctx, extra_meta);
1334 } else
1335 /* demuxer not yet opened, defer picture attachment */
1336 pls->id3_deferred_extra = extra_meta;
1337
1338 ff_id3v2_parse_priv_dict(&metadata, extra_meta);
1339 av_dict_copy(&pls->ctx->metadata, metadata, 0);
1340 pls->id3_initial = metadata;
1341
1342 } else {
1343 if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) {
1344 avpriv_report_missing_feature(pls->parent, "Changing ID3 metadata in HLS audio elementary stream");
1345 pls->id3_changed = 1;
1346 }
1348 }
1349
1350 if (pls->id3_deferred_extra != extra_meta)
1351 ff_id3v2_free_extra_meta(&extra_meta);
1352}
1353
1354static void intercept_id3(struct playlist *pls, uint8_t *buf,
1355 int buf_size, int *len)
1356{
1357 /* intercept id3 tags, we do not want to pass them to the raw
1358 * demuxer on all segment switches */
1359 int bytes;
1360 int id3_buf_pos = 0;
1361 int fill_buf = 0;
1362 struct segment *seg = current_segment(pls);
1363
1364 /* gather all the id3 tags */
1365 while (1) {
1366 /* see if we can retrieve enough data for ID3 header */
1368 bytes = read_from_url(pls, seg, buf + *len, ID3v2_HEADER_SIZE - *len);
1369 if (bytes > 0) {
1370
1371 if (bytes == ID3v2_HEADER_SIZE - *len)
1372 /* no EOF yet, so fill the caller buffer again after
1373 * we have stripped the ID3 tags */
1374 fill_buf = 1;
1375
1376 *len += bytes;
1377
1378 } else if (*len <= 0) {
1379 /* error/EOF */
1380 *len = bytes;
1381 fill_buf = 0;
1382 }
1383 }
1384
1385 if (*len < ID3v2_HEADER_SIZE)
1386 break;
1387
1389 int64_t maxsize = seg->size >= 0 ? seg->size : 1024*1024;
1390 int taglen = ff_id3v2_tag_len(buf);
1391 int tag_got_bytes = FFMIN(taglen, *len);
1392 int remaining = taglen - tag_got_bytes;
1393
1394 if (taglen > maxsize) {
1395 av_log(pls->parent, AV_LOG_ERROR, "Too large HLS ID3 tag (%d > %"PRId64" bytes)\n",
1396 taglen, maxsize);
1397 break;
1398 }
1399
1400 /*
1401 * Copy the id3 tag to our temporary id3 buffer.
1402 * We could read a small id3 tag directly without memcpy, but
1403 * we would still need to copy the large tags, and handling
1404 * both of those cases together with the possibility for multiple
1405 * tags would make the handling a bit complex.
1406 */
1407 pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen);
1408 if (!pls->id3_buf)
1409 break;
1410 memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes);
1411 id3_buf_pos += tag_got_bytes;
1412
1413 /* strip the intercepted bytes */
1414 *len -= tag_got_bytes;
1415 memmove(buf, buf + tag_got_bytes, *len);
1416 av_log(pls->parent, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes);
1417
1418 if (remaining > 0) {
1419 /* read the rest of the tag in */
1420 if (read_from_url(pls, seg, pls->id3_buf + id3_buf_pos, remaining) != remaining)
1421 break;
1422 id3_buf_pos += remaining;
1423 av_log(pls->parent, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining);
1424 }
1425
1426 } else {
1427 /* no more ID3 tags */
1428 break;
1429 }
1430 }
1431
1432 /* re-fill buffer for the caller unless EOF */
1433 if (*len >= 0 && (fill_buf || *len == 0)) {
1434 bytes = read_from_url(pls, seg, buf + *len, buf_size - *len);
1435
1436 /* ignore error if we already had some data */
1437 if (bytes >= 0)
1438 *len += bytes;
1439 else if (*len == 0)
1440 *len = bytes;
1441 }
1442
1443 if (pls->id3_buf) {
1444 /* Now parse all the ID3 tags */
1445 FFIOContext id3ioctx;
1446 ffio_init_read_context(&id3ioctx, pls->id3_buf, id3_buf_pos);
1447 handle_id3(&id3ioctx.pub, pls);
1448 }
1449
1450 if (pls->is_id3_timestamped == -1)
1452}
1453
1454static int read_key(HLSContext *c, struct playlist *pls, struct segment *seg)
1455{
1456 AVIOContext *pb = NULL;
1457
1458 int ret = open_url(pls->parent, &pb, seg->key, &c->avio_opts, NULL, NULL);
1459 if (ret < 0) {
1460 av_log(pls->parent, AV_LOG_ERROR, "Unable to open key file %s, %s\n",
1461 seg->key, av_err2str(ret));
1462 return ret;
1463 }
1464
1465 ret = avio_read(pb, pls->key, sizeof(pls->key));
1466 ff_format_io_close(pls->parent, &pb);
1467 if (ret != sizeof(pls->key)) {
1468 if (ret < 0) {
1469 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, %s\n",
1470 seg->key, av_err2str(ret));
1471 } else {
1472 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, read bytes %d != %zu\n",
1473 seg->key, ret, sizeof(pls->key));
1474 ret = AVERROR_INVALIDDATA;
1475 }
1476
1477 return ret;
1478 }
1479
1480 av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url));
1481
1482 return 0;
1483}
1484
1485static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg,
1486 AVIOContext **in, int64_t *end)
1487{
1489 int ret;
1490 int is_http = 0;
1491
1492 if (c->http_persistent)
1493 av_dict_set(&opts, "multiple_requests", "1", 0);
1494
1495 *end = -1;
1496 if (seg->size >= 0) {
1497 /* Restrict the request to the wanted byte range, extended so that one
1498 * request serves the whole run of segments that continue it. */
1499 *end = range_end(pls, seg);
1500 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1501 av_dict_set_int(&opts, "end_offset", *end, 0);
1502 }
1503
1504 av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
1505 seg->url, seg->url_offset, pls->index);
1506
1507 if (seg->key_type == KEY_AES_128 || seg->key_type == KEY_SAMPLE_AES) {
1508 if (strcmp(seg->key, pls->key_url)) {
1509 ret = read_key(c, pls, seg);
1510 if (ret < 0)
1511 goto cleanup;
1512 }
1513 }
1514
1515 if (seg->key_type == KEY_AES_128) {
1516 char iv[33], key[33], url[MAX_URL_SIZE];
1517 ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
1518 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
1519 if (strstr(seg->url, "://"))
1520 snprintf(url, sizeof(url), "crypto+%s", seg->url);
1521 else
1522 snprintf(url, sizeof(url), "crypto:%s", seg->url);
1523
1524 av_dict_set(&opts, "key", key, 0);
1525 av_dict_set(&opts, "iv", iv, 0);
1526
1527 ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http);
1528 if (ret < 0) {
1529 goto cleanup;
1530 }
1531 ret = 0;
1532 } else {
1533 ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http);
1534 }
1535
1536 /* Seek to the requested position. If this was a HTTP request, the offset
1537 * should already be where want it to, but this allows e.g. local testing
1538 * without a HTTP server.
1539 *
1540 * This is not done for HTTP at all as avio_seek() does internal bookkeeping
1541 * of file offset which is out-of-sync with the actual offset when "offset"
1542 * AVOption is used with http protocol, causing the seek to not be a no-op
1543 * as would be expected. Wrong offset received from the server will not be
1544 * noticed without the call, though.
1545 */
1546 if (ret == 0 && !is_http && seg->url_offset) {
1547 int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET);
1548 if (seekret < 0) {
1549 av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
1550 ret = seekret;
1551 ff_format_io_close(pls->parent, in);
1552 }
1553 }
1554
1555cleanup:
1557 pls->cur_seg_offset = 0;
1558 return ret;
1559}
1560
1561static int update_init_section(struct playlist *pls, struct segment *seg)
1562{
1563 static const int max_init_section_size = 1024*1024;
1564 HLSContext *c = pls->parent->priv_data;
1565 int64_t sec_size;
1566 int64_t urlsize;
1567 int ret;
1568
1569 if (seg->init_section == pls->cur_init_section)
1570 return 0;
1571
1572 pls->cur_init_section = NULL;
1573
1574 if (!seg->init_section)
1575 return 0;
1576
1577 ret = open_input(c, pls, seg->init_section, &pls->input, &pls->input_end);
1578 if (ret < 0) {
1580 "Failed to open an initialization section in playlist %d\n",
1581 pls->index);
1582 return ret;
1583 }
1584
1585 if (seg->init_section->size >= 0)
1586 sec_size = seg->init_section->size;
1587 else if ((urlsize = avio_size(pls->input)) >= 0)
1588 sec_size = urlsize;
1589 else
1590 sec_size = max_init_section_size;
1591
1593 "Downloading an initialization section of size %"PRId64"\n",
1594 sec_size);
1595
1596 sec_size = FFMIN(sec_size, max_init_section_size);
1597
1598 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1599
1600 ret = read_from_url(pls, seg->init_section, pls->init_sec_buf,
1601 pls->init_sec_buf_size);
1602 ff_format_io_close(pls->parent, &pls->input);
1603
1604 if (ret < 0)
1605 return ret;
1606
1607 pls->cur_init_section = seg->init_section;
1608 pls->init_sec_data_len = ret;
1609 pls->init_sec_buf_read_offset = 0;
1610
1611 /* spec says audio elementary streams do not have media initialization
1612 * sections, so there should be no ID3 timestamps */
1613 pls->is_id3_timestamped = 0;
1614
1615 return 0;
1616}
1617
1619{
1620 return pls->n_segments > 0 ?
1621 pls->segments[pls->n_segments - 1]->duration :
1622 pls->target_duration;
1623}
1624
1625static int playlist_needed(struct playlist *pls)
1626{
1627 AVFormatContext *s = pls->parent;
1628 int i, j;
1629 int stream_needed = 0;
1630 int first_st;
1631
1632 /* If there is no context or streams yet, the playlist is needed */
1633 if ((!pls->ctx || !pls->n_main_streams) && !pls->is_subtitle)
1634 return 1;
1635
1636 /* check if any of the streams in the playlist are needed */
1637 for (i = 0; i < pls->n_main_streams; i++) {
1638 if (pls->main_streams[i]->discard < AVDISCARD_ALL) {
1639 stream_needed = 1;
1640 break;
1641 }
1642 }
1643
1644 /* If all streams in the playlist were discarded, the playlist is not
1645 * needed (regardless of whether whole programs are discarded or not). */
1646 if (!stream_needed)
1647 return 0;
1648
1649 /* Otherwise, check if all the programs (variants) this playlist is in are
1650 * discarded. Since all streams in the playlist are part of the same programs
1651 * we can just check the programs of the first stream. */
1652
1653 first_st = pls->main_streams[0]->index;
1654
1655 for (i = 0; i < s->nb_programs; i++) {
1656 AVProgram *program = s->programs[i];
1657 if (program->discard < AVDISCARD_ALL) {
1658 for (j = 0; j < program->nb_stream_indexes; j++) {
1659 if (program->stream_index[j] == first_st) {
1660 /* playlist is in an undiscarded program */
1661 return 1;
1662 }
1663 }
1664 }
1665 }
1666
1667 /* some streams were not discarded but all the programs were */
1668 return 0;
1669}
1670
1671static int reload_playlist(struct playlist *v, HLSContext *c)
1672{
1673 int ret = 0;
1674 int reload_count = 0;
1675
1676 v->needed = playlist_needed(v);
1677
1678 if (!v->needed)
1679 return AVERROR_EOF;
1680
1681 if (!v->input || (c->http_persistent && v->input_read_done)) {
1682 int64_t reload_interval;
1683
1684 /* Check that the playlist is still needed before opening a new
1685 * segment. */
1686 v->needed = playlist_needed(v);
1687
1688 if (!v->needed) {
1689 av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d ('%s')\n",
1690 v->index, v->url);
1691 return AVERROR_EOF;
1692 }
1693
1694 /* If this is a live stream and the reload interval has elapsed since
1695 * the last playlist reload, reload the playlists now. */
1696 reload_interval = default_reload_interval(v);
1697
1698reload:
1699 reload_count++;
1700 if (reload_count > c->max_reload)
1701 return AVERROR_EOF;
1702 if (!v->finished &&
1703 av_gettime_relative() - v->last_load_time >= reload_interval) {
1704 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) {
1705 if (ret != AVERROR_EXIT)
1706 av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n",
1707 v->index);
1708 return ret;
1709 }
1710 /* If we need to reload the playlist again below (if
1711 * there's still no more segments), switch to a reload
1712 * interval of half the target duration. */
1713 reload_interval = v->target_duration / 2;
1714 }
1715 if (v->cur_seq_no < v->start_seq_no) {
1717 "skipping %"PRId64" segments ahead, expired from playlists\n",
1718 v->start_seq_no - v->cur_seq_no);
1719 v->cur_seq_no = v->start_seq_no;
1720 }
1721 if (v->cur_seq_no > v->last_seq_no) {
1722 v->last_seq_no = v->cur_seq_no;
1723 v->m3u8_hold_counters = 0;
1724 } else if (v->last_seq_no == v->cur_seq_no) {
1725 v->m3u8_hold_counters++;
1726 if (v->m3u8_hold_counters >= c->m3u8_hold_counters) {
1727 return AVERROR_EOF;
1728 }
1729 } else {
1730 av_log(v->parent, AV_LOG_WARNING, "The m3u8 list sequence may have been wrapped.\n");
1731 }
1732 if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
1733 if (v->finished || v->is_subtitle)
1734 return AVERROR_EOF;
1735 while (av_gettime_relative() - v->last_load_time < reload_interval) {
1736 if (ff_check_interrupt(c->interrupt_callback))
1737 return AVERROR_EXIT;
1738 av_usleep(100*1000);
1739 }
1740 /* Enough time has elapsed since the last reload */
1741 goto reload;
1742 }
1743
1744 }
1745 return ret;
1746}
1747
1748static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
1749{
1750 struct playlist *v = opaque;
1752 int ret;
1753 int just_opened = 0;
1754 int segment_retries = 0;
1755 struct segment *seg;
1756
1757 if (c->http_persistent && v->input_read_done) {
1758 av_assert0(v->input);
1759 ret = reload_playlist(v, c);
1760 if (ret < 0)
1761 return ret;
1762 }
1763
1764 v->input_read_done = 0;
1765
1766restart:
1767 ret = reload_playlist(v, c);
1768 if (ret < 0)
1769 return ret;
1770
1771 seg = current_segment(v);
1772
1773 if (!v->input || v->input_read_done) {
1774 /* load/update Media Initialization Section, if any */
1775 ret = update_init_section(v, seg);
1776 if (ret)
1777 return ret;
1778
1779 if (c->http_multiple == 1 && v->input_next_requested) {
1782 v->cur_seg_offset = 0;
1783 v->input_next_requested = 0;
1784 ret = 0;
1785 } else {
1786 ret = open_input(c, v, seg, &v->input, &v->input_end);
1787 }
1788 if (ret < 0) {
1789 if (ff_check_interrupt(c->interrupt_callback))
1790 return AVERROR_EXIT;
1791 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1792 v->cur_seq_no,
1793 v->index);
1794 if (segment_retries >= c->seg_max_retry) {
1795 av_log(v->parent, AV_LOG_WARNING, "Segment %"PRId64" of playlist %d failed too many times, skipping\n",
1796 v->cur_seq_no,
1797 v->index);
1798 v->cur_seq_no++;
1799 segment_retries = 0;
1800 } else {
1801 segment_retries++;
1802 }
1803 goto restart;
1804 }
1805 segment_retries = 0;
1806 just_opened = 1;
1807 v->input_read_done = 0;
1808 if (v->first_read_seq_no < 0)
1810 }
1811
1812 if (c->http_multiple == -1) {
1813 uint8_t *http_version_opt = NULL;
1814 int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt);
1815 if (r >= 0) {
1816 c->http_multiple = (!strncmp((const char *)http_version_opt, "1.1", 3) || !strncmp((const char *)http_version_opt, "2.0", 3));
1817 av_freep(&http_version_opt);
1818 }
1819 }
1820
1821 seg = next_segment(v);
1822 if (c->http_multiple == 1 && !v->input_next_requested &&
1823 seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
1825 ret = open_input(c, v, seg, &v->input_next, &v->input_next_end);
1826 if (ret < 0) {
1827 if (ff_check_interrupt(c->interrupt_callback))
1828 return AVERROR_EXIT;
1829 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1830 v->cur_seq_no + 1,
1831 v->index);
1832 } else {
1833 v->input_next_requested = 1;
1834 }
1835 }
1836
1838 /* Push init section out first before first actual segment */
1839 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
1840 memcpy(buf, v->init_sec_buf + v->init_sec_buf_read_offset, copy_size);
1841 v->init_sec_buf_read_offset += copy_size;
1842 return copy_size;
1843 }
1844
1845 seg = current_segment(v);
1846 ret = read_from_url(v, seg, buf, buf_size);
1847 if (ret > 0) {
1848 if (just_opened && v->is_id3_timestamped != 0) {
1849 /* Intercept ID3 tags here, elementary audio streams are required
1850 * to convey timestamps using them in the beginning of each segment. */
1851 intercept_id3(v, buf, buf_size, &ret);
1852 }
1853
1854 return ret;
1855 }
1856 if (ret == 0 && segment_continues(seg, next_segment(v), v->input_end)) {
1857 /* The next segment follows within the requested range and is read
1858 * on from the same input. */
1859 v->cur_seq_no++;
1860 c->cur_seq_no = v->cur_seq_no;
1861 v->cur_seg_offset = 0;
1862 just_opened = 1;
1863 goto restart;
1864 }
1865 /* Keep the connection for the next request only if its reply was read
1866 * to the end. */
1867 if (c->http_persistent && is_native_http(v->input) &&
1868 seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
1869 (seg->size >= 0 ? ret == 0 && seg->url_offset + seg->size == v->input_end
1870 : ret == AVERROR_EOF)) {
1871 v->input_read_done = 1;
1872 } else {
1874 }
1875 v->cur_seq_no++;
1876
1877 c->cur_seq_no = v->cur_seq_no;
1878
1879 goto restart;
1880}
1881
1882static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
1883{
1884 struct playlist *v = opaque;
1886 int ret;
1887 struct segment *seg;
1888
1889 if (!v->needed || v->cur_seq_no - v->start_seq_no >= v->n_segments) {
1890 return AVERROR_EOF;
1891 } else {
1892 seg = current_segment(v);
1893 }
1894
1895 if (!v->input) {
1896 ret = open_input(c, v, seg, &v->input, &v->input_end);
1897 if (ret < 0) {
1898 if (ff_check_interrupt(c->interrupt_callback))
1899 return AVERROR_EXIT;
1900 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment of playlist %d\n",
1901 v->index);
1902 return ret;
1903 }
1904 if (v->first_read_seq_no < 0)
1906 }
1907
1908 return read_from_url(v, seg, buf, buf_size);
1909}
1910
1911static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
1912 int flags, AVDictionary **opts)
1913{
1915 "A HLS playlist item '%s' referred to an external file '%s'. "
1916 "Opening this file was forbidden for security reasons\n",
1917 s->url, url);
1918 return AVERROR(EPERM);
1919}
1920
1921static int init_subtitle_context(struct playlist *pls)
1922{
1923 HLSContext *c = pls->parent->priv_data;
1924 const AVInputFormat *in_fmt;
1926 uint8_t *buf;
1927 int ret;
1928
1929 if (!(pls->ctx = avformat_alloc_context()))
1930 return AVERROR(ENOMEM);
1931
1933 if (!buf) {
1935 pls->ctx = NULL;
1936 return AVERROR(ENOMEM);
1937 }
1938
1939 av_freep(&pls->pb.pub.buffer);
1940 ffio_init_context(&pls->pb, buf, INITIAL_BUFFER_SIZE, 0, pls,
1942 pls->pb.pub.seekable = 0;
1943 pls->ctx->pb = &pls->pb.pub;
1944 pls->ctx->io_open = nested_io_open;
1945
1946 ret = ff_copy_whiteblacklists(pls->ctx, pls->parent);
1947 if (ret < 0) {
1949 pls->ctx = NULL;
1950 return ret;
1951 }
1952
1953 in_fmt = av_find_input_format("webvtt");
1954 av_dict_copy(&opts, c->seg_format_opts, 0);
1955 ret = avformat_open_input(&pls->ctx, current_segment(pls)->url, in_fmt, &opts);
1957
1958 return ret;
1959}
1960
1962{
1964 int ret;
1965
1966restart:
1967 ret = reload_playlist(v, c);
1968 if (ret < 0)
1969 return ret;
1970
1971 if (v->input && !v->ctx)
1973
1974 if (!v->input && !v->ctx) {
1975 ret = init_subtitle_context(v);
1976 if (ret < 0)
1977 return ret;
1978 }
1979
1980 ret = av_read_frame(v->ctx, v->pkt);
1981 if (!ret) {
1982 return ret;
1983 }
1985 v->cur_seq_no++;
1986 c->cur_seq_no = v->cur_seq_no;
1987
1989
1990 goto restart;
1991}
1992
1994 enum AVMediaType type, const char *group_id)
1995{
1996 int i;
1997
1998 for (i = 0; i < c->n_renditions; i++) {
1999 struct rendition *rend = c->renditions[i];
2000
2001 if (rend->type == type && !strcmp(rend->group_id, group_id)) {
2002
2003 if (rend->playlist)
2004 /* rendition is an external playlist
2005 * => add the playlist to the variant */
2006 dynarray_add(&var->playlists, &var->n_playlists, rend->playlist);
2007 else
2008 /* rendition is part of the variant main Media Playlist
2009 * => add the rendition to the main Media Playlist */
2011 &var->playlists[0]->n_renditions,
2012 rend);
2013 }
2014 }
2015}
2016
2018 enum AVMediaType type)
2019{
2020 int rend_idx = 0;
2021 int i;
2022
2023 for (i = 0; i < pls->n_main_streams; i++) {
2024 AVStream *st = pls->main_streams[i];
2025
2026 if (st->codecpar->codec_type != type)
2027 continue;
2028
2029 for (; rend_idx < pls->n_renditions; rend_idx++) {
2030 struct rendition *rend = pls->renditions[rend_idx];
2031
2032 if (rend->type != type)
2033 continue;
2034
2035 if (rend->language[0])
2036 av_dict_set(&st->metadata, "language", rend->language, 0);
2037 if (rend->name[0])
2038 av_dict_set(&st->metadata, "comment", rend->name, 0);
2039
2040 st->disposition |= rend->disposition;
2041 }
2042 if (rend_idx >=pls->n_renditions)
2043 break;
2044 }
2045}
2046
2047/* if timestamp was in valid range: returns 1 and sets seq_no
2048 * if not: returns 0 and sets seq_no to closest segment */
2050 int64_t timestamp, int64_t *seq_no,
2051 int64_t *seg_start_ts)
2052{
2053 int i;
2054 int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
2055 0 : c->first_timestamp;
2056
2057 if (timestamp < pos) {
2058 /* Seeking before the start of the playlist, clamp to the first segment. */
2059 *seq_no = pls->start_seq_no;
2060 if (seg_start_ts)
2061 *seg_start_ts = pos;
2062 return 1;
2063 }
2064
2065 for (i = 0; i < pls->n_segments; i++) {
2066 int64_t diff = pos + pls->segments[i]->duration - timestamp;
2067 if (diff > 0) {
2068 *seq_no = pls->start_seq_no + i;
2069 if (seg_start_ts) {
2070 *seg_start_ts = pos;
2071 }
2072 return 1;
2073 }
2074 pos += pls->segments[i]->duration;
2075 }
2076
2077 *seq_no = pls->start_seq_no + pls->n_segments - 1;
2078
2079 if (!pls->finished && pls->n_segments > 0) {
2080 if (seg_start_ts)
2081 *seg_start_ts = pos - pls->segments[pls->n_segments - 1]->duration;
2082 return 1;
2083 }
2084
2085 return 0;
2086}
2087
2089{
2090 int64_t seq_no;
2091
2092 if (!pls->finished && !c->first_packet &&
2094 /* reload the playlist since it was suspended */
2095 parse_playlist(c, pls->url, pls, NULL);
2096
2097 /* If playback is already in progress (we are just selecting a new
2098 * playlist) and this is a complete file, find the matching segment
2099 * by counting durations. */
2100 if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
2101 find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL);
2102 return seq_no;
2103 }
2104
2105 if (!pls->finished) {
2106 if (!c->first_packet && /* we are doing a segment selection during playback */
2107 c->cur_seq_no >= pls->start_seq_no &&
2108 c->cur_seq_no < pls->start_seq_no + pls->n_segments)
2109 /* While spec 3.4.3 says that we cannot assume anything about the
2110 * content at the same sequence number on different playlists,
2111 * in practice this seems to work and doing it otherwise would
2112 * require us to download a segment to inspect its timestamps. */
2113 return c->cur_seq_no;
2114
2115 /* If this is a live stream, start live_start_index segments from the
2116 * start or end */
2117 if (c->live_start_index < 0)
2118 seq_no = pls->start_seq_no + FFMAX(pls->n_segments +
2119 c->live_start_index, 0);
2120 else
2121 seq_no = pls->start_seq_no + FFMIN(c->live_start_index,
2122 pls->n_segments - 1);
2123
2124 /* If #EXT-X-START in playlist, need to recalculate */
2125 if (pls->time_offset_flag && c->prefer_x_start) {
2126 int64_t start_timestamp;
2127 int64_t playlist_duration = 0;
2128 int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 :
2129 c->cur_timestamp;
2130
2131 for (int i = 0; i < pls->n_segments; i++)
2132 playlist_duration += pls->segments[i]->duration;
2133
2134 /* If the absolute value of TIME-OFFSET exceeds
2135 * the duration of the playlist, it indicates either the end of the
2136 * playlist (if positive) or the beginning of the playlist (if
2137 * negative). */
2138 if (pls->start_time_offset >=0 &&
2139 pls->start_time_offset > playlist_duration)
2140 start_timestamp = cur_timestamp + playlist_duration;
2141 else if (pls->start_time_offset >= 0 &&
2142 pls->start_time_offset <= playlist_duration)
2143 start_timestamp = cur_timestamp + pls->start_time_offset;
2144 else if (pls->start_time_offset < 0 &&
2145 pls->start_time_offset < -playlist_duration)
2146 start_timestamp = cur_timestamp;
2147 else if (pls->start_time_offset < 0 &&
2148 pls->start_time_offset > -playlist_duration)
2149 start_timestamp = cur_timestamp + playlist_duration +
2150 pls->start_time_offset;
2151 else
2152 start_timestamp = cur_timestamp;
2153
2154 find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL);
2155 }
2156 return seq_no;
2157 }
2158
2159 /* Otherwise just start on the first segment. */
2160 return pls->start_seq_no;
2161}
2162
2163static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
2164{
2165 HLSContext *c = s->priv_data;
2166 int i, j;
2167 int bandwidth = -1;
2168
2169 for (i = 0; i < c->n_variants; i++) {
2170 struct variant *v = c->variants[i];
2171
2172 for (j = 0; j < v->n_playlists; j++) {
2173 if (v->playlists[j] != pls)
2174 continue;
2175
2177
2178 if (bandwidth == -1)
2179 bandwidth = v->bandwidth;
2180 else if (bandwidth != v->bandwidth)
2181 bandwidth = -2; /* stream in multiple variants with different bandwidths */
2182 }
2183 }
2184
2185 if (bandwidth >= 0)
2186 av_dict_set_int(&stream->metadata, "variant_bitrate", bandwidth, 0);
2187}
2188
2190{
2191 int err;
2192
2193 err = avcodec_parameters_copy(st->codecpar, ist->codecpar);
2194 if (err < 0)
2195 return err;
2196
2197 if (pls->is_id3_timestamped) /* custom timestamps via id3 */
2199 else
2201
2202 // copy disposition
2203 st->disposition = ist->disposition;
2204
2205 av_dict_copy(&st->metadata, ist->metadata, 0);
2206
2208
2209 return 0;
2210}
2211
2212/* add new subdemuxer streams to our context, if any */
2214{
2215 HLSContext *c = s->priv_data;
2216 int err;
2217
2218 while (pls->n_main_streams < pls->ctx->nb_streams) {
2219 int ist_idx = pls->n_main_streams;
2221 AVStream *ist = pls->ctx->streams[ist_idx];
2222
2223 if (!st)
2224 return AVERROR(ENOMEM);
2225
2226 st->id = pls->index;
2227 dynarray_add(&pls->main_streams, &pls->n_main_streams, st);
2228
2229 add_stream_to_programs(s, pls, st);
2230
2231 err = set_stream_info_from_input_stream(st, pls, ist);
2232 if (err < 0)
2233 return err;
2234
2235 /* Match the start_time of streams created before playback began. */
2236 if (c->first_timestamp != AV_NOPTS_VALUE)
2237 st->start_time = av_rescale_q(c->first_timestamp,
2239
2241 if (pls->timed_id3_stream_index < 0)
2242 pls->timed_id3_stream_index = ist_idx;
2243 else
2245 "Playlist has multiple timed ID3 streams; "
2246 "only stream %d will be used for metadata propagation\n",
2248 }
2249 }
2250
2251 return 0;
2252}
2253
2255{
2256 HLSContext *c = s->priv_data;
2257 int flag_needed = 0;
2258 int i;
2259
2260 for (i = 0; i < c->n_playlists; i++) {
2261 struct playlist *pls = c->playlists[i];
2262
2263 if (pls->has_noheader_flag) {
2264 flag_needed = 1;
2265 break;
2266 }
2267 }
2268
2269 if (flag_needed)
2270 s->ctx_flags |= AVFMTCTX_NOHEADER;
2271 else
2272 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
2273}
2274
2276{
2277 HLSContext *c = s->priv_data;
2278
2282
2283 if (c->crypto_ctx.aes_ctx)
2284 av_free(c->crypto_ctx.aes_ctx);
2285
2286 av_dict_free(&c->avio_opts);
2287 ff_format_io_close(c->ctx, &c->playlist_pb);
2288
2289 return 0;
2290}
2291
2293{
2294 HLSContext *c = s->priv_data;
2295 int ret = 0, i;
2296 int64_t highest_cur_seq_no = 0;
2297
2298 c->ctx = s;
2299 c->interrupt_callback = &s->interrupt_callback;
2300
2301 c->first_packet = 1;
2302 c->first_timestamp = AV_NOPTS_VALUE;
2303 c->first_timestamp_pls = NULL;
2304 c->first_timestamp_locked = 0;
2305 c->cur_timestamp = AV_NOPTS_VALUE;
2306
2307 if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0)
2308 return ret;
2309
2310 /* http_persistent and http_multiple auto-detection both rely on the
2311 * AVIOContext being backed by the builtin URLContext. Neither works
2312 * when io_open is overridden with a custom callback. */
2313 if (!ffio_geturlcontext(s->pb)) {
2314 if (c->http_persistent) {
2315 av_log(s, AV_LOG_WARNING, "Disabling http_persistent due to custom io_open.\n");
2316 c->http_persistent = 0;
2317 }
2318 /* Only auto-detection is disabled, enabling http_multiple can still work
2319 * with custom io_open. */
2320 if (c->http_multiple == -1) {
2321 av_log(s, AV_LOG_WARNING, "Disabling http_multiple due to custom io_open.\n");
2322 c->http_multiple = 0;
2323 }
2324 }
2325
2326 /* XXX: Some HLS servers don't like being sent the range header,
2327 in this case, we need to set http_seekable = 0 to disable
2328 the range header */
2329 av_dict_set_int(&c->avio_opts, "seekable", c->http_seekable, 0);
2330
2331 if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
2332 return ret;
2333
2334 if (c->n_variants == 0) {
2335 av_log(s, AV_LOG_WARNING, "Empty playlist\n");
2336 return AVERROR_EOF;
2337 }
2338 /* If the playlist only contained playlists (Master Playlist),
2339 * parse each individual playlist. */
2340 if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
2341 for (i = 0; i < c->n_playlists; i++) {
2342 struct playlist *pls = c->playlists[i];
2343 pls->m3u8_hold_counters = 0;
2344 if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0) {
2345 av_log(s, AV_LOG_WARNING, "parse_playlist error %s [%s]\n", av_err2str(ret), pls->url);
2346 pls->broken = 1;
2347 if (c->n_playlists > 1)
2348 continue;
2349 return ret;
2350 }
2351 }
2352 }
2353
2354 for (i = 0; i < c->n_variants; i++) {
2355 if (c->variants[i]->playlists[0]->n_segments == 0) {
2356 av_log(s, AV_LOG_WARNING, "Empty segment [%s]\n", c->variants[i]->playlists[0]->url);
2357 c->variants[i]->playlists[0]->broken = 1;
2358 }
2359 }
2360
2361 /* Calculate the total duration of the stream if all segments are
2362 * available (finished or EVENT playlists). */
2363 if (c->variants[0]->playlists[0]->finished ||
2364 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT) {
2365 int64_t duration = 0;
2366 for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
2367 duration += c->variants[0]->playlists[0]->segments[i]->duration;
2368 s->duration = duration;
2369 }
2370
2371 /* Associate renditions with variants */
2372 for (i = 0; i < c->n_variants; i++) {
2373 struct variant *var = c->variants[i];
2374
2375 if (var->audio_group[0])
2377 if (var->video_group[0])
2379 if (var->subtitles_group[0])
2381 }
2382
2383 /* Create a program for each variant */
2384 for (i = 0; i < c->n_variants; i++) {
2385 struct variant *v = c->variants[i];
2386 AVProgram *program;
2387
2388 program = av_new_program(s, i);
2389 if (!program)
2390 return AVERROR(ENOMEM);
2391 av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
2392 }
2393
2394 /* Select the starting segments */
2395 for (i = 0; i < c->n_playlists; i++) {
2396 struct playlist *pls = c->playlists[i];
2397
2398 if (pls->n_segments == 0)
2399 continue;
2400
2401 pls->cur_seq_no = select_cur_seq_no(c, pls);
2402 highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no);
2403 }
2404
2405 av_dict_set(&c->seg_format_opts, "prefer_hls_mpegts_pts", "1", 0);
2406
2407 /* Open the demuxer for each playlist */
2408 for (i = 0; i < c->n_playlists; i++) {
2409 struct playlist *pls = c->playlists[i];
2410 const AVInputFormat *in_fmt = NULL;
2411 char *url;
2413 struct segment *seg = NULL;
2414 uint8_t *buf;
2415 int buf_size;
2416
2417 if (!(pls->ctx = avformat_alloc_context()))
2418 return AVERROR(ENOMEM);
2419
2420 if (pls->n_segments == 0)
2421 continue;
2422
2423 pls->index = i;
2424 pls->needed = 1;
2425 pls->parent = s;
2426
2427 /*
2428 * If this is a live stream and this playlist looks like it is one segment
2429 * behind, try to sync it up so that every substream starts at the same
2430 * time position (so e.g. avformat_find_stream_info() will see packets from
2431 * all active streams within the first few seconds). This is not very generic,
2432 * though, as the sequence numbers are technically independent.
2433 */
2434 if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 &&
2435 highest_cur_seq_no < pls->start_seq_no + pls->n_segments) {
2436 pls->cur_seq_no = highest_cur_seq_no;
2437 }
2438
2439 if (pls->is_subtitle) {
2440 buf_size = strlen("WEBVTT\n");
2441 buf = av_memdup("WEBVTT\n", buf_size);
2442 } else {
2443 buf_size = INITIAL_BUFFER_SIZE;
2444 buf = av_malloc(buf_size);
2445 }
2446 if (!buf) {
2448 pls->ctx = NULL;
2449 return AVERROR(ENOMEM);
2450 }
2451
2452 ffio_init_context(&pls->pb, buf, buf_size, 0, pls,
2454
2455 /*
2456 * If encryption scheme is SAMPLE-AES, try to read ID3 tags of
2457 * external audio track that contains audio setup information
2458 */
2459 seg = current_segment(pls);
2460 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->n_renditions > 0 &&
2461 pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO) {
2462 uint8_t buf[HLS_MAX_ID3_TAGS_DATA_LEN];
2463 if ((ret = avio_read(&pls->pb.pub, buf, HLS_MAX_ID3_TAGS_DATA_LEN)) < 0) {
2464 /* Fail if error was not end of file */
2465 if (ret != AVERROR_EOF) {
2467 pls->ctx = NULL;
2468 return ret;
2469 }
2470 }
2471 ret = 0;
2472 /* Reset reading */
2473 ff_format_io_close(pls->parent, &pls->input);
2474 pls->input = NULL;
2475 pls->input_read_done = 0;
2477 pls->input_next = NULL;
2478 pls->input_next_requested = 0;
2479 pls->cur_seg_offset = 0;
2480 pls->cur_init_section = NULL;
2481 /* Reset EOF flag */
2482 pls->pb.pub.eof_reached = 0;
2483 /* Clear any buffered data */
2484 pls->pb.pub.buf_end = pls->pb.pub.buf_ptr = pls->pb.pub.buffer;
2485 /* Reset the position */
2486 pls->pb.pub.pos = 0;
2487 }
2488
2489 /*
2490 * If encryption scheme is SAMPLE-AES and audio setup information is present in external audio track,
2491 * use that information to find the media format, otherwise probe input data
2492 */
2493 seg = current_segment(pls);
2494 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->is_id3_timestamped &&
2499 // Keep this list in sync with ff_hls_senc_read_audio_setup_info()
2501 pls->audio_setup_info.codec_id == AV_CODEC_ID_AC3 ? "ac3" : "eac3");
2502 } else {
2503 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
2504 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
2505 pls->ctx->interrupt_callback = s->interrupt_callback;
2506 url = av_strdup(pls->segments[0]->url);
2507 ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, url, NULL, 0, 0);
2508
2509 for (int n = 0; n < pls->n_segments; n++)
2510 if (ret >= 0)
2511 ret = test_segment(s, in_fmt, pls, pls->segments[n]);
2512
2513 if (ret < 0) {
2514 /* Free the ctx - it isn't initialized properly at this point,
2515 * so avformat_close_input shouldn't be called. If
2516 * avformat_open_input fails below, it frees and zeros the
2517 * context, so it doesn't need any special treatment like this. */
2518 av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url);
2520 pls->ctx = NULL;
2521 av_free(url);
2522 return ret;
2523 }
2524 av_free(url);
2525 }
2526
2527 seg = current_segment(pls);
2528 if (seg && seg->key_type == KEY_SAMPLE_AES) {
2529 if (strstr(in_fmt->name, "mov")) {
2530 char key[33];
2531 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
2532 av_dict_set(&options, "decryption_key", key, 0);
2533 } else if (!c->crypto_ctx.aes_ctx) {
2534 c->crypto_ctx.aes_ctx = av_aes_alloc();
2535 if (!c->crypto_ctx.aes_ctx) {
2537 pls->ctx = NULL;
2538 return AVERROR(ENOMEM);
2539 }
2540 }
2541 }
2542
2543 pls->ctx->pb = &pls->pb.pub;
2544 pls->ctx->io_open = nested_io_open;
2545 pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
2546
2547 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
2548 return ret;
2549
2550 av_dict_copy(&options, c->seg_format_opts, 0);
2551
2552 ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &options);
2554 if (ret < 0)
2555 return ret;
2556
2557 if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) {
2563 }
2564
2565 if (pls->is_id3_timestamped == -1)
2566 av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n");
2567
2568 /*
2569 * For ID3 timestamped raw audio streams we need to detect the packet
2570 * durations to calculate timestamps in fill_timing_for_id3_timestamped_stream(),
2571 * but for other streams we can rely on our user calling avformat_find_stream_info()
2572 * on us if they want to.
2573 */
2574 if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) {
2575 seg = current_segment(pls);
2576 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->audio_setup_info.setup_data_length > 0 &&
2577 pls->ctx->nb_streams == 1)
2579 else
2580 ret = avformat_find_stream_info(pls->ctx, NULL);
2581
2582 if (ret < 0)
2583 return ret;
2584 }
2585
2587
2588 /* Create new AVStreams for each stream in this playlist */
2590 if (ret < 0)
2591 return ret;
2592
2593 /*
2594 * Copy any metadata from playlist to main streams, but do not set
2595 * event flags.
2596 */
2597 if (pls->n_main_streams)
2598 av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0);
2599
2600 if (pls->is_subtitle) {
2602 pls->ctx = NULL;
2603 pls->needed = 0;
2605 }
2606
2610 }
2611
2613
2614 return 0;
2615}
2616
2618{
2619 HLSContext *c = s->priv_data;
2620 int i, changed = 0;
2621 int cur_needed;
2622
2623 /* Check if any new streams are needed */
2624 for (i = 0; i < c->n_playlists; i++) {
2625 struct playlist *pls = c->playlists[i];
2626
2627 cur_needed = playlist_needed(c->playlists[i]);
2628
2629 if (pls->broken) {
2630 continue;
2631 }
2632 if (cur_needed && !pls->needed) {
2633 pls->needed = 1;
2634 changed = 1;
2635 pls->cur_seq_no = select_cur_seq_no(c, pls);
2636 pls->first_read_seq_no = -1;
2637 pls->pb.pub.eof_reached = 0;
2638 if (c->cur_timestamp != AV_NOPTS_VALUE) {
2639 /* catch up */
2640 pls->seek_timestamp = c->cur_timestamp +
2641 (pls->ts_offset != AV_NOPTS_VALUE ? pls->ts_offset : 0);
2643 pls->seek_stream_index = -1;
2644 }
2645 av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no);
2646 } else if (first && !cur_needed && pls->needed) {
2647 ff_format_io_close(pls->parent, &pls->input);
2648 pls->input_read_done = 0;
2650 pls->input_next_requested = 0;
2651 if (pls->is_subtitle)
2653 pls->needed = 0;
2654 changed = 1;
2655 av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
2656 }
2657 }
2658 return changed;
2659}
2660
2662{
2663 if (pls->id3_offset >= 0) {
2664 pls->pkt->dts = pls->id3_mpegts_timestamp +
2666 pls->ctx->streams[pls->pkt->stream_index]->time_base,
2668 if (pls->pkt->duration)
2669 pls->id3_offset += pls->pkt->duration;
2670 else
2671 pls->id3_offset = -1;
2672 } else {
2673 /* there have been packets with unknown duration
2674 * since the last id3 tag, should not normally happen */
2675 pls->pkt->dts = AV_NOPTS_VALUE;
2676 }
2677
2678 if (pls->pkt->duration)
2679 pls->pkt->duration = av_rescale_q(pls->pkt->duration,
2680 pls->ctx->streams[pls->pkt->stream_index]->time_base,
2682
2683 pls->pkt->pts = AV_NOPTS_VALUE;
2684}
2685
2687{
2688 if (pls->is_id3_timestamped)
2689 return MPEG_TIME_BASE_Q;
2690
2691 return pls->ctx->streams[pls->pkt->stream_index]->time_base;
2692}
2693
2694static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a,
2695 int64_t ts_b, struct playlist *pls_b)
2696{
2697 int64_t scaled_ts_a = av_rescale_q(ts_a, get_timebase(pls_a), MPEG_TIME_BASE_Q);
2698 int64_t scaled_ts_b = av_rescale_q(ts_b, get_timebase(pls_b), MPEG_TIME_BASE_Q);
2699
2700 return av_compare_mod(scaled_ts_a, scaled_ts_b, 1LL << 33);
2701}
2702
2703/**
2704 * Check whether any entry in @p update differs from the corresponding entry
2705 * in @p current.
2706 *
2707 * HLS segments can repeat timed ID3 metadata in every segment so that
2708 * listeners joining mid-stream immediately receive the current track
2709 * information. Only signal a metadata change when values actually differ
2710 * to avoid spurious updates on every segment boundary.
2711 */
2713 const AVDictionary *update)
2714{
2716 return 1;
2717 const AVDictionaryEntry *e = NULL;
2718 while ((e = av_dict_iterate(update, e))) {
2719 const AVDictionaryEntry *cur = av_dict_get(current, e->key, NULL, 0);
2720 if (!cur || strcmp(cur->value, e->value))
2721 return 1;
2722 }
2723 return 0;
2724}
2725
2727{
2728 HLSContext *c = s->priv_data;
2729 int ret, i, minplaylist = -1;
2730
2731 recheck_discard_flags(s, c->first_packet);
2732 c->first_packet = 0;
2733
2734 for (i = 0; i < c->n_playlists; i++) {
2735 struct playlist *pls = c->playlists[i];
2736 /* Make sure we've got one buffered packet from each open playlist
2737 * stream */
2738 if (pls->needed && !pls->pkt->data) {
2739 while (1) {
2740 int64_t ts_diff;
2741 AVRational tb;
2742 struct segment *seg = NULL;
2743 if (pls->is_subtitle)
2744 ret = read_subtitle_packet(pls, pls->pkt);
2745 else
2746 ret = av_read_frame(pls->ctx, pls->pkt);
2747 if (ret < 0) {
2748 if (!avio_feof(&pls->pb.pub) && ret != AVERROR_EOF)
2749 return ret;
2750 break;
2751 }
2752
2753 /* stream_index check prevents matching picture attachments etc. */
2754 if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) {
2755 /* audio elementary streams are id3 timestamped */
2757 }
2758
2759 if (pls->pkt->dts != AV_NOPTS_VALUE &&
2760 (pls->ts_offset == AV_NOPTS_VALUE ||
2761 (!c->first_timestamp_locked &&
2762 c->first_timestamp_pls == pls &&
2763 pls->cur_seq_no <= pls->first_read_seq_no + 1))) {
2764 int adjusted = 0;
2765 /* Packet timestamp rebased onto the start of the segment list. */
2766 int64_t seg_idx = pls->first_read_seq_no - pls->start_seq_no;
2767 int64_t ts = av_rescale_q(pls->pkt->pts != AV_NOPTS_VALUE ?
2768 pls->pkt->pts : pls->pkt->dts,
2770
2771 for (int64_t k = 0; k < seg_idx && k < pls->n_segments; k++)
2772 ts -= pls->segments[k]->duration;
2773
2774 if (c->first_timestamp == AV_NOPTS_VALUE) {
2775 c->first_timestamp = ts;
2776 c->first_timestamp_pls = pls;
2777 adjusted = 1;
2778 } else if (c->first_timestamp_pls == pls) {
2779 /* first_timestamp came from the first packet in mux
2780 * order, but another stream in the same segment may
2781 * start earlier. Lower the estimate while packets of
2782 * that segment are still arriving. */
2783 int64_t delta = c->first_timestamp - ts;
2784
2785 if (delta > 0 && delta <= pls->target_duration) {
2786 c->first_timestamp = ts;
2787 for (int k = 0; k < c->n_playlists; k++)
2788 if (c->playlists[k]->ts_offset != AV_NOPTS_VALUE)
2789 c->playlists[k]->ts_offset += delta;
2790 adjusted = 1;
2791 }
2792 }
2793
2794 if (pls->ts_offset == AV_NOPTS_VALUE)
2795 pls->ts_offset = ts - c->first_timestamp;
2796
2797 if (adjusted) {
2798 for (unsigned k = 0; k < s->nb_streams; k++)
2799 s->streams[k]->start_time =
2800 av_rescale_q(c->first_timestamp, AV_TIME_BASE_Q,
2801 s->streams[k]->time_base);
2803 "First timestamp %"PRId64" from playlist %d\n",
2804 c->first_timestamp, pls->index);
2805 }
2806 }
2807
2808 seg = current_segment(pls);
2809 if (seg && seg->key_type == KEY_SAMPLE_AES && !strstr(pls->ctx->iformat->name, "mov")) {
2811 memcpy(c->crypto_ctx.iv, seg->iv, sizeof(seg->iv));
2812 memcpy(c->crypto_ctx.key, pls->key, sizeof(pls->key));
2813 ff_hls_senc_decrypt_frame(codec_id, &c->crypto_ctx, pls->pkt);
2814 }
2815
2816 if (pls->seek_timestamp == AV_NOPTS_VALUE)
2817 break;
2818
2819 if (pls->seek_stream_index < 0 ||
2820 pls->seek_stream_index == pls->pkt->stream_index) {
2821
2822 if (pls->pkt->dts == AV_NOPTS_VALUE) {
2824 break;
2825 }
2826
2827 tb = get_timebase(pls);
2828 ts_diff = av_rescale_q_rnd(pls->pkt->dts, tb,
2830 pls->seek_timestamp;
2831 if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
2832 pls->pkt->flags & AV_PKT_FLAG_KEY)) {
2834 break;
2835 }
2836 }
2837 av_packet_unref(pls->pkt);
2838 }
2839 }
2840 /* Check if this stream has the packet with the lowest dts */
2841 if (pls->pkt->data) {
2842 struct playlist *minpls = minplaylist < 0 ?
2843 NULL : c->playlists[minplaylist];
2844 if (minplaylist < 0) {
2845 minplaylist = i;
2846 } else {
2847 int64_t dts = pls->pkt->dts;
2848 int64_t mindts = minpls->pkt->dts;
2849
2850 if (dts == AV_NOPTS_VALUE ||
2851 (mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
2852 minplaylist = i;
2853 }
2854 }
2855 }
2856
2857 /* If we got a packet, return it */
2858 if (minplaylist >= 0) {
2859 struct playlist *pls = c->playlists[minplaylist];
2860 AVStream *ist;
2861 AVStream *st;
2862
2864 if (ret < 0) {
2865 av_packet_unref(pls->pkt);
2866 return ret;
2867 }
2868
2869 // If sub-demuxer reports updated metadata, copy it to the first stream
2870 // and set its AVSTREAM_EVENT_FLAG_METADATA_UPDATED flag.
2872 if (pls->n_main_streams) {
2873 st = pls->main_streams[0];
2874 av_dict_copy(&st->metadata, pls->ctx->metadata, 0);
2876 }
2878 }
2879
2880 /* check if noheader flag has been cleared by the subdemuxer */
2881 if (pls->has_noheader_flag && !(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER)) {
2882 pls->has_noheader_flag = 0;
2884 }
2885
2886 if (pls->pkt->stream_index >= pls->n_main_streams) {
2887 av_log(s, AV_LOG_ERROR, "stream index inconsistency: index %d, %d main streams, %d subdemuxer streams\n",
2888 pls->pkt->stream_index, pls->n_main_streams, pls->ctx->nb_streams);
2889 av_packet_unref(pls->pkt);
2890 return AVERROR_BUG;
2891 }
2892
2893 ist = pls->ctx->streams[pls->pkt->stream_index];
2894 st = pls->main_streams[pls->pkt->stream_index];
2895
2896 // Propagate timed-ID3 metadata changes to the main stream, suppressing
2897 // duplicate packets that repeat unchanged metadata at segment boundaries.
2899 pls->pkt->stream_index == pls->timed_id3_stream_index &&
2903 int ret = av_dict_copy(&st->metadata, ist->metadata, 0);
2904 if (ret < 0)
2905 return ret;
2907 ret = av_dict_copy(&pls->timed_id3_metadata, ist->metadata, 0);
2908 if (ret < 0)
2909 return ret;
2911 }
2912 }
2913
2914 av_packet_move_ref(pkt, pls->pkt);
2915 pkt->stream_index = st->index;
2916
2917 if (pkt->dts != AV_NOPTS_VALUE)
2918 c->cur_timestamp = av_rescale_q(pkt->dts,
2919 ist->time_base,
2921
2922 /* There may be more situations where this would be useful, but this at least
2923 * handles newly probed codecs properly (i.e. request_probe by mpegts). */
2924 if (ist->codecpar->codec_id != st->codecpar->codec_id) {
2925 ret = set_stream_info_from_input_stream(st, pls, ist);
2926 if (ret < 0) {
2927 return ret;
2928 }
2929 }
2930
2931 return 0;
2932 }
2933 return AVERROR_EOF;
2934}
2935
2936static int hls_read_seek(AVFormatContext *s, int stream_index,
2937 int64_t timestamp, int flags)
2938{
2939 HLSContext *c = s->priv_data;
2940 struct playlist *seek_pls = NULL;
2941 int i, j;
2942 int stream_subdemuxer_index;
2943 int64_t first_timestamp, seek_timestamp, duration;
2944 int64_t seq_no, seg_start_ts;
2945 int snapped_to_segment = 0;
2946
2947 if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
2948 return AVERROR(ENOSYS);
2949
2950 seek_timestamp = av_rescale_q_rnd(timestamp,
2951 s->streams[stream_index]->time_base,
2953
2954 /* find the playlist with the specified stream */
2955 for (i = 0; i < c->n_playlists; i++) {
2956 struct playlist *pls = c->playlists[i];
2957 for (j = 0; j < pls->n_main_streams; j++) {
2958 if (pls->main_streams[j] == s->streams[stream_index]) {
2959 seek_pls = pls;
2960 stream_subdemuxer_index = j;
2961 break;
2962 }
2963 }
2964 }
2965 if (!seek_pls)
2966 return AVERROR(EIO);
2967
2968 /* Live and EVENT playlists may have gained segments since the last load,
2969 * and sliding-window playlists may also have expired some. Refresh before
2970 * resolving a seek that may fall outside the loaded window, rate limited
2971 * to the minimum reload interval (RFC 8216 6.3.4). */
2972 if (!seek_pls->finished) {
2973 int64_t elapsed = av_gettime_relative() - seek_pls->last_load_time;
2974 int64_t start = c->first_timestamp == AV_NOPTS_VALUE ?
2975 0 : c->first_timestamp;
2976 int64_t end = start;
2977 int need_refresh;
2978 for (i = 0; i < seek_pls->n_segments; i++)
2979 end += seek_pls->segments[i]->duration;
2980 need_refresh = seek_timestamp >= end;
2981 if (seek_pls->type == PLS_TYPE_UNSPECIFIED)
2982 need_refresh |= seek_timestamp < start + elapsed +
2983 seek_pls->target_duration;
2984 if (need_refresh && elapsed >= default_reload_interval(seek_pls))
2985 parse_playlist(c, seek_pls->url, seek_pls, NULL);
2986 }
2987
2988 first_timestamp = c->first_timestamp == AV_NOPTS_VALUE ?
2989 0 : c->first_timestamp;
2990
2991 duration = s->duration == AV_NOPTS_VALUE ?
2992 0 : s->duration;
2993
2994 /* Only finished playlists cannot grow past the known duration. */
2995 if (seek_pls->finished &&
2996 0 < duration && duration < seek_timestamp - first_timestamp)
2997 return AVERROR(EIO);
2998
2999 /* check if the timestamp is valid for the playlist with the
3000 * specified stream index */
3001 if (!find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts))
3002 return AVERROR(EIO);
3003
3004 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
3006 /* Seeking to start of segment ensures we seek to a keyframe located
3007 * before the given timestamp. */
3008 seek_timestamp = seg_start_ts;
3009 snapped_to_segment = 1;
3010 }
3011
3012 av_log(s, AV_LOG_DEBUG, "Seek to %"PRId64" mapped to segment %"PRId64
3013 " of playlist %d (start %"PRId64")\n",
3014 seek_timestamp, seq_no, seek_pls->index, seg_start_ts);
3015
3016 /* Keep timeline start fixed from now on. */
3017 c->first_timestamp_locked = 1;
3018
3019 /* set segment now so we do not need to search again below */
3020 seek_pls->cur_seq_no = seq_no;
3021 seek_pls->seek_stream_index = stream_subdemuxer_index;
3022
3023 /* Reset PTS wrap detection so backward seeks don't get misinterpreted
3024 * as a forward PTS wrap. */
3025 for (i = 0; i < (int)s->nb_streams; i++)
3027
3028 for (i = 0; i < c->n_playlists; i++) {
3029 /* Reset reading */
3030 struct playlist *pls = c->playlists[i];
3031 AVIOContext *const pb = &pls->pb.pub;
3032 ff_format_io_close(pls->parent, &pls->input);
3033 pls->input_read_done = 0;
3034 pls->first_read_seq_no = -1;
3036 pls->input_next_requested = 0;
3037 av_packet_unref(pls->pkt);
3038 pb->eof_reached = 0;
3039 /* Clear any buffered data */
3040 pb->buf_end = pb->buf_ptr = pb->buffer;
3041 /* Reset the pos, to let the mpegts/mov demuxer know we've seeked. */
3042 pb->pos = 0;
3043 /* Flush the packet queue of the subdemuxer. */
3044 if (pls->ctx) {
3046 for (j = 0; j < (int)pls->ctx->nb_streams; j++)
3048 }
3049 if (pls->is_subtitle)
3051
3052 /* Reset the init segment so it's re-fetched and served appropriately */
3053 pls->cur_init_section = NULL;
3054
3055 /* The discard in hls_read_packet compares this playlist's packet DTS
3056 * against seek_timestamp, but seek_timestamp is on c->first_timestamp's
3057 * baseline (whichever stream produced the first packet). Streams can
3058 * have different DTS baselines, so translate the threshold onto this
3059 * playlist's own baseline using its captured offset. */
3060 if (pls->ts_offset != AV_NOPTS_VALUE)
3062 else
3064 pls->seek_flags = flags;
3065
3066 if (pls == seek_pls && snapped_to_segment) {
3067 /* The first keyframe of the target segment may have a slightly
3068 * lower DTS than the EXTINF-derived seg_start_ts. Relax the cutoff
3069 * to not discard the keyframe and land one segment late. */
3070 int64_t idx = seq_no - pls->start_seq_no;
3071 if (idx >= 0 && idx < pls->n_segments)
3072 pls->seek_timestamp -= pls->segments[idx]->duration;
3073 }
3074
3075 if (pls != seek_pls) {
3076 /* set closest segment seq_no for playlists not handled above */
3078 /* seek the playlist to the given position without taking
3079 * keyframes into account since this playlist does not have the
3080 * specified stream where we should look for the keyframes */
3081 pls->seek_stream_index = -1;
3083 }
3084
3085 pls->last_seq_no = pls->cur_seq_no;
3086 }
3087
3088 c->cur_timestamp = seek_timestamp;
3089
3090 return 0;
3091}
3092
3093static int hls_probe(const AVProbeData *p)
3094{
3095 /* Require #EXTM3U at the start, and either one of the ones below
3096 * somewhere for a proper match. */
3097 if (strncmp(p->buf, "#EXTM3U", 7))
3098 return 0;
3099
3100 if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
3101 strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
3102 strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:")) {
3103
3104 int mime_ok = p->mime_type && !(
3105 av_strcasecmp(p->mime_type, "application/vnd.apple.mpegurl") &&
3106 av_strcasecmp(p->mime_type, "audio/mpegurl")
3107 );
3108
3109 int mime_x = p->mime_type && !(
3110 av_strcasecmp(p->mime_type, "audio/x-mpegurl") &&
3111 av_strcasecmp(p->mime_type, "application/x-mpegurl")
3112 );
3113
3114 if (!mime_ok &&
3115 !mime_x &&
3116 !av_match_ext (p->filename, "m3u8,m3u") &&
3117 ff_match_url_ext(p->filename, "m3u8,m3u") <= 0) {
3118 av_log(NULL, AV_LOG_ERROR, "Not detecting m3u8/hls with non standard extension and non standard mime type\n");
3119 return 0;
3120 }
3121 if (mime_x)
3122 av_log(NULL, AV_LOG_WARNING, "mime type is not rfc8216 compliant\n");
3123
3124 return AVPROBE_SCORE_MAX;
3125 }
3126 return 0;
3127}
3128
3129#define OFFSET(x) offsetof(HLSContext, x)
3130#define FLAGS AV_OPT_FLAG_DECODING_PARAM
3131static const AVOption hls_options[] = {
3132 {"live_start_index", "segment index to start live streams at (negative values are from the end)",
3133 OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
3134 {"prefer_x_start", "prefer to use #EXT-X-START if it's in playlist instead of live_start_index",
3135 OFFSET(prefer_x_start), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS},
3136 {"allowed_extensions", "List of file extensions that hls is allowed to access",
3137 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
3138 {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,vtt,wav,webvtt"
3139 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3140 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3141 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3142 },
3143 INT_MIN, INT_MAX, FLAGS},
3144 {"allowed_segment_extensions", "List of file extensions that hls is allowed to access",
3145 OFFSET(allowed_segment_extensions), AV_OPT_TYPE_STRING,
3146 {.str = "3gp,aac,avi,ac3,eac3,flac,mkv,m3u8,m4a,m4s,m4v,mpg,mov,mp2,mp3,mp4,mpeg,mpegts,ogg,ogv,oga,ts,vob,vtt,wav,webvtt"
3147 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3148 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3149 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3150 ",html" // https://flash1.bogulus.cfd/
3151 },
3152 INT_MIN, INT_MAX, FLAGS},
3153 {"extension_picky", "Be picky with all extensions matching",
3154 OFFSET(extension_picky), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
3155 {"max_reload", "Maximum number of times a insufficient list is attempted to be reloaded",
3156 OFFSET(max_reload), AV_OPT_TYPE_INT, {.i64 = 100}, 0, INT_MAX, FLAGS},
3157 {"m3u8_hold_counters", "The maximum number of times to load m3u8 when it refreshes without new segments",
3158 OFFSET(m3u8_hold_counters), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS},
3159 {"http_persistent", "Use persistent HTTP connections",
3160 OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
3161 {"http_multiple", "Use multiple HTTP connections for fetching segments",
3162 OFFSET(http_multiple), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, FLAGS},
3163 {"http_seekable", "Use HTTP partial requests, 0 = disable, 1 = enable, -1 = auto",
3164 OFFSET(http_seekable), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, FLAGS},
3165 {"seg_format_options", "Set options for segment demuxer",
3166 OFFSET(seg_format_opts), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, FLAGS},
3167 {"seg_max_retry", "Maximum number of times to reload a segment on error.",
3168 OFFSET(seg_max_retry), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
3169 {NULL}
3170};
3171
3172static const AVClass hls_class = {
3173 .class_name = "hls demuxer",
3174 .item_name = av_default_item_name,
3175 .option = hls_options,
3176 .version = LIBAVUTIL_VERSION_INT,
3177};
3178
3180 .p.name = "hls",
3181 .p.long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
3182 .p.priv_class = &hls_class,
3184 .priv_data_size = sizeof(HLSContext),
3185 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
3191};
static av_always_inline void update(AVFilterContext *ctx, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
const FFInputFormat ff_hls_demuxer
Definition hls.c:3179
#define entry
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition avformat.c:983
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:901
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition avformat.c:358
Main libavformat public API header.
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1795
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1351
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition avformat.h:502
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:508
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2712
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition avformat.h:507
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition avformat.h:498
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition avformat.h:2713
#define AVFMTCTX_UNSEEKABLE
signal that the stream is definitely not seekable, and attempts to call the seek function will fail.
Definition avformat.h:1353
#define AV_DISPOSITION_FORCED
Track should be used during playback by default.
Definition avformat.h:674
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:886
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition avformat.h:1560
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2711
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition avformat.h:641
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition avio.c:113
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition avio.c:929
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition avio.c:732
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition aviobuf.c:50
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition aviobuf.c:99
int ffio_copy_url_options(AVIOContext *pb, AVDictionary **avio_opts)
Read url related dictionary options from the AVIOContext and write to the given dictionary.
Definition aviobuf.c:953
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
Same as ff_get_line but strip the white-space characters in the text tail.
Definition aviobuf.c:756
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define FLAGS
Definition cmdutils.c:596
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define INITIAL_BUFFER_SIZE
Definition dashdec.c:36
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition dashdec.c:1938
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:719
static AVPacket * pkt
Public dictionary API.
static struct @111144215057303131116103221376075045141373005341 current
static int read_key(void)
Definition ffmpeg.c:264
const char * key
static int64_t duration
Definition ffplay.c:411
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static av_cold void cleanup(FlashSV2Context *s)
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:610
@ AV_CODEC_ID_EAC3
Definition codec_id.h:497
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AAC
Definition codec_id.h:459
@ AV_CODEC_ID_AC3
Definition codec_id.h:460
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:246
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition avformat.c:297
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:164
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition format.c:146
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1611
int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Like av_probe_input_buffer2() but returns 0 on success.
Definition format.c:348
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2639
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:400
int avformat_queue_attached_pictures(AVFormatContext *s)
Definition demux_utils.c:87
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition aes.c:37
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:38
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition dict.h:74
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:180
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition error.h:65
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare the remainders of two integer operands divided by a common divisor.
@ AV_ROUND_DOWN
Round toward -infinity.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:601
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition avstring.c:344
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition avstring.c:218
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition opt.c:1291
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
static void update_noheader_flag(AVFormatContext *s)
Definition hls.c:2254
#define MAX_CHARACTERISTICS_LEN
Definition hls.c:54
#define MAX_FIELD_LEN
Definition hls.c:53
static int is_native_http(AVIOContext *pb)
Definition hls.c:669
static int update_init_section(struct playlist *pls, struct segment *seg)
Definition hls.c:1561
static struct rendition * new_rendition(HLSContext *c, struct rendition_info *info, const char *url_base)
Definition hls.c:525
static int read_from_url(struct playlist *pls, struct segment *seg, uint8_t *buf, int buf_size)
Definition hls.c:1236
static struct segment * new_init_section(struct playlist *pls, struct init_section_info *info, const char *url_base)
Definition hls.c:451
static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, int64_t timestamp, int64_t *seq_no, int64_t *seg_start_ts)
Definition hls.c:2049
static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:1748
static int hls_read_header(AVFormatContext *s)
Definition hls.c:2292
static struct playlist * new_playlist(HLSContext *c, const char *url, const char *base)
Definition hls.c:335
static int read_subtitle_packet(struct playlist *v, AVPacket *pkt)
Definition hls.c:1961
static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls, AVStream *ist)
Definition hls.c:2189
static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
Definition hls.c:785
static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in, int64_t *end)
Definition hls.c:1485
static void free_segment_list(struct playlist *pls)
Definition hls.c:265
static void handle_key_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:430
#define MPEG_TIME_BASE
Definition hls.c:56
static void free_init_section_list(struct playlist *pls)
Definition hls.c:272
static void add_renditions_to_variant(HLSContext *c, struct variant *var, enum AVMediaType type, const char *group_id)
Definition hls.c:1993
static int hls_probe(const AVProbeData *p)
Definition hls.c:3093
static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
Definition hls.c:658
static int segment_continues(const struct segment *cur, const struct segment *next, int64_t end)
Definition hls.c:1210
PlaylistType
Definition hls.c:93
@ PLS_TYPE_UNSPECIFIED
Definition hls.c:94
@ PLS_TYPE_VOD
Definition hls.c:96
@ PLS_TYPE_EVENT
Definition hls.c:95
static int recheck_discard_flags(AVFormatContext *s, int first)
Definition hls.c:2617
static AVRational get_timebase(struct playlist *pls)
Definition hls.c:2686
static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
Definition hls.c:2661
static int64_t default_reload_interval(struct playlist *pls)
Definition hls.c:1618
static int open_url_keepalive(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **options)
Definition hls.c:679
static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls, enum AVMediaType type)
Definition hls.c:2017
static int update_streams_from_subdemuxer(AVFormatContext *s, struct playlist *pls)
Definition hls.c:2213
static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
Definition hls.c:2163
static int hls_close(AVFormatContext *s)
Definition hls.c:2275
static int metadata_has_changed(const AVDictionary *current, const AVDictionary *update)
Check whether any entry in update differs from the corresponding entry in current.
Definition hls.c:2712
static struct segment * next_segment(struct playlist *pls)
Definition hls.c:1198
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition hls.c:1911
static int reload_playlist(struct playlist *v, HLSContext *c)
Definition hls.c:1671
static void handle_id3(AVIOContext *pb, struct playlist *pls)
Definition hls.c:1311
#define PACKED_AUDIO_FORMATS
Definition hls.c:59
static void handle_rendition_args(void *vinfo, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:612
static void free_segment_dynarray(struct segment **segments, int n_segments)
Definition hls.c:255
static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:1882
static int init_subtitle_context(struct playlist *pls)
Definition hls.c:1921
static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a, int64_t ts_b, struct playlist *pls_b)
Definition hls.c:2694
static int parse_playlist(HLSContext *c, const char *url, struct playlist *pls, AVIOContext *in)
Definition hls.c:832
#define MPEG_TIME_BASE_Q
Definition hls.c:57
KeyType
Definition hls.c:73
@ KEY_SAMPLE_AES
Definition hls.c:76
@ KEY_NONE
Definition hls.c:74
@ KEY_AES_128
Definition hls.c:75
static void handle_variant_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:405
static struct segment * current_segment(struct playlist *pls)
Definition hls.c:1190
static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition hls.c:2726
static const AVOption hls_options[]
Definition hls.c:3131
static void free_rendition_list(HLSContext *c)
Definition hls.c:326
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
Definition hls.c:697
#define OFFSET(x)
Definition hls.c:3129
static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, ID3v2ExtraMetaAPIC *apic)
Definition hls.c:1282
static void free_playlist_list(HLSContext *c)
Definition hls.c:284
static const AVClass hls_class
Definition hls.c:3172
static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
Definition hls.c:2088
static struct variant * new_variant(HLSContext *c, struct variant_info *info, const char *url, const char *base)
Definition hls.c:379
static void intercept_id3(struct playlist *pls, uint8_t *buf, int buf_size, int *len)
Definition hls.c:1354
static int playlist_needed(struct playlist *pls)
Definition hls.c:1625
static void free_variant_list(HLSContext *c)
Definition hls.c:314
static int64_t range_end(struct playlist *pls, const struct segment *seg)
Definition hls.c:1222
static void parse_id3(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info, ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
Definition hls.c:1253
static void handle_init_section_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:500
static int hls_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition hls.c:2936
int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt)
int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info)
void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size)
Apple HTTP Live Streaming Sample Encryption https://developer.apple.com/library/ios/documentation/Aud...
#define HLS_MAX_ID3_TAGS_DATA_LEN
int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
Send a new HTTP request, reusing the old connection.
Definition http.c:589
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition id3v2.c:165
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition id3v2.c:152
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition id3v2.c:1186
int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
Parse PRIV tags into a dictionary.
Definition id3v2.c:1258
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition id3v2.c:1202
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition id3v2.c:1298
void ff_id3v2_read_dict(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition id3v2.c:1174
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define ID3v2_HEADER_SIZE
Definition id3v2.h:30
#define r
Definition input.c:42
#define AV_WB64(p, v)
#define AV_RB64(p)
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
int ff_match_url_ext(const char *url, const char *extensions)
Return a positive value if the given url has one of the given extensions, negative AVERROR on error,...
Definition format.c:54
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition utils.c:495
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define dynarray_add(tab, nb_ptr, elem)
Definition internal.h:376
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:473
#define MAX_URL_SIZE
Definition internal.h:30
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf, void *context)
Parse a string with comma-separated key=value pairs.
Definition utils.c:523
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
unsigned int pos
Definition spdifenc.c:431
double strtod(const char *, char **)
uint8_t * data
The data buffer.
Definition buffer.h:90
size_t size
Size of data in bytes.
Definition buffer.h:94
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
char * key
Definition dict.h:91
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1400
int event_flags
Flags indicating events happening on the file, a combination of AVFMT_EVENT_FLAG_*.
Definition avformat.h:1788
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1456
AVIOContext * pb
I/O context.
Definition avformat.h:1442
AVDictionary * metadata
Metadata that applies to the whole file.
Definition avformat.h:1648
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1552
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition avformat.h:1608
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1449
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1686
const struct AVInputFormat * iformat
The input container format.
Definition avformat.h:1412
void * priv_data
Format private data.
Definition avformat.h:1428
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition avformat.h:1600
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1468
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition avformat.h:2021
Bytestream IO Context.
Definition avio.h:160
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition avio.h:228
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
unsigned char * buf_ptr
Current position in the buffer.
Definition avio.h:227
int64_t pos
position in the file of the current buffer
Definition avio.h:237
unsigned char * buffer
Start of the buffer.
Definition avio.h:225
int eof_reached
true if was unable to read due to error or eof
Definition avio.h:238
Callback for checking whether to abort blocking functions.
Definition avio.h:59
const char * name
A comma separated list of short names for the format.
Definition avformat.h:572
const char * extensions
If extensions are defined, then no probe is done.
Definition avformat.h:594
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int stream_index
Definition packet.h:612
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:616
int size
Definition packet.h:611
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:609
uint8_t * data
Definition packet.h:610
This structure contains the data a format has to probe a file.
Definition avformat.h:473
New fields can be added to the end with minor version bumps.
Definition avformat.h:1324
unsigned int nb_stream_indexes
Definition avformat.h:1329
unsigned int * stream_index
Definition avformat.h:1328
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition avformat.h:1327
AVDictionary * metadata
Definition avformat.h:1330
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:839
AVDictionary * metadata
Definition avformat.h:848
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int pts_wrap_bits
Number of bits in timestamps.
Definition avformat.h:911
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition avformat.h:866
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition avformat.h:879
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
AVIOContext pub
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition internal.h:262
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition internal.h:180
int prefer_x_start
Definition hls.c:234
int first_timestamp_locked
Definition hls.c:238
int n_renditions
Definition hls.c:228
int http_multiple
Definition hls.c:248
AVDictionary * seg_format_opts
Definition hls.c:242
struct rendition ** renditions
Definition hls.c:229
int m3u8_hold_counters
Definition hls.c:232
AVIOInterruptCB * interrupt_callback
Definition hls.c:240
int seg_max_retry
Definition hls.c:250
AVDictionary * avio_opts
Definition hls.c:241
char * allowed_segment_extensions
Definition hls.c:244
int n_variants
Definition hls.c:224
int http_persistent
Definition hls.c:247
struct variant ** variants
Definition hls.c:225
int extension_picky
Definition hls.c:245
AVFormatContext * ctx
Definition hls.c:223
int64_t cur_seq_no
Definition hls.c:231
int live_start_index
Definition hls.c:233
AVIOContext * playlist_pb
Definition hls.c:251
int first_packet
Definition hls.c:235
int max_reload
Definition hls.c:246
int n_playlists
Definition hls.c:226
struct playlist * first_timestamp_pls
Definition hls.c:237
int64_t cur_timestamp
Definition hls.c:239
HLSCryptoContext crypto_ctx
Definition hls.c:252
int64_t first_timestamp
Definition hls.c:236
struct playlist ** playlists
Definition hls.c:227
char * allowed_extensions
Definition hls.c:243
int http_seekable
Definition hls.c:249
AVBufferRef * buf
Definition id3v2.h:66
uint8_t * data
Definition id3v2.h:74
uint32_t datasize
Definition id3v2.h:75
uint8_t * owner
Definition id3v2.h:73
ID3v2ExtraMetaPRIV priv
Definition id3v2.h:91
ID3v2ExtraMetaAPIC apic
Definition id3v2.h:88
union ID3v2ExtraMeta::@257041253361347165140143067330023232307201221326 data
const char * tag
Definition id3v2.h:85
struct ID3v2ExtraMeta * next
Definition id3v2.h:86
const struct URLProtocol * prot
Definition url.h:37
const char * name
Definition url.h:55
char uri[MAX_URL_SIZE]
Definition hls.c:447
char byterange[32]
Definition hls.c:448
char iv[35]
Definition hls.c:427
char uri[MAX_URL_SIZE]
Definition hls.c:425
char method[11]
Definition hls.c:426
int finished
Definition hls.c:124
int id3_changed
Definition hls.c:160
int broken
Definition hls.c:133
int64_t cur_seg_offset
Definition hls.c:138
AVIOContext * input
Definition hls.c:107
int64_t cur_seq_no
Definition hls.c:134
int n_renditions
Definition hls.c:176
int64_t first_read_seq_no
Definition hls.c:136
int64_t id3_mpegts_timestamp
Definition hls.c:154
FFIOContext pb
Definition hls.c:106
struct rendition ** renditions
Definition hls.c:177
unsigned int init_sec_data_len
Definition hls.c:145
int64_t id3_offset
Definition hls.c:155
int64_t target_duration
Definition hls.c:126
int64_t last_load_time
Definition hls.c:139
int64_t last_seq_no
Definition hls.c:135
int seek_stream_index
Definition hls.c:170
int64_t input_next_end
Definition hls.c:112
HLSAudioSetupInfo audio_setup_info
Definition hls.c:163
int64_t start_seq_no
Definition hls.c:127
int is_subtitle
Definition hls.c:183
unsigned int id3_buf_size
Definition hls.c:157
AVStream ** main_streams
Definition hls.c:121
AVIOContext * input_next
Definition hls.c:110
unsigned int init_sec_buf_read_offset
Definition hls.c:146
int64_t ts_offset
Definition hls.c:167
int64_t seek_timestamp
Definition hls.c:168
int64_t input_end
Definition hls.c:109
AVPacket * pkt
Definition hls.c:116
int input_read_done
Definition hls.c:108
AVFormatContext * ctx
Definition hls.c:115
uint8_t * init_sec_buf
Definition hls.c:143
struct segment * cur_init_section
Definition hls.c:142
uint8_t * id3_buf
Definition hls.c:156
uint8_t key[16]
Definition hls.c:149
int id3_found
Definition hls.c:159
int index
Definition hls.c:114
int m3u8_hold_counters
Definition hls.c:137
int needed
Definition hls.c:132
int has_noheader_flag
Definition hls.c:117
char key_url[MAX_URL_SIZE]
Definition hls.c:148
int timed_id3_stream_index
Definition hls.c:191
int n_init_sections
Definition hls.c:181
int64_t start_time_offset
Definition hls.c:129
AVDictionary * timed_id3_metadata
Definition hls.c:190
struct segment ** segments
Definition hls.c:131
int n_segments
Definition hls.c:130
struct segment ** init_sections
Definition hls.c:182
char url[MAX_URL_SIZE]
Definition hls.c:105
AVFormatContext * parent
Definition hls.c:113
int time_offset_flag
Definition hls.c:128
int seek_flags
Definition hls.c:169
int is_id3_timestamped
Definition hls.c:153
unsigned int init_sec_buf_size
Definition hls.c:144
int n_main_streams
Definition hls.c:122
int input_next_requested
Definition hls.c:111
ID3v2ExtraMeta * id3_deferred_extra
Definition hls.c:161
AVDictionary * id3_initial
Definition hls.c:158
enum PlaylistType type
Definition hls.c:125
char type[16]
Definition hls.c:514
char defaultr[4]
Definition hls.c:520
char name[MAX_FIELD_LEN]
Definition hls.c:519
char language[MAX_FIELD_LEN]
Definition hls.c:517
char forced[4]
Definition hls.c:521
char uri[MAX_URL_SIZE]
Definition hls.c:515
char group_id[MAX_FIELD_LEN]
Definition hls.c:516
char characteristics[MAX_CHARACTERISTICS_LEN]
Definition hls.c:522
char assoc_language[MAX_FIELD_LEN]
Definition hls.c:518
char name[MAX_FIELD_LEN]
Definition hls.c:205
enum AVMediaType type
Definition hls.c:201
char group_id[MAX_FIELD_LEN]
Definition hls.c:203
char language[MAX_FIELD_LEN]
Definition hls.c:204
int disposition
Definition hls.c:206
struct playlist * playlist
Definition hls.c:202
Definition hls.c:79
struct segment * init_section
Definition hls.c:88
int64_t size
Definition hls.c:82
uint8_t iv[16]
Definition hls.c:86
char * url
Definition hls.c:83
int64_t duration
Definition hls.c:80
int64_t url_offset
Definition hls.c:81
char * key
Definition hls.c:84
enum KeyType key_type
Definition hls.c:85
char bandwidth[20]
Definition hls.c:372
char video[MAX_FIELD_LEN]
Definition hls.c:375
char subtitles[MAX_FIELD_LEN]
Definition hls.c:376
char audio[MAX_FIELD_LEN]
Definition hls.c:374
Definition hls.c:209
int bandwidth
Definition hls.c:210
char audio_group[MAX_FIELD_LEN]
Definition hls.c:216
int n_playlists
Definition hls.c:213
struct playlist ** playlists
Definition hls.c:214
char video_group[MAX_FIELD_LEN]
Definition hls.c:217
char subtitles_group[MAX_FIELD_LEN]
Definition hls.c:218
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int size
int ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition url.c:321
unbuffered private I/O API
enum AVCodecID codec_id
static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
float delta
int len
uint8_t base
Definition vp3data.h:128
static double c[64]