FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hls.c
Date: 2026-10-08 05:59:33
Exec Total Coverage
Lines: 1104 1707 64.7%
Functions: 49 57 86.0%
Branches: 649 1284 50.5%

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