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 "libavutil/tree.h"
43#include "avformat.h"
44#include "demux.h"
45#include "internal.h"
46#include "avio_internal.h"
47#include "id3v2.h"
48#include "url.h"
49
51
52#define INITIAL_BUFFER_SIZE 32768
53
54#define MAX_FIELD_LEN 64
55#define MAX_CHARACTERISTICS_LEN 512
56
57#define MPEG_TIME_BASE 90000
58#define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE}
59
60#define PACKED_AUDIO_FORMATS "aac,ac3,eac3,mp3"
61
62/*
63 * An apple http stream consists of a playlist with media segment files,
64 * played sequentially. There may be several playlists with the same
65 * video content, in different bandwidth variants, that are played in
66 * parallel (preferably only one bandwidth variant at a time). In this case,
67 * the user supplied the url to a main playlist that only lists the variant
68 * playlists.
69 *
70 * If the main playlist doesn't point at any variants, we still create
71 * one anonymous toplevel variant for this, to maintain the structure.
72 */
73
79
80struct segment {
84 char *url;
85 char *key;
87 uint8_t iv[16];
89 /* associated Media Initialization Section, treated as a segment */
91};
92
93struct rendition;
94struct rendition_group;
95struct variant;
96
102
103/*
104 * Each playlist has its own demuxer. If it currently is active,
105 * it has an open AVIOContext too, and potentially an AVPacket
106 * containing the next packet from this stream.
107 */
108struct playlist {
113 int64_t input_end; /* end of the byte range requested on input, or -1 */
118 int index;
121 int64_t last_cue_pts; /* reference for the cues read without media */
125 int cues_unanchored; /* cues kept before the media position was known */
126 uint64_t cues_discontinuity_seq; /* of the segment the kept cues are from */
130
131 /* main demuxer streams associated with this playlist
132 * indexed by the subdemuxer stream indexes */
135
152
153 /* Currently active Media Initialization Section */
155 uint8_t *init_sec_buf;
156 unsigned int init_sec_buf_size;
157 unsigned int init_sec_data_len;
159
161 uint8_t key[16];
162
163 /* ID3 timestamp handling (elementary audio streams have ID3 timestamps
164 * (and possibly other ID3 tags) in the beginning of each segment) */
165 int is_id3_timestamped; /* -1: not yet known */
166 int64_t id3_mpegts_timestamp; /* in mpegts tb */
167 int64_t id3_offset; /* in stream original tb */
168 uint8_t* id3_buf; /* temp buffer for id3 parsing */
169 unsigned int id3_buf_size;
170 AVDictionary *id3_initial; /* data from first id3 tag */
171 int id3_found; /* ID3 tag found at some point */
172 int id3_changed; /* ID3 tag data has changed at some point */
173 ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */
174
176
177 /* Constant offset of this playlist's DTS baseline relative to
178 * c->first_timestamp. */
182 int seek_stream_index; /* into subdemuxer stream array */
183
184 /* Renditions associated with this playlist, if any.
185 * Alternative rendition playlists have a single rendition associated
186 * with them, and variant main Media Playlists may have
187 * multiple (playlist-less) renditions associated with them. */
190
191 /* The last variant this playlist was added to and the last rendition
192 * group whose renditions were added to it. */
195
196 /* Media Initialization Sections (EXT-X-MAP) associated with this
197 * playlist, if any. */
200 int is_subtitle; /* Indicates if it's a subtitle playlist */
201
202 /* Some HLS streams repeat the current timed-ID3 metadata at every segment
203 * boundary so late-joining clients get a fresh copy. Track the last seen
204 * value to avoid raising AVSTREAM_EVENT_FLAG_METADATA_UPDATED spuriously
205 * on those duplicates. timed_id3_stream_index is the subdemuxer stream
206 * index of the first timed-ID3 stream seen; -1 if none. */
209};
210
211/*
212 * Renditions are e.g. alternative subtitle or audio streams.
213 * The rendition may either be an external playlist or it may be
214 * contained in the main Media Playlist of the variant (in which case
215 * playlist is NULL).
216 */
224
225struct variant {
227
228 /* every variant contains at least the main Media Playlist in index 0 */
231
235};
236
237/*
238 * The renditions with the same TYPE and GROUP-ID, which variants reference
239 * by the group id.
240 */
244
245 /* playlists of the external renditions */
248
249 /* renditions that are part of the variant main Media Playlists */
252
253 /* variants that reference the group */
256};
257
296
298{
299 int i;
300 for (i = 0; i < n_segments; i++) {
304 }
305}
306
307static void free_segment_list(struct playlist *pls)
308{
310 av_freep(&pls->segments);
311 pls->n_segments = 0;
312}
313
314static void free_init_section_list(struct playlist *pls)
315{
316 int i;
317 for (i = 0; i < pls->n_init_sections; i++) {
318 av_freep(&pls->init_sections[i]->key);
319 av_freep(&pls->init_sections[i]->url);
320 av_freep(&pls->init_sections[i]);
321 }
322 av_freep(&pls->init_sections);
323 pls->n_init_sections = 0;
324}
325
326static void free_cue_list(AVPacket ***cues, int *n_cues)
327{
328 for (int i = 0; i < *n_cues; i++)
329 av_packet_free(&(*cues)[i]);
330 av_freep(cues);
331 *n_cues = 0;
332}
333
334static void free_subtitle_cues(struct playlist *pls)
335{
336 free_cue_list(&pls->prev_cues, &pls->n_prev_cues);
337 free_cue_list(&pls->cur_cues, &pls->n_cur_cues);
338 pls->prev_cue_pos = 0;
339 pls->cues_unanchored = 0;
341}
342
344{
345 int i;
346 for (i = 0; i < c->n_playlists; i++) {
347 struct playlist *pls = c->playlists[i];
351 av_freep(&pls->main_streams);
352 av_freep(&pls->renditions);
353 av_freep(&pls->id3_buf);
357 av_freep(&pls->init_sec_buf);
358 av_packet_free(&pls->pkt);
359 av_freep(&pls->pb.pub.buffer);
360 ff_format_io_close(c->ctx, &pls->input);
361 pls->input_read_done = 0;
362 ff_format_io_close(c->ctx, &pls->input_next);
363 pls->input_next_requested = 0;
364 if (pls->ctx) {
365 pls->ctx->pb = NULL;
367 }
368 av_free(pls);
369 }
370 av_freep(&c->playlists);
371 c->n_playlists = 0;
372 av_tree_destroy(c->playlists_by_url);
373 c->playlists_by_url = NULL;
374}
375
377{
378 int i;
379 for (i = 0; i < c->n_variants; i++) {
380 struct variant *var = c->variants[i];
381 av_freep(&var->playlists);
382 av_free(var);
383 }
384 av_freep(&c->variants);
385 c->n_variants = 0;
386}
387
389{
390 int i;
391 for (i = 0; i < c->n_renditions; i++)
392 av_freep(&c->renditions[i]);
393 av_freep(&c->renditions);
394 c->n_renditions = 0;
395 for (i = 0; i < c->n_rendition_groups; i++) {
396 av_freep(&c->rendition_groups[i]->playlists);
397 av_freep(&c->rendition_groups[i]->renditions);
398 av_freep(&c->rendition_groups[i]->variants);
399 av_freep(&c->rendition_groups[i]);
400 }
401 av_freep(&c->rendition_groups);
402 c->n_rendition_groups = 0;
403 av_tree_destroy(c->rendition_groups_by_id);
404 c->rendition_groups_by_id = NULL;
405}
406
407static int cmp_url_playlist(const void *key, const void *b)
408{
409 const struct playlist *pls = b;
410 return strcmp(key, pls->url);
411}
412
413static int cmp_playlist_url(const void *a, const void *b)
414{
415 const struct playlist *pa = a, *pb = b;
416 return strcmp(pa->url, pb->url);
417}
418
419static struct playlist *new_playlist(HLSContext *c, const char *url,
420 const char *base)
421{
422 struct playlist *pls;
423 struct AVTreeNode *node;
424 char abs_url[MAX_URL_SIZE];
425
426 ff_make_absolute_url(abs_url, sizeof(abs_url), base, url);
427 if (!abs_url[0])
428 return NULL;
429
430 /* the same URL in several variants or renditions shares one playlist */
431 pls = av_tree_find(c->playlists_by_url, abs_url, cmp_url_playlist, NULL);
432 if (pls)
433 return pls;
434
435 pls = av_mallocz(sizeof(struct playlist));
436 node = av_tree_node_alloc();
437 if (!pls || !node)
438 goto fail;
439
440 pls->pkt = av_packet_alloc();
441 if (!pls->pkt)
442 goto fail;
443
444 av_strlcpy(pls->url, abs_url, sizeof(pls->url));
448 pls->first_read_seq_no = -1;
449
450 pls->is_id3_timestamped = -1;
452 pls->timed_id3_stream_index = -1;
453
454 if (av_dynarray_add_nofree(&c->playlists, &c->n_playlists, pls) < 0)
455 goto fail;
456 av_tree_insert(&c->playlists_by_url, pls, cmp_playlist_url, &node);
457 return pls;
458
459fail:
460 if (pls)
461 av_packet_free(&pls->pkt);
462 av_free(pls);
463 av_free(node);
464 return NULL;
465}
466
468 char bandwidth[20];
469 /* variant group ids: */
473};
474
475static struct variant *new_variant(HLSContext *c, struct variant_info *info,
476 const char *url, const char *base)
477{
478 struct variant *var;
479 struct playlist *pls;
480
481 pls = new_playlist(c, url, base);
482 if (!pls)
483 return NULL;
484
485 var = av_mallocz(sizeof(struct variant));
486 if (!var)
487 return NULL;
488
489 if (info) {
490 var->bandwidth = atoi(info->bandwidth);
491 strcpy(var->audio_group, info->audio);
492 strcpy(var->video_group, info->video);
493 strcpy(var->subtitles_group, info->subtitles);
494 }
495
496 dynarray_add(&c->variants, &c->n_variants, var);
497 dynarray_add(&var->playlists, &var->n_playlists, pls);
498 return var;
499}
500
501static void handle_variant_args(void *context, const char *key,
502 int key_len, char **dest, int *dest_len)
503{
504 struct variant_info *info = context;
505 if (!strncmp(key, "BANDWIDTH=", key_len)) {
506 *dest = info->bandwidth;
507 *dest_len = sizeof(info->bandwidth);
508 } else if (!strncmp(key, "AUDIO=", key_len)) {
509 *dest = info->audio;
510 *dest_len = sizeof(info->audio);
511 } else if (!strncmp(key, "VIDEO=", key_len)) {
512 *dest = info->video;
513 *dest_len = sizeof(info->video);
514 } else if (!strncmp(key, "SUBTITLES=", key_len)) {
515 *dest = info->subtitles;
516 *dest_len = sizeof(info->subtitles);
517 }
518}
519
520struct key_info {
522 char method[11];
523 char iv[35];
524};
525
526static void handle_key_args(void *context, const char *key,
527 int key_len, char **dest, int *dest_len)
528{
529 struct key_info *info = context;
530 if (!strncmp(key, "METHOD=", key_len)) {
531 *dest = info->method;
532 *dest_len = sizeof(info->method);
533 } else if (!strncmp(key, "URI=", key_len)) {
534 *dest = info->uri;
535 *dest_len = sizeof(info->uri);
536 } else if (!strncmp(key, "IV=", key_len)) {
537 *dest = info->iv;
538 *dest_len = sizeof(info->iv);
539 }
540}
541
544 char byterange[32];
545};
546
547static struct segment *new_init_section(struct playlist *pls,
548 struct init_section_info *info,
549 const char *url_base)
550{
551 struct segment *sec;
552 char tmp_str[MAX_URL_SIZE], *ptr = tmp_str;
553
554 if (!info->uri[0])
555 return NULL;
556
557 sec = av_mallocz(sizeof(*sec));
558 if (!sec)
559 return NULL;
560
561 if (!av_strncasecmp(info->uri, "data:", 5)) {
562 ptr = info->uri;
563 } else {
564 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url_base, info->uri);
565 if (!tmp_str[0]) {
566 av_free(sec);
567 return NULL;
568 }
569 }
570 sec->url = av_strdup(ptr);
571 if (!sec->url) {
572 av_free(sec);
573 return NULL;
574 }
575
576 if (info->byterange[0]) {
577 sec->size = strtoll(info->byterange, NULL, 10);
578 ptr = strchr(info->byterange, '@');
579 if (ptr)
580 sec->url_offset = strtoll(ptr+1, NULL, 10);
581 if (sec->size < 0 || sec->url_offset < 0 || sec->size > INT64_MAX - sec->url_offset) {
582 av_freep(&sec->url);
583 av_free(sec);
584 return NULL;
585 }
586 } else {
587 /* the entire file is the init section */
588 sec->size = -1;
589 }
590
591 dynarray_add(&pls->init_sections, &pls->n_init_sections, sec);
592
593 return sec;
594}
595
596static void handle_init_section_args(void *context, const char *key,
597 int key_len, char **dest, int *dest_len)
598{
599 struct init_section_info *info = context;
600 if (!strncmp(key, "URI=", key_len)) {
601 *dest = info->uri;
602 *dest_len = sizeof(info->uri);
603 } else if (!strncmp(key, "BYTERANGE=", key_len)) {
604 *dest = info->byterange;
605 *dest_len = sizeof(info->byterange);
606 }
607}
608
612
613static void handle_start_args(void *context, const char *key,
614 int key_len, char **dest, int *dest_len)
615{
616 struct start_info *info = context;
617 if (!strncmp(key, "TIME-OFFSET=", key_len)) {
618 *dest = info->time_offset;
619 *dest_len = sizeof(info->time_offset);
620 }
621}
622
634
635static int cmp_rendition_group(const void *a, const void *b)
636{
637 const struct rendition_group *ga = a, *gb = b;
638
639 if (ga->type != gb->type)
640 return FFDIFFSIGN(ga->type, gb->type);
641 return strcmp(ga->id, gb->id);
642}
643
645 enum AVMediaType type,
646 const char *id)
647{
648 struct rendition_group key = { .type = type };
649
650 av_strlcpy(key.id, id, sizeof(key.id));
651 return av_tree_find(c->rendition_groups_by_id, &key, cmp_rendition_group, NULL);
652}
653
655 enum AVMediaType type,
656 const char *id)
657{
658 struct rendition_group *group = find_rendition_group(c, type, id);
659 struct AVTreeNode *node;
660
661 if (group)
662 return group;
663
664 group = av_mallocz(sizeof(*group));
665 node = av_tree_node_alloc();
666 if (!group || !node ||
667 av_dynarray_add_nofree(&c->rendition_groups, &c->n_rendition_groups, group) < 0) {
668 av_free(group);
669 av_free(node);
670 return NULL;
671 }
672 group->type = type;
673 av_strlcpy(group->id, id, sizeof(group->id));
674 av_tree_insert(&c->rendition_groups_by_id, group, cmp_rendition_group, &node);
675 return group;
676}
677
678static int new_rendition(HLSContext *c, struct rendition_info *info,
679 const char *url_base)
680{
681 struct rendition_group *group;
682 struct rendition *rend;
684 char *characteristic;
685 char *chr_ptr;
686 char *saveptr;
687 int ret;
688
689 if (!strcmp(info->type, "AUDIO"))
691 else if (!strcmp(info->type, "VIDEO"))
693 else if (!strcmp(info->type, "SUBTITLES"))
695 else if (!strcmp(info->type, "CLOSED-CAPTIONS"))
696 /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in
697 * AVC SEI RBSP anyway */
698 return 0;
699
700 if (type == AVMEDIA_TYPE_UNKNOWN) {
701 av_log(c->ctx, AV_LOG_WARNING, "Can't support the type: %s\n", info->type);
702 return 0;
703 }
704
705 /* URI is mandatory for subtitles as per spec */
706 if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0]) {
707 av_log(c->ctx, AV_LOG_ERROR, "The URI tag is REQUIRED for subtitle.\n");
708 return 0;
709 }
710
711 group = new_rendition_group(c, type, info->group_id);
712 if (!group)
713 return AVERROR(ENOMEM);
714
715 rend = av_mallocz(sizeof(struct rendition));
716 if (!rend)
717 return AVERROR(ENOMEM);
718
719 ret = av_dynarray_add_nofree(&c->renditions, &c->n_renditions, rend);
720 if (ret < 0) {
721 av_free(rend);
722 return ret;
723 }
724
725 rend->type = type;
726 strcpy(rend->language, info->language);
727 strcpy(rend->name, info->name);
728
729 /* add the playlist if this is an external rendition */
730 if (info->uri[0]) {
731 rend->playlist = new_playlist(c, info->uri, url_base);
732 if (rend->playlist) {
734 rend->playlist->is_subtitle = 1;
735 rend->playlist->is_id3_timestamped = 0;
736 }
738 &rend->playlist->n_renditions, rend);
739 if (ret < 0)
740 return ret;
741 }
742 }
743 if (rend->playlist)
744 ret = av_dynarray_add_nofree(&group->playlists, &group->n_playlists, rend->playlist);
745 else
746 ret = av_dynarray_add_nofree(&group->renditions, &group->n_renditions, rend);
747 if (ret < 0)
748 return ret;
749
750 if (info->assoc_language[0]) {
751 size_t langlen = strlen(rend->language);
752 if (langlen < sizeof(rend->language) - 3) {
753 size_t assoc_len;
754 rend->language[langlen] = ',';
755 assoc_len = av_strlcpy(rend->language + langlen + 1,
756 info->assoc_language,
757 sizeof(rend->language) - langlen - 1);
758 if (langlen + assoc_len + 2 > sizeof(rend->language)) // truncation occurred
759 av_log(c->ctx, AV_LOG_WARNING, "Truncated rendition language: %s\n",
760 info->assoc_language);
761 }
762 }
763
764 if (!strcmp(info->defaultr, "YES"))
766 if (!strcmp(info->forced, "YES"))
768
769 chr_ptr = info->characteristics;
770 while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) {
771 if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound"))
773 else if (!strcmp(characteristic, "public.accessibility.describes-video"))
775
776 chr_ptr = NULL;
777 }
778
779 return 0;
780}
781
782static void handle_rendition_args(void *vinfo, const char *key,
783 int key_len, char **dest, int *dest_len)
784{
785 struct rendition_info *info = vinfo;
786
787 if (!strncmp(key, "TYPE=", key_len)) {
788 *dest = info->type;
789 *dest_len = sizeof(info->type);
790 } else if (!strncmp(key, "URI=", key_len)) {
791 *dest = info->uri;
792 *dest_len = sizeof(info->uri);
793 } else if (!strncmp(key, "GROUP-ID=", key_len)) {
794 *dest = info->group_id;
795 *dest_len = sizeof(info->group_id);
796 } else if (!strncmp(key, "LANGUAGE=", key_len)) {
797 *dest = info->language;
798 *dest_len = sizeof(info->language);
799 } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) {
800 *dest = info->assoc_language;
801 *dest_len = sizeof(info->assoc_language);
802 } else if (!strncmp(key, "NAME=", key_len)) {
803 *dest = info->name;
804 *dest_len = sizeof(info->name);
805 } else if (!strncmp(key, "DEFAULT=", key_len)) {
806 *dest = info->defaultr;
807 *dest_len = sizeof(info->defaultr);
808 } else if (!strncmp(key, "FORCED=", key_len)) {
809 *dest = info->forced;
810 *dest_len = sizeof(info->forced);
811 } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) {
812 *dest = info->characteristics;
813 *dest_len = sizeof(info->characteristics);
814 }
815 /*
816 * ignored:
817 * - AUTOSELECT: client may autoselect based on e.g. system language
818 * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4")
819 */
820}
821
822/* used by parse_playlist to allocate a new variant+playlist when the
823 * playlist is detected to be a Media Playlist (not Master Playlist)
824 * and we have no parent Master Playlist (parsing of which would have
825 * allocated the variant and playlist already)
826 * *pls == NULL => Master Playlist or parentless Media Playlist
827 * *pls != NULL => parented Media Playlist, playlist+variant allocated */
828static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
829{
830 if (*pls)
831 return 0;
832 struct variant *var = new_variant(c, NULL, url, NULL);
833 if (!var)
834 return AVERROR(ENOMEM);
835 *pls = var->playlists[0];
836 return 0;
837}
838
840{
841#if !CONFIG_HTTP_PROTOCOL
842 return 0;
843#else
845 return uc && !strncmp(uc->prot->name, "http", 4);
846#endif
847}
848
850 const char *url, AVDictionary **options)
851{
852#if !CONFIG_HTTP_PROTOCOL
854#else
855 int ret;
857 av_assert0(uc);
858 (*pb)->eof_reached = 0;
860 if (ret < 0) {
862 }
863 return ret;
864#endif
865}
866
867static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url,
868 AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
869{
870 HLSContext *c = s->priv_data;
872 const char *proto_name = NULL;
873 int ret;
874 int is_http = 0;
875
876 if (av_strstart(url, "crypto", NULL)) {
877 if (url[6] == '+' || url[6] == ':')
878 proto_name = avio_find_protocol_name(url + 7);
879 } else if (av_strstart(url, "data", NULL)) {
880 if (url[4] == '+' || url[4] == ':')
881 proto_name = avio_find_protocol_name(url + 5);
882 }
883
884 if (!proto_name)
885 proto_name = avio_find_protocol_name(url);
886
887 if (!proto_name)
888 return AVERROR_INVALIDDATA;
889
890 // only http(s) & file are allowed
891 if (av_strstart(proto_name, "file", NULL)) {
892 if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) {
894 "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n"
895 "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n",
896 url);
897 return AVERROR_INVALIDDATA;
898 }
899 } else if (av_strstart(proto_name, "http", NULL)) {
900 is_http = 1;
901 } else if (av_strstart(proto_name, "data", NULL)) {
902 ;
903 } else
904 return AVERROR_INVALIDDATA;
905
906 if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':')
907 ;
908 else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':')
909 ;
910 else if (av_strstart(url, "data", NULL) && !strncmp(proto_name, url + 5, strlen(proto_name)) && url[5 + strlen(proto_name)] == ':')
911 ;
912 else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5))
913 return AVERROR_INVALIDDATA;
914
915 av_dict_copy(&tmp, *opts, 0);
916 av_dict_copy(&tmp, opts2, 0);
917
918 if (is_http && c->http_persistent && is_native_http(*pb)) {
919 ret = open_url_keepalive(c->ctx, pb, url, &tmp);
920 if (ret == AVERROR_EXIT) {
922 return ret;
923 } else if (ret < 0) {
924 if (ret != AVERROR_EOF)
926 "keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n",
927 url, av_err2str(ret));
928 av_dict_copy(&tmp, *opts, 0);
929 av_dict_copy(&tmp, opts2, 0);
930 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
931 }
932 } else {
933 av_assert0(!*pb);
934 ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp);
935 }
936 if (ret >= 0) {
937 // update cookies on http response with setcookies.
938 char *new_cookies = NULL;
939
940 if (!(s->flags & AVFMT_FLAG_CUSTOM_IO))
941 av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies);
942
943 if (new_cookies)
944 av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL);
945 }
946
948
949 if (is_http_out)
950 *is_http_out = is_http;
951
952 return ret;
953}
954
955static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
956{
957 HLSContext *c = s->priv_data;
958 int matchA = 3;
959 int matchF = 0;
960
961 if (!c->extension_picky)
962 return 0;
963
964 if (strcmp(c->allowed_segment_extensions, "ALL"))
965 matchA = av_match_ext (seg->url, c->allowed_segment_extensions)
966 + 2*(ff_match_url_ext(seg->url, c->allowed_segment_extensions) > 0);
967
968 if (!matchA) {
969 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);
970 return AVERROR_INVALIDDATA;
971 }
972
973 if (in_fmt) {
974 if (in_fmt->extensions) {
975 matchF = av_match_ext( seg->url, in_fmt->extensions)
976 + 2*(ff_match_url_ext(seg->url, in_fmt->extensions) > 0);
977 // Youtube uses aac files with .ts extension
978 if(av_match_name("mp4", in_fmt->name) || av_match_name("aac", in_fmt->name)) {
979 matchF |= av_match_ext( seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa")
980 + 2*(ff_match_url_ext(seg->url, "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts,cmfv,cmfa") > 0);
981 }
982 } else if (!strcmp(in_fmt->name, "mpegts")) {
983 const char *str = "ts,m2t,m2ts,mts,mpg,m4s,mpeg,mpegts"
984 ",html" // https://flash1.bogulus.cfd/
985 ;
986 matchF = av_match_ext( seg->url, str)
987 + 2*(ff_match_url_ext(seg->url, str) > 0);
988 } else if (!strcmp(in_fmt->name, "webvtt")) {
989 matchF = av_match_ext( seg->url, "vtt,webvtt")
990 + 2*(ff_match_url_ext(seg->url, "vtt,webvtt") > 0);
991 }
992
993 if (!(matchA & matchF)) {
994 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);
995 return AVERROR_INVALIDDATA;
996 }
997 }
998
999 return 0;
1000}
1001
1002static int parse_playlist(HLSContext *c, const char *url,
1003 struct playlist *pls, AVIOContext *in)
1004{
1005 int ret = 0, is_segment = 0, is_variant = 0;
1006 int64_t duration = 0;
1007 uint64_t discontinuity_seq = 0;
1008 enum KeyType key_type = KEY_NONE;
1009 uint8_t iv[16] = "";
1010 int has_iv = 0;
1011 char key[MAX_URL_SIZE] = "";
1012 char line[MAX_URL_SIZE];
1013 const char *ptr;
1014 int close_in = 0;
1015 int64_t seg_offset = 0;
1016 int64_t seg_size = -1;
1017 uint8_t *new_url = NULL;
1019 char tmp_str[MAX_URL_SIZE];
1020 struct segment *cur_init_section = NULL;
1021 int is_http = av_strstart(url, "http", NULL);
1022 struct segment **prev_segments = NULL;
1023 int prev_n_segments = 0;
1024 int64_t prev_start_seq_no = -1;
1025 int64_t load_start = av_gettime_relative();
1026 int64_t start_time_offset = 0;
1027 int time_offset_flag = 0;
1028 int finished = 0;
1029
1030 if (is_http && !in && c->http_persistent && c->playlist_pb) {
1031 in = c->playlist_pb;
1032 ret = open_url_keepalive(c->ctx, &c->playlist_pb, url, NULL);
1033 if (ret == AVERROR_EXIT) {
1034 return ret;
1035 } else if (ret < 0) {
1036 if (ret != AVERROR_EOF)
1037 av_log(c->ctx, AV_LOG_WARNING,
1038 "keepalive request failed for '%s' with error: '%s' when parsing playlist\n",
1039 url, av_err2str(ret));
1040 in = NULL;
1041 }
1042 }
1043
1044 if (!in) {
1046 av_dict_copy(&opts, c->avio_opts, 0);
1047
1048 if (c->http_persistent)
1049 av_dict_set(&opts, "multiple_requests", "1", 0);
1050
1051 ret = open_url(c->ctx, &in, url, &opts, NULL, NULL);
1053 if (ret < 0)
1054 return ret;
1055
1056 if (is_http && c->http_persistent && is_native_http(in))
1057 c->playlist_pb = in;
1058 else
1059 close_in = 1;
1060 }
1061
1062 if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0)
1063 url = new_url;
1064
1065 ff_get_chomp_line(in, line, sizeof(line));
1066 if (strcmp(line, "#EXTM3U")) {
1068 goto fail;
1069 }
1070
1071 if (pls) {
1072 prev_start_seq_no = pls->start_seq_no;
1073 prev_segments = pls->segments;
1074 prev_n_segments = pls->n_segments;
1075 pls->segments = NULL;
1076 pls->n_segments = 0;
1077
1078 pls->finished = 0;
1080 }
1081 while (!avio_feof(in)) {
1082 ff_get_chomp_line(in, line, sizeof(line));
1083 if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {
1084 if (pls) {
1085 av_log(c->ctx, AV_LOG_ERROR,
1086 "Master Playlist tag found in a Media Playlist\n");
1088 goto fail;
1089 }
1090 is_variant = 1;
1091 memset(&variant_info, 0, sizeof(variant_info));
1093 } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {
1094 struct key_info info = {{0}};
1096 key_type = KEY_NONE;
1097 has_iv = 0;
1098 if (!strcmp(info.method, "AES-128"))
1099 key_type = KEY_AES_128;
1100 if (!strcmp(info.method, "SAMPLE-AES"))
1101 key_type = KEY_SAMPLE_AES;
1102 if (!av_strncasecmp(info.iv, "0x", 2)) {
1103 ff_hex_to_data(iv, info.iv + 2);
1104 has_iv = 1;
1105 }
1106 av_strlcpy(key, info.uri, sizeof(key));
1107 } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) {
1108 struct rendition_info info = {{0}};
1109 if (pls) {
1110 av_log(c->ctx, AV_LOG_ERROR,
1111 "Master Playlist tag found in a Media Playlist\n");
1113 goto fail;
1114 }
1116 ret = new_rendition(c, &info, url);
1117 if (ret < 0)
1118 goto fail;
1119 } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {
1120 int64_t t;
1121 ret = ensure_playlist(c, &pls, url);
1122 if (ret < 0)
1123 goto fail;
1124 t = strtoll(ptr, NULL, 10);
1125 if (t < 0 || t >= INT64_MAX / AV_TIME_BASE) {
1127 goto fail;
1128 }
1129 pls->target_duration = t * AV_TIME_BASE;
1130 } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
1131 uint64_t seq_no;
1132 ret = ensure_playlist(c, &pls, url);
1133 if (ret < 0)
1134 goto fail;
1135 seq_no = strtoull(ptr, NULL, 10);
1136 if (seq_no > INT64_MAX/2) {
1137 av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than "
1138 "INT64_MAX/2, mask out the highest bit\n");
1139 seq_no &= INT64_MAX/2;
1140 }
1141 pls->start_seq_no = seq_no;
1142 } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) {
1143 ret = ensure_playlist(c, &pls, url);
1144 if (ret < 0)
1145 goto fail;
1146 if (!strcmp(ptr, "EVENT"))
1147 pls->type = PLS_TYPE_EVENT;
1148 else if (!strcmp(ptr, "VOD"))
1149 pls->type = PLS_TYPE_VOD;
1150 } else if (av_strstart(line, "#EXT-X-MAP:", &ptr)) {
1151 struct init_section_info info = {{0}};
1152 ret = ensure_playlist(c, &pls, url);
1153 if (ret < 0)
1154 goto fail;
1156 cur_init_section = new_init_section(pls, &info, url);
1157 if (!cur_init_section) {
1158 ret = AVERROR(ENOMEM);
1159 goto fail;
1160 }
1161 cur_init_section->key_type = key_type;
1162 if (has_iv) {
1163 memcpy(cur_init_section->iv, iv, sizeof(iv));
1164 } else {
1165 int64_t seq = pls->start_seq_no + pls->n_segments;
1166 memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv));
1167 AV_WB64(cur_init_section->iv + 8, seq);
1168 }
1169
1170 if (key_type != KEY_NONE) {
1171 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
1172 if (!tmp_str[0]) {
1173 av_free(cur_init_section);
1175 goto fail;
1176 }
1177 cur_init_section->key = av_strdup(tmp_str);
1178 if (!cur_init_section->key) {
1179 av_free(cur_init_section);
1180 ret = AVERROR(ENOMEM);
1181 goto fail;
1182 }
1183 } else {
1184 cur_init_section->key = NULL;
1185 }
1186
1187 } else if (av_strstart(line, "#EXT-X-START:", &ptr)) {
1188 struct start_info info = {0};
1190 if (info.time_offset[0]) {
1191 double offset = strtod(info.time_offset, NULL) * AV_TIME_BASE;
1192 if (offset >= -0x1p63 && offset < 0x1p63) {
1193 start_time_offset = offset;
1194 time_offset_flag = 1;
1195 } else {
1196 av_log(c->ctx, AV_LOG_WARNING, "TIME-OFFSET value is "
1197 "invalid, it will be ignored\n");
1198 }
1199 } else {
1200 av_log(c->ctx, AV_LOG_WARNING, "#EXT-X-START value is "
1201 "invalid, it will be ignored\n");
1202 continue;
1203 }
1204 } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {
1205 finished = 1;
1206 } else if (av_strstart(line, "#EXT-X-DISCONTINUITY-SEQUENCE:", &ptr)) {
1207 discontinuity_seq = strtoull(ptr, NULL, 10);
1208 } else if (!strcmp(line, "#EXT-X-DISCONTINUITY")) {
1209 discontinuity_seq++;
1210 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
1211 double d = atof(ptr) * AV_TIME_BASE;
1212 if (d < 0 || d > INT64_MAX || isnan(d)) {
1213 av_log(c->ctx, AV_LOG_WARNING, "EXTINF %f unsupported\n", d / AV_TIME_BASE);
1214 d = 0;
1215 }
1216 duration = d;
1217 is_segment = 1;
1218 } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) {
1219 seg_size = strtoll(ptr, NULL, 10);
1220 ptr = strchr(ptr, '@');
1221 if (ptr)
1222 seg_offset = strtoll(ptr+1, NULL, 10);
1223 if (seg_size < 0 || seg_offset > INT64_MAX - seg_size) {
1225 goto fail;
1226 }
1227 } else if (av_strstart(line, "#", NULL)) {
1228 av_log(c->ctx, AV_LOG_VERBOSE, "Skip ('%s')\n", line);
1229 continue;
1230 } else if (line[0]) {
1231 if (is_variant) {
1232 if (!new_variant(c, &variant_info, line, url)) {
1233 ret = AVERROR(ENOMEM);
1234 goto fail;
1235 }
1236 is_variant = 0;
1237 }
1238 if (is_segment) {
1239 struct segment *seg;
1240 ret = ensure_playlist(c, &pls, url);
1241 if (ret < 0)
1242 goto fail;
1243 seg = av_malloc(sizeof(struct segment));
1244 if (!seg) {
1245 ret = AVERROR(ENOMEM);
1246 goto fail;
1247 }
1248 if (has_iv) {
1249 memcpy(seg->iv, iv, sizeof(iv));
1250 } else {
1251 uint64_t seq = pls->start_seq_no + (uint64_t)pls->n_segments;
1252 memset(seg->iv, 0, sizeof(seg->iv));
1253 AV_WB64(seg->iv + 8, seq);
1254 }
1255
1256 if (key_type != KEY_NONE) {
1257 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key);
1258 if (!tmp_str[0]) {
1260 av_free(seg);
1261 goto fail;
1262 }
1263 seg->key = av_strdup(tmp_str);
1264 if (!seg->key) {
1265 av_free(seg);
1266 ret = AVERROR(ENOMEM);
1267 goto fail;
1268 }
1269 } else {
1270 seg->key = NULL;
1271 }
1272
1273 ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line);
1274 if (!tmp_str[0]) {
1276 if (seg->key)
1277 av_free(seg->key);
1278 av_free(seg);
1279 goto fail;
1280 }
1281 seg->url = av_strdup(tmp_str);
1282 if (!seg->url) {
1283 av_free(seg->key);
1284 av_free(seg);
1285 ret = AVERROR(ENOMEM);
1286 goto fail;
1287 }
1288
1289 ret = test_segment(c->ctx, pls->ctx ? pls->ctx->iformat : NULL, pls, seg);
1290 if (ret < 0) {
1291 av_free(seg->url);
1292 av_free(seg->key);
1293 av_free(seg);
1294 goto fail;
1295 }
1296
1297 if (duration < 0.001 * AV_TIME_BASE) {
1298 av_log(c->ctx, AV_LOG_WARNING, "Cannot get correct #EXTINF value of segment %s,"
1299 " set to default value to 1ms.\n", seg->url);
1300 duration = 0.001 * AV_TIME_BASE;
1301 }
1302 seg->duration = duration;
1303 seg->key_type = key_type;
1305 dynarray_add(&pls->segments, &pls->n_segments, seg);
1306 is_segment = 0;
1307
1308 seg->size = seg_size;
1309 if (seg_size >= 0) {
1310 seg->url_offset = seg_offset;
1311 seg_offset += seg_size;
1312 seg_size = -1;
1313 } else {
1314 seg->url_offset = 0;
1315 seg_offset = 0;
1316 }
1317
1318 seg->init_section = cur_init_section;
1319 }
1320 }
1321 }
1322 if (prev_segments) {
1323 if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE &&
1324 c->first_timestamp_pls == pls) {
1325 int64_t prev_timestamp = c->first_timestamp;
1326 int i;
1327 int64_t diff = pls->start_seq_no - prev_start_seq_no;
1328 for (i = 0; i < prev_n_segments && i < diff; i++) {
1329 c->first_timestamp += prev_segments[i]->duration;
1330 }
1331 av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")"
1332 " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n",
1333 prev_start_seq_no, pls->start_seq_no,
1334 prev_timestamp, c->first_timestamp);
1335 } else if (pls->start_seq_no < prev_start_seq_no) {
1336 av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n",
1337 prev_start_seq_no, pls->start_seq_no);
1338 }
1339 }
1340
1341 /* Media Playlist tags may precede the first tag that allocates the
1342 * playlist. */
1343 if (pls && finished)
1344 pls->finished = 1;
1345
1346 /* EXT-X-START is valid in both playlist types. A value from the Master
1347 * Playlist applies to all its Media Playlists, which then ignore their
1348 * own (RFC 8216 4.3.5). */
1349 if (time_offset_flag && !c->master_time_offset_flag) {
1350 if (pls) {
1351 pls->start_time_offset = start_time_offset;
1352 pls->time_offset_flag = 1;
1353 } else {
1354 c->master_time_offset_flag = 1;
1355 for (int i = 0; i < c->n_playlists; i++) {
1356 c->playlists[i]->start_time_offset = start_time_offset;
1357 c->playlists[i]->time_offset_flag = 1;
1358 }
1359 }
1360 }
1361
1362fail:
1363 free_segment_dynarray(prev_segments, prev_n_segments);
1364 av_freep(&prev_segments);
1365 if (pls)
1366 pls->last_load_time = load_start;
1367 av_free(new_url);
1368 if (close_in)
1369 ff_format_io_close(c->ctx, &in);
1370 c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE;
1371 if (!c->n_variants || !c->variants[0]->n_playlists ||
1372 !c->variants[0]->playlists[0]->n_segments)
1373 c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE;
1374
1375 if (c->n_variants && c->variants[0]->n_playlists &&
1376 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT &&
1377 !c->variants[0]->playlists[0]->finished) {
1378 struct playlist *p = c->variants[0]->playlists[0];
1379 int64_t duration = 0;
1380 for (int i = 0; i < p->n_segments; i++)
1381 duration += p->segments[i]->duration;
1382 c->ctx->duration = duration;
1383 }
1384
1385 return ret;
1386}
1387
1388static struct segment *current_segment(struct playlist *pls)
1389{
1390 int64_t n = pls->cur_seq_no - pls->start_seq_no;
1391 if (n >= pls->n_segments)
1392 return NULL;
1393 return pls->segments[n];
1394}
1395
1396static struct segment *next_segment(struct playlist *pls)
1397{
1398 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1399 if (n >= pls->n_segments)
1400 return NULL;
1401 return pls->segments[n];
1402}
1403
1404/* True if 'next' is an unencrypted byte range that directly follows 'cur' in
1405 * the same resource and ends within the range requested up to 'end', so that
1406 * it is read on from the input of 'cur' (i.e. consecutive EXT-X-BYTERANGE
1407 * segments). */
1408static int segment_continues(const struct segment *cur,
1409 const struct segment *next, int64_t end)
1410{
1411 return cur && next &&
1412 cur->size >= 0 && next->size >= 0 &&
1413 next->url_offset == cur->url_offset + cur->size &&
1414 next->url_offset + next->size <= end &&
1415 cur->key_type == KEY_NONE && next->key_type == KEY_NONE &&
1416 next->init_section == cur->init_section &&
1417 !strcmp(next->url, cur->url);
1418}
1419
1420static int64_t range_end(struct playlist *pls, const struct segment *seg)
1421{
1422 int64_t n = pls->cur_seq_no - pls->start_seq_no + 1;
1423
1424 if (seg == next_segment(pls))
1425 n++;
1426 else if (seg != current_segment(pls))
1427 return seg->url_offset + seg->size;
1428 for (; n < pls->n_segments &&
1429 segment_continues(seg, pls->segments[n], INT64_MAX); n++)
1430 seg = pls->segments[n];
1431 return seg->url_offset + seg->size;
1432}
1433
1434static int read_from_url(struct playlist *pls, struct segment *seg,
1435 uint8_t *buf, int buf_size)
1436{
1437 int ret;
1438
1439 /* limit read if the segment was only a part of a file */
1440 if (seg->size >= 0)
1441 buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset);
1442
1443 ret = avio_read(pls->input, buf, buf_size);
1444 if (ret > 0)
1445 pls->cur_seg_offset += ret;
1446
1447 return ret;
1448}
1449
1450/* Parse the raw ID3 data and pass contents to caller */
1452 AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info,
1453 ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta)
1454{
1455 static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp";
1456 static const char id3_priv_owner_audio_setup[] = "com.apple.streaming.audioDescription";
1457 ID3v2ExtraMeta *meta;
1458
1460 for (meta = *extra_meta; meta; meta = meta->next) {
1461 if (!strcmp(meta->tag, "PRIV")) {
1462 ID3v2ExtraMetaPRIV *priv = &meta->data.priv;
1463 if (priv->datasize == 8 && !av_strncasecmp(priv->owner, id3_priv_owner_ts, 44)) {
1464 /* 33-bit MPEG timestamp */
1465 int64_t ts = AV_RB64(priv->data);
1466 av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts);
1467 if ((ts & ~((1ULL << 33) - 1)) == 0)
1468 *dts = ts;
1469 else
1470 av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts);
1471 } else if (priv->datasize >= 8 && !av_strncasecmp(priv->owner, id3_priv_owner_audio_setup, 36)) {
1472 ff_hls_senc_read_audio_setup_info(audio_setup_info, priv->data, priv->datasize);
1473 }
1474 } else if (!strcmp(meta->tag, "APIC") && apic)
1475 *apic = &meta->data.apic;
1476 }
1477}
1478
1479/* Check if the ID3 metadata contents have changed */
1481 ID3v2ExtraMetaAPIC *apic)
1482{
1483 const AVDictionaryEntry *entry = NULL;
1484 const AVDictionaryEntry *oldentry;
1485 /* check that no keys have changed values */
1486 while ((entry = av_dict_iterate(metadata, entry))) {
1487 oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE);
1488 if (!oldentry || strcmp(oldentry->value, entry->value) != 0)
1489 return 1;
1490 }
1491
1492 /* check if apic appeared */
1493 if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data))
1494 return 1;
1495
1496 if (apic) {
1497 int size = pls->ctx->streams[1]->attached_pic.size;
1498 if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE)
1499 return 1;
1500
1501 if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0)
1502 return 1;
1503 }
1504
1505 return 0;
1506}
1507
1508/* Parse ID3 data and handle the found data */
1509static void handle_id3(AVIOContext *pb, struct playlist *pls)
1510{
1512 ID3v2ExtraMetaAPIC *apic = NULL;
1513 ID3v2ExtraMeta *extra_meta = NULL;
1514 int64_t timestamp = AV_NOPTS_VALUE;
1515
1516 parse_id3(pls->ctx, pb, &metadata, &timestamp, &pls->audio_setup_info, &apic, &extra_meta);
1517
1518 if (timestamp != AV_NOPTS_VALUE) {
1519 pls->id3_mpegts_timestamp = timestamp;
1520 pls->id3_offset = 0;
1521 }
1522
1523 if (!pls->id3_found) {
1524 /* initial ID3 tags */
1526 pls->id3_found = 1;
1527
1528 /* get picture attachment and set text metadata */
1529 if (pls->ctx->nb_streams) {
1531 ff_id3v2_parse_apic(pls->ctx, extra_meta);
1532 } else
1533 /* demuxer not yet opened, defer picture attachment */
1534 pls->id3_deferred_extra = extra_meta;
1535
1536 ff_id3v2_parse_priv_dict(&metadata, extra_meta);
1537 av_dict_copy(&pls->ctx->metadata, metadata, 0);
1538 pls->id3_initial = metadata;
1539
1540 } else {
1541 if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) {
1542 avpriv_report_missing_feature(pls->parent, "Changing ID3 metadata in HLS audio elementary stream");
1543 pls->id3_changed = 1;
1544 }
1546 }
1547
1548 if (pls->id3_deferred_extra != extra_meta)
1549 ff_id3v2_free_extra_meta(&extra_meta);
1550}
1551
1552static void intercept_id3(struct playlist *pls, uint8_t *buf,
1553 int buf_size, int *len)
1554{
1555 /* intercept id3 tags, we do not want to pass them to the raw
1556 * demuxer on all segment switches */
1557 int bytes;
1558 int id3_buf_pos = 0;
1559 int fill_buf = 0;
1560 struct segment *seg = current_segment(pls);
1561
1562 /* gather all the id3 tags */
1563 while (1) {
1564 /* see if we can retrieve enough data for ID3 header */
1566 bytes = read_from_url(pls, seg, buf + *len, ID3v2_HEADER_SIZE - *len);
1567 if (bytes > 0) {
1568
1569 if (bytes == ID3v2_HEADER_SIZE - *len)
1570 /* no EOF yet, so fill the caller buffer again after
1571 * we have stripped the ID3 tags */
1572 fill_buf = 1;
1573
1574 *len += bytes;
1575
1576 } else if (*len <= 0) {
1577 /* error/EOF */
1578 *len = bytes;
1579 fill_buf = 0;
1580 }
1581 }
1582
1583 if (*len < ID3v2_HEADER_SIZE)
1584 break;
1585
1587 int64_t maxsize = seg->size >= 0 ? seg->size : 1024*1024;
1588 int taglen = ff_id3v2_tag_len(buf);
1589 int tag_got_bytes = FFMIN(taglen, *len);
1590 int remaining = taglen - tag_got_bytes;
1591
1592 if (taglen > maxsize) {
1593 av_log(pls->parent, AV_LOG_ERROR, "Too large HLS ID3 tag (%d > %"PRId64" bytes)\n",
1594 taglen, maxsize);
1595 break;
1596 }
1597
1598 /*
1599 * Copy the id3 tag to our temporary id3 buffer.
1600 * We could read a small id3 tag directly without memcpy, but
1601 * we would still need to copy the large tags, and handling
1602 * both of those cases together with the possibility for multiple
1603 * tags would make the handling a bit complex.
1604 */
1605 pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen);
1606 if (!pls->id3_buf)
1607 break;
1608 memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes);
1609 id3_buf_pos += tag_got_bytes;
1610
1611 /* strip the intercepted bytes */
1612 *len -= tag_got_bytes;
1613 memmove(buf, buf + tag_got_bytes, *len);
1614 av_log(pls->parent, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes);
1615
1616 if (remaining > 0) {
1617 /* read the rest of the tag in */
1618 if (read_from_url(pls, seg, pls->id3_buf + id3_buf_pos, remaining) != remaining)
1619 break;
1620 id3_buf_pos += remaining;
1621 av_log(pls->parent, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining);
1622 }
1623
1624 } else {
1625 /* no more ID3 tags */
1626 break;
1627 }
1628 }
1629
1630 /* re-fill buffer for the caller unless EOF */
1631 if (*len >= 0 && (fill_buf || *len == 0)) {
1632 bytes = read_from_url(pls, seg, buf + *len, buf_size - *len);
1633
1634 /* ignore error if we already had some data */
1635 if (bytes >= 0)
1636 *len += bytes;
1637 else if (*len == 0)
1638 *len = bytes;
1639 }
1640
1641 if (pls->id3_buf) {
1642 /* Now parse all the ID3 tags */
1643 FFIOContext id3ioctx;
1644 ffio_init_read_context(&id3ioctx, pls->id3_buf, id3_buf_pos);
1645 handle_id3(&id3ioctx.pub, pls);
1646 }
1647
1648 if (pls->is_id3_timestamped == -1)
1650}
1651
1652static int read_key(HLSContext *c, struct playlist *pls, struct segment *seg)
1653{
1654 AVIOContext *pb = NULL;
1655
1656 int ret = open_url(pls->parent, &pb, seg->key, &c->avio_opts, NULL, NULL);
1657 if (ret < 0) {
1658 av_log(pls->parent, AV_LOG_ERROR, "Unable to open key file %s, %s\n",
1659 seg->key, av_err2str(ret));
1660 return ret;
1661 }
1662
1663 ret = avio_read(pb, pls->key, sizeof(pls->key));
1664 ff_format_io_close(pls->parent, &pb);
1665 if (ret != sizeof(pls->key)) {
1666 if (ret < 0) {
1667 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, %s\n",
1668 seg->key, av_err2str(ret));
1669 } else {
1670 av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s, read bytes %d != %zu\n",
1671 seg->key, ret, sizeof(pls->key));
1673 }
1674
1675 return ret;
1676 }
1677
1678 av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url));
1679
1680 return 0;
1681}
1682
1683static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg,
1684 AVIOContext **in, int64_t *end)
1685{
1687 int ret;
1688 int is_http = 0;
1689
1690 if (c->http_persistent)
1691 av_dict_set(&opts, "multiple_requests", "1", 0);
1692
1693 *end = -1;
1694 if (seg->size >= 0) {
1695 /* Restrict the request to the wanted byte range, extended so that one
1696 * request serves the whole run of segments that continue it. */
1697 *end = range_end(pls, seg);
1698 av_dict_set_int(&opts, "offset", seg->url_offset, 0);
1699 av_dict_set_int(&opts, "end_offset", *end, 0);
1700 }
1701
1702 av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n",
1703 seg->url, seg->url_offset, pls->index);
1704
1705 if (seg->key_type == KEY_AES_128 || seg->key_type == KEY_SAMPLE_AES) {
1706 if (strcmp(seg->key, pls->key_url)) {
1707 ret = read_key(c, pls, seg);
1708 if (ret < 0)
1709 goto cleanup;
1710 }
1711 }
1712
1713 if (seg->key_type == KEY_AES_128) {
1714 char iv[33], key[33], url[MAX_URL_SIZE];
1715 ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0);
1716 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
1717 if (strstr(seg->url, "://"))
1718 snprintf(url, sizeof(url), "crypto+%s", seg->url);
1719 else
1720 snprintf(url, sizeof(url), "crypto:%s", seg->url);
1721
1722 av_dict_set(&opts, "key", key, 0);
1723 av_dict_set(&opts, "iv", iv, 0);
1724
1725 ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http);
1726 if (ret < 0) {
1727 goto cleanup;
1728 }
1729 ret = 0;
1730 } else {
1731 ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http);
1732 }
1733
1734 /* Seek to the requested position. If this was a HTTP request, the offset
1735 * should already be where want it to, but this allows e.g. local testing
1736 * without a HTTP server.
1737 *
1738 * This is not done for HTTP at all as avio_seek() does internal bookkeeping
1739 * of file offset which is out-of-sync with the actual offset when "offset"
1740 * AVOption is used with http protocol, causing the seek to not be a no-op
1741 * as would be expected. Wrong offset received from the server will not be
1742 * noticed without the call, though.
1743 */
1744 if (ret == 0 && !is_http && seg->url_offset) {
1745 int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET);
1746 if (seekret < 0) {
1747 av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url);
1748 ret = seekret;
1749 ff_format_io_close(pls->parent, in);
1750 }
1751 }
1752
1753cleanup:
1755 pls->cur_seg_offset = 0;
1756 return ret;
1757}
1758
1759static int update_init_section(struct playlist *pls, struct segment *seg)
1760{
1761 static const int max_init_section_size = 1024*1024;
1762 HLSContext *c = pls->parent->priv_data;
1763 int64_t sec_size;
1764 int64_t urlsize;
1765 int ret;
1766
1767 if (seg->init_section == pls->cur_init_section)
1768 return 0;
1769
1770 pls->cur_init_section = NULL;
1771
1772 if (!seg->init_section)
1773 return 0;
1774
1775 ret = open_input(c, pls, seg->init_section, &pls->input, &pls->input_end);
1776 if (ret < 0) {
1778 "Failed to open an initialization section in playlist %d\n",
1779 pls->index);
1780 return ret;
1781 }
1782
1783 if (seg->init_section->size >= 0)
1784 sec_size = seg->init_section->size;
1785 else if ((urlsize = avio_size(pls->input)) >= 0)
1786 sec_size = urlsize;
1787 else
1788 sec_size = max_init_section_size;
1789
1791 "Downloading an initialization section of size %"PRId64"\n",
1792 sec_size);
1793
1794 sec_size = FFMIN(sec_size, max_init_section_size);
1795
1796 av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size);
1797
1798 ret = read_from_url(pls, seg->init_section, pls->init_sec_buf,
1799 pls->init_sec_buf_size);
1800 ff_format_io_close(pls->parent, &pls->input);
1801
1802 if (ret < 0)
1803 return ret;
1804
1805 pls->cur_init_section = seg->init_section;
1806 pls->init_sec_data_len = ret;
1807 pls->init_sec_buf_read_offset = 0;
1808
1809 /* spec says audio elementary streams do not have media initialization
1810 * sections, so there should be no ID3 timestamps */
1811 pls->is_id3_timestamped = 0;
1812
1813 return 0;
1814}
1815
1817{
1818 return pls->n_segments > 0 ?
1819 pls->segments[pls->n_segments - 1]->duration :
1820 pls->target_duration;
1821}
1822
1823static int playlist_needed(struct playlist *pls)
1824{
1825 AVFormatContext *s = pls->parent;
1826 int i, j;
1827 int stream_needed = 0;
1828 int first_st;
1829
1830 /* If there is no context or streams yet, the playlist is needed */
1831 if ((!pls->ctx || !pls->n_main_streams) && !pls->is_subtitle)
1832 return 1;
1833
1834 /* check if any of the streams in the playlist are needed */
1835 for (i = 0; i < pls->n_main_streams; i++) {
1836 if (pls->main_streams[i]->discard < AVDISCARD_ALL) {
1837 stream_needed = 1;
1838 break;
1839 }
1840 }
1841
1842 /* If all streams in the playlist were discarded, the playlist is not
1843 * needed (regardless of whether whole programs are discarded or not). */
1844 if (!stream_needed)
1845 return 0;
1846
1847 /* Otherwise, check if all the programs (variants) this playlist is in are
1848 * discarded. Since all streams in the playlist are part of the same programs
1849 * we can just check the programs of the first stream. */
1850
1851 first_st = pls->main_streams[0]->index;
1852
1853 for (i = 0; i < s->nb_programs; i++) {
1854 AVProgram *program = s->programs[i];
1855 if (program->discard < AVDISCARD_ALL) {
1856 for (j = 0; j < program->nb_stream_indexes; j++) {
1857 if (program->stream_index[j] == first_st) {
1858 /* playlist is in an undiscarded program */
1859 return 1;
1860 }
1861 }
1862 }
1863 }
1864
1865 /* some streams were not discarded but all the programs were */
1866 return 0;
1867}
1868
1869static int reload_playlist(struct playlist *v, HLSContext *c)
1870{
1871 int ret = 0;
1872 int reload_count = 0;
1873
1874 v->needed = playlist_needed(v);
1875
1876 if (!v->needed)
1877 return AVERROR_EOF;
1878
1879 if (!v->input || (c->http_persistent && v->input_read_done)) {
1880 int64_t reload_interval;
1881
1882 /* Check that the playlist is still needed before opening a new
1883 * segment. */
1884 v->needed = playlist_needed(v);
1885
1886 if (!v->needed) {
1887 av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d ('%s')\n",
1888 v->index, v->url);
1889 return AVERROR_EOF;
1890 }
1891
1892 /* If this is a live stream and the reload interval has elapsed since
1893 * the last playlist reload, reload the playlists now. */
1894 reload_interval = default_reload_interval(v);
1895
1896reload:
1897 reload_count++;
1898 if (reload_count > c->max_reload)
1899 return AVERROR_EOF;
1900 if (!v->finished &&
1901 av_gettime_relative() - v->last_load_time >= reload_interval) {
1902 if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) {
1903 if (ret != AVERROR_EXIT)
1904 av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n",
1905 v->index);
1906 return ret;
1907 }
1908 /* If we need to reload the playlist again below (if
1909 * there's still no more segments), switch to a reload
1910 * interval of half the target duration. */
1911 reload_interval = v->target_duration / 2;
1912 }
1913 if (v->cur_seq_no < v->start_seq_no) {
1915 "skipping %"PRId64" segments ahead, expired from playlists\n",
1916 v->start_seq_no - v->cur_seq_no);
1917 v->cur_seq_no = v->start_seq_no;
1918 }
1919 if (v->cur_seq_no > v->last_seq_no) {
1920 v->last_seq_no = v->cur_seq_no;
1921 v->m3u8_hold_counters = 0;
1922 } else if (v->last_seq_no == v->cur_seq_no) {
1923 v->m3u8_hold_counters++;
1924 if (v->m3u8_hold_counters >= c->m3u8_hold_counters) {
1925 return AVERROR_EOF;
1926 }
1927 } else {
1928 av_log(v->parent, AV_LOG_WARNING, "The m3u8 list sequence may have been wrapped.\n");
1929 }
1930 if (v->cur_seq_no >= v->start_seq_no + v->n_segments) {
1931 if (v->finished || v->is_subtitle)
1932 return AVERROR_EOF;
1933 while (av_gettime_relative() - v->last_load_time < reload_interval) {
1934 if (ff_check_interrupt(c->interrupt_callback))
1935 return AVERROR_EXIT;
1936 av_usleep(100*1000);
1937 }
1938 /* Enough time has elapsed since the last reload */
1939 goto reload;
1940 }
1941
1942 }
1943 return ret;
1944}
1945
1946static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
1947{
1948 struct playlist *v = opaque;
1950 int ret;
1951 int just_opened = 0;
1952 int segment_retries = 0;
1953 struct segment *seg;
1954
1955 if (c->http_persistent && v->input_read_done) {
1956 av_assert0(v->input);
1957 ret = reload_playlist(v, c);
1958 if (ret < 0)
1959 return ret;
1960 }
1961
1962 v->input_read_done = 0;
1963
1964restart:
1965 ret = reload_playlist(v, c);
1966 if (ret < 0)
1967 return ret;
1968
1969 seg = current_segment(v);
1970
1971 if (!v->input || v->input_read_done) {
1972 /* load/update Media Initialization Section, if any */
1973 ret = update_init_section(v, seg);
1974 if (ret)
1975 return ret;
1976
1977 if (c->http_multiple == 1 && v->input_next_requested) {
1980 v->cur_seg_offset = 0;
1981 v->input_next_requested = 0;
1982 ret = 0;
1983 } else {
1984 ret = open_input(c, v, seg, &v->input, &v->input_end);
1985 }
1986 if (ret < 0) {
1987 if (ff_check_interrupt(c->interrupt_callback))
1988 return AVERROR_EXIT;
1989 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
1990 v->cur_seq_no,
1991 v->index);
1992 if (segment_retries >= c->seg_max_retry) {
1993 av_log(v->parent, AV_LOG_WARNING, "Segment %"PRId64" of playlist %d failed too many times, skipping\n",
1994 v->cur_seq_no,
1995 v->index);
1996 v->cur_seq_no++;
1997 segment_retries = 0;
1998 } else {
1999 segment_retries++;
2000 }
2001 goto restart;
2002 }
2003 segment_retries = 0;
2004 just_opened = 1;
2005 v->input_read_done = 0;
2006 if (v->first_read_seq_no < 0)
2008 }
2009
2010 if (c->http_multiple == -1) {
2011 uint8_t *http_version_opt = NULL;
2012 int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt);
2013 if (r >= 0) {
2014 c->http_multiple = (!strncmp((const char *)http_version_opt, "1.1", 3) || !strncmp((const char *)http_version_opt, "2.0", 3));
2015 av_freep(&http_version_opt);
2016 }
2017 }
2018
2019 seg = next_segment(v);
2020 if (c->http_multiple == 1 && !v->input_next_requested &&
2021 seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
2023 ret = open_input(c, v, seg, &v->input_next, &v->input_next_end);
2024 if (ret < 0) {
2025 if (ff_check_interrupt(c->interrupt_callback))
2026 return AVERROR_EXIT;
2027 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n",
2028 v->cur_seq_no + 1,
2029 v->index);
2030 } else {
2031 v->input_next_requested = 1;
2032 }
2033 }
2034
2036 /* Push init section out first before first actual segment */
2037 int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size);
2038 memcpy(buf, v->init_sec_buf + v->init_sec_buf_read_offset, copy_size);
2039 v->init_sec_buf_read_offset += copy_size;
2040 return copy_size;
2041 }
2042
2043 seg = current_segment(v);
2044 ret = read_from_url(v, seg, buf, buf_size);
2045 if (ret > 0) {
2046 if (just_opened && v->is_id3_timestamped != 0) {
2047 /* Intercept ID3 tags here, elementary audio streams are required
2048 * to convey timestamps using them in the beginning of each segment. */
2049 intercept_id3(v, buf, buf_size, &ret);
2050 }
2051
2052 return ret;
2053 }
2054 if (ret == 0 && segment_continues(seg, next_segment(v), v->input_end)) {
2055 /* The next segment follows within the requested range and is read
2056 * on from the same input. */
2057 v->cur_seq_no++;
2058 c->cur_seq_no = v->cur_seq_no;
2059 v->cur_seg_offset = 0;
2060 just_opened = 1;
2061 goto restart;
2062 }
2063 /* Keep the connection for the next request only if its reply was read
2064 * to the end. */
2065 if (c->http_persistent && is_native_http(v->input) &&
2066 seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL) &&
2067 (seg->size >= 0 ? ret == 0 && seg->url_offset + seg->size == v->input_end
2068 : ret == AVERROR_EOF)) {
2069 v->input_read_done = 1;
2070 } else {
2072 }
2073 v->cur_seq_no++;
2074
2075 c->cur_seq_no = v->cur_seq_no;
2076
2077 goto restart;
2078}
2079
2080static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
2081{
2082 struct playlist *v = opaque;
2084 int ret;
2085 struct segment *seg;
2086
2087 if (!v->needed || v->cur_seq_no - v->start_seq_no >= v->n_segments) {
2088 return AVERROR_EOF;
2089 } else {
2090 seg = current_segment(v);
2091 }
2092
2093 if (!v->input) {
2094 ret = open_input(c, v, seg, &v->input, &v->input_end);
2095 if (ret < 0) {
2096 if (ff_check_interrupt(c->interrupt_callback))
2097 return AVERROR_EXIT;
2098 av_log(v->parent, AV_LOG_WARNING, "Failed to open segment of playlist %d\n",
2099 v->index);
2100 return ret;
2101 }
2102 if (v->first_read_seq_no < 0)
2104 }
2105
2106 return read_from_url(v, seg, buf, buf_size);
2107}
2108
2109static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
2110 int flags, AVDictionary **opts)
2111{
2113 "A HLS playlist item '%s' referred to an external file '%s'. "
2114 "Opening this file was forbidden for security reasons\n",
2115 s->url, url);
2116 return AVERROR(EPERM);
2117}
2118
2119static int init_subtitle_context(struct playlist *pls)
2120{
2121 HLSContext *c = pls->parent->priv_data;
2122 const AVInputFormat *in_fmt;
2124 uint8_t *buf;
2125 int ret;
2126
2127 if (!(pls->ctx = avformat_alloc_context()))
2128 return AVERROR(ENOMEM);
2129
2131 if (!buf) {
2133 pls->ctx = NULL;
2134 return AVERROR(ENOMEM);
2135 }
2136
2137 av_freep(&pls->pb.pub.buffer);
2138 ffio_init_context(&pls->pb, buf, INITIAL_BUFFER_SIZE, 0, pls,
2140 pls->pb.pub.seekable = 0;
2141 pls->ctx->pb = &pls->pb.pub;
2142 pls->ctx->io_open = nested_io_open;
2143
2144 ret = ff_copy_whiteblacklists(pls->ctx, pls->parent);
2145 if (ret < 0) {
2147 pls->ctx = NULL;
2148 return ret;
2149 }
2150
2151 in_fmt = av_find_input_format("webvtt");
2152 av_dict_copy(&opts, c->seg_format_opts, 0);
2153 ret = avformat_open_input(&pls->ctx, current_segment(pls)->url, in_fmt, &opts);
2155
2156 return ret;
2157}
2158
2159static int cue_repeated(struct playlist *v, const AVPacket *pkt)
2160{
2161 /* both segments are in pts order, so the candidates start where the
2162 * previous lookup stopped */
2163 while (v->prev_cue_pos < v->n_prev_cues &&
2164 v->prev_cues[v->prev_cue_pos]->pts < pkt->pts)
2165 v->prev_cue_pos++;
2166
2167 for (int i = v->prev_cue_pos; i < v->n_prev_cues; i++) {
2168 const AVPacket *prev = v->prev_cues[i];
2169 const uint8_t *settings, *prev_settings;
2170 size_t size, prev_size;
2171
2172 if (prev->pts != pkt->pts)
2173 break;
2174 if (prev->duration != pkt->duration || prev->size != pkt->size ||
2175 memcmp(prev->data, pkt->data, pkt->size))
2176 continue;
2179 &prev_size);
2180 if (size == prev_size && (!size || !memcmp(settings, prev_settings, size)))
2181 return 1;
2182 }
2183 return 0;
2184}
2185
2186static int remember_cue(struct playlist *v, const AVPacket *pkt)
2187{
2189
2190 if (!cue)
2191 return AVERROR(ENOMEM);
2192 if (av_dynarray_add_nofree(&v->cur_cues, &v->n_cur_cues, cue) < 0) {
2193 av_packet_free(&cue);
2194 return AVERROR(ENOMEM);
2195 }
2196 return 0;
2197}
2198
2199static AVRational get_timebase(struct playlist *pls);
2200
2201/* The position of the media, the reference for the cue times: the last
2202 * returned packet, or a packet buffered from a media playlist before
2203 * anything was returned. AV_NOPTS_VALUE without media. */
2205{
2206 if (c->cur_timestamp != AV_NOPTS_VALUE)
2207 return c->cur_timestamp;
2208 for (int i = 0; i < c->n_playlists; i++) {
2209 struct playlist *pls = c->playlists[i];
2210
2211 if (!pls->is_subtitle && pls->needed && pls->pkt->data &&
2212 pls->pkt->dts != AV_NOPTS_VALUE)
2213 return av_rescale_q(pls->pkt->dts, get_timebase(pls), AV_TIME_BASE_Q);
2214 }
2215 return AV_NOPTS_VALUE;
2216}
2217
2218/* The MPEG-TS time of a cue is on the 33 bit PES clock, this is the period
2219 * of that clock nearest to ref, both in MPEG_TIME_BASE_Q. */
2221{
2222 const int64_t period = 1LL << 33, half = 1LL << 32;
2223 int64_t d = pts - ref;
2224
2225 return pts - (d >= 0 ? d + half : d - half) / period * period;
2226}
2227
2229{
2230 int64_t pts;
2231
2232 if (pkt->pts == AV_NOPTS_VALUE)
2233 return;
2235 pkt->pts = pkt->dts = av_rescale_q(pts, MPEG_TIME_BASE_Q, tb);
2236}
2237
2238/* Put a cue on the period nearest to the media position, where the
2239 * timestamps of the other renditions continue after a wrap. Without media
2240 * a cue follows the previous cue of its playlist, and the cues kept from
2241 * that time move to the media position once it is known. */
2243{
2244 AVRational tb = v->ctx->streams[pkt->stream_index]->time_base;
2246
2247 if (ref != AV_NOPTS_VALUE) {
2249 if (v->cues_unanchored) {
2250 for (int i = 0; i < v->n_prev_cues; i++)
2251 unwrap_pkt(v->prev_cues[i], tb, ref);
2252 for (int i = 0; i < v->n_cur_cues; i++)
2253 unwrap_pkt(v->cur_cues[i], tb, ref);
2254 v->prev_cue_pos = 0;
2255 v->cues_unanchored = 0;
2256 }
2257 unwrap_pkt(pkt, tb, ref);
2258 } else {
2259 v->cues_unanchored = 1;
2260 if (v->last_cue_pts != AV_NOPTS_VALUE)
2262 }
2263 v->last_cue_pts = pkt->pts;
2264}
2265
2267{
2269 struct segment *seg;
2270 int ret;
2271
2272restart:
2273 ret = reload_playlist(v, c);
2274 if (ret < 0)
2275 return ret;
2276
2277 if (v->input && !v->ctx)
2279
2280 if (!v->input && !v->ctx) {
2281 /* nothing is repeated across a discontinuity */
2282 seg = current_segment(v);
2283 if (seg && seg->discontinuity_seq != v->cues_discontinuity_seq) {
2286 }
2288 if (ret < 0)
2289 return ret;
2290 }
2291
2292 while (!(ret = av_read_frame(v->ctx, v->pkt))) {
2293 int repeated;
2294
2295 unwrap_cue_pts(c, v, v->pkt);
2296 repeated = cue_repeated(v, v->pkt);
2297
2298 ret = remember_cue(v, v->pkt);
2299 if (ret < 0) {
2300 av_packet_unref(v->pkt);
2301 return ret;
2302 }
2303 if (!repeated)
2304 return 0;
2305 av_packet_unref(v->pkt);
2306 }
2308 v->cur_seq_no++;
2309 c->cur_seq_no = v->cur_seq_no;
2310
2312
2314 FFSWAP(AVPacket **, v->prev_cues, v->cur_cues);
2315 FFSWAP(int, v->n_prev_cues, v->n_cur_cues);
2316 v->prev_cue_pos = 0;
2317
2318 goto restart;
2319}
2320
2322 enum AVMediaType type, const char *group_id)
2323{
2324 struct rendition_group *group = find_rendition_group(c, type, group_id);
2325 int ret;
2326
2327 if (!group)
2328 return 0;
2329
2330 ret = av_dynarray_add_nofree(&group->variants, &group->n_variants, var);
2331 if (ret < 0)
2332 return ret;
2333
2334 /* add the playlists of the external renditions to the variant */
2335 for (int i = 0; i < group->n_playlists; i++) {
2336 struct playlist *pls = group->playlists[i];
2337
2338 if (pls->last_variant != var) {
2339 ret = av_dynarray_add_nofree(&var->playlists, &var->n_playlists, pls);
2340 if (ret < 0)
2341 return ret;
2342 pls->last_variant = var;
2343 }
2344 }
2345 return 0;
2346}
2347
2348/* The renditions without a playlist are part of the main Media Playlist of
2349 * every variant that references their group. */
2351{
2352 for (int i = 0; i < c->n_rendition_groups; i++) {
2353 struct rendition_group *group = c->rendition_groups[i];
2354
2355 if (group->type != type || !group->n_renditions)
2356 continue;
2357
2358 for (int j = 0; j < group->n_variants; j++) {
2359 struct playlist *pls = group->variants[j]->playlists[0];
2360
2361 if (pls->last_group == group)
2362 continue;
2363
2364 for (int k = 0; k < group->n_renditions; k++) {
2366 group->renditions[k]);
2367 if (ret < 0)
2368 return ret;
2369 }
2370 pls->last_group = group;
2371 }
2372 }
2373 return 0;
2374}
2375
2377 enum AVMediaType type)
2378{
2379 int rend_idx = 0;
2380 int i;
2381
2382 for (i = 0; i < pls->n_main_streams; i++) {
2383 AVStream *st = pls->main_streams[i];
2384
2385 if (st->codecpar->codec_type != type)
2386 continue;
2387
2388 for (; rend_idx < pls->n_renditions; rend_idx++) {
2389 struct rendition *rend = pls->renditions[rend_idx];
2390
2391 if (rend->type != type)
2392 continue;
2393
2394 if (rend->language[0])
2395 av_dict_set(&st->metadata, "language", rend->language, 0);
2396 if (rend->name[0])
2397 av_dict_set(&st->metadata, "comment", rend->name, 0);
2398
2399 st->disposition |= rend->disposition;
2400 }
2401 if (rend_idx >=pls->n_renditions)
2402 break;
2403 }
2404}
2405
2406/* if timestamp was in valid range: returns 1 and sets seq_no
2407 * if not: returns 0 and sets seq_no to closest segment */
2409 int64_t timestamp, int64_t *seq_no,
2410 int64_t *seg_start_ts)
2411{
2412 int i;
2413 int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ?
2414 0 : c->first_timestamp;
2415
2416 if (timestamp < pos) {
2417 /* Seeking before the start of the playlist, clamp to the first segment. */
2418 *seq_no = pls->start_seq_no;
2419 if (seg_start_ts)
2420 *seg_start_ts = pos;
2421 return 1;
2422 }
2423
2424 for (i = 0; i < pls->n_segments; i++) {
2425 int64_t diff = pos + pls->segments[i]->duration - timestamp;
2426 if (diff > 0) {
2427 *seq_no = pls->start_seq_no + i;
2428 if (seg_start_ts) {
2429 *seg_start_ts = pos;
2430 }
2431 return 1;
2432 }
2433 pos += pls->segments[i]->duration;
2434 }
2435
2436 *seq_no = pls->start_seq_no + pls->n_segments - 1;
2437
2438 if (!pls->finished && pls->n_segments > 0) {
2439 if (seg_start_ts)
2440 *seg_start_ts = pos - pls->segments[pls->n_segments - 1]->duration;
2441 return 1;
2442 }
2443
2444 return 0;
2445}
2446
2448{
2449 int64_t seq_no;
2450
2451 if (!pls->finished && !c->first_packet &&
2453 /* reload the playlist since it was suspended */
2454 parse_playlist(c, pls->url, pls, NULL);
2455
2456 /* If playback is already in progress (we are just selecting a new
2457 * playlist) and this is a complete file, find the matching segment
2458 * by counting durations. */
2459 if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) {
2460 find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL);
2461 return seq_no;
2462 }
2463
2464 if (!pls->finished) {
2465 if (!c->first_packet && /* we are doing a segment selection during playback */
2466 c->cur_seq_no >= pls->start_seq_no &&
2467 c->cur_seq_no < pls->start_seq_no + pls->n_segments)
2468 /* While spec 3.4.3 says that we cannot assume anything about the
2469 * content at the same sequence number on different playlists,
2470 * in practice this seems to work and doing it otherwise would
2471 * require us to download a segment to inspect its timestamps. */
2472 return c->cur_seq_no;
2473
2474 /* If this is a live stream, start live_start_index segments from the
2475 * start or end */
2476 if (c->live_start_index < 0)
2477 seq_no = pls->start_seq_no + FFMAX(pls->n_segments +
2478 c->live_start_index, 0);
2479 else
2480 seq_no = pls->start_seq_no + FFMIN(c->live_start_index,
2481 pls->n_segments - 1);
2482
2483 /* If #EXT-X-START in playlist, need to recalculate */
2484 if (pls->time_offset_flag && c->prefer_x_start) {
2485 int64_t start_timestamp;
2486 int64_t playlist_duration = 0;
2487 int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 :
2488 c->cur_timestamp;
2489
2490 for (int i = 0; i < pls->n_segments; i++)
2491 playlist_duration += pls->segments[i]->duration;
2492
2493 /* If the absolute value of TIME-OFFSET exceeds
2494 * the duration of the playlist, it indicates either the end of the
2495 * playlist (if positive) or the beginning of the playlist (if
2496 * negative). */
2497 if (pls->start_time_offset >=0 &&
2498 pls->start_time_offset > playlist_duration)
2499 start_timestamp = cur_timestamp + playlist_duration;
2500 else if (pls->start_time_offset >= 0 &&
2501 pls->start_time_offset <= playlist_duration)
2502 start_timestamp = cur_timestamp + pls->start_time_offset;
2503 else if (pls->start_time_offset < 0 &&
2504 pls->start_time_offset < -playlist_duration)
2505 start_timestamp = cur_timestamp;
2506 else if (pls->start_time_offset < 0 &&
2507 pls->start_time_offset > -playlist_duration)
2508 start_timestamp = cur_timestamp + playlist_duration +
2509 pls->start_time_offset;
2510 else
2511 start_timestamp = cur_timestamp;
2512
2513 find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL);
2514 }
2515 return seq_no;
2516 }
2517
2518 /* Otherwise just start on the first segment. */
2519 return pls->start_seq_no;
2520}
2521
2522static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
2523{
2524 HLSContext *c = s->priv_data;
2525 int i, j;
2526 int bandwidth = -1;
2527
2528 for (i = 0; i < c->n_variants; i++) {
2529 struct variant *v = c->variants[i];
2530
2531 for (j = 0; j < v->n_playlists; j++) {
2532 if (v->playlists[j] != pls)
2533 continue;
2534
2536
2537 if (bandwidth == -1)
2538 bandwidth = v->bandwidth;
2539 else if (bandwidth != v->bandwidth)
2540 bandwidth = -2; /* stream in multiple variants with different bandwidths */
2541 }
2542 }
2543
2544 if (bandwidth >= 0)
2545 av_dict_set_int(&stream->metadata, "variant_bitrate", bandwidth, 0);
2546}
2547
2549{
2550 int err;
2551
2552 err = avcodec_parameters_copy(st->codecpar, ist->codecpar);
2553 if (err < 0)
2554 return err;
2555
2556 if (pls->is_id3_timestamped) /* custom timestamps via id3 */
2558 else
2560
2561 // add the dispositions of the subdemuxer stream to the rendition ones
2562 st->disposition |= ist->disposition;
2563
2564 av_dict_copy(&st->metadata, ist->metadata, 0);
2565
2567
2568 return 0;
2569}
2570
2571/* add new subdemuxer streams to our context, if any */
2573{
2574 HLSContext *c = s->priv_data;
2575 int err;
2576
2577 while (pls->n_main_streams < pls->ctx->nb_streams) {
2578 int ist_idx = pls->n_main_streams;
2580 AVStream *ist = pls->ctx->streams[ist_idx];
2581
2582 if (!st)
2583 return AVERROR(ENOMEM);
2584
2585 st->id = pls->index;
2586 dynarray_add(&pls->main_streams, &pls->n_main_streams, st);
2587
2588 add_stream_to_programs(s, pls, st);
2589
2590 err = set_stream_info_from_input_stream(st, pls, ist);
2591 if (err < 0)
2592 return err;
2593
2594 /* Match the start_time of streams created before playback began. */
2595 if (c->first_timestamp != AV_NOPTS_VALUE)
2596 st->start_time = av_rescale_q(c->first_timestamp,
2598
2600 if (pls->timed_id3_stream_index < 0)
2601 pls->timed_id3_stream_index = ist_idx;
2602 else
2604 "Playlist has multiple timed ID3 streams; "
2605 "only stream %d will be used for metadata propagation\n",
2607 }
2608 }
2609
2610 return 0;
2611}
2612
2614{
2615 HLSContext *c = s->priv_data;
2616 int flag_needed = 0;
2617 int i;
2618
2619 for (i = 0; i < c->n_playlists; i++) {
2620 struct playlist *pls = c->playlists[i];
2621
2622 if (pls->has_noheader_flag) {
2623 flag_needed = 1;
2624 break;
2625 }
2626 }
2627
2628 if (flag_needed)
2629 s->ctx_flags |= AVFMTCTX_NOHEADER;
2630 else
2631 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
2632}
2633
2635{
2636 HLSContext *c = s->priv_data;
2637
2641
2642 if (c->crypto_ctx.aes_ctx)
2643 av_free(c->crypto_ctx.aes_ctx);
2644
2645 av_dict_free(&c->avio_opts);
2646 ff_format_io_close(c->ctx, &c->playlist_pb);
2647
2648 return 0;
2649}
2650
2652{
2653 HLSContext *c = s->priv_data;
2654 int ret = 0, i;
2655 int64_t highest_cur_seq_no = 0;
2656
2657 c->ctx = s;
2658 c->interrupt_callback = &s->interrupt_callback;
2659
2660 c->first_packet = 1;
2661 c->first_timestamp = AV_NOPTS_VALUE;
2662 c->first_timestamp_pls = NULL;
2663 c->first_timestamp_locked = 0;
2664 c->cur_timestamp = AV_NOPTS_VALUE;
2665
2666 if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0)
2667 return ret;
2668
2669 /* http_persistent and http_multiple auto-detection both rely on the
2670 * AVIOContext being backed by the builtin URLContext. Neither works
2671 * when io_open is overridden with a custom callback. */
2672 if (!ffio_geturlcontext(s->pb)) {
2673 if (c->http_persistent) {
2674 av_log(s, AV_LOG_WARNING, "Disabling http_persistent due to custom io_open.\n");
2675 c->http_persistent = 0;
2676 }
2677 /* Only auto-detection is disabled, enabling http_multiple can still work
2678 * with custom io_open. */
2679 if (c->http_multiple == -1) {
2680 av_log(s, AV_LOG_WARNING, "Disabling http_multiple due to custom io_open.\n");
2681 c->http_multiple = 0;
2682 }
2683 }
2684
2685 /* XXX: Some HLS servers don't like being sent the range header,
2686 in this case, we need to set http_seekable = 0 to disable
2687 the range header */
2688 av_dict_set_int(&c->avio_opts, "seekable", c->http_seekable, 0);
2689
2690 if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0)
2691 return ret;
2692
2693 if (c->n_variants == 0) {
2694 av_log(s, AV_LOG_WARNING, "Empty playlist\n");
2695 return AVERROR_EOF;
2696 }
2697 /* If the playlist only contained playlists (Master Playlist),
2698 * parse each individual playlist. */
2699 if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) {
2700 for (i = 0; i < c->n_playlists; i++) {
2701 struct playlist *pls = c->playlists[i];
2702 pls->m3u8_hold_counters = 0;
2703 if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0) {
2704 av_log(s, AV_LOG_WARNING, "parse_playlist error %s [%s]\n", av_err2str(ret), pls->url);
2705 pls->broken = 1;
2706 if (c->n_playlists > 1)
2707 continue;
2708 return ret;
2709 }
2710 }
2711 }
2712
2713 for (i = 0; i < c->n_variants; i++) {
2714 if (c->variants[i]->playlists[0]->n_segments == 0) {
2715 av_log(s, AV_LOG_WARNING, "Empty segment [%s]\n", c->variants[i]->playlists[0]->url);
2716 c->variants[i]->playlists[0]->broken = 1;
2717 }
2718 }
2719
2720 /* Calculate the total duration of the stream if all segments are
2721 * available (finished or EVENT playlists). */
2722 if (c->variants[0]->playlists[0]->finished ||
2723 c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT) {
2724 int64_t duration = 0;
2725 for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++)
2726 duration += c->variants[0]->playlists[0]->segments[i]->duration;
2727 s->duration = duration;
2728 }
2729
2730 /* Associate renditions with variants */
2731 for (i = 0; i < c->n_variants; i++) {
2732 struct variant *var = c->variants[i];
2733
2734 if (var->audio_group[0] &&
2736 return ret;
2737 if (var->video_group[0] &&
2739 return ret;
2740 if (var->subtitles_group[0] &&
2742 return ret;
2743 }
2746 return ret;
2747
2748 /* Create a program for each variant */
2749 for (i = 0; i < c->n_variants; i++) {
2750 struct variant *v = c->variants[i];
2751 AVProgram *program;
2752
2753 program = av_new_program(s, i);
2754 if (!program)
2755 return AVERROR(ENOMEM);
2756 av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0);
2757 }
2758
2759 /* Select the starting segments */
2760 for (i = 0; i < c->n_playlists; i++) {
2761 struct playlist *pls = c->playlists[i];
2762
2763 if (pls->n_segments == 0)
2764 continue;
2765
2766 pls->cur_seq_no = select_cur_seq_no(c, pls);
2767 highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no);
2768 }
2769
2770 av_dict_set(&c->seg_format_opts, "timestamp_map", "1", 0);
2771
2772 /* Open the demuxer for each playlist */
2773 for (i = 0; i < c->n_playlists; i++) {
2774 struct playlist *pls = c->playlists[i];
2775 const AVInputFormat *in_fmt = NULL;
2776 char *url;
2778 struct segment *seg = NULL;
2779 uint8_t *buf;
2780 int buf_size;
2781
2782 if (!(pls->ctx = avformat_alloc_context()))
2783 return AVERROR(ENOMEM);
2784
2785 if (pls->n_segments == 0)
2786 continue;
2787
2788 pls->index = i;
2789 pls->needed = 1;
2790 pls->parent = s;
2791
2792 /*
2793 * If this is a live stream and this playlist looks like it is one segment
2794 * behind, try to sync it up so that every substream starts at the same
2795 * time position (so e.g. avformat_find_stream_info() will see packets from
2796 * all active streams within the first few seconds). This is not very generic,
2797 * though, as the sequence numbers are technically independent.
2798 */
2799 if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 &&
2800 highest_cur_seq_no < pls->start_seq_no + pls->n_segments) {
2801 pls->cur_seq_no = highest_cur_seq_no;
2802 }
2803
2804 if (pls->is_subtitle) {
2805 buf_size = strlen("WEBVTT\n");
2806 buf = av_memdup("WEBVTT\n", buf_size);
2807 } else {
2808 buf_size = INITIAL_BUFFER_SIZE;
2809 buf = av_malloc(buf_size);
2810 }
2811 if (!buf) {
2813 pls->ctx = NULL;
2814 return AVERROR(ENOMEM);
2815 }
2816
2817 ffio_init_context(&pls->pb, buf, buf_size, 0, pls,
2819
2820 /*
2821 * If encryption scheme is SAMPLE-AES, try to read ID3 tags of
2822 * external audio track that contains audio setup information
2823 */
2824 seg = current_segment(pls);
2825 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->n_renditions > 0 &&
2826 pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO) {
2827 uint8_t buf[HLS_MAX_ID3_TAGS_DATA_LEN];
2828 if ((ret = avio_read(&pls->pb.pub, buf, HLS_MAX_ID3_TAGS_DATA_LEN)) < 0) {
2829 /* Fail if error was not end of file */
2830 if (ret != AVERROR_EOF) {
2832 pls->ctx = NULL;
2833 return ret;
2834 }
2835 }
2836 ret = 0;
2837 /* Reset reading */
2838 ff_format_io_close(pls->parent, &pls->input);
2839 pls->input = NULL;
2840 pls->input_read_done = 0;
2842 pls->input_next = NULL;
2843 pls->input_next_requested = 0;
2844 pls->cur_seg_offset = 0;
2845 pls->cur_init_section = NULL;
2846 /* Reset EOF flag */
2847 pls->pb.pub.eof_reached = 0;
2848 /* Clear any buffered data */
2849 pls->pb.pub.buf_end = pls->pb.pub.buf_ptr = pls->pb.pub.buffer;
2850 /* Reset the position */
2851 pls->pb.pub.pos = 0;
2852 }
2853
2854 /*
2855 * If encryption scheme is SAMPLE-AES and audio setup information is present in external audio track,
2856 * use that information to find the media format, otherwise probe input data
2857 */
2858 seg = current_segment(pls);
2859 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->is_id3_timestamped &&
2864 // Keep this list in sync with ff_hls_senc_read_audio_setup_info()
2866 pls->audio_setup_info.codec_id == AV_CODEC_ID_AC3 ? "ac3" : "eac3");
2867 } else {
2868 pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4;
2869 pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE;
2870 pls->ctx->interrupt_callback = s->interrupt_callback;
2871 url = av_strdup(pls->segments[0]->url);
2872 ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, url, NULL, 0, 0);
2873
2874 for (int n = 0; n < pls->n_segments; n++)
2875 if (ret >= 0)
2876 ret = test_segment(s, in_fmt, pls, pls->segments[n]);
2877
2878 if (ret < 0) {
2879 /* Free the ctx - it isn't initialized properly at this point,
2880 * so avformat_close_input shouldn't be called. If
2881 * avformat_open_input fails below, it frees and zeros the
2882 * context, so it doesn't need any special treatment like this. */
2883 av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url);
2885 pls->ctx = NULL;
2886 av_free(url);
2887 return ret;
2888 }
2889 av_free(url);
2890 }
2891
2892 seg = current_segment(pls);
2893 if (seg && seg->key_type == KEY_SAMPLE_AES) {
2894 if (strstr(in_fmt->name, "mov")) {
2895 char key[33];
2896 ff_data_to_hex(key, pls->key, sizeof(pls->key), 0);
2897 av_dict_set(&options, "decryption_key", key, 0);
2898 } else if (!c->crypto_ctx.aes_ctx) {
2899 c->crypto_ctx.aes_ctx = av_aes_alloc();
2900 if (!c->crypto_ctx.aes_ctx) {
2902 pls->ctx = NULL;
2903 return AVERROR(ENOMEM);
2904 }
2905 }
2906 }
2907
2908 pls->ctx->pb = &pls->pb.pub;
2909 pls->ctx->io_open = nested_io_open;
2910 pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO;
2911
2912 if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0)
2913 return ret;
2914
2915 av_dict_copy(&options, c->seg_format_opts, 0);
2916
2917 ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &options);
2919 if (ret < 0)
2920 return ret;
2921
2922 if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) {
2928 }
2929
2930 if (pls->is_id3_timestamped == -1)
2931 av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n");
2932
2933 /*
2934 * For ID3 timestamped raw audio streams we need to detect the packet
2935 * durations to calculate timestamps in fill_timing_for_id3_timestamped_stream(),
2936 * but for other streams we can rely on our user calling avformat_find_stream_info()
2937 * on us if they want to.
2938 */
2939 if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) {
2940 seg = current_segment(pls);
2941 if (seg && seg->key_type == KEY_SAMPLE_AES && pls->audio_setup_info.setup_data_length > 0 &&
2942 pls->ctx->nb_streams == 1)
2944 else
2946
2947 if (ret < 0)
2948 return ret;
2949 }
2950
2952
2953 /* Create new AVStreams for each stream in this playlist */
2955 if (ret < 0)
2956 return ret;
2957
2958 /*
2959 * Copy any metadata from playlist to main streams, but do not set
2960 * event flags.
2961 */
2962 if (pls->n_main_streams)
2963 av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0);
2964
2965 if (pls->is_subtitle) {
2967 pls->ctx = NULL;
2968 pls->needed = 0;
2970 }
2971
2975 }
2976
2978
2979 return 0;
2980}
2981
2983{
2984 HLSContext *c = s->priv_data;
2985 int i, changed = 0;
2986 int cur_needed;
2987
2988 /* Check if any new streams are needed */
2989 for (i = 0; i < c->n_playlists; i++) {
2990 struct playlist *pls = c->playlists[i];
2991
2992 cur_needed = playlist_needed(c->playlists[i]);
2993
2994 if (pls->broken) {
2995 continue;
2996 }
2997 if (cur_needed && !pls->needed) {
2998 pls->needed = 1;
2999 changed = 1;
3000 pls->cur_seq_no = select_cur_seq_no(c, pls);
3001 pls->first_read_seq_no = -1;
3002 pls->pb.pub.eof_reached = 0;
3003 if (c->cur_timestamp != AV_NOPTS_VALUE) {
3004 /* catch up */
3005 pls->seek_timestamp = av_sat_add64(c->cur_timestamp,
3006 pls->ts_offset != AV_NOPTS_VALUE ? pls->ts_offset : 0);
3008 pls->seek_stream_index = -1;
3009 }
3010 av_log(s, AV_LOG_INFO, "Now receiving playlist %d, segment %"PRId64"\n", i, pls->cur_seq_no);
3011 } else if (first && !cur_needed && pls->needed) {
3012 ff_format_io_close(pls->parent, &pls->input);
3013 pls->input_read_done = 0;
3015 pls->input_next_requested = 0;
3016 if (pls->is_subtitle) {
3018 free_subtitle_cues(pls);
3019 }
3020 pls->needed = 0;
3021 changed = 1;
3022 av_log(s, AV_LOG_INFO, "No longer receiving playlist %d\n", i);
3023 }
3024 }
3025 return changed;
3026}
3027
3029{
3030 if (pls->id3_offset >= 0) {
3033 pls->ctx->streams[pls->pkt->stream_index]->time_base,
3035 if (pls->pkt->duration)
3036 pls->id3_offset = av_sat_add64(pls->id3_offset, pls->pkt->duration);
3037 else
3038 pls->id3_offset = -1;
3039 } else {
3040 /* there have been packets with unknown duration
3041 * since the last id3 tag, should not normally happen */
3042 pls->pkt->dts = AV_NOPTS_VALUE;
3043 }
3044
3045 if (pls->pkt->duration)
3046 pls->pkt->duration = av_rescale_q(pls->pkt->duration,
3047 pls->ctx->streams[pls->pkt->stream_index]->time_base,
3049
3050 pls->pkt->pts = AV_NOPTS_VALUE;
3051}
3052
3054{
3055 if (pls->is_id3_timestamped)
3056 return MPEG_TIME_BASE_Q;
3057
3058 return pls->ctx->streams[pls->pkt->stream_index]->time_base;
3059}
3060
3061static int compare_ts_with_wrapdetect(int64_t ts_a, struct playlist *pls_a,
3062 int64_t ts_b, struct playlist *pls_b)
3063{
3064 int64_t scaled_ts_a = av_rescale_q(ts_a, get_timebase(pls_a), MPEG_TIME_BASE_Q);
3065 int64_t scaled_ts_b = av_rescale_q(ts_b, get_timebase(pls_b), MPEG_TIME_BASE_Q);
3066
3067 return av_compare_mod(scaled_ts_a, scaled_ts_b, 1LL << 33);
3068}
3069
3070/**
3071 * Check whether any entry in @p update differs from the corresponding entry
3072 * in @p current.
3073 *
3074 * HLS segments can repeat timed ID3 metadata in every segment so that
3075 * listeners joining mid-stream immediately receive the current track
3076 * information. Only signal a metadata change when values actually differ
3077 * to avoid spurious updates on every segment boundary.
3078 */
3080 const AVDictionary *update)
3081{
3083 return 1;
3084 const AVDictionaryEntry *e = NULL;
3085 while ((e = av_dict_iterate(update, e))) {
3086 const AVDictionaryEntry *cur = av_dict_get(current, e->key, NULL, 0);
3087 if (!cur || strcmp(cur->value, e->value))
3088 return 1;
3089 }
3090 return 0;
3091}
3092
3094{
3095 HLSContext *c = s->priv_data;
3096 int ret, i, minplaylist = -1, deferred = 0, unanchored = 0;
3097
3098 recheck_discard_flags(s, c->first_packet);
3099 c->first_packet = 0;
3100
3101retry:
3102 for (i = 0; i < c->n_playlists; i++) {
3103 struct playlist *pls = c->playlists[i];
3104 /* Make sure we've got one buffered packet from each open playlist
3105 * stream */
3106 if (pls->needed && !pls->pkt->data) {
3107 /* a cue is placed relative to the media, so the media playlists
3108 * buffer their packets first */
3109 if (pls->is_subtitle && !unanchored &&
3111 deferred = 1;
3112 continue;
3113 }
3114 while (1) {
3115 int64_t ts_diff;
3116 AVRational tb;
3117 struct segment *seg = NULL;
3118 if (pls->is_subtitle)
3119 ret = read_subtitle_packet(pls, pls->pkt);
3120 else
3121 ret = av_read_frame(pls->ctx, pls->pkt);
3122 if (ret < 0) {
3123 if (!avio_feof(&pls->pb.pub) && ret != AVERROR_EOF)
3124 return ret;
3125 break;
3126 }
3127
3128 /* stream_index check prevents matching picture attachments etc. */
3129 if (pls->is_id3_timestamped && pls->pkt->stream_index == 0) {
3130 /* audio elementary streams are id3 timestamped */
3132 }
3133
3134 if (pls->is_subtitle) {
3135 /* the cues are on the shared timeline */
3136 if (pls->ts_offset == AV_NOPTS_VALUE)
3137 pls->ts_offset = 0;
3138 } else if (pls->pkt->dts != AV_NOPTS_VALUE &&
3139 (pls->ts_offset == AV_NOPTS_VALUE ||
3140 (!c->first_timestamp_locked &&
3141 c->first_timestamp_pls == pls &&
3142 pls->cur_seq_no <= pls->first_read_seq_no + 1))) {
3143 int adjusted = 0;
3144 /* Packet timestamp rebased onto the start of the segment list. */
3145 int64_t seg_idx = pls->first_read_seq_no - pls->start_seq_no;
3146 int64_t ts = av_rescale_q(pls->pkt->pts != AV_NOPTS_VALUE ?
3147 pls->pkt->pts : pls->pkt->dts,
3149
3150 for (int64_t k = 0; k < seg_idx && k < pls->n_segments; k++)
3151 ts = av_sat_sub64(ts, pls->segments[k]->duration);
3152
3153 if (c->first_timestamp == AV_NOPTS_VALUE) {
3154 c->first_timestamp = ts;
3155 c->first_timestamp_pls = pls;
3156 adjusted = 1;
3157 } else if (c->first_timestamp_pls == pls) {
3158 /* first_timestamp came from the first packet in mux
3159 * order, but another stream in the same segment may
3160 * start earlier. Lower the estimate while packets of
3161 * that segment are still arriving. */
3162 int64_t delta = av_sat_sub64(c->first_timestamp, ts);
3163
3164 if (delta > 0 && delta <= pls->target_duration) {
3165 c->first_timestamp = ts;
3166 for (int k = 0; k < c->n_playlists; k++)
3167 if (c->playlists[k]->ts_offset != AV_NOPTS_VALUE &&
3168 !c->playlists[k]->is_subtitle)
3169 c->playlists[k]->ts_offset = av_sat_add64(c->playlists[k]->ts_offset, delta);
3170 adjusted = 1;
3171 }
3172 }
3173
3174 if (pls->ts_offset == AV_NOPTS_VALUE)
3175 pls->ts_offset = av_sat_sub64(ts, c->first_timestamp);
3176
3177 if (adjusted) {
3178 for (unsigned k = 0; k < s->nb_streams; k++)
3179 s->streams[k]->start_time =
3180 av_rescale_q(c->first_timestamp, AV_TIME_BASE_Q,
3181 s->streams[k]->time_base);
3183 "First timestamp %"PRId64" from playlist %d\n",
3184 c->first_timestamp, pls->index);
3185 }
3186 }
3187
3188 seg = current_segment(pls);
3189 if (seg && seg->key_type == KEY_SAMPLE_AES && !strstr(pls->ctx->iformat->name, "mov")) {
3191 memcpy(c->crypto_ctx.iv, seg->iv, sizeof(seg->iv));
3192 memcpy(c->crypto_ctx.key, pls->key, sizeof(pls->key));
3193 ff_hls_senc_decrypt_frame(codec_id, &c->crypto_ctx, pls->pkt);
3194 }
3195
3196 if (pls->seek_timestamp == AV_NOPTS_VALUE)
3197 break;
3198
3199 if (pls->seek_stream_index < 0 ||
3200 pls->seek_stream_index == pls->pkt->stream_index) {
3201
3202 if (pls->pkt->dts == AV_NOPTS_VALUE) {
3204 break;
3205 }
3206
3207 tb = get_timebase(pls);
3208 ts_diff = av_rescale_q_rnd(pls->pkt->dts, tb,
3210 pls->seek_timestamp;
3211 if (ts_diff >= 0 && (pls->seek_flags & AVSEEK_FLAG_ANY ||
3212 pls->pkt->flags & AV_PKT_FLAG_KEY)) {
3214 break;
3215 }
3216 /* a cue still shown at the target is delivered, the cues
3217 * after it may still have ended before the target */
3218 if (pls->is_subtitle && pls->pkt->duration > 0 &&
3219 av_rescale_q_rnd(pls->pkt->dts + pls->pkt->duration, tb,
3221 pls->seek_timestamp)
3222 break;
3223 }
3224 av_packet_unref(pls->pkt);
3225 }
3226 }
3227 }
3228
3229 for (i = 0; i < c->n_playlists; i++) {
3230 struct playlist *pls = c->playlists[i];
3231
3232 /* a cue buffered without media is placed once the media is */
3233 if (pls->is_subtitle && pls->pkt->data)
3234 unwrap_cue_pts(c, pls, pls->pkt);
3235 /* Check if this stream has the packet with the lowest dts */
3236 if (pls->pkt->data) {
3237 struct playlist *minpls = minplaylist < 0 ?
3238 NULL : c->playlists[minplaylist];
3239 if (minplaylist < 0) {
3240 minplaylist = i;
3241 } else {
3242 int64_t dts = pls->pkt->dts;
3243 int64_t mindts = minpls->pkt->dts;
3244
3245 if (dts == AV_NOPTS_VALUE ||
3246 (mindts != AV_NOPTS_VALUE && compare_ts_with_wrapdetect(dts, pls, mindts, minpls) < 0))
3247 minplaylist = i;
3248 }
3249 }
3250 }
3251
3252 /* without media the cues are read as they are */
3253 if (minplaylist < 0 && deferred) {
3254 deferred = 0;
3255 unanchored = 1;
3256 goto retry;
3257 }
3258
3259 /* If we got a packet, return it */
3260 if (minplaylist >= 0) {
3261 struct playlist *pls = c->playlists[minplaylist];
3262 AVStream *ist;
3263 AVStream *st;
3264
3266 if (ret < 0) {
3267 av_packet_unref(pls->pkt);
3268 return ret;
3269 }
3270
3271 // If sub-demuxer reports updated metadata, copy it to the first stream
3272 // and set its AVSTREAM_EVENT_FLAG_METADATA_UPDATED flag.
3274 if (pls->n_main_streams) {
3275 st = pls->main_streams[0];
3276 av_dict_copy(&st->metadata, pls->ctx->metadata, 0);
3278 }
3280 }
3281
3282 /* check if noheader flag has been cleared by the subdemuxer */
3283 if (pls->has_noheader_flag && !(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER)) {
3284 pls->has_noheader_flag = 0;
3286 }
3287
3288 if (pls->pkt->stream_index >= pls->n_main_streams) {
3289 av_log(s, AV_LOG_ERROR, "stream index inconsistency: index %d, %d main streams, %d subdemuxer streams\n",
3290 pls->pkt->stream_index, pls->n_main_streams, pls->ctx->nb_streams);
3291 av_packet_unref(pls->pkt);
3292 return AVERROR_BUG;
3293 }
3294
3295 ist = pls->ctx->streams[pls->pkt->stream_index];
3296 st = pls->main_streams[pls->pkt->stream_index];
3297
3298 // Propagate timed-ID3 metadata changes to the main stream, suppressing
3299 // duplicate packets that repeat unchanged metadata at segment boundaries.
3301 pls->pkt->stream_index == pls->timed_id3_stream_index &&
3305 int ret = av_dict_copy(&st->metadata, ist->metadata, 0);
3306 if (ret < 0)
3307 return ret;
3309 ret = av_dict_copy(&pls->timed_id3_metadata, ist->metadata, 0);
3310 if (ret < 0)
3311 return ret;
3313 }
3314 }
3315
3316 av_packet_move_ref(pkt, pls->pkt);
3317 pkt->stream_index = st->index;
3318
3319 /* a cue read without media does not set the media position */
3320 if (pkt->dts != AV_NOPTS_VALUE &&
3321 (!pls->is_subtitle || c->first_timestamp != AV_NOPTS_VALUE))
3322 c->cur_timestamp = av_rescale_q(pkt->dts,
3323 ist->time_base,
3325
3326 /* There may be more situations where this would be useful, but this at least
3327 * handles newly probed codecs properly (i.e. request_probe by mpegts). */
3328 if (ist->codecpar->codec_id != st->codecpar->codec_id) {
3329 ret = set_stream_info_from_input_stream(st, pls, ist);
3330 if (ret < 0) {
3331 return ret;
3332 }
3333 }
3334
3335 return 0;
3336 }
3337 return AVERROR_EOF;
3338}
3339
3340static int hls_read_seek(AVFormatContext *s, int stream_index,
3341 int64_t timestamp, int flags)
3342{
3343 HLSContext *c = s->priv_data;
3344 struct playlist *seek_pls = NULL;
3345 int i, j;
3346 int stream_subdemuxer_index;
3347 int64_t first_timestamp, seek_timestamp, duration;
3348 int64_t seq_no, seg_start_ts;
3349 int snapped_to_segment = 0;
3350
3351 if ((flags & AVSEEK_FLAG_BYTE) || (c->ctx->ctx_flags & AVFMTCTX_UNSEEKABLE))
3352 return AVERROR(ENOSYS);
3353
3354 seek_timestamp = av_rescale_q_rnd(timestamp,
3355 s->streams[stream_index]->time_base,
3357
3358 /* find the playlist with the specified stream */
3359 for (i = 0; i < c->n_playlists; i++) {
3360 struct playlist *pls = c->playlists[i];
3361 for (j = 0; j < pls->n_main_streams; j++) {
3362 if (pls->main_streams[j] == s->streams[stream_index]) {
3363 seek_pls = pls;
3364 stream_subdemuxer_index = j;
3365 break;
3366 }
3367 }
3368 }
3369 if (!seek_pls)
3370 return AVERROR(EIO);
3371
3372 /* Live and EVENT playlists may have gained segments since the last load,
3373 * and sliding-window playlists may also have expired some. Refresh before
3374 * resolving a seek that may fall outside the loaded window, rate limited
3375 * to the minimum reload interval (RFC 8216 6.3.4). */
3376 if (!seek_pls->finished) {
3377 int64_t elapsed = av_gettime_relative() - seek_pls->last_load_time;
3378 int64_t start = c->first_timestamp == AV_NOPTS_VALUE ?
3379 0 : c->first_timestamp;
3380 int64_t end = start;
3381 int need_refresh;
3382 for (i = 0; i < seek_pls->n_segments; i++)
3383 end += seek_pls->segments[i]->duration;
3384 need_refresh = seek_timestamp >= end;
3385 if (seek_pls->type == PLS_TYPE_UNSPECIFIED)
3386 need_refresh |= seek_timestamp < start + elapsed +
3387 seek_pls->target_duration;
3388 if (need_refresh && elapsed >= default_reload_interval(seek_pls))
3389 parse_playlist(c, seek_pls->url, seek_pls, NULL);
3390 }
3391
3392 first_timestamp = c->first_timestamp == AV_NOPTS_VALUE ?
3393 0 : c->first_timestamp;
3394
3395 duration = s->duration == AV_NOPTS_VALUE ?
3396 0 : s->duration;
3397
3398 /* Only finished playlists cannot grow past the known duration. */
3399 if (seek_pls->finished &&
3400 0 < duration && duration < seek_timestamp - first_timestamp)
3401 return AVERROR(EIO);
3402
3403 /* check if the timestamp is valid for the playlist with the
3404 * specified stream index */
3405 if (!find_timestamp_in_playlist(c, seek_pls, seek_timestamp, &seq_no, &seg_start_ts))
3406 return AVERROR(EIO);
3407
3408 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
3410 /* Seeking to start of segment ensures we seek to a keyframe located
3411 * before the given timestamp. */
3412 seek_timestamp = seg_start_ts;
3413 snapped_to_segment = 1;
3414 }
3415
3416 av_log(s, AV_LOG_DEBUG, "Seek to %"PRId64" mapped to segment %"PRId64
3417 " of playlist %d (start %"PRId64")\n",
3418 seek_timestamp, seq_no, seek_pls->index, seg_start_ts);
3419
3420 /* Keep timeline start fixed from now on. */
3421 c->first_timestamp_locked = 1;
3422
3423 /* set segment now so we do not need to search again below */
3424 seek_pls->cur_seq_no = seq_no;
3425 seek_pls->seek_stream_index = stream_subdemuxer_index;
3426
3427 /* Reset PTS wrap detection so backward seeks don't get misinterpreted
3428 * as a forward PTS wrap. */
3429 for (i = 0; i < (int)s->nb_streams; i++)
3431
3432 for (i = 0; i < c->n_playlists; i++) {
3433 /* Reset reading */
3434 struct playlist *pls = c->playlists[i];
3435 AVIOContext *const pb = &pls->pb.pub;
3436 ff_format_io_close(pls->parent, &pls->input);
3437 pls->input_read_done = 0;
3438 pls->first_read_seq_no = -1;
3440 pls->input_next_requested = 0;
3441 av_packet_unref(pls->pkt);
3442 pb->eof_reached = 0;
3443 /* Clear any buffered data */
3444 pb->buf_end = pb->buf_ptr = pb->buffer;
3445 /* Reset the pos, to let the mpegts/mov demuxer know we've seeked. */
3446 pb->pos = 0;
3447 /* Flush the packet queue of the subdemuxer. */
3448 if (pls->ctx) {
3450 for (j = 0; j < (int)pls->ctx->nb_streams; j++)
3452 }
3453 if (pls->is_subtitle) {
3455 free_subtitle_cues(pls);
3456 }
3457
3458 /* Reset the init segment so it's re-fetched and served appropriately */
3459 pls->cur_init_section = NULL;
3460
3461 /* The discard in hls_read_packet compares this playlist's packet DTS
3462 * against seek_timestamp, but seek_timestamp is on c->first_timestamp's
3463 * baseline (whichever stream produced the first packet). Streams can
3464 * have different DTS baselines, so translate the threshold onto this
3465 * playlist's own baseline using its captured offset. */
3466 if (pls->ts_offset != AV_NOPTS_VALUE)
3468 else
3470 pls->seek_flags = flags;
3471
3472 if (pls == seek_pls && snapped_to_segment) {
3473 /* The first keyframe of the target segment may have a slightly
3474 * lower DTS than the EXTINF-derived seg_start_ts. Relax the cutoff
3475 * to not discard the keyframe and land one segment late. */
3476 int64_t idx = seq_no - pls->start_seq_no;
3477 if (idx >= 0 && idx < pls->n_segments)
3479 }
3480
3481 if (pls != seek_pls) {
3482 /* set closest segment seq_no for playlists not handled above */
3484 /* seek the playlist to the given position without taking
3485 * keyframes into account since this playlist does not have the
3486 * specified stream where we should look for the keyframes */
3487 pls->seek_stream_index = -1;
3489 }
3490
3491 pls->last_seq_no = pls->cur_seq_no;
3492 }
3493
3494 c->cur_timestamp = seek_timestamp;
3495
3496 return 0;
3497}
3498
3499static int hls_probe(const AVProbeData *p)
3500{
3501 /* Require #EXTM3U at the start, and either one of the ones below
3502 * somewhere for a proper match. */
3503 if (strncmp(p->buf, "#EXTM3U", 7))
3504 return 0;
3505
3506 if (strstr(p->buf, "#EXT-X-STREAM-INF:") ||
3507 strstr(p->buf, "#EXT-X-TARGETDURATION:") ||
3508 strstr(p->buf, "#EXT-X-MEDIA-SEQUENCE:")) {
3509
3510 int mime_ok = p->mime_type && !(
3511 av_strcasecmp(p->mime_type, "application/vnd.apple.mpegurl") &&
3512 av_strcasecmp(p->mime_type, "audio/mpegurl")
3513 );
3514
3515 int mime_x = p->mime_type && !(
3516 av_strcasecmp(p->mime_type, "audio/x-mpegurl") &&
3517 av_strcasecmp(p->mime_type, "application/x-mpegurl")
3518 );
3519
3520 if (!mime_ok &&
3521 !mime_x &&
3522 !av_match_ext (p->filename, "m3u8,m3u") &&
3523 ff_match_url_ext(p->filename, "m3u8,m3u") <= 0) {
3524 av_log(NULL, AV_LOG_ERROR, "Not detecting m3u8/hls with non standard extension and non standard mime type\n");
3525 return 0;
3526 }
3527 if (mime_x)
3528 av_log(NULL, AV_LOG_WARNING, "mime type is not rfc8216 compliant\n");
3529
3530 return AVPROBE_SCORE_MAX;
3531 }
3532 return 0;
3533}
3534
3535#define OFFSET(x) offsetof(HLSContext, x)
3536#define FLAGS AV_OPT_FLAG_DECODING_PARAM
3537static const AVOption hls_options[] = {
3538 {"live_start_index", "segment index to start live streams at (negative values are from the end)",
3539 OFFSET(live_start_index), AV_OPT_TYPE_INT, {.i64 = -3}, INT_MIN, INT_MAX, FLAGS},
3540 {"prefer_x_start", "prefer to use #EXT-X-START if it's in playlist instead of live_start_index",
3541 OFFSET(prefer_x_start), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS},
3542 {"allowed_extensions", "List of file extensions that hls is allowed to access",
3543 OFFSET(allowed_extensions), AV_OPT_TYPE_STRING,
3544 {.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"
3545 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3546 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3547 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3548 },
3549 INT_MIN, INT_MAX, FLAGS},
3550 {"allowed_segment_extensions", "List of file extensions that hls is allowed to access",
3551 OFFSET(allowed_segment_extensions), AV_OPT_TYPE_STRING,
3552 {.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"
3553 ",cmfv,cmfa" // Ticket11526 www.nicovideo.jp
3554 ",ec3" // part of Ticket11435 (Elisa Viihde (Finnish online recording service))
3555 ",fmp4" // https://github.com/yt-dlp/yt-dlp/issues/12700
3556 ",html" // https://flash1.bogulus.cfd/
3557 },
3558 INT_MIN, INT_MAX, FLAGS},
3559 {"extension_picky", "Be picky with all extensions matching",
3560 OFFSET(extension_picky), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS},
3561 {"max_reload", "Maximum number of times a insufficient list is attempted to be reloaded",
3562 OFFSET(max_reload), AV_OPT_TYPE_INT, {.i64 = 100}, 0, INT_MAX, FLAGS},
3563 {"m3u8_hold_counters", "The maximum number of times to load m3u8 when it refreshes without new segments",
3564 OFFSET(m3u8_hold_counters), AV_OPT_TYPE_INT, {.i64 = 1000}, 0, INT_MAX, FLAGS},
3565 {"http_persistent", "Use persistent HTTP connections",
3566 OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
3567 {"http_multiple", "Use multiple HTTP connections for fetching segments",
3568 OFFSET(http_multiple), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, FLAGS},
3569 {"http_seekable", "Use HTTP partial requests, 0 = disable, 1 = enable, -1 = auto",
3570 OFFSET(http_seekable), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, FLAGS},
3571 {"seg_format_options", "Set options for segment demuxer",
3572 OFFSET(seg_format_opts), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, FLAGS},
3573 {"seg_max_retry", "Maximum number of times to reload a segment on error.",
3574 OFFSET(seg_max_retry), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS},
3575 {NULL}
3576};
3577
3578static const AVClass hls_class = {
3579 .class_name = "hls demuxer",
3580 .item_name = av_default_item_name,
3581 .option = hls_options,
3582 .version = LIBAVUTIL_VERSION_INT,
3583};
3584
3586 .p.name = "hls",
3587 .p.long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
3588 .p.priv_class = &hls_class,
3590 .priv_data_size = sizeof(HLSContext),
3591 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
3597};
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:3585
#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:992
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:910
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:865
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition avformat.c:367
Main libavformat public API header.
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1796
#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:1352
#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:2748
#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:2749
#define AVFMTCTX_UNSEEKABLE
signal that the stream is definitely not seekable, and attempts to call the seek function will fail.
Definition avformat.h:1354
#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:1561
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2747
#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:935
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition avio.c:733
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:630
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:620
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:962
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:765
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 av_sat_sub64
Definition common.h:142
#define av_sat_add64
Definition common.h:139
#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:1960
#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:835
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:359
int ret
Definition ffplay.c:391
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
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:612
@ AV_CODEC_ID_EAC3
Definition codec_id.h:499
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_AC3
Definition codec_id.h:462
#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:248
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition packet.h:199
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
AVPacket * av_packet_clone(const AVPacket *src)
Create a new packet that references the same data as src.
Definition packet.c:479
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
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:299
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:165
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:1646
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:373
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:2674
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:247
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.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
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
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(const void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
Definition tree.c:59
void av_tree_destroy(AVTreeNode *t)
Definition tree.c:146
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
Definition tree.c:34
#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:1304
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:604
int a
static void update_noheader_flag(AVFormatContext *s)
Definition hls.c:2613
#define MAX_CHARACTERISTICS_LEN
Definition hls.c:55
static void free_subtitle_cues(struct playlist *pls)
Definition hls.c:334
#define MAX_FIELD_LEN
Definition hls.c:54
static int is_native_http(AVIOContext *pb)
Definition hls.c:839
static int update_init_section(struct playlist *pls, struct segment *seg)
Definition hls.c:1759
static struct rendition_group * find_rendition_group(HLSContext *c, enum AVMediaType type, const char *id)
Definition hls.c:644
static int cmp_rendition_group(const void *a, const void *b)
Definition hls.c:635
static int read_from_url(struct playlist *pls, struct segment *seg, uint8_t *buf, int buf_size)
Definition hls.c:1434
static int add_renditions_to_variant(HLSContext *c, struct variant *var, enum AVMediaType type, const char *group_id)
Definition hls.c:2321
static struct segment * new_init_section(struct playlist *pls, struct init_section_info *info, const char *url_base)
Definition hls.c:547
static int cmp_url_playlist(const void *key, const void *b)
Definition hls.c:407
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:2408
static int read_data_continuous(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:1946
static int hls_read_header(AVFormatContext *s)
Definition hls.c:2651
static struct playlist * new_playlist(HLSContext *c, const char *url, const char *base)
Definition hls.c:419
static int read_subtitle_packet(struct playlist *v, AVPacket *pkt)
Definition hls.c:2266
static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls, AVStream *ist)
Definition hls.c:2548
static int test_segment(AVFormatContext *s, const AVInputFormat *in_fmt, struct playlist *pls, struct segment *seg)
Definition hls.c:955
static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in, int64_t *end)
Definition hls.c:1683
static void free_segment_list(struct playlist *pls)
Definition hls.c:307
static int64_t media_position(HLSContext *c)
Definition hls.c:2204
static void handle_key_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:526
#define MPEG_TIME_BASE
Definition hls.c:57
static void free_init_section_list(struct playlist *pls)
Definition hls.c:314
static int hls_probe(const AVProbeData *p)
Definition hls.c:3499
static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url)
Definition hls.c:828
static int segment_continues(const struct segment *cur, const struct segment *next, int64_t end)
Definition hls.c:1408
PlaylistType
Definition hls.c:97
@ PLS_TYPE_UNSPECIFIED
Definition hls.c:98
@ PLS_TYPE_VOD
Definition hls.c:100
@ PLS_TYPE_EVENT
Definition hls.c:99
static int cue_repeated(struct playlist *v, const AVPacket *pkt)
Definition hls.c:2159
static int recheck_discard_flags(AVFormatContext *s, int first)
Definition hls.c:2982
static AVRational get_timebase(struct playlist *pls)
Definition hls.c:3053
static void fill_timing_for_id3_timestamped_stream(struct playlist *pls)
Definition hls.c:3028
static int64_t default_reload_interval(struct playlist *pls)
Definition hls.c:1816
static int open_url_keepalive(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **options)
Definition hls.c:849
static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls, enum AVMediaType type)
Definition hls.c:2376
static int update_streams_from_subdemuxer(AVFormatContext *s, struct playlist *pls)
Definition hls.c:2572
static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream)
Definition hls.c:2522
static int64_t nearest_period(int64_t pts, int64_t ref)
Definition hls.c:2220
static void free_cue_list(AVPacket ***cues, int *n_cues)
Definition hls.c:326
static int hls_close(AVFormatContext *s)
Definition hls.c:2634
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:3079
static struct segment * next_segment(struct playlist *pls)
Definition hls.c:1396
static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **opts)
Definition hls.c:2109
static int reload_playlist(struct playlist *v, HLSContext *c)
Definition hls.c:1869
static void handle_id3(AVIOContext *pb, struct playlist *pls)
Definition hls.c:1509
#define PACKED_AUDIO_FORMATS
Definition hls.c:60
static int add_renditions_to_playlists(HLSContext *c, enum AVMediaType type)
Definition hls.c:2350
static void handle_rendition_args(void *vinfo, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:782
static int new_rendition(HLSContext *c, struct rendition_info *info, const char *url_base)
Definition hls.c:678
static void free_segment_dynarray(struct segment **segments, int n_segments)
Definition hls.c:297
static int read_data_subtitle_segment(void *opaque, uint8_t *buf, int buf_size)
Definition hls.c:2080
static int init_subtitle_context(struct playlist *pls)
Definition hls.c:2119
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:3061
static int parse_playlist(HLSContext *c, const char *url, struct playlist *pls, AVIOContext *in)
Definition hls.c:1002
#define MPEG_TIME_BASE_Q
Definition hls.c:58
KeyType
Definition hls.c:74
@ KEY_SAMPLE_AES
Definition hls.c:77
@ KEY_NONE
Definition hls.c:75
@ KEY_AES_128
Definition hls.c:76
static void handle_variant_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:501
static struct segment * current_segment(struct playlist *pls)
Definition hls.c:1388
static int hls_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition hls.c:3093
static const AVOption hls_options[]
Definition hls.c:3537
static void free_rendition_list(HLSContext *c)
Definition hls.c:388
static void handle_start_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:613
static int remember_cue(struct playlist *v, const AVPacket *pkt)
Definition hls.c:2186
static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, AVDictionary **opts, AVDictionary *opts2, int *is_http_out)
Definition hls.c:867
#define OFFSET(x)
Definition hls.c:3535
static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, ID3v2ExtraMetaAPIC *apic)
Definition hls.c:1480
static void free_playlist_list(HLSContext *c)
Definition hls.c:343
static const AVClass hls_class
Definition hls.c:3578
static void unwrap_pkt(AVPacket *pkt, AVRational tb, int64_t ref)
Definition hls.c:2228
static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls)
Definition hls.c:2447
static struct variant * new_variant(HLSContext *c, struct variant_info *info, const char *url, const char *base)
Definition hls.c:475
static void intercept_id3(struct playlist *pls, uint8_t *buf, int buf_size, int *len)
Definition hls.c:1552
static struct rendition_group * new_rendition_group(HLSContext *c, enum AVMediaType type, const char *id)
Definition hls.c:654
static int playlist_needed(struct playlist *pls)
Definition hls.c:1823
static void free_variant_list(HLSContext *c)
Definition hls.c:376
static int64_t range_end(struct playlist *pls, const struct segment *seg)
Definition hls.c:1420
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:1451
static void handle_init_section_args(void *context, const char *key, int key_len, char **dest, int *dest_len)
Definition hls.c:596
static void unwrap_cue_pts(HLSContext *c, struct playlist *v, AVPacket *pkt)
Definition hls.c:2242
static int hls_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition hls.c:3340
static int cmp_playlist_url(const void *a, const void *b)
Definition hls.c:413
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:590
int ff_id3v2_parse_priv_dict(AVDictionary **d, ID3v2ExtraMeta *extra_meta)
Parse PRIV tags into a dictionary.
Definition reader.c:1303
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition reader.c:165
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition reader.c:152
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define ID3v2_HEADER_SIZE
Definition id3v2.h:30
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition reader.c:1231
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition reader.c:1247
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition reader.c:1343
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 reader.c:1219
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_WB64(p, v)
#define AV_RB64(p)
enum MPFType type
Definition jpegmpfenc.c:55
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:390
#define dynarray_add(tab, nb_ptr, elem)
Definition internal.h:401
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
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
Memory handling functions.
static uint8_t half(int a, int b)
Definition mobiclip.c:540
#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:1401
int event_flags
Flags indicating events happening on the file, a combination of AVFMT_EVENT_FLAG_*.
Definition avformat.h:1789
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1457
AVIOContext * pb
I/O context.
Definition avformat.h:1443
AVDictionary * metadata
Metadata that applies to the whole file.
Definition avformat.h:1649
int flags
Flags modifying the (de)muxer behaviour.
Definition avformat.h:1553
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:1609
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1450
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition avformat.h:1687
const struct AVInputFormat * iformat
The input container format.
Definition avformat.h:1413
void * priv_data
Format private data.
Definition avformat.h:1429
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition avformat.h:1601
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1469
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:2027
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:1325
unsigned int nb_stream_indexes
Definition avformat.h:1330
unsigned int * stream_index
Definition avformat.h:1329
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition avformat.h:1328
AVDictionary * metadata
Definition avformat.h:1331
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:287
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition internal.h:185
int prefer_x_start
Definition hls.c:275
int first_timestamp_locked
Definition hls.c:280
int n_renditions
Definition hls.c:266
int http_multiple
Definition hls.c:290
AVDictionary * seg_format_opts
Definition hls.c:284
struct rendition ** renditions
Definition hls.c:267
int m3u8_hold_counters
Definition hls.c:273
AVIOInterruptCB * interrupt_callback
Definition hls.c:282
int seg_max_retry
Definition hls.c:292
int master_time_offset_flag
EXT-X-START was set by the Master Playlist.
Definition hls.c:276
AVDictionary * avio_opts
Definition hls.c:283
char * allowed_segment_extensions
Definition hls.c:286
int n_variants
Definition hls.c:261
int http_persistent
Definition hls.c:289
struct AVTreeNode * rendition_groups_by_id
Definition hls.c:270
struct variant ** variants
Definition hls.c:262
int extension_picky
Definition hls.c:287
AVFormatContext * ctx
Definition hls.c:260
int64_t cur_seq_no
Definition hls.c:272
int live_start_index
Definition hls.c:274
AVIOContext * playlist_pb
Definition hls.c:293
int first_packet
Definition hls.c:277
int max_reload
Definition hls.c:288
int n_playlists
Definition hls.c:263
struct playlist * first_timestamp_pls
Definition hls.c:279
int64_t cur_timestamp
Definition hls.c:281
HLSCryptoContext crypto_ctx
Definition hls.c:294
int n_rendition_groups
Definition hls.c:268
struct AVTreeNode * playlists_by_url
Definition hls.c:265
int64_t first_timestamp
Definition hls.c:278
struct playlist ** playlists
Definition hls.c:264
char * allowed_extensions
Definition hls.c:285
int http_seekable
Definition hls.c:291
struct rendition_group ** rendition_groups
Definition hls.c:269
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:543
char byterange[32]
Definition hls.c:544
char iv[35]
Definition hls.c:523
char uri[MAX_URL_SIZE]
Definition hls.c:521
char method[11]
Definition hls.c:522
int finished
Definition hls.c:136
int n_prev_cues
Definition hls.c:123
int id3_changed
Definition hls.c:172
int broken
Definition hls.c:145
int64_t cur_seg_offset
Definition hls.c:150
int prev_cue_pos
Definition hls.c:124
AVIOContext * input
Definition hls.c:111
int64_t cur_seq_no
Definition hls.c:146
AVPacket ** cur_cues
Definition hls.c:127
int n_renditions
Definition hls.c:188
int64_t first_read_seq_no
Definition hls.c:148
int64_t id3_mpegts_timestamp
Definition hls.c:166
int64_t last_cue_pts
Definition hls.c:121
uint64_t cues_discontinuity_seq
Definition hls.c:126
FFIOContext pb
Definition hls.c:110
struct rendition ** renditions
Definition hls.c:189
unsigned int init_sec_data_len
Definition hls.c:157
int64_t id3_offset
Definition hls.c:167
int64_t target_duration
Definition hls.c:138
int64_t last_load_time
Definition hls.c:151
int64_t last_seq_no
Definition hls.c:147
int seek_stream_index
Definition hls.c:182
int64_t input_next_end
Definition hls.c:116
HLSAudioSetupInfo audio_setup_info
Definition hls.c:175
int64_t start_seq_no
Definition hls.c:139
int is_subtitle
Definition hls.c:200
unsigned int id3_buf_size
Definition hls.c:169
int cues_unanchored
Definition hls.c:125
AVStream ** main_streams
Definition hls.c:133
AVIOContext * input_next
Definition hls.c:114
unsigned int init_sec_buf_read_offset
Definition hls.c:158
struct variant * last_variant
Definition hls.c:193
int64_t ts_offset
Definition hls.c:179
int64_t seek_timestamp
Definition hls.c:180
int64_t input_end
Definition hls.c:113
AVPacket * pkt
Definition hls.c:120
int input_read_done
Definition hls.c:112
AVFormatContext * ctx
Definition hls.c:119
uint8_t * init_sec_buf
Definition hls.c:155
struct segment * cur_init_section
Definition hls.c:154
uint8_t * id3_buf
Definition hls.c:168
int n_cur_cues
Definition hls.c:128
uint8_t key[16]
Definition hls.c:161
int id3_found
Definition hls.c:171
int index
Definition hls.c:118
int m3u8_hold_counters
Definition hls.c:149
int needed
Definition hls.c:144
int has_noheader_flag
Definition hls.c:129
char key_url[MAX_URL_SIZE]
Definition hls.c:160
int timed_id3_stream_index
Definition hls.c:208
struct rendition_group * last_group
Definition hls.c:194
int n_init_sections
Definition hls.c:198
int64_t start_time_offset
Definition hls.c:141
AVDictionary * timed_id3_metadata
Definition hls.c:207
struct segment ** segments
Definition hls.c:143
AVPacket ** prev_cues
Definition hls.c:122
int n_segments
Definition hls.c:142
struct segment ** init_sections
Definition hls.c:199
char url[MAX_URL_SIZE]
Definition hls.c:109
AVFormatContext * parent
Definition hls.c:117
int time_offset_flag
Definition hls.c:140
int seek_flags
Definition hls.c:181
int is_id3_timestamped
Definition hls.c:165
unsigned int init_sec_buf_size
Definition hls.c:156
int n_main_streams
Definition hls.c:134
int input_next_requested
Definition hls.c:115
ID3v2ExtraMeta * id3_deferred_extra
Definition hls.c:173
AVDictionary * id3_initial
Definition hls.c:170
enum PlaylistType type
Definition hls.c:137
struct rendition ** renditions
Definition hls.c:251
int n_playlists
Definition hls.c:246
int n_variants
Definition hls.c:254
struct variant ** variants
Definition hls.c:255
char id[MAX_FIELD_LEN]
Definition hls.c:243
int n_renditions
Definition hls.c:250
struct playlist ** playlists
Definition hls.c:247
enum AVMediaType type
Definition hls.c:242
char type[16]
Definition hls.c:624
char defaultr[4]
Definition hls.c:630
char name[MAX_FIELD_LEN]
Definition hls.c:629
char language[MAX_FIELD_LEN]
Definition hls.c:627
char forced[4]
Definition hls.c:631
char uri[MAX_URL_SIZE]
Definition hls.c:625
char group_id[MAX_FIELD_LEN]
Definition hls.c:626
char characteristics[MAX_CHARACTERISTICS_LEN]
Definition hls.c:632
char assoc_language[MAX_FIELD_LEN]
Definition hls.c:628
char name[MAX_FIELD_LEN]
Definition hls.c:221
enum AVMediaType type
Definition hls.c:218
char language[MAX_FIELD_LEN]
Definition hls.c:220
int disposition
Definition hls.c:222
struct playlist * playlist
Definition hls.c:219
Definition hls.c:80
struct segment * init_section
Definition hls.c:90
uint64_t discontinuity_seq
Definition hls.c:88
int64_t size
Definition hls.c:83
uint8_t iv[16]
Definition hls.c:87
char * url
Definition hls.c:84
int64_t duration
Definition hls.c:81
int64_t url_offset
Definition hls.c:82
char * key
Definition hls.c:85
enum KeyType key_type
Definition hls.c:86
char time_offset[MAX_FIELD_LEN]
Definition hls.c:610
char bandwidth[20]
Definition hls.c:468
char video[MAX_FIELD_LEN]
Definition hls.c:471
char subtitles[MAX_FIELD_LEN]
Definition hls.c:472
char audio[MAX_FIELD_LEN]
Definition hls.c:470
Definition hls.c:225
int bandwidth
Definition hls.c:226
char audio_group[MAX_FIELD_LEN]
Definition hls.c:232
int n_playlists
Definition hls.c:229
struct playlist ** playlists
Definition hls.c:230
char video_group[MAX_FIELD_LEN]
Definition hls.c:233
char subtitles_group[MAX_FIELD_LEN]
Definition hls.c:234
#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
static int ref[MAX_W *MAX_W]
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
static int64_t pts
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition tree.c:39
A tree container.
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]