FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hls.c
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 1214 1807 67.2%
Functions: 57 65 87.7%
Branches: 716 1340 53.4%

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