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/opt.h" | ||
39 | #include "libavutil/dict.h" | ||
40 | #include "libavutil/time.h" | ||
41 | #include "avformat.h" | ||
42 | #include "demux.h" | ||
43 | #include "internal.h" | ||
44 | #include "avio_internal.h" | ||
45 | #include "id3v2.h" | ||
46 | #include "url.h" | ||
47 | |||
48 | #include "hls_sample_encryption.h" | ||
49 | |||
50 | #define INITIAL_BUFFER_SIZE 32768 | ||
51 | |||
52 | #define MAX_FIELD_LEN 64 | ||
53 | #define MAX_CHARACTERISTICS_LEN 512 | ||
54 | |||
55 | #define MPEG_TIME_BASE 90000 | ||
56 | #define MPEG_TIME_BASE_Q (AVRational){1, MPEG_TIME_BASE} | ||
57 | |||
58 | /* | ||
59 | * An apple http stream consists of a playlist with media segment files, | ||
60 | * played sequentially. There may be several playlists with the same | ||
61 | * video content, in different bandwidth variants, that are played in | ||
62 | * parallel (preferably only one bandwidth variant at a time). In this case, | ||
63 | * the user supplied the url to a main playlist that only lists the variant | ||
64 | * playlists. | ||
65 | * | ||
66 | * If the main playlist doesn't point at any variants, we still create | ||
67 | * one anonymous toplevel variant for this, to maintain the structure. | ||
68 | */ | ||
69 | |||
70 | enum KeyType { | ||
71 | KEY_NONE, | ||
72 | KEY_AES_128, | ||
73 | KEY_SAMPLE_AES | ||
74 | }; | ||
75 | |||
76 | struct segment { | ||
77 | int64_t duration; | ||
78 | int64_t url_offset; | ||
79 | int64_t size; | ||
80 | char *url; | ||
81 | char *key; | ||
82 | enum KeyType key_type; | ||
83 | uint8_t iv[16]; | ||
84 | /* associated Media Initialization Section, treated as a segment */ | ||
85 | struct segment *init_section; | ||
86 | }; | ||
87 | |||
88 | struct rendition; | ||
89 | |||
90 | enum PlaylistType { | ||
91 | PLS_TYPE_UNSPECIFIED, | ||
92 | PLS_TYPE_EVENT, | ||
93 | PLS_TYPE_VOD | ||
94 | }; | ||
95 | |||
96 | /* | ||
97 | * Each playlist has its own demuxer. If it currently is active, | ||
98 | * it has an open AVIOContext too, and potentially an AVPacket | ||
99 | * containing the next packet from this stream. | ||
100 | */ | ||
101 | struct playlist { | ||
102 | char url[MAX_URL_SIZE]; | ||
103 | FFIOContext pb; | ||
104 | uint8_t* read_buffer; | ||
105 | AVIOContext *input; | ||
106 | int input_read_done; | ||
107 | AVIOContext *input_next; | ||
108 | int input_next_requested; | ||
109 | AVFormatContext *parent; | ||
110 | int index; | ||
111 | AVFormatContext *ctx; | ||
112 | AVPacket *pkt; | ||
113 | int has_noheader_flag; | ||
114 | |||
115 | /* main demuxer streams associated with this playlist | ||
116 | * indexed by the subdemuxer stream indexes */ | ||
117 | AVStream **main_streams; | ||
118 | int n_main_streams; | ||
119 | |||
120 | int finished; | ||
121 | enum PlaylistType type; | ||
122 | int64_t target_duration; | ||
123 | int64_t start_seq_no; | ||
124 | int time_offset_flag; | ||
125 | int64_t start_time_offset; | ||
126 | int n_segments; | ||
127 | struct segment **segments; | ||
128 | int needed; | ||
129 | int broken; | ||
130 | int64_t cur_seq_no; | ||
131 | int64_t last_seq_no; | ||
132 | int m3u8_hold_counters; | ||
133 | int64_t cur_seg_offset; | ||
134 | int64_t last_load_time; | ||
135 | |||
136 | /* Currently active Media Initialization Section */ | ||
137 | struct segment *cur_init_section; | ||
138 | uint8_t *init_sec_buf; | ||
139 | unsigned int init_sec_buf_size; | ||
140 | unsigned int init_sec_data_len; | ||
141 | unsigned int init_sec_buf_read_offset; | ||
142 | |||
143 | char key_url[MAX_URL_SIZE]; | ||
144 | uint8_t key[16]; | ||
145 | |||
146 | /* ID3 timestamp handling (elementary audio streams have ID3 timestamps | ||
147 | * (and possibly other ID3 tags) in the beginning of each segment) */ | ||
148 | int is_id3_timestamped; /* -1: not yet known */ | ||
149 | int64_t id3_mpegts_timestamp; /* in mpegts tb */ | ||
150 | int64_t id3_offset; /* in stream original tb */ | ||
151 | uint8_t* id3_buf; /* temp buffer for id3 parsing */ | ||
152 | unsigned int id3_buf_size; | ||
153 | AVDictionary *id3_initial; /* data from first id3 tag */ | ||
154 | int id3_found; /* ID3 tag found at some point */ | ||
155 | int id3_changed; /* ID3 tag data has changed at some point */ | ||
156 | ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */ | ||
157 | |||
158 | HLSAudioSetupInfo audio_setup_info; | ||
159 | |||
160 | int64_t seek_timestamp; | ||
161 | int seek_flags; | ||
162 | int seek_stream_index; /* into subdemuxer stream array */ | ||
163 | |||
164 | /* Renditions associated with this playlist, if any. | ||
165 | * Alternative rendition playlists have a single rendition associated | ||
166 | * with them, and variant main Media Playlists may have | ||
167 | * multiple (playlist-less) renditions associated with them. */ | ||
168 | int n_renditions; | ||
169 | struct rendition **renditions; | ||
170 | |||
171 | /* Media Initialization Sections (EXT-X-MAP) associated with this | ||
172 | * playlist, if any. */ | ||
173 | int n_init_sections; | ||
174 | struct segment **init_sections; | ||
175 | }; | ||
176 | |||
177 | /* | ||
178 | * Renditions are e.g. alternative subtitle or audio streams. | ||
179 | * The rendition may either be an external playlist or it may be | ||
180 | * contained in the main Media Playlist of the variant (in which case | ||
181 | * playlist is NULL). | ||
182 | */ | ||
183 | struct rendition { | ||
184 | enum AVMediaType type; | ||
185 | struct playlist *playlist; | ||
186 | char group_id[MAX_FIELD_LEN]; | ||
187 | char language[MAX_FIELD_LEN]; | ||
188 | char name[MAX_FIELD_LEN]; | ||
189 | int disposition; | ||
190 | }; | ||
191 | |||
192 | struct variant { | ||
193 | int bandwidth; | ||
194 | |||
195 | /* every variant contains at least the main Media Playlist in index 0 */ | ||
196 | int n_playlists; | ||
197 | struct playlist **playlists; | ||
198 | |||
199 | char audio_group[MAX_FIELD_LEN]; | ||
200 | char video_group[MAX_FIELD_LEN]; | ||
201 | char subtitles_group[MAX_FIELD_LEN]; | ||
202 | }; | ||
203 | |||
204 | typedef struct HLSContext { | ||
205 | AVClass *class; | ||
206 | AVFormatContext *ctx; | ||
207 | int n_variants; | ||
208 | struct variant **variants; | ||
209 | int n_playlists; | ||
210 | struct playlist **playlists; | ||
211 | int n_renditions; | ||
212 | struct rendition **renditions; | ||
213 | |||
214 | int64_t cur_seq_no; | ||
215 | int m3u8_hold_counters; | ||
216 | int live_start_index; | ||
217 | int prefer_x_start; | ||
218 | int first_packet; | ||
219 | int64_t first_timestamp; | ||
220 | int64_t cur_timestamp; | ||
221 | AVIOInterruptCB *interrupt_callback; | ||
222 | AVDictionary *avio_opts; | ||
223 | AVDictionary *seg_format_opts; | ||
224 | char *allowed_extensions; | ||
225 | int max_reload; | ||
226 | int http_persistent; | ||
227 | int http_multiple; | ||
228 | int http_seekable; | ||
229 | int seg_max_retry; | ||
230 | AVIOContext *playlist_pb; | ||
231 | HLSCryptoContext crypto_ctx; | ||
232 | } HLSContext; | ||
233 | |||
234 | 12 | static void free_segment_dynarray(struct segment **segments, int n_segments) | |
235 | { | ||
236 | int i; | ||
237 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 12 times.
|
77 | for (i = 0; i < n_segments; i++) { |
238 | 65 | av_freep(&segments[i]->key); | |
239 | 65 | av_freep(&segments[i]->url); | |
240 | 65 | av_freep(&segments[i]); | |
241 | } | ||
242 | 12 | } | |
243 | |||
244 | 12 | static void free_segment_list(struct playlist *pls) | |
245 | { | ||
246 | 12 | free_segment_dynarray(pls->segments, pls->n_segments); | |
247 | 12 | av_freep(&pls->segments); | |
248 | 12 | pls->n_segments = 0; | |
249 | 12 | } | |
250 | |||
251 | 12 | static void free_init_section_list(struct playlist *pls) | |
252 | { | ||
253 | int i; | ||
254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | for (i = 0; i < pls->n_init_sections; i++) { |
255 | ✗ | av_freep(&pls->init_sections[i]->key); | |
256 | ✗ | av_freep(&pls->init_sections[i]->url); | |
257 | ✗ | av_freep(&pls->init_sections[i]); | |
258 | } | ||
259 | 12 | av_freep(&pls->init_sections); | |
260 | 12 | pls->n_init_sections = 0; | |
261 | 12 | } | |
262 | |||
263 | 12 | static void free_playlist_list(HLSContext *c) | |
264 | { | ||
265 | int i; | ||
266 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_playlists; i++) { |
267 | 12 | struct playlist *pls = c->playlists[i]; | |
268 | 12 | free_segment_list(pls); | |
269 | 12 | free_init_section_list(pls); | |
270 | 12 | av_freep(&pls->main_streams); | |
271 | 12 | av_freep(&pls->renditions); | |
272 | 12 | av_freep(&pls->id3_buf); | |
273 | 12 | av_dict_free(&pls->id3_initial); | |
274 | 12 | ff_id3v2_free_extra_meta(&pls->id3_deferred_extra); | |
275 | 12 | av_freep(&pls->init_sec_buf); | |
276 | 12 | av_packet_free(&pls->pkt); | |
277 | 12 | av_freep(&pls->pb.pub.buffer); | |
278 | 12 | ff_format_io_close(c->ctx, &pls->input); | |
279 | 12 | pls->input_read_done = 0; | |
280 | 12 | ff_format_io_close(c->ctx, &pls->input_next); | |
281 | 12 | pls->input_next_requested = 0; | |
282 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (pls->ctx) { |
283 | 12 | pls->ctx->pb = NULL; | |
284 | 12 | avformat_close_input(&pls->ctx); | |
285 | } | ||
286 | 12 | av_free(pls); | |
287 | } | ||
288 | 12 | av_freep(&c->playlists); | |
289 | 12 | c->n_playlists = 0; | |
290 | 12 | } | |
291 | |||
292 | 12 | static void free_variant_list(HLSContext *c) | |
293 | { | ||
294 | int i; | ||
295 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_variants; i++) { |
296 | 12 | struct variant *var = c->variants[i]; | |
297 | 12 | av_freep(&var->playlists); | |
298 | 12 | av_free(var); | |
299 | } | ||
300 | 12 | av_freep(&c->variants); | |
301 | 12 | c->n_variants = 0; | |
302 | 12 | } | |
303 | |||
304 | 12 | static void free_rendition_list(HLSContext *c) | |
305 | { | ||
306 | int i; | ||
307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | for (i = 0; i < c->n_renditions; i++) |
308 | ✗ | av_freep(&c->renditions[i]); | |
309 | 12 | av_freep(&c->renditions); | |
310 | 12 | c->n_renditions = 0; | |
311 | 12 | } | |
312 | |||
313 | 12 | static struct playlist *new_playlist(HLSContext *c, const char *url, | |
314 | const char *base) | ||
315 | { | ||
316 | 12 | struct playlist *pls = av_mallocz(sizeof(struct playlist)); | |
317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!pls) |
318 | ✗ | return NULL; | |
319 | 12 | pls->pkt = av_packet_alloc(); | |
320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!pls->pkt) { |
321 | ✗ | av_free(pls); | |
322 | ✗ | return NULL; | |
323 | } | ||
324 | 12 | ff_make_absolute_url(pls->url, sizeof(pls->url), base, url); | |
325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!pls->url[0]) { |
326 | ✗ | av_packet_free(&pls->pkt); | |
327 | ✗ | av_free(pls); | |
328 | ✗ | return NULL; | |
329 | } | ||
330 | 12 | pls->seek_timestamp = AV_NOPTS_VALUE; | |
331 | |||
332 | 12 | pls->is_id3_timestamped = -1; | |
333 | 12 | pls->id3_mpegts_timestamp = AV_NOPTS_VALUE; | |
334 | |||
335 | 12 | dynarray_add(&c->playlists, &c->n_playlists, pls); | |
336 | 12 | return pls; | |
337 | } | ||
338 | |||
339 | struct variant_info { | ||
340 | char bandwidth[20]; | ||
341 | /* variant group ids: */ | ||
342 | char audio[MAX_FIELD_LEN]; | ||
343 | char video[MAX_FIELD_LEN]; | ||
344 | char subtitles[MAX_FIELD_LEN]; | ||
345 | }; | ||
346 | |||
347 | 12 | static struct variant *new_variant(HLSContext *c, struct variant_info *info, | |
348 | const char *url, const char *base) | ||
349 | { | ||
350 | struct variant *var; | ||
351 | struct playlist *pls; | ||
352 | |||
353 | 12 | pls = new_playlist(c, url, base); | |
354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!pls) |
355 | ✗ | return NULL; | |
356 | |||
357 | 12 | var = av_mallocz(sizeof(struct variant)); | |
358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!var) |
359 | ✗ | return NULL; | |
360 | |||
361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (info) { |
362 | ✗ | var->bandwidth = atoi(info->bandwidth); | |
363 | ✗ | strcpy(var->audio_group, info->audio); | |
364 | ✗ | strcpy(var->video_group, info->video); | |
365 | ✗ | strcpy(var->subtitles_group, info->subtitles); | |
366 | } | ||
367 | |||
368 | 12 | dynarray_add(&c->variants, &c->n_variants, var); | |
369 | 12 | dynarray_add(&var->playlists, &var->n_playlists, pls); | |
370 | 12 | return var; | |
371 | } | ||
372 | |||
373 | ✗ | static void handle_variant_args(struct variant_info *info, const char *key, | |
374 | int key_len, char **dest, int *dest_len) | ||
375 | { | ||
376 | ✗ | if (!strncmp(key, "BANDWIDTH=", key_len)) { | |
377 | ✗ | *dest = info->bandwidth; | |
378 | ✗ | *dest_len = sizeof(info->bandwidth); | |
379 | ✗ | } else if (!strncmp(key, "AUDIO=", key_len)) { | |
380 | ✗ | *dest = info->audio; | |
381 | ✗ | *dest_len = sizeof(info->audio); | |
382 | ✗ | } else if (!strncmp(key, "VIDEO=", key_len)) { | |
383 | ✗ | *dest = info->video; | |
384 | ✗ | *dest_len = sizeof(info->video); | |
385 | ✗ | } else if (!strncmp(key, "SUBTITLES=", key_len)) { | |
386 | ✗ | *dest = info->subtitles; | |
387 | ✗ | *dest_len = sizeof(info->subtitles); | |
388 | } | ||
389 | ✗ | } | |
390 | |||
391 | struct key_info { | ||
392 | char uri[MAX_URL_SIZE]; | ||
393 | char method[11]; | ||
394 | char iv[35]; | ||
395 | }; | ||
396 | |||
397 | ✗ | static void handle_key_args(struct key_info *info, const char *key, | |
398 | int key_len, char **dest, int *dest_len) | ||
399 | { | ||
400 | ✗ | if (!strncmp(key, "METHOD=", key_len)) { | |
401 | ✗ | *dest = info->method; | |
402 | ✗ | *dest_len = sizeof(info->method); | |
403 | ✗ | } else if (!strncmp(key, "URI=", key_len)) { | |
404 | ✗ | *dest = info->uri; | |
405 | ✗ | *dest_len = sizeof(info->uri); | |
406 | ✗ | } else if (!strncmp(key, "IV=", key_len)) { | |
407 | ✗ | *dest = info->iv; | |
408 | ✗ | *dest_len = sizeof(info->iv); | |
409 | } | ||
410 | ✗ | } | |
411 | |||
412 | struct init_section_info { | ||
413 | char uri[MAX_URL_SIZE]; | ||
414 | char byterange[32]; | ||
415 | }; | ||
416 | |||
417 | ✗ | static struct segment *new_init_section(struct playlist *pls, | |
418 | struct init_section_info *info, | ||
419 | const char *url_base) | ||
420 | { | ||
421 | struct segment *sec; | ||
422 | ✗ | char tmp_str[MAX_URL_SIZE], *ptr = tmp_str; | |
423 | |||
424 | ✗ | if (!info->uri[0]) | |
425 | ✗ | return NULL; | |
426 | |||
427 | ✗ | sec = av_mallocz(sizeof(*sec)); | |
428 | ✗ | if (!sec) | |
429 | ✗ | return NULL; | |
430 | |||
431 | ✗ | if (!av_strncasecmp(info->uri, "data:", 5)) { | |
432 | ✗ | ptr = info->uri; | |
433 | } else { | ||
434 | ✗ | ff_make_absolute_url(tmp_str, sizeof(tmp_str), url_base, info->uri); | |
435 | ✗ | if (!tmp_str[0]) { | |
436 | ✗ | av_free(sec); | |
437 | ✗ | return NULL; | |
438 | } | ||
439 | } | ||
440 | ✗ | sec->url = av_strdup(ptr); | |
441 | ✗ | if (!sec->url) { | |
442 | ✗ | av_free(sec); | |
443 | ✗ | return NULL; | |
444 | } | ||
445 | |||
446 | ✗ | if (info->byterange[0]) { | |
447 | ✗ | sec->size = strtoll(info->byterange, NULL, 10); | |
448 | ✗ | ptr = strchr(info->byterange, '@'); | |
449 | ✗ | if (ptr) | |
450 | ✗ | sec->url_offset = strtoll(ptr+1, NULL, 10); | |
451 | } else { | ||
452 | /* the entire file is the init section */ | ||
453 | ✗ | sec->size = -1; | |
454 | } | ||
455 | |||
456 | ✗ | dynarray_add(&pls->init_sections, &pls->n_init_sections, sec); | |
457 | |||
458 | ✗ | return sec; | |
459 | } | ||
460 | |||
461 | ✗ | static void handle_init_section_args(struct init_section_info *info, const char *key, | |
462 | int key_len, char **dest, int *dest_len) | ||
463 | { | ||
464 | ✗ | if (!strncmp(key, "URI=", key_len)) { | |
465 | ✗ | *dest = info->uri; | |
466 | ✗ | *dest_len = sizeof(info->uri); | |
467 | ✗ | } else if (!strncmp(key, "BYTERANGE=", key_len)) { | |
468 | ✗ | *dest = info->byterange; | |
469 | ✗ | *dest_len = sizeof(info->byterange); | |
470 | } | ||
471 | ✗ | } | |
472 | |||
473 | struct rendition_info { | ||
474 | char type[16]; | ||
475 | char uri[MAX_URL_SIZE]; | ||
476 | char group_id[MAX_FIELD_LEN]; | ||
477 | char language[MAX_FIELD_LEN]; | ||
478 | char assoc_language[MAX_FIELD_LEN]; | ||
479 | char name[MAX_FIELD_LEN]; | ||
480 | char defaultr[4]; | ||
481 | char forced[4]; | ||
482 | char characteristics[MAX_CHARACTERISTICS_LEN]; | ||
483 | }; | ||
484 | |||
485 | ✗ | static struct rendition *new_rendition(HLSContext *c, struct rendition_info *info, | |
486 | const char *url_base) | ||
487 | { | ||
488 | struct rendition *rend; | ||
489 | ✗ | enum AVMediaType type = AVMEDIA_TYPE_UNKNOWN; | |
490 | char *characteristic; | ||
491 | char *chr_ptr; | ||
492 | char *saveptr; | ||
493 | |||
494 | ✗ | if (!strcmp(info->type, "AUDIO")) | |
495 | ✗ | type = AVMEDIA_TYPE_AUDIO; | |
496 | ✗ | else if (!strcmp(info->type, "VIDEO")) | |
497 | ✗ | type = AVMEDIA_TYPE_VIDEO; | |
498 | ✗ | else if (!strcmp(info->type, "SUBTITLES")) | |
499 | ✗ | type = AVMEDIA_TYPE_SUBTITLE; | |
500 | ✗ | else if (!strcmp(info->type, "CLOSED-CAPTIONS")) | |
501 | /* CLOSED-CAPTIONS is ignored since we do not support CEA-608 CC in | ||
502 | * AVC SEI RBSP anyway */ | ||
503 | ✗ | return NULL; | |
504 | |||
505 | ✗ | if (type == AVMEDIA_TYPE_UNKNOWN) { | |
506 | ✗ | av_log(c->ctx, AV_LOG_WARNING, "Can't support the type: %s\n", info->type); | |
507 | ✗ | return NULL; | |
508 | } | ||
509 | |||
510 | /* URI is mandatory for subtitles as per spec */ | ||
511 | ✗ | if (type == AVMEDIA_TYPE_SUBTITLE && !info->uri[0]) { | |
512 | ✗ | av_log(c->ctx, AV_LOG_ERROR, "The URI tag is REQUIRED for subtitle.\n"); | |
513 | ✗ | return NULL; | |
514 | } | ||
515 | |||
516 | /* TODO: handle subtitles (each segment has to parsed separately) */ | ||
517 | ✗ | if (c->ctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) | |
518 | ✗ | if (type == AVMEDIA_TYPE_SUBTITLE) { | |
519 | ✗ | av_log(c->ctx, AV_LOG_WARNING, "Can't support the subtitle(uri: %s)\n", info->uri); | |
520 | ✗ | return NULL; | |
521 | } | ||
522 | |||
523 | ✗ | rend = av_mallocz(sizeof(struct rendition)); | |
524 | ✗ | if (!rend) | |
525 | ✗ | return NULL; | |
526 | |||
527 | ✗ | dynarray_add(&c->renditions, &c->n_renditions, rend); | |
528 | |||
529 | ✗ | rend->type = type; | |
530 | ✗ | strcpy(rend->group_id, info->group_id); | |
531 | ✗ | strcpy(rend->language, info->language); | |
532 | ✗ | strcpy(rend->name, info->name); | |
533 | |||
534 | /* add the playlist if this is an external rendition */ | ||
535 | ✗ | if (info->uri[0]) { | |
536 | ✗ | rend->playlist = new_playlist(c, info->uri, url_base); | |
537 | ✗ | if (rend->playlist) | |
538 | ✗ | dynarray_add(&rend->playlist->renditions, | |
539 | &rend->playlist->n_renditions, rend); | ||
540 | } | ||
541 | |||
542 | ✗ | if (info->assoc_language[0]) { | |
543 | ✗ | int langlen = strlen(rend->language); | |
544 | ✗ | if (langlen < sizeof(rend->language) - 3) { | |
545 | ✗ | rend->language[langlen] = ','; | |
546 | ✗ | strncpy(rend->language + langlen + 1, info->assoc_language, | |
547 | sizeof(rend->language) - langlen - 2); | ||
548 | } | ||
549 | } | ||
550 | |||
551 | ✗ | if (!strcmp(info->defaultr, "YES")) | |
552 | ✗ | rend->disposition |= AV_DISPOSITION_DEFAULT; | |
553 | ✗ | if (!strcmp(info->forced, "YES")) | |
554 | ✗ | rend->disposition |= AV_DISPOSITION_FORCED; | |
555 | |||
556 | ✗ | chr_ptr = info->characteristics; | |
557 | ✗ | while ((characteristic = av_strtok(chr_ptr, ",", &saveptr))) { | |
558 | ✗ | if (!strcmp(characteristic, "public.accessibility.describes-music-and-sound")) | |
559 | ✗ | rend->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; | |
560 | ✗ | else if (!strcmp(characteristic, "public.accessibility.describes-video")) | |
561 | ✗ | rend->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; | |
562 | |||
563 | ✗ | chr_ptr = NULL; | |
564 | } | ||
565 | |||
566 | ✗ | return rend; | |
567 | } | ||
568 | |||
569 | ✗ | static void handle_rendition_args(struct rendition_info *info, const char *key, | |
570 | int key_len, char **dest, int *dest_len) | ||
571 | { | ||
572 | ✗ | if (!strncmp(key, "TYPE=", key_len)) { | |
573 | ✗ | *dest = info->type; | |
574 | ✗ | *dest_len = sizeof(info->type); | |
575 | ✗ | } else if (!strncmp(key, "URI=", key_len)) { | |
576 | ✗ | *dest = info->uri; | |
577 | ✗ | *dest_len = sizeof(info->uri); | |
578 | ✗ | } else if (!strncmp(key, "GROUP-ID=", key_len)) { | |
579 | ✗ | *dest = info->group_id; | |
580 | ✗ | *dest_len = sizeof(info->group_id); | |
581 | ✗ | } else if (!strncmp(key, "LANGUAGE=", key_len)) { | |
582 | ✗ | *dest = info->language; | |
583 | ✗ | *dest_len = sizeof(info->language); | |
584 | ✗ | } else if (!strncmp(key, "ASSOC-LANGUAGE=", key_len)) { | |
585 | ✗ | *dest = info->assoc_language; | |
586 | ✗ | *dest_len = sizeof(info->assoc_language); | |
587 | ✗ | } else if (!strncmp(key, "NAME=", key_len)) { | |
588 | ✗ | *dest = info->name; | |
589 | ✗ | *dest_len = sizeof(info->name); | |
590 | ✗ | } else if (!strncmp(key, "DEFAULT=", key_len)) { | |
591 | ✗ | *dest = info->defaultr; | |
592 | ✗ | *dest_len = sizeof(info->defaultr); | |
593 | ✗ | } else if (!strncmp(key, "FORCED=", key_len)) { | |
594 | ✗ | *dest = info->forced; | |
595 | ✗ | *dest_len = sizeof(info->forced); | |
596 | ✗ | } else if (!strncmp(key, "CHARACTERISTICS=", key_len)) { | |
597 | ✗ | *dest = info->characteristics; | |
598 | ✗ | *dest_len = sizeof(info->characteristics); | |
599 | } | ||
600 | /* | ||
601 | * ignored: | ||
602 | * - AUTOSELECT: client may autoselect based on e.g. system language | ||
603 | * - INSTREAM-ID: EIA-608 closed caption number ("CC1".."CC4") | ||
604 | */ | ||
605 | ✗ | } | |
606 | |||
607 | /* used by parse_playlist to allocate a new variant+playlist when the | ||
608 | * playlist is detected to be a Media Playlist (not Master Playlist) | ||
609 | * and we have no parent Master Playlist (parsing of which would have | ||
610 | * allocated the variant and playlist already) | ||
611 | * *pls == NULL => Master Playlist or parentless Media Playlist | ||
612 | * *pls != NULL => parented Media Playlist, playlist+variant allocated */ | ||
613 | 89 | static int ensure_playlist(HLSContext *c, struct playlist **pls, const char *url) | |
614 | { | ||
615 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 12 times.
|
89 | if (*pls) |
616 | 77 | return 0; | |
617 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (!new_variant(c, NULL, url, NULL)) |
618 | ✗ | return AVERROR(ENOMEM); | |
619 | 12 | *pls = c->playlists[c->n_playlists - 1]; | |
620 | 12 | return 0; | |
621 | } | ||
622 | |||
623 | ✗ | static int open_url_keepalive(AVFormatContext *s, AVIOContext **pb, | |
624 | const char *url, AVDictionary **options) | ||
625 | { | ||
626 | #if !CONFIG_HTTP_PROTOCOL | ||
627 | return AVERROR_PROTOCOL_NOT_FOUND; | ||
628 | #else | ||
629 | int ret; | ||
630 | ✗ | URLContext *uc = ffio_geturlcontext(*pb); | |
631 | ✗ | av_assert0(uc); | |
632 | ✗ | (*pb)->eof_reached = 0; | |
633 | ✗ | ret = ff_http_do_new_request2(uc, url, options); | |
634 | ✗ | if (ret < 0) { | |
635 | ✗ | ff_format_io_close(s, pb); | |
636 | } | ||
637 | ✗ | return ret; | |
638 | #endif | ||
639 | } | ||
640 | |||
641 | 61 | static int open_url(AVFormatContext *s, AVIOContext **pb, const char *url, | |
642 | AVDictionary **opts, AVDictionary *opts2, int *is_http_out) | ||
643 | { | ||
644 | 61 | HLSContext *c = s->priv_data; | |
645 | 61 | AVDictionary *tmp = NULL; | |
646 | 61 | const char *proto_name = NULL; | |
647 | int ret; | ||
648 | 61 | int is_http = 0; | |
649 | |||
650 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
|
61 | if (av_strstart(url, "crypto", NULL)) { |
651 | ✗ | if (url[6] == '+' || url[6] == ':') | |
652 | ✗ | proto_name = avio_find_protocol_name(url + 7); | |
653 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
|
61 | } else if (av_strstart(url, "data", NULL)) { |
654 | ✗ | if (url[4] == '+' || url[4] == ':') | |
655 | ✗ | proto_name = avio_find_protocol_name(url + 5); | |
656 | } | ||
657 | |||
658 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (!proto_name) |
659 | 61 | proto_name = avio_find_protocol_name(url); | |
660 | |||
661 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (!proto_name) |
662 | ✗ | return AVERROR_INVALIDDATA; | |
663 | |||
664 | // only http(s) & file are allowed | ||
665 |
1/2✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
|
61 | if (av_strstart(proto_name, "file", NULL)) { |
666 |
2/4✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 61 times.
|
61 | if (strcmp(c->allowed_extensions, "ALL") && !av_match_ext(url, c->allowed_extensions)) { |
667 | ✗ | av_log(s, AV_LOG_ERROR, | |
668 | "Filename extension of \'%s\' is not a common multimedia extension, blocked for security reasons.\n" | ||
669 | "If you wish to override this adjust allowed_extensions, you can set it to \'ALL\' to allow all\n", | ||
670 | url); | ||
671 | ✗ | return AVERROR_INVALIDDATA; | |
672 | } | ||
673 | ✗ | } else if (av_strstart(proto_name, "http", NULL)) { | |
674 | ✗ | is_http = 1; | |
675 | ✗ | } else if (av_strstart(proto_name, "data", NULL)) { | |
676 | ; | ||
677 | } else | ||
678 | ✗ | return AVERROR_INVALIDDATA; | |
679 | |||
680 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
61 | if (!strncmp(proto_name, url, strlen(proto_name)) && url[strlen(proto_name)] == ':') |
681 | ; | ||
682 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
61 | else if (av_strstart(url, "crypto", NULL) && !strncmp(proto_name, url + 7, strlen(proto_name)) && url[7 + strlen(proto_name)] == ':') |
683 | ; | ||
684 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
61 | else if (av_strstart(url, "data", NULL) && !strncmp(proto_name, url + 5, strlen(proto_name)) && url[5 + strlen(proto_name)] == ':') |
685 | ; | ||
686 |
2/4✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
|
61 | else if (strcmp(proto_name, "file") || !strncmp(url, "file,", 5)) |
687 | ✗ | return AVERROR_INVALIDDATA; | |
688 | |||
689 | 61 | av_dict_copy(&tmp, *opts, 0); | |
690 | 61 | av_dict_copy(&tmp, opts2, 0); | |
691 | |||
692 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
61 | if (is_http && c->http_persistent && *pb) { |
693 | ✗ | ret = open_url_keepalive(c->ctx, pb, url, &tmp); | |
694 | ✗ | if (ret == AVERROR_EXIT) { | |
695 | ✗ | av_dict_free(&tmp); | |
696 | ✗ | return ret; | |
697 | ✗ | } else if (ret < 0) { | |
698 | ✗ | if (ret != AVERROR_EOF) | |
699 | ✗ | av_log(s, AV_LOG_WARNING, | |
700 | "keepalive request failed for '%s' with error: '%s' when opening url, retrying with new connection\n", | ||
701 | ✗ | url, av_err2str(ret)); | |
702 | ✗ | av_dict_copy(&tmp, *opts, 0); | |
703 | ✗ | av_dict_copy(&tmp, opts2, 0); | |
704 | ✗ | ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); | |
705 | } | ||
706 | } else { | ||
707 | 61 | ret = s->io_open(s, pb, url, AVIO_FLAG_READ, &tmp); | |
708 | } | ||
709 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (ret >= 0) { |
710 | // update cookies on http response with setcookies. | ||
711 | 61 | char *new_cookies = NULL; | |
712 | |||
713 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (!(s->flags & AVFMT_FLAG_CUSTOM_IO)) |
714 | 61 | av_opt_get(*pb, "cookies", AV_OPT_SEARCH_CHILDREN, (uint8_t**)&new_cookies); | |
715 | |||
716 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (new_cookies) |
717 | ✗ | av_dict_set(opts, "cookies", new_cookies, AV_DICT_DONT_STRDUP_VAL); | |
718 | } | ||
719 | |||
720 | 61 | av_dict_free(&tmp); | |
721 | |||
722 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (is_http_out) |
723 | 61 | *is_http_out = is_http; | |
724 | |||
725 | 61 | return ret; | |
726 | } | ||
727 | |||
728 | 12 | static int parse_playlist(HLSContext *c, const char *url, | |
729 | struct playlist *pls, AVIOContext *in) | ||
730 | { | ||
731 | 12 | int ret = 0, is_segment = 0, is_variant = 0; | |
732 | 12 | int64_t duration = 0; | |
733 | 12 | enum KeyType key_type = KEY_NONE; | |
734 | 12 | uint8_t iv[16] = ""; | |
735 | 12 | int has_iv = 0; | |
736 | 12 | char key[MAX_URL_SIZE] = ""; | |
737 | char line[MAX_URL_SIZE]; | ||
738 | const char *ptr; | ||
739 | 12 | int close_in = 0; | |
740 | 12 | int64_t seg_offset = 0; | |
741 | 12 | int64_t seg_size = -1; | |
742 | 12 | uint8_t *new_url = NULL; | |
743 | struct variant_info variant_info; | ||
744 | char tmp_str[MAX_URL_SIZE]; | ||
745 | 12 | struct segment *cur_init_section = NULL; | |
746 | 12 | int is_http = av_strstart(url, "http", NULL); | |
747 | 12 | struct segment **prev_segments = NULL; | |
748 | 12 | int prev_n_segments = 0; | |
749 | 12 | int64_t prev_start_seq_no = -1; | |
750 | |||
751 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 12 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.
|
12 | if (is_http && !in && c->http_persistent && c->playlist_pb) { |
752 | ✗ | in = c->playlist_pb; | |
753 | ✗ | ret = open_url_keepalive(c->ctx, &c->playlist_pb, url, NULL); | |
754 | ✗ | if (ret == AVERROR_EXIT) { | |
755 | ✗ | return ret; | |
756 | ✗ | } else if (ret < 0) { | |
757 | ✗ | if (ret != AVERROR_EOF) | |
758 | ✗ | av_log(c->ctx, AV_LOG_WARNING, | |
759 | "keepalive request failed for '%s' with error: '%s' when parsing playlist\n", | ||
760 | ✗ | url, av_err2str(ret)); | |
761 | ✗ | in = NULL; | |
762 | } | ||
763 | } | ||
764 | |||
765 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!in) { |
766 | ✗ | AVDictionary *opts = NULL; | |
767 | ✗ | av_dict_copy(&opts, c->avio_opts, 0); | |
768 | |||
769 | ✗ | if (c->http_persistent) | |
770 | ✗ | av_dict_set(&opts, "multiple_requests", "1", 0); | |
771 | |||
772 | ✗ | ret = c->ctx->io_open(c->ctx, &in, url, AVIO_FLAG_READ, &opts); | |
773 | ✗ | av_dict_free(&opts); | |
774 | ✗ | if (ret < 0) | |
775 | ✗ | return ret; | |
776 | |||
777 | ✗ | if (is_http && c->http_persistent) | |
778 | ✗ | c->playlist_pb = in; | |
779 | else | ||
780 | ✗ | close_in = 1; | |
781 | } | ||
782 | |||
783 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (av_opt_get(in, "location", AV_OPT_SEARCH_CHILDREN, &new_url) >= 0) |
784 | ✗ | url = new_url; | |
785 | |||
786 | 12 | ff_get_chomp_line(in, line, sizeof(line)); | |
787 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (strcmp(line, "#EXTM3U")) { |
788 | ✗ | ret = AVERROR_INVALIDDATA; | |
789 | ✗ | goto fail; | |
790 | } | ||
791 | |||
792 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls) { |
793 | ✗ | prev_start_seq_no = pls->start_seq_no; | |
794 | ✗ | prev_segments = pls->segments; | |
795 | ✗ | prev_n_segments = pls->n_segments; | |
796 | ✗ | pls->segments = NULL; | |
797 | ✗ | pls->n_segments = 0; | |
798 | |||
799 | ✗ | pls->finished = 0; | |
800 | ✗ | pls->type = PLS_TYPE_UNSPECIFIED; | |
801 | } | ||
802 |
2/2✓ Branch 1 taken 213 times.
✓ Branch 2 taken 12 times.
|
225 | while (!avio_feof(in)) { |
803 | 213 | ff_get_chomp_line(in, line, sizeof(line)); | |
804 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
|
213 | if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) { |
805 | ✗ | is_variant = 1; | |
806 | ✗ | memset(&variant_info, 0, sizeof(variant_info)); | |
807 | ✗ | ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args, | |
808 | &variant_info); | ||
809 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
|
213 | } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) { |
810 | ✗ | struct key_info info = {{0}}; | |
811 | ✗ | ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args, | |
812 | &info); | ||
813 | ✗ | key_type = KEY_NONE; | |
814 | ✗ | has_iv = 0; | |
815 | ✗ | if (!strcmp(info.method, "AES-128")) | |
816 | ✗ | key_type = KEY_AES_128; | |
817 | ✗ | if (!strcmp(info.method, "SAMPLE-AES")) | |
818 | ✗ | key_type = KEY_SAMPLE_AES; | |
819 | ✗ | if (!av_strncasecmp(info.iv, "0x", 2)) { | |
820 | ✗ | ff_hex_to_data(iv, info.iv + 2); | |
821 | ✗ | has_iv = 1; | |
822 | } | ||
823 | ✗ | av_strlcpy(key, info.uri, sizeof(key)); | |
824 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
|
213 | } else if (av_strstart(line, "#EXT-X-MEDIA:", &ptr)) { |
825 | ✗ | struct rendition_info info = {{0}}; | |
826 | ✗ | ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_rendition_args, | |
827 | &info); | ||
828 | ✗ | new_rendition(c, &info, url); | |
829 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 201 times.
|
213 | } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) { |
830 | int64_t t; | ||
831 | 12 | ret = ensure_playlist(c, &pls, url); | |
832 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
833 | ✗ | goto fail; | |
834 | 12 | t = strtoll(ptr, NULL, 10); | |
835 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
12 | if (t < 0 || t >= INT64_MAX / AV_TIME_BASE) { |
836 | ✗ | ret = AVERROR_INVALIDDATA; | |
837 | ✗ | goto fail; | |
838 | } | ||
839 | 12 | pls->target_duration = t * AV_TIME_BASE; | |
840 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 189 times.
|
201 | } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) { |
841 | uint64_t seq_no; | ||
842 | 12 | ret = ensure_playlist(c, &pls, url); | |
843 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
844 | ✗ | goto fail; | |
845 | 12 | seq_no = strtoull(ptr, NULL, 10); | |
846 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (seq_no > INT64_MAX/2) { |
847 | ✗ | av_log(c->ctx, AV_LOG_DEBUG, "MEDIA-SEQUENCE higher than " | |
848 | "INT64_MAX/2, mask out the highest bit\n"); | ||
849 | ✗ | seq_no &= INT64_MAX/2; | |
850 | } | ||
851 | 12 | pls->start_seq_no = seq_no; | |
852 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
|
189 | } else if (av_strstart(line, "#EXT-X-PLAYLIST-TYPE:", &ptr)) { |
853 | ✗ | ret = ensure_playlist(c, &pls, url); | |
854 | ✗ | if (ret < 0) | |
855 | ✗ | goto fail; | |
856 | ✗ | if (!strcmp(ptr, "EVENT")) | |
857 | ✗ | pls->type = PLS_TYPE_EVENT; | |
858 | ✗ | else if (!strcmp(ptr, "VOD")) | |
859 | ✗ | pls->type = PLS_TYPE_VOD; | |
860 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
|
189 | } else if (av_strstart(line, "#EXT-X-MAP:", &ptr)) { |
861 | ✗ | struct init_section_info info = {{0}}; | |
862 | ✗ | ret = ensure_playlist(c, &pls, url); | |
863 | ✗ | if (ret < 0) | |
864 | ✗ | goto fail; | |
865 | ✗ | ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_init_section_args, | |
866 | &info); | ||
867 | ✗ | cur_init_section = new_init_section(pls, &info, url); | |
868 | ✗ | if (!cur_init_section) { | |
869 | ✗ | ret = AVERROR(ENOMEM); | |
870 | ✗ | goto fail; | |
871 | } | ||
872 | ✗ | cur_init_section->key_type = key_type; | |
873 | ✗ | if (has_iv) { | |
874 | ✗ | memcpy(cur_init_section->iv, iv, sizeof(iv)); | |
875 | } else { | ||
876 | ✗ | int64_t seq = pls->start_seq_no + pls->n_segments; | |
877 | ✗ | memset(cur_init_section->iv, 0, sizeof(cur_init_section->iv)); | |
878 | ✗ | AV_WB64(cur_init_section->iv + 8, seq); | |
879 | } | ||
880 | |||
881 | ✗ | if (key_type != KEY_NONE) { | |
882 | ✗ | ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key); | |
883 | ✗ | if (!tmp_str[0]) { | |
884 | ✗ | av_free(cur_init_section); | |
885 | ✗ | ret = AVERROR_INVALIDDATA; | |
886 | ✗ | goto fail; | |
887 | } | ||
888 | ✗ | cur_init_section->key = av_strdup(tmp_str); | |
889 | ✗ | if (!cur_init_section->key) { | |
890 | ✗ | av_free(cur_init_section); | |
891 | ✗ | ret = AVERROR(ENOMEM); | |
892 | ✗ | goto fail; | |
893 | } | ||
894 | } else { | ||
895 | ✗ | cur_init_section->key = NULL; | |
896 | } | ||
897 | |||
898 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
|
189 | } else if (av_strstart(line, "#EXT-X-START:", &ptr)) { |
899 | ✗ | const char *time_offset_value = NULL; | |
900 | ✗ | ret = ensure_playlist(c, &pls, url); | |
901 | ✗ | if (ret < 0) { | |
902 | ✗ | goto fail; | |
903 | } | ||
904 | ✗ | if (av_strstart(ptr, "TIME-OFFSET=", &time_offset_value)) { | |
905 | ✗ | float offset = strtof(time_offset_value, NULL); | |
906 | ✗ | pls->start_time_offset = offset * AV_TIME_BASE; | |
907 | ✗ | pls->time_offset_flag = 1; | |
908 | } else { | ||
909 | ✗ | av_log(c->ctx, AV_LOG_WARNING, "#EXT-X-START value is" | |
910 | "invalid, it will be ignored"); | ||
911 | ✗ | continue; | |
912 | } | ||
913 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 178 times.
|
189 | } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) { |
914 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (pls) |
915 | 11 | pls->finished = 1; | |
916 |
2/2✓ Branch 1 taken 65 times.
✓ Branch 2 taken 113 times.
|
178 | } else if (av_strstart(line, "#EXTINF:", &ptr)) { |
917 | 65 | is_segment = 1; | |
918 | 65 | duration = atof(ptr) * AV_TIME_BASE; | |
919 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 93 times.
|
113 | } else if (av_strstart(line, "#EXT-X-BYTERANGE:", &ptr)) { |
920 | 20 | seg_size = strtoll(ptr, NULL, 10); | |
921 | 20 | ptr = strchr(ptr, '@'); | |
922 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (ptr) |
923 | 20 | seg_offset = strtoll(ptr+1, NULL, 10); | |
924 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 77 times.
|
93 | } else if (av_strstart(line, "#", NULL)) { |
925 | 16 | av_log(c->ctx, AV_LOG_INFO, "Skip ('%s')\n", line); | |
926 | 16 | continue; | |
927 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 12 times.
|
77 | } else if (line[0]) { |
928 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (is_variant) { |
929 | ✗ | if (!new_variant(c, &variant_info, line, url)) { | |
930 | ✗ | ret = AVERROR(ENOMEM); | |
931 | ✗ | goto fail; | |
932 | } | ||
933 | ✗ | is_variant = 0; | |
934 | } | ||
935 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | if (is_segment) { |
936 | struct segment *seg; | ||
937 | 65 | ret = ensure_playlist(c, &pls, url); | |
938 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (ret < 0) |
939 | ✗ | goto fail; | |
940 | 65 | seg = av_malloc(sizeof(struct segment)); | |
941 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (!seg) { |
942 | ✗ | ret = AVERROR(ENOMEM); | |
943 | ✗ | goto fail; | |
944 | } | ||
945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (has_iv) { |
946 | ✗ | memcpy(seg->iv, iv, sizeof(iv)); | |
947 | } else { | ||
948 | 65 | uint64_t seq = pls->start_seq_no + (uint64_t)pls->n_segments; | |
949 | 65 | memset(seg->iv, 0, sizeof(seg->iv)); | |
950 | 65 | AV_WB64(seg->iv + 8, seq); | |
951 | } | ||
952 | |||
953 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (key_type != KEY_NONE) { |
954 | ✗ | ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, key); | |
955 | ✗ | if (!tmp_str[0]) { | |
956 | ✗ | ret = AVERROR_INVALIDDATA; | |
957 | ✗ | av_free(seg); | |
958 | ✗ | goto fail; | |
959 | } | ||
960 | ✗ | seg->key = av_strdup(tmp_str); | |
961 | ✗ | if (!seg->key) { | |
962 | ✗ | av_free(seg); | |
963 | ✗ | ret = AVERROR(ENOMEM); | |
964 | ✗ | goto fail; | |
965 | } | ||
966 | } else { | ||
967 | 65 | seg->key = NULL; | |
968 | } | ||
969 | |||
970 | 65 | ff_make_absolute_url(tmp_str, sizeof(tmp_str), url, line); | |
971 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (!tmp_str[0]) { |
972 | ✗ | ret = AVERROR_INVALIDDATA; | |
973 | ✗ | if (seg->key) | |
974 | ✗ | av_free(seg->key); | |
975 | ✗ | av_free(seg); | |
976 | ✗ | goto fail; | |
977 | } | ||
978 | 65 | seg->url = av_strdup(tmp_str); | |
979 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (!seg->url) { |
980 | ✗ | av_free(seg->key); | |
981 | ✗ | av_free(seg); | |
982 | ✗ | ret = AVERROR(ENOMEM); | |
983 | ✗ | goto fail; | |
984 | } | ||
985 | |||
986 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (duration < 0.001 * AV_TIME_BASE) { |
987 | ✗ | av_log(c->ctx, AV_LOG_WARNING, "Cannot get correct #EXTINF value of segment %s," | |
988 | " set to default value to 1ms.\n", seg->url); | ||
989 | ✗ | duration = 0.001 * AV_TIME_BASE; | |
990 | } | ||
991 | 65 | seg->duration = duration; | |
992 | 65 | seg->key_type = key_type; | |
993 | 65 | dynarray_add(&pls->segments, &pls->n_segments, seg); | |
994 | 65 | is_segment = 0; | |
995 | |||
996 | 65 | seg->size = seg_size; | |
997 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 45 times.
|
65 | if (seg_size >= 0) { |
998 | 20 | seg->url_offset = seg_offset; | |
999 | 20 | seg_offset += seg_size; | |
1000 | 20 | seg_size = -1; | |
1001 | } else { | ||
1002 | 45 | seg->url_offset = 0; | |
1003 | 45 | seg_offset = 0; | |
1004 | } | ||
1005 | |||
1006 | 65 | seg->init_section = cur_init_section; | |
1007 | } | ||
1008 | } | ||
1009 | } | ||
1010 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (prev_segments) { |
1011 | ✗ | if (pls->start_seq_no > prev_start_seq_no && c->first_timestamp != AV_NOPTS_VALUE) { | |
1012 | ✗ | int64_t prev_timestamp = c->first_timestamp; | |
1013 | int i; | ||
1014 | ✗ | int64_t diff = pls->start_seq_no - prev_start_seq_no; | |
1015 | ✗ | for (i = 0; i < prev_n_segments && i < diff; i++) { | |
1016 | ✗ | c->first_timestamp += prev_segments[i]->duration; | |
1017 | } | ||
1018 | ✗ | av_log(c->ctx, AV_LOG_DEBUG, "Media sequence change (%"PRId64" -> %"PRId64")" | |
1019 | " reflected in first_timestamp: %"PRId64" -> %"PRId64"\n", | ||
1020 | ✗ | prev_start_seq_no, pls->start_seq_no, | |
1021 | prev_timestamp, c->first_timestamp); | ||
1022 | ✗ | } else if (pls->start_seq_no < prev_start_seq_no) { | |
1023 | ✗ | av_log(c->ctx, AV_LOG_WARNING, "Media sequence changed unexpectedly: %"PRId64" -> %"PRId64"\n", | |
1024 | ✗ | prev_start_seq_no, pls->start_seq_no); | |
1025 | } | ||
1026 | ✗ | free_segment_dynarray(prev_segments, prev_n_segments); | |
1027 | ✗ | av_freep(&prev_segments); | |
1028 | } | ||
1029 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls) |
1030 | 12 | pls->last_load_time = av_gettime_relative(); | |
1031 | |||
1032 | ✗ | fail: | |
1033 | 12 | av_free(new_url); | |
1034 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (close_in) |
1035 | ✗ | ff_format_io_close(c->ctx, &in); | |
1036 | 12 | c->ctx->ctx_flags = c->ctx->ctx_flags & ~(unsigned)AVFMTCTX_UNSEEKABLE; | |
1037 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | if (!c->n_variants || !c->variants[0]->n_playlists || |
1038 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
|
12 | !(c->variants[0]->playlists[0]->finished || |
1039 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | c->variants[0]->playlists[0]->type == PLS_TYPE_EVENT)) |
1040 | 1 | c->ctx->ctx_flags |= AVFMTCTX_UNSEEKABLE; | |
1041 | 12 | return ret; | |
1042 | } | ||
1043 | |||
1044 | 6784 | static struct segment *current_segment(struct playlist *pls) | |
1045 | { | ||
1046 | 6784 | int64_t n = pls->cur_seq_no - pls->start_seq_no; | |
1047 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6782 times.
|
6784 | if (n >= pls->n_segments) |
1048 | 2 | return NULL; | |
1049 | 6782 | return pls->segments[n]; | |
1050 | } | ||
1051 | |||
1052 | 366 | static struct segment *next_segment(struct playlist *pls) | |
1053 | { | ||
1054 | 366 | int64_t n = pls->cur_seq_no - pls->start_seq_no + 1; | |
1055 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 294 times.
|
366 | if (n >= pls->n_segments) |
1056 | 72 | return NULL; | |
1057 | 294 | return pls->segments[n]; | |
1058 | } | ||
1059 | |||
1060 | 366 | static int read_from_url(struct playlist *pls, struct segment *seg, | |
1061 | uint8_t *buf, int buf_size) | ||
1062 | { | ||
1063 | int ret; | ||
1064 | |||
1065 | /* limit read if the segment was only a part of a file */ | ||
1066 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 266 times.
|
366 | if (seg->size >= 0) |
1067 | 100 | buf_size = FFMIN(buf_size, seg->size - pls->cur_seg_offset); | |
1068 | |||
1069 | 366 | ret = avio_read(pls->input, buf, buf_size); | |
1070 |
2/2✓ Branch 0 taken 307 times.
✓ Branch 1 taken 59 times.
|
366 | if (ret > 0) |
1071 | 307 | pls->cur_seg_offset += ret; | |
1072 | |||
1073 | 366 | return ret; | |
1074 | } | ||
1075 | |||
1076 | /* Parse the raw ID3 data and pass contents to caller */ | ||
1077 | ✗ | static void parse_id3(AVFormatContext *s, AVIOContext *pb, | |
1078 | AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info, | ||
1079 | ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta) | ||
1080 | { | ||
1081 | static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp"; | ||
1082 | static const char id3_priv_owner_audio_setup[] = "com.apple.streaming.audioDescription"; | ||
1083 | ID3v2ExtraMeta *meta; | ||
1084 | |||
1085 | ✗ | ff_id3v2_read_dict(pb, metadata, ID3v2_DEFAULT_MAGIC, extra_meta); | |
1086 | ✗ | for (meta = *extra_meta; meta; meta = meta->next) { | |
1087 | ✗ | if (!strcmp(meta->tag, "PRIV")) { | |
1088 | ✗ | ID3v2ExtraMetaPRIV *priv = &meta->data.priv; | |
1089 | ✗ | if (priv->datasize == 8 && !av_strncasecmp(priv->owner, id3_priv_owner_ts, 44)) { | |
1090 | /* 33-bit MPEG timestamp */ | ||
1091 | ✗ | int64_t ts = AV_RB64(priv->data); | |
1092 | ✗ | av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts); | |
1093 | ✗ | if ((ts & ~((1ULL << 33) - 1)) == 0) | |
1094 | ✗ | *dts = ts; | |
1095 | else | ||
1096 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts); | |
1097 | ✗ | } else if (priv->datasize >= 8 && !av_strncasecmp(priv->owner, id3_priv_owner_audio_setup, 36)) { | |
1098 | ✗ | ff_hls_senc_read_audio_setup_info(audio_setup_info, priv->data, priv->datasize); | |
1099 | } | ||
1100 | ✗ | } else if (!strcmp(meta->tag, "APIC") && apic) | |
1101 | ✗ | *apic = &meta->data.apic; | |
1102 | } | ||
1103 | ✗ | } | |
1104 | |||
1105 | /* Check if the ID3 metadata contents have changed */ | ||
1106 | ✗ | static int id3_has_changed_values(struct playlist *pls, AVDictionary *metadata, | |
1107 | ID3v2ExtraMetaAPIC *apic) | ||
1108 | { | ||
1109 | ✗ | const AVDictionaryEntry *entry = NULL; | |
1110 | const AVDictionaryEntry *oldentry; | ||
1111 | /* check that no keys have changed values */ | ||
1112 | ✗ | while ((entry = av_dict_iterate(metadata, entry))) { | |
1113 | ✗ | oldentry = av_dict_get(pls->id3_initial, entry->key, NULL, AV_DICT_MATCH_CASE); | |
1114 | ✗ | if (!oldentry || strcmp(oldentry->value, entry->value) != 0) | |
1115 | ✗ | return 1; | |
1116 | } | ||
1117 | |||
1118 | /* check if apic appeared */ | ||
1119 | ✗ | if (apic && (pls->ctx->nb_streams != 2 || !pls->ctx->streams[1]->attached_pic.data)) | |
1120 | ✗ | return 1; | |
1121 | |||
1122 | ✗ | if (apic) { | |
1123 | ✗ | int size = pls->ctx->streams[1]->attached_pic.size; | |
1124 | ✗ | if (size != apic->buf->size - AV_INPUT_BUFFER_PADDING_SIZE) | |
1125 | ✗ | return 1; | |
1126 | |||
1127 | ✗ | if (memcmp(apic->buf->data, pls->ctx->streams[1]->attached_pic.data, size) != 0) | |
1128 | ✗ | return 1; | |
1129 | } | ||
1130 | |||
1131 | ✗ | return 0; | |
1132 | } | ||
1133 | |||
1134 | /* Parse ID3 data and handle the found data */ | ||
1135 | ✗ | static void handle_id3(AVIOContext *pb, struct playlist *pls) | |
1136 | { | ||
1137 | ✗ | AVDictionary *metadata = NULL; | |
1138 | ✗ | ID3v2ExtraMetaAPIC *apic = NULL; | |
1139 | ✗ | ID3v2ExtraMeta *extra_meta = NULL; | |
1140 | ✗ | int64_t timestamp = AV_NOPTS_VALUE; | |
1141 | |||
1142 | ✗ | parse_id3(pls->ctx, pb, &metadata, ×tamp, &pls->audio_setup_info, &apic, &extra_meta); | |
1143 | |||
1144 | ✗ | if (timestamp != AV_NOPTS_VALUE) { | |
1145 | ✗ | pls->id3_mpegts_timestamp = timestamp; | |
1146 | ✗ | pls->id3_offset = 0; | |
1147 | } | ||
1148 | |||
1149 | ✗ | if (!pls->id3_found) { | |
1150 | /* initial ID3 tags */ | ||
1151 | ✗ | av_assert0(!pls->id3_deferred_extra); | |
1152 | ✗ | pls->id3_found = 1; | |
1153 | |||
1154 | /* get picture attachment and set text metadata */ | ||
1155 | ✗ | if (pls->ctx->nb_streams) | |
1156 | ✗ | ff_id3v2_parse_apic(pls->ctx, extra_meta); | |
1157 | else | ||
1158 | /* demuxer not yet opened, defer picture attachment */ | ||
1159 | ✗ | pls->id3_deferred_extra = extra_meta; | |
1160 | |||
1161 | ✗ | ff_id3v2_parse_priv_dict(&metadata, extra_meta); | |
1162 | ✗ | av_dict_copy(&pls->ctx->metadata, metadata, 0); | |
1163 | ✗ | pls->id3_initial = metadata; | |
1164 | |||
1165 | } else { | ||
1166 | ✗ | if (!pls->id3_changed && id3_has_changed_values(pls, metadata, apic)) { | |
1167 | ✗ | avpriv_report_missing_feature(pls->parent, "Changing ID3 metadata in HLS audio elementary stream"); | |
1168 | ✗ | pls->id3_changed = 1; | |
1169 | } | ||
1170 | ✗ | av_dict_free(&metadata); | |
1171 | } | ||
1172 | |||
1173 | ✗ | if (!pls->id3_deferred_extra) | |
1174 | ✗ | ff_id3v2_free_extra_meta(&extra_meta); | |
1175 | ✗ | } | |
1176 | |||
1177 | 12 | static void intercept_id3(struct playlist *pls, uint8_t *buf, | |
1178 | int buf_size, int *len) | ||
1179 | { | ||
1180 | /* intercept id3 tags, we do not want to pass them to the raw | ||
1181 | * demuxer on all segment switches */ | ||
1182 | int bytes; | ||
1183 | 12 | int id3_buf_pos = 0; | |
1184 | 12 | int fill_buf = 0; | |
1185 | 12 | struct segment *seg = current_segment(pls); | |
1186 | |||
1187 | /* gather all the id3 tags */ | ||
1188 | while (1) { | ||
1189 | /* see if we can retrieve enough data for ID3 header */ | ||
1190 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | if (*len < ID3v2_HEADER_SIZE && buf_size >= ID3v2_HEADER_SIZE) { |
1191 | ✗ | bytes = read_from_url(pls, seg, buf + *len, ID3v2_HEADER_SIZE - *len); | |
1192 | ✗ | if (bytes > 0) { | |
1193 | |||
1194 | ✗ | if (bytes == ID3v2_HEADER_SIZE - *len) | |
1195 | /* no EOF yet, so fill the caller buffer again after | ||
1196 | * we have stripped the ID3 tags */ | ||
1197 | ✗ | fill_buf = 1; | |
1198 | |||
1199 | ✗ | *len += bytes; | |
1200 | |||
1201 | ✗ | } else if (*len <= 0) { | |
1202 | /* error/EOF */ | ||
1203 | ✗ | *len = bytes; | |
1204 | ✗ | fill_buf = 0; | |
1205 | } | ||
1206 | } | ||
1207 | |||
1208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (*len < ID3v2_HEADER_SIZE) |
1209 | ✗ | break; | |
1210 | |||
1211 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) { |
1212 | ✗ | int64_t maxsize = seg->size >= 0 ? seg->size : 1024*1024; | |
1213 | ✗ | int taglen = ff_id3v2_tag_len(buf); | |
1214 | ✗ | int tag_got_bytes = FFMIN(taglen, *len); | |
1215 | ✗ | int remaining = taglen - tag_got_bytes; | |
1216 | |||
1217 | ✗ | if (taglen > maxsize) { | |
1218 | ✗ | av_log(pls->parent, AV_LOG_ERROR, "Too large HLS ID3 tag (%d > %"PRId64" bytes)\n", | |
1219 | taglen, maxsize); | ||
1220 | ✗ | break; | |
1221 | } | ||
1222 | |||
1223 | /* | ||
1224 | * Copy the id3 tag to our temporary id3 buffer. | ||
1225 | * We could read a small id3 tag directly without memcpy, but | ||
1226 | * we would still need to copy the large tags, and handling | ||
1227 | * both of those cases together with the possibility for multiple | ||
1228 | * tags would make the handling a bit complex. | ||
1229 | */ | ||
1230 | ✗ | pls->id3_buf = av_fast_realloc(pls->id3_buf, &pls->id3_buf_size, id3_buf_pos + taglen); | |
1231 | ✗ | if (!pls->id3_buf) | |
1232 | ✗ | break; | |
1233 | ✗ | memcpy(pls->id3_buf + id3_buf_pos, buf, tag_got_bytes); | |
1234 | ✗ | id3_buf_pos += tag_got_bytes; | |
1235 | |||
1236 | /* strip the intercepted bytes */ | ||
1237 | ✗ | *len -= tag_got_bytes; | |
1238 | ✗ | memmove(buf, buf + tag_got_bytes, *len); | |
1239 | ✗ | av_log(pls->parent, AV_LOG_DEBUG, "Stripped %d HLS ID3 bytes\n", tag_got_bytes); | |
1240 | |||
1241 | ✗ | if (remaining > 0) { | |
1242 | /* read the rest of the tag in */ | ||
1243 | ✗ | if (read_from_url(pls, seg, pls->id3_buf + id3_buf_pos, remaining) != remaining) | |
1244 | ✗ | break; | |
1245 | ✗ | id3_buf_pos += remaining; | |
1246 | ✗ | av_log(pls->parent, AV_LOG_DEBUG, "Stripped additional %d HLS ID3 bytes\n", remaining); | |
1247 | } | ||
1248 | |||
1249 | } else { | ||
1250 | /* no more ID3 tags */ | ||
1251 | 12 | break; | |
1252 | } | ||
1253 | } | ||
1254 | |||
1255 | /* re-fill buffer for the caller unless EOF */ | ||
1256 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
|
12 | if (*len >= 0 && (fill_buf || *len == 0)) { |
1257 | ✗ | bytes = read_from_url(pls, seg, buf + *len, buf_size - *len); | |
1258 | |||
1259 | /* ignore error if we already had some data */ | ||
1260 | ✗ | if (bytes >= 0) | |
1261 | ✗ | *len += bytes; | |
1262 | ✗ | else if (*len == 0) | |
1263 | ✗ | *len = bytes; | |
1264 | } | ||
1265 | |||
1266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls->id3_buf) { |
1267 | /* Now parse all the ID3 tags */ | ||
1268 | FFIOContext id3ioctx; | ||
1269 | ✗ | ffio_init_read_context(&id3ioctx, pls->id3_buf, id3_buf_pos); | |
1270 | ✗ | handle_id3(&id3ioctx.pub, pls); | |
1271 | } | ||
1272 | |||
1273 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (pls->is_id3_timestamped == -1) |
1274 | 12 | pls->is_id3_timestamped = (pls->id3_mpegts_timestamp != AV_NOPTS_VALUE); | |
1275 | 12 | } | |
1276 | |||
1277 | 61 | static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, AVIOContext **in) | |
1278 | { | ||
1279 | 61 | AVDictionary *opts = NULL; | |
1280 | int ret; | ||
1281 | 61 | int is_http = 0; | |
1282 | |||
1283 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (c->http_persistent) |
1284 | 61 | av_dict_set(&opts, "multiple_requests", "1", 0); | |
1285 | |||
1286 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 41 times.
|
61 | if (seg->size >= 0) { |
1287 | /* try to restrict the HTTP request to the part we want | ||
1288 | * (if this is in fact a HTTP request) */ | ||
1289 | 20 | av_dict_set_int(&opts, "offset", seg->url_offset, 0); | |
1290 | 20 | av_dict_set_int(&opts, "end_offset", seg->url_offset + seg->size, 0); | |
1291 | } | ||
1292 | |||
1293 | 61 | av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n", | |
1294 | seg->url, seg->url_offset, pls->index); | ||
1295 | |||
1296 |
2/4✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
|
61 | if (seg->key_type == KEY_AES_128 || seg->key_type == KEY_SAMPLE_AES) { |
1297 | ✗ | if (strcmp(seg->key, pls->key_url)) { | |
1298 | ✗ | AVIOContext *pb = NULL; | |
1299 | ✗ | if (open_url(pls->parent, &pb, seg->key, &c->avio_opts, opts, NULL) == 0) { | |
1300 | ✗ | ret = avio_read(pb, pls->key, sizeof(pls->key)); | |
1301 | ✗ | if (ret != sizeof(pls->key)) { | |
1302 | ✗ | av_log(pls->parent, AV_LOG_ERROR, "Unable to read key file %s\n", | |
1303 | seg->key); | ||
1304 | } | ||
1305 | ✗ | ff_format_io_close(pls->parent, &pb); | |
1306 | } else { | ||
1307 | ✗ | av_log(pls->parent, AV_LOG_ERROR, "Unable to open key file %s\n", | |
1308 | seg->key); | ||
1309 | } | ||
1310 | ✗ | av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url)); | |
1311 | } | ||
1312 | } | ||
1313 | |||
1314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (seg->key_type == KEY_AES_128) { |
1315 | char iv[33], key[33], url[MAX_URL_SIZE]; | ||
1316 | ✗ | ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0); | |
1317 | ✗ | ff_data_to_hex(key, pls->key, sizeof(pls->key), 0); | |
1318 | ✗ | if (strstr(seg->url, "://")) | |
1319 | ✗ | snprintf(url, sizeof(url), "crypto+%s", seg->url); | |
1320 | else | ||
1321 | ✗ | snprintf(url, sizeof(url), "crypto:%s", seg->url); | |
1322 | |||
1323 | ✗ | av_dict_set(&opts, "key", key, 0); | |
1324 | ✗ | av_dict_set(&opts, "iv", iv, 0); | |
1325 | |||
1326 | ✗ | ret = open_url(pls->parent, in, url, &c->avio_opts, opts, &is_http); | |
1327 | ✗ | if (ret < 0) { | |
1328 | ✗ | goto cleanup; | |
1329 | } | ||
1330 | ✗ | ret = 0; | |
1331 | } else { | ||
1332 | 61 | ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http); | |
1333 | } | ||
1334 | |||
1335 | /* Seek to the requested position. If this was a HTTP request, the offset | ||
1336 | * should already be where want it to, but this allows e.g. local testing | ||
1337 | * without a HTTP server. | ||
1338 | * | ||
1339 | * This is not done for HTTP at all as avio_seek() does internal bookkeeping | ||
1340 | * of file offset which is out-of-sync with the actual offset when "offset" | ||
1341 | * AVOption is used with http protocol, causing the seek to not be a no-op | ||
1342 | * as would be expected. Wrong offset received from the server will not be | ||
1343 | * noticed without the call, though. | ||
1344 | */ | ||
1345 |
4/6✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 46 times.
✓ Branch 5 taken 15 times.
|
61 | if (ret == 0 && !is_http && seg->url_offset) { |
1346 | 15 | int64_t seekret = avio_seek(*in, seg->url_offset, SEEK_SET); | |
1347 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if (seekret < 0) { |
1348 | ✗ | av_log(pls->parent, AV_LOG_ERROR, "Unable to seek to offset %"PRId64" of HLS segment '%s'\n", seg->url_offset, seg->url); | |
1349 | ✗ | ret = seekret; | |
1350 | ✗ | ff_format_io_close(pls->parent, in); | |
1351 | } | ||
1352 | } | ||
1353 | |||
1354 | 61 | cleanup: | |
1355 | 61 | av_dict_free(&opts); | |
1356 | 61 | pls->cur_seg_offset = 0; | |
1357 | 61 | return ret; | |
1358 | } | ||
1359 | |||
1360 | 61 | static int update_init_section(struct playlist *pls, struct segment *seg) | |
1361 | { | ||
1362 | static const int max_init_section_size = 1024*1024; | ||
1363 | 61 | HLSContext *c = pls->parent->priv_data; | |
1364 | int64_t sec_size; | ||
1365 | int64_t urlsize; | ||
1366 | int ret; | ||
1367 | |||
1368 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (seg->init_section == pls->cur_init_section) |
1369 | 61 | return 0; | |
1370 | |||
1371 | ✗ | pls->cur_init_section = NULL; | |
1372 | |||
1373 | ✗ | if (!seg->init_section) | |
1374 | ✗ | return 0; | |
1375 | |||
1376 | ✗ | ret = open_input(c, pls, seg->init_section, &pls->input); | |
1377 | ✗ | if (ret < 0) { | |
1378 | ✗ | av_log(pls->parent, AV_LOG_WARNING, | |
1379 | "Failed to open an initialization section in playlist %d\n", | ||
1380 | pls->index); | ||
1381 | ✗ | return ret; | |
1382 | } | ||
1383 | |||
1384 | ✗ | if (seg->init_section->size >= 0) | |
1385 | ✗ | sec_size = seg->init_section->size; | |
1386 | ✗ | else if ((urlsize = avio_size(pls->input)) >= 0) | |
1387 | ✗ | sec_size = urlsize; | |
1388 | else | ||
1389 | ✗ | sec_size = max_init_section_size; | |
1390 | |||
1391 | ✗ | av_log(pls->parent, AV_LOG_DEBUG, | |
1392 | "Downloading an initialization section of size %"PRId64"\n", | ||
1393 | sec_size); | ||
1394 | |||
1395 | ✗ | sec_size = FFMIN(sec_size, max_init_section_size); | |
1396 | |||
1397 | ✗ | av_fast_malloc(&pls->init_sec_buf, &pls->init_sec_buf_size, sec_size); | |
1398 | |||
1399 | ✗ | ret = read_from_url(pls, seg->init_section, pls->init_sec_buf, | |
1400 | ✗ | pls->init_sec_buf_size); | |
1401 | ✗ | ff_format_io_close(pls->parent, &pls->input); | |
1402 | |||
1403 | ✗ | if (ret < 0) | |
1404 | ✗ | return ret; | |
1405 | |||
1406 | ✗ | pls->cur_init_section = seg->init_section; | |
1407 | ✗ | pls->init_sec_data_len = ret; | |
1408 | ✗ | pls->init_sec_buf_read_offset = 0; | |
1409 | |||
1410 | /* spec says audio elementary streams do not have media initialization | ||
1411 | * sections, so there should be no ID3 timestamps */ | ||
1412 | ✗ | pls->is_id3_timestamped = 0; | |
1413 | |||
1414 | ✗ | return 0; | |
1415 | } | ||
1416 | |||
1417 | 93 | static int64_t default_reload_interval(struct playlist *pls) | |
1418 | { | ||
1419 | 93 | return pls->n_segments > 0 ? | |
1420 |
1/2✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
|
93 | pls->segments[pls->n_segments - 1]->duration : |
1421 | pls->target_duration; | ||
1422 | } | ||
1423 | |||
1424 | 6436 | static int playlist_needed(struct playlist *pls) | |
1425 | { | ||
1426 | 6436 | AVFormatContext *s = pls->parent; | |
1427 | int i, j; | ||
1428 | 6436 | int stream_needed = 0; | |
1429 | int first_st; | ||
1430 | |||
1431 | /* If there is no context or streams yet, the playlist is needed */ | ||
1432 |
3/4✓ Branch 0 taken 6436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 6424 times.
|
6436 | if (!pls->ctx || !pls->n_main_streams) |
1433 | 12 | return 1; | |
1434 | |||
1435 | /* check if any of the streams in the playlist are needed */ | ||
1436 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | for (i = 0; i < pls->n_main_streams; i++) { |
1437 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | if (pls->main_streams[i]->discard < AVDISCARD_ALL) { |
1438 | 6424 | stream_needed = 1; | |
1439 | 6424 | break; | |
1440 | } | ||
1441 | } | ||
1442 | |||
1443 | /* If all streams in the playlist were discarded, the playlist is not | ||
1444 | * needed (regardless of whether whole programs are discarded or not). */ | ||
1445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6424 times.
|
6424 | if (!stream_needed) |
1446 | ✗ | return 0; | |
1447 | |||
1448 | /* Otherwise, check if all the programs (variants) this playlist is in are | ||
1449 | * discarded. Since all streams in the playlist are part of the same programs | ||
1450 | * we can just check the programs of the first stream. */ | ||
1451 | |||
1452 | 6424 | first_st = pls->main_streams[0]->index; | |
1453 | |||
1454 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | for (i = 0; i < s->nb_programs; i++) { |
1455 | 6424 | AVProgram *program = s->programs[i]; | |
1456 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | if (program->discard < AVDISCARD_ALL) { |
1457 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | for (j = 0; j < program->nb_stream_indexes; j++) { |
1458 |
1/2✓ Branch 0 taken 6424 times.
✗ Branch 1 not taken.
|
6424 | if (program->stream_index[j] == first_st) { |
1459 | /* playlist is in an undiscarded program */ | ||
1460 | 6424 | return 1; | |
1461 | } | ||
1462 | } | ||
1463 | } | ||
1464 | } | ||
1465 | |||
1466 | /* some streams were not discarded but all the programs were */ | ||
1467 | ✗ | return 0; | |
1468 | } | ||
1469 | |||
1470 | 339 | static int read_data(void *opaque, uint8_t *buf, int buf_size) | |
1471 | { | ||
1472 | 339 | struct playlist *v = opaque; | |
1473 | 339 | HLSContext *c = v->parent->priv_data; | |
1474 | int ret; | ||
1475 | 339 | int just_opened = 0; | |
1476 | 339 | int reload_count = 0; | |
1477 | 339 | int segment_retries = 0; | |
1478 | struct segment *seg; | ||
1479 | |||
1480 | 398 | restart: | |
1481 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 398 times.
|
398 | if (!v->needed) |
1482 | ✗ | return AVERROR_EOF; | |
1483 | |||
1484 |
4/6✓ Branch 0 taken 305 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 305 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 305 times.
|
398 | if (!v->input || (c->http_persistent && v->input_read_done)) { |
1485 | int64_t reload_interval; | ||
1486 | |||
1487 | /* Check that the playlist is still needed before opening a new | ||
1488 | * segment. */ | ||
1489 | 93 | v->needed = playlist_needed(v); | |
1490 | |||
1491 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (!v->needed) { |
1492 | ✗ | av_log(v->parent, AV_LOG_INFO, "No longer receiving playlist %d ('%s')\n", | |
1493 | ✗ | v->index, v->url); | |
1494 | ✗ | return AVERROR_EOF; | |
1495 | } | ||
1496 | |||
1497 | /* If this is a live stream and the reload interval has elapsed since | ||
1498 | * the last playlist reload, reload the playlists now. */ | ||
1499 | 93 | reload_interval = default_reload_interval(v); | |
1500 | |||
1501 | ✗ | reload: | |
1502 | 93 | reload_count++; | |
1503 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (reload_count > c->max_reload) |
1504 | ✗ | return AVERROR_EOF; | |
1505 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 90 times.
|
93 | if (!v->finished && |
1506 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | av_gettime_relative() - v->last_load_time >= reload_interval) { |
1507 | ✗ | if ((ret = parse_playlist(c, v->url, v, NULL)) < 0) { | |
1508 | ✗ | if (ret != AVERROR_EXIT) | |
1509 | ✗ | av_log(v->parent, AV_LOG_WARNING, "Failed to reload playlist %d\n", | |
1510 | v->index); | ||
1511 | ✗ | return ret; | |
1512 | } | ||
1513 | /* If we need to reload the playlist again below (if | ||
1514 | * there's still no more segments), switch to a reload | ||
1515 | * interval of half the target duration. */ | ||
1516 | ✗ | reload_interval = v->target_duration / 2; | |
1517 | } | ||
1518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (v->cur_seq_no < v->start_seq_no) { |
1519 | ✗ | av_log(v->parent, AV_LOG_WARNING, | |
1520 | "skipping %"PRId64" segments ahead, expired from playlists\n", | ||
1521 | ✗ | v->start_seq_no - v->cur_seq_no); | |
1522 | ✗ | v->cur_seq_no = v->start_seq_no; | |
1523 | } | ||
1524 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 30 times.
|
93 | if (v->cur_seq_no > v->last_seq_no) { |
1525 | 63 | v->last_seq_no = v->cur_seq_no; | |
1526 | 63 | v->m3u8_hold_counters = 0; | |
1527 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | } else if (v->last_seq_no == v->cur_seq_no) { |
1528 | 30 | v->m3u8_hold_counters++; | |
1529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (v->m3u8_hold_counters >= c->m3u8_hold_counters) { |
1530 | ✗ | return AVERROR_EOF; | |
1531 | } | ||
1532 | } else { | ||
1533 | ✗ | av_log(v->parent, AV_LOG_WARNING, "The m3u8 list sequence may have been wrapped.\n"); | |
1534 | } | ||
1535 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 61 times.
|
93 | if (v->cur_seq_no >= v->start_seq_no + v->n_segments) { |
1536 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (v->finished) |
1537 | 32 | return AVERROR_EOF; | |
1538 | ✗ | while (av_gettime_relative() - v->last_load_time < reload_interval) { | |
1539 | ✗ | if (ff_check_interrupt(c->interrupt_callback)) | |
1540 | ✗ | return AVERROR_EXIT; | |
1541 | ✗ | av_usleep(100*1000); | |
1542 | } | ||
1543 | /* Enough time has elapsed since the last reload */ | ||
1544 | ✗ | goto reload; | |
1545 | } | ||
1546 | |||
1547 | 61 | v->input_read_done = 0; | |
1548 | 61 | seg = current_segment(v); | |
1549 | |||
1550 | /* load/update Media Initialization Section, if any */ | ||
1551 | 61 | ret = update_init_section(v, seg); | |
1552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret) |
1553 | ✗ | return ret; | |
1554 | |||
1555 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
61 | if (c->http_multiple == 1 && v->input_next_requested) { |
1556 | ✗ | FFSWAP(AVIOContext *, v->input, v->input_next); | |
1557 | ✗ | v->cur_seg_offset = 0; | |
1558 | ✗ | v->input_next_requested = 0; | |
1559 | ✗ | ret = 0; | |
1560 | } else { | ||
1561 | 61 | ret = open_input(c, v, seg, &v->input); | |
1562 | } | ||
1563 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret < 0) { |
1564 | ✗ | if (ff_check_interrupt(c->interrupt_callback)) | |
1565 | ✗ | return AVERROR_EXIT; | |
1566 | ✗ | av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n", | |
1567 | v->cur_seq_no, | ||
1568 | v->index); | ||
1569 | ✗ | if (segment_retries >= c->seg_max_retry) { | |
1570 | ✗ | av_log(v->parent, AV_LOG_WARNING, "Segment %"PRId64" of playlist %d failed too many times, skipping\n", | |
1571 | v->cur_seq_no, | ||
1572 | v->index); | ||
1573 | ✗ | v->cur_seq_no++; | |
1574 | ✗ | segment_retries = 0; | |
1575 | } else { | ||
1576 | ✗ | segment_retries++; | |
1577 | } | ||
1578 | ✗ | goto reload; | |
1579 | } | ||
1580 | 61 | segment_retries = 0; | |
1581 | 61 | just_opened = 1; | |
1582 | } | ||
1583 | |||
1584 |
1/2✓ Branch 0 taken 366 times.
✗ Branch 1 not taken.
|
366 | if (c->http_multiple == -1) { |
1585 | 366 | uint8_t *http_version_opt = NULL; | |
1586 | 366 | int r = av_opt_get(v->input, "http_version", AV_OPT_SEARCH_CHILDREN, &http_version_opt); | |
1587 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 366 times.
|
366 | if (r >= 0) { |
1588 | ✗ | c->http_multiple = (!strncmp((const char *)http_version_opt, "1.1", 3) || !strncmp((const char *)http_version_opt, "2.0", 3)); | |
1589 | ✗ | av_freep(&http_version_opt); | |
1590 | } | ||
1591 | } | ||
1592 | |||
1593 | 366 | seg = next_segment(v); | |
1594 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
366 | if (c->http_multiple == 1 && !v->input_next_requested && |
1595 | ✗ | seg && seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) { | |
1596 | ✗ | ret = open_input(c, v, seg, &v->input_next); | |
1597 | ✗ | if (ret < 0) { | |
1598 | ✗ | if (ff_check_interrupt(c->interrupt_callback)) | |
1599 | ✗ | return AVERROR_EXIT; | |
1600 | ✗ | av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n", | |
1601 | ✗ | v->cur_seq_no + 1, | |
1602 | v->index); | ||
1603 | } else { | ||
1604 | ✗ | v->input_next_requested = 1; | |
1605 | } | ||
1606 | } | ||
1607 | |||
1608 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 366 times.
|
366 | if (v->init_sec_buf_read_offset < v->init_sec_data_len) { |
1609 | /* Push init section out first before first actual segment */ | ||
1610 | ✗ | int copy_size = FFMIN(v->init_sec_data_len - v->init_sec_buf_read_offset, buf_size); | |
1611 | ✗ | memcpy(buf, v->init_sec_buf, copy_size); | |
1612 | ✗ | v->init_sec_buf_read_offset += copy_size; | |
1613 | ✗ | return copy_size; | |
1614 | } | ||
1615 | |||
1616 | 366 | seg = current_segment(v); | |
1617 | 366 | ret = read_from_url(v, seg, buf, buf_size); | |
1618 |
2/2✓ Branch 0 taken 307 times.
✓ Branch 1 taken 59 times.
|
366 | if (ret > 0) { |
1619 |
4/4✓ Branch 0 taken 61 times.
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 49 times.
|
307 | if (just_opened && v->is_id3_timestamped != 0) { |
1620 | /* Intercept ID3 tags here, elementary audio streams are required | ||
1621 | * to convey timestamps using them in the beginning of each segment. */ | ||
1622 | 12 | intercept_id3(v, buf, buf_size, &ret); | |
1623 | } | ||
1624 | |||
1625 | 307 | return ret; | |
1626 | } | ||
1627 |
1/2✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
|
59 | if (c->http_persistent && |
1628 |
2/4✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 59 times.
|
59 | seg->key_type == KEY_NONE && av_strstart(seg->url, "http", NULL)) { |
1629 | ✗ | v->input_read_done = 1; | |
1630 | } else { | ||
1631 | 59 | ff_format_io_close(v->parent, &v->input); | |
1632 | } | ||
1633 | 59 | v->cur_seq_no++; | |
1634 | |||
1635 | 59 | c->cur_seq_no = v->cur_seq_no; | |
1636 | |||
1637 | 59 | goto restart; | |
1638 | } | ||
1639 | |||
1640 | ✗ | static void add_renditions_to_variant(HLSContext *c, struct variant *var, | |
1641 | enum AVMediaType type, const char *group_id) | ||
1642 | { | ||
1643 | int i; | ||
1644 | |||
1645 | ✗ | for (i = 0; i < c->n_renditions; i++) { | |
1646 | ✗ | struct rendition *rend = c->renditions[i]; | |
1647 | |||
1648 | ✗ | if (rend->type == type && !strcmp(rend->group_id, group_id)) { | |
1649 | |||
1650 | ✗ | if (rend->playlist) | |
1651 | /* rendition is an external playlist | ||
1652 | * => add the playlist to the variant */ | ||
1653 | ✗ | dynarray_add(&var->playlists, &var->n_playlists, rend->playlist); | |
1654 | else | ||
1655 | /* rendition is part of the variant main Media Playlist | ||
1656 | * => add the rendition to the main Media Playlist */ | ||
1657 | ✗ | dynarray_add(&var->playlists[0]->renditions, | |
1658 | &var->playlists[0]->n_renditions, | ||
1659 | rend); | ||
1660 | } | ||
1661 | } | ||
1662 | ✗ | } | |
1663 | |||
1664 | 36 | static void add_metadata_from_renditions(AVFormatContext *s, struct playlist *pls, | |
1665 | enum AVMediaType type) | ||
1666 | { | ||
1667 | 36 | int rend_idx = 0; | |
1668 | int i; | ||
1669 | |||
1670 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 24 times.
|
60 | for (i = 0; i < pls->n_main_streams; i++) { |
1671 | 36 | AVStream *st = pls->main_streams[i]; | |
1672 | |||
1673 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | if (st->codecpar->codec_type != type) |
1674 | 24 | continue; | |
1675 | |||
1676 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | for (; rend_idx < pls->n_renditions; rend_idx++) { |
1677 | ✗ | struct rendition *rend = pls->renditions[rend_idx]; | |
1678 | |||
1679 | ✗ | if (rend->type != type) | |
1680 | ✗ | continue; | |
1681 | |||
1682 | ✗ | if (rend->language[0]) | |
1683 | ✗ | av_dict_set(&st->metadata, "language", rend->language, 0); | |
1684 | ✗ | if (rend->name[0]) | |
1685 | ✗ | av_dict_set(&st->metadata, "comment", rend->name, 0); | |
1686 | |||
1687 | ✗ | st->disposition |= rend->disposition; | |
1688 | } | ||
1689 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (rend_idx >=pls->n_renditions) |
1690 | 12 | break; | |
1691 | } | ||
1692 | 36 | } | |
1693 | |||
1694 | /* if timestamp was in valid range: returns 1 and sets seq_no | ||
1695 | * if not: returns 0 and sets seq_no to closest segment */ | ||
1696 | ✗ | static int find_timestamp_in_playlist(HLSContext *c, struct playlist *pls, | |
1697 | int64_t timestamp, int64_t *seq_no, | ||
1698 | int64_t *seg_start_ts) | ||
1699 | { | ||
1700 | int i; | ||
1701 | ✗ | int64_t pos = c->first_timestamp == AV_NOPTS_VALUE ? | |
1702 | ✗ | 0 : c->first_timestamp; | |
1703 | |||
1704 | ✗ | if (timestamp < pos) { | |
1705 | ✗ | *seq_no = pls->start_seq_no; | |
1706 | ✗ | return 0; | |
1707 | } | ||
1708 | |||
1709 | ✗ | for (i = 0; i < pls->n_segments; i++) { | |
1710 | ✗ | int64_t diff = pos + pls->segments[i]->duration - timestamp; | |
1711 | ✗ | if (diff > 0) { | |
1712 | ✗ | *seq_no = pls->start_seq_no + i; | |
1713 | ✗ | if (seg_start_ts) { | |
1714 | ✗ | *seg_start_ts = pos; | |
1715 | } | ||
1716 | ✗ | return 1; | |
1717 | } | ||
1718 | ✗ | pos += pls->segments[i]->duration; | |
1719 | } | ||
1720 | |||
1721 | ✗ | *seq_no = pls->start_seq_no + pls->n_segments - 1; | |
1722 | |||
1723 | ✗ | return 0; | |
1724 | } | ||
1725 | |||
1726 | 12 | static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) | |
1727 | { | ||
1728 | int64_t seq_no; | ||
1729 | |||
1730 |
3/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 | if (!pls->finished && !c->first_packet && |
1731 | ✗ | av_gettime_relative() - pls->last_load_time >= default_reload_interval(pls)) | |
1732 | /* reload the playlist since it was suspended */ | ||
1733 | ✗ | parse_playlist(c, pls->url, pls, NULL); | |
1734 | |||
1735 | /* If playback is already in progress (we are just selecting a new | ||
1736 | * playlist) and this is a complete file, find the matching segment | ||
1737 | * by counting durations. */ | ||
1738 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
12 | if (pls->finished && c->cur_timestamp != AV_NOPTS_VALUE) { |
1739 | ✗ | find_timestamp_in_playlist(c, pls, c->cur_timestamp, &seq_no, NULL); | |
1740 | ✗ | return seq_no; | |
1741 | } | ||
1742 | |||
1743 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
|
12 | if (!pls->finished) { |
1744 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!c->first_packet && /* we are doing a segment selection during playback */ |
1745 | ✗ | c->cur_seq_no >= pls->start_seq_no && | |
1746 | ✗ | c->cur_seq_no < pls->start_seq_no + pls->n_segments) | |
1747 | /* While spec 3.4.3 says that we cannot assume anything about the | ||
1748 | * content at the same sequence number on different playlists, | ||
1749 | * in practice this seems to work and doing it otherwise would | ||
1750 | * require us to download a segment to inspect its timestamps. */ | ||
1751 | ✗ | return c->cur_seq_no; | |
1752 | |||
1753 | /* If this is a live stream, start live_start_index segments from the | ||
1754 | * start or end */ | ||
1755 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (c->live_start_index < 0) |
1756 | 1 | seq_no = pls->start_seq_no + FFMAX(pls->n_segments + | |
1757 | c->live_start_index, 0); | ||
1758 | else | ||
1759 | ✗ | seq_no = pls->start_seq_no + FFMIN(c->live_start_index, | |
1760 | pls->n_segments - 1); | ||
1761 | |||
1762 | /* If #EXT-X-START in playlist, need to recalculate */ | ||
1763 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if (pls->time_offset_flag && c->prefer_x_start) { |
1764 | int64_t start_timestamp; | ||
1765 | ✗ | int64_t playlist_duration = 0; | |
1766 | ✗ | int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 : | |
1767 | c->cur_timestamp; | ||
1768 | |||
1769 | ✗ | for (int i = 0; i < pls->n_segments; i++) | |
1770 | ✗ | playlist_duration += pls->segments[i]->duration; | |
1771 | |||
1772 | /* If the absolute value of TIME-OFFSET exceeds | ||
1773 | * the duration of the playlist, it indicates either the end of the | ||
1774 | * playlist (if positive) or the beginning of the playlist (if | ||
1775 | * negative). */ | ||
1776 | ✗ | if (pls->start_time_offset >=0 && | |
1777 | ✗ | pls->start_time_offset > playlist_duration) | |
1778 | ✗ | start_timestamp = cur_timestamp + playlist_duration; | |
1779 | ✗ | else if (pls->start_time_offset >= 0 && | |
1780 | ✗ | pls->start_time_offset <= playlist_duration) | |
1781 | ✗ | start_timestamp = cur_timestamp + pls->start_time_offset; | |
1782 | ✗ | else if (pls->start_time_offset < 0 && | |
1783 | ✗ | pls->start_time_offset < -playlist_duration) | |
1784 | ✗ | start_timestamp = cur_timestamp; | |
1785 | ✗ | else if (pls->start_time_offset < 0 && | |
1786 | ✗ | pls->start_time_offset > -playlist_duration) | |
1787 | ✗ | start_timestamp = cur_timestamp + playlist_duration + | |
1788 | ✗ | pls->start_time_offset; | |
1789 | else | ||
1790 | ✗ | start_timestamp = cur_timestamp; | |
1791 | |||
1792 | ✗ | find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL); | |
1793 | } | ||
1794 | 1 | return seq_no; | |
1795 | } | ||
1796 | |||
1797 | /* Otherwise just start on the first segment. */ | ||
1798 | 11 | return pls->start_seq_no; | |
1799 | } | ||
1800 | |||
1801 | ✗ | static int nested_io_open(AVFormatContext *s, AVIOContext **pb, const char *url, | |
1802 | int flags, AVDictionary **opts) | ||
1803 | { | ||
1804 | ✗ | av_log(s, AV_LOG_ERROR, | |
1805 | "A HLS playlist item '%s' referred to an external file '%s'. " | ||
1806 | "Opening this file was forbidden for security reasons\n", | ||
1807 | s->url, url); | ||
1808 | ✗ | return AVERROR(EPERM); | |
1809 | } | ||
1810 | |||
1811 | 12 | static void add_stream_to_programs(AVFormatContext *s, struct playlist *pls, AVStream *stream) | |
1812 | { | ||
1813 | 12 | HLSContext *c = s->priv_data; | |
1814 | int i, j; | ||
1815 | 12 | int bandwidth = -1; | |
1816 | |||
1817 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_variants; i++) { |
1818 | 12 | struct variant *v = c->variants[i]; | |
1819 | |||
1820 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (j = 0; j < v->n_playlists; j++) { |
1821 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (v->playlists[j] != pls) |
1822 | ✗ | continue; | |
1823 | |||
1824 | 12 | av_program_add_stream_index(s, i, stream->index); | |
1825 | |||
1826 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (bandwidth < 0) |
1827 | 12 | bandwidth = v->bandwidth; | |
1828 | ✗ | else if (bandwidth != v->bandwidth) | |
1829 | ✗ | bandwidth = -1; /* stream in multiple variants with different bandwidths */ | |
1830 | } | ||
1831 | } | ||
1832 | |||
1833 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (bandwidth >= 0) |
1834 | 12 | av_dict_set_int(&stream->metadata, "variant_bitrate", bandwidth, 0); | |
1835 | 12 | } | |
1836 | |||
1837 | 22 | static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls, AVStream *ist) | |
1838 | { | ||
1839 | int err; | ||
1840 | |||
1841 | 22 | err = avcodec_parameters_copy(st->codecpar, ist->codecpar); | |
1842 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (err < 0) |
1843 | ✗ | return err; | |
1844 | |||
1845 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (pls->is_id3_timestamped) /* custom timestamps via id3 */ |
1846 | ✗ | avpriv_set_pts_info(st, 33, 1, MPEG_TIME_BASE); | |
1847 | else | ||
1848 | 22 | avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den); | |
1849 | |||
1850 | // copy disposition | ||
1851 | 22 | st->disposition = ist->disposition; | |
1852 | |||
1853 | 22 | av_dict_copy(&st->metadata, ist->metadata, 0); | |
1854 | |||
1855 | 22 | ffstream(st)->need_context_update = 1; | |
1856 | |||
1857 | 22 | return 0; | |
1858 | } | ||
1859 | |||
1860 | /* add new subdemuxer streams to our context, if any */ | ||
1861 | 6345 | static int update_streams_from_subdemuxer(AVFormatContext *s, struct playlist *pls) | |
1862 | { | ||
1863 | int err; | ||
1864 | |||
1865 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6345 times.
|
6357 | while (pls->n_main_streams < pls->ctx->nb_streams) { |
1866 | 12 | int ist_idx = pls->n_main_streams; | |
1867 | 12 | AVStream *st = avformat_new_stream(s, NULL); | |
1868 | 12 | AVStream *ist = pls->ctx->streams[ist_idx]; | |
1869 | |||
1870 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!st) |
1871 | ✗ | return AVERROR(ENOMEM); | |
1872 | |||
1873 | 12 | st->id = pls->index; | |
1874 | 12 | dynarray_add(&pls->main_streams, &pls->n_main_streams, st); | |
1875 | |||
1876 | 12 | add_stream_to_programs(s, pls, st); | |
1877 | |||
1878 | 12 | err = set_stream_info_from_input_stream(st, pls, ist); | |
1879 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (err < 0) |
1880 | ✗ | return err; | |
1881 | } | ||
1882 | |||
1883 | 6345 | return 0; | |
1884 | } | ||
1885 | |||
1886 | 23 | static void update_noheader_flag(AVFormatContext *s) | |
1887 | { | ||
1888 | 23 | HLSContext *c = s->priv_data; | |
1889 | 23 | int flag_needed = 0; | |
1890 | int i; | ||
1891 | |||
1892 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 12 times.
|
35 | for (i = 0; i < c->n_playlists; i++) { |
1893 | 23 | struct playlist *pls = c->playlists[i]; | |
1894 | |||
1895 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
|
23 | if (pls->has_noheader_flag) { |
1896 | 11 | flag_needed = 1; | |
1897 | 11 | break; | |
1898 | } | ||
1899 | } | ||
1900 | |||
1901 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
|
23 | if (flag_needed) |
1902 | 11 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
1903 | else | ||
1904 | 12 | s->ctx_flags &= ~AVFMTCTX_NOHEADER; | |
1905 | 23 | } | |
1906 | |||
1907 | 12 | static int hls_close(AVFormatContext *s) | |
1908 | { | ||
1909 | 12 | HLSContext *c = s->priv_data; | |
1910 | |||
1911 | 12 | free_playlist_list(c); | |
1912 | 12 | free_variant_list(c); | |
1913 | 12 | free_rendition_list(c); | |
1914 | |||
1915 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (c->crypto_ctx.aes_ctx) |
1916 | ✗ | av_free(c->crypto_ctx.aes_ctx); | |
1917 | |||
1918 | 12 | av_dict_free(&c->avio_opts); | |
1919 | 12 | ff_format_io_close(c->ctx, &c->playlist_pb); | |
1920 | |||
1921 | 12 | return 0; | |
1922 | } | ||
1923 | |||
1924 | 12 | static int hls_read_header(AVFormatContext *s) | |
1925 | { | ||
1926 | 12 | HLSContext *c = s->priv_data; | |
1927 | 12 | int ret = 0, i; | |
1928 | 12 | int64_t highest_cur_seq_no = 0; | |
1929 | |||
1930 | 12 | c->ctx = s; | |
1931 | 12 | c->interrupt_callback = &s->interrupt_callback; | |
1932 | |||
1933 | 12 | c->first_packet = 1; | |
1934 | 12 | c->first_timestamp = AV_NOPTS_VALUE; | |
1935 | 12 | c->cur_timestamp = AV_NOPTS_VALUE; | |
1936 | |||
1937 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if ((ret = ffio_copy_url_options(s->pb, &c->avio_opts)) < 0) |
1938 | ✗ | return ret; | |
1939 | |||
1940 | /* XXX: Some HLS servers don't like being sent the range header, | ||
1941 | in this case, need to setting http_seekable = 0 to disable | ||
1942 | the range header */ | ||
1943 | 12 | av_dict_set_int(&c->avio_opts, "seekable", c->http_seekable, 0); | |
1944 | |||
1945 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if ((ret = parse_playlist(c, s->url, NULL, s->pb)) < 0) |
1946 | ✗ | return ret; | |
1947 | |||
1948 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (c->n_variants == 0) { |
1949 | ✗ | av_log(s, AV_LOG_WARNING, "Empty playlist\n"); | |
1950 | ✗ | return AVERROR_EOF; | |
1951 | } | ||
1952 | /* If the playlist only contained playlists (Master Playlist), | ||
1953 | * parse each individual playlist. */ | ||
1954 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
12 | if (c->n_playlists > 1 || c->playlists[0]->n_segments == 0) { |
1955 | ✗ | for (i = 0; i < c->n_playlists; i++) { | |
1956 | ✗ | struct playlist *pls = c->playlists[i]; | |
1957 | ✗ | pls->m3u8_hold_counters = 0; | |
1958 | ✗ | if ((ret = parse_playlist(c, pls->url, pls, NULL)) < 0) { | |
1959 | ✗ | av_log(s, AV_LOG_WARNING, "parse_playlist error %s [%s]\n", av_err2str(ret), pls->url); | |
1960 | ✗ | pls->broken = 1; | |
1961 | ✗ | if (c->n_playlists > 1) | |
1962 | ✗ | continue; | |
1963 | ✗ | return ret; | |
1964 | } | ||
1965 | } | ||
1966 | } | ||
1967 | |||
1968 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_variants; i++) { |
1969 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (c->variants[i]->playlists[0]->n_segments == 0) { |
1970 | ✗ | av_log(s, AV_LOG_WARNING, "Empty segment [%s]\n", c->variants[i]->playlists[0]->url); | |
1971 | ✗ | c->variants[i]->playlists[0]->broken = 1; | |
1972 | } | ||
1973 | } | ||
1974 | |||
1975 | /* If this isn't a live stream, calculate the total duration of the | ||
1976 | * stream. */ | ||
1977 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
|
12 | if (c->variants[0]->playlists[0]->finished) { |
1978 | 11 | int64_t duration = 0; | |
1979 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 11 times.
|
71 | for (i = 0; i < c->variants[0]->playlists[0]->n_segments; i++) |
1980 | 60 | duration += c->variants[0]->playlists[0]->segments[i]->duration; | |
1981 | 11 | s->duration = duration; | |
1982 | } | ||
1983 | |||
1984 | /* Associate renditions with variants */ | ||
1985 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_variants; i++) { |
1986 | 12 | struct variant *var = c->variants[i]; | |
1987 | |||
1988 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (var->audio_group[0]) |
1989 | ✗ | add_renditions_to_variant(c, var, AVMEDIA_TYPE_AUDIO, var->audio_group); | |
1990 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (var->video_group[0]) |
1991 | ✗ | add_renditions_to_variant(c, var, AVMEDIA_TYPE_VIDEO, var->video_group); | |
1992 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (var->subtitles_group[0]) |
1993 | ✗ | add_renditions_to_variant(c, var, AVMEDIA_TYPE_SUBTITLE, var->subtitles_group); | |
1994 | } | ||
1995 | |||
1996 | /* Create a program for each variant */ | ||
1997 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_variants; i++) { |
1998 | 12 | struct variant *v = c->variants[i]; | |
1999 | AVProgram *program; | ||
2000 | |||
2001 | 12 | program = av_new_program(s, i); | |
2002 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!program) |
2003 | ✗ | return AVERROR(ENOMEM); | |
2004 | 12 | av_dict_set_int(&program->metadata, "variant_bitrate", v->bandwidth, 0); | |
2005 | } | ||
2006 | |||
2007 | /* Select the starting segments */ | ||
2008 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_playlists; i++) { |
2009 | 12 | struct playlist *pls = c->playlists[i]; | |
2010 | |||
2011 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls->n_segments == 0) |
2012 | ✗ | continue; | |
2013 | |||
2014 | 12 | pls->cur_seq_no = select_cur_seq_no(c, pls); | |
2015 | 12 | highest_cur_seq_no = FFMAX(highest_cur_seq_no, pls->cur_seq_no); | |
2016 | } | ||
2017 | |||
2018 | /* Open the demuxer for each playlist */ | ||
2019 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | for (i = 0; i < c->n_playlists; i++) { |
2020 | 12 | struct playlist *pls = c->playlists[i]; | |
2021 | 12 | const AVInputFormat *in_fmt = NULL; | |
2022 | char *url; | ||
2023 | 12 | AVDictionary *options = NULL; | |
2024 | 12 | struct segment *seg = NULL; | |
2025 | |||
2026 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (!(pls->ctx = avformat_alloc_context())) |
2027 | ✗ | return AVERROR(ENOMEM); | |
2028 | |||
2029 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls->n_segments == 0) |
2030 | ✗ | continue; | |
2031 | |||
2032 | 12 | pls->index = i; | |
2033 | 12 | pls->needed = 1; | |
2034 | 12 | pls->parent = s; | |
2035 | |||
2036 | /* | ||
2037 | * If this is a live stream and this playlist looks like it is one segment | ||
2038 | * behind, try to sync it up so that every substream starts at the same | ||
2039 | * time position (so e.g. avformat_find_stream_info() will see packets from | ||
2040 | * all active streams within the first few seconds). This is not very generic, | ||
2041 | * though, as the sequence numbers are technically independent. | ||
2042 | */ | ||
2043 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
12 | if (!pls->finished && pls->cur_seq_no == highest_cur_seq_no - 1 && |
2044 | ✗ | highest_cur_seq_no < pls->start_seq_no + pls->n_segments) { | |
2045 | ✗ | pls->cur_seq_no = highest_cur_seq_no; | |
2046 | } | ||
2047 | |||
2048 | 12 | pls->read_buffer = av_malloc(INITIAL_BUFFER_SIZE); | |
2049 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!pls->read_buffer){ |
2050 | ✗ | avformat_free_context(pls->ctx); | |
2051 | ✗ | pls->ctx = NULL; | |
2052 | ✗ | return AVERROR(ENOMEM); | |
2053 | } | ||
2054 | |||
2055 | 12 | ffio_init_context(&pls->pb, pls->read_buffer, INITIAL_BUFFER_SIZE, 0, pls, | |
2056 | read_data, NULL, NULL); | ||
2057 | |||
2058 | /* | ||
2059 | * If encryption scheme is SAMPLE-AES, try to read ID3 tags of | ||
2060 | * external audio track that contains audio setup information | ||
2061 | */ | ||
2062 | 12 | seg = current_segment(pls); | |
2063 |
2/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 | if (seg && seg->key_type == KEY_SAMPLE_AES && pls->n_renditions > 0 && |
2064 | ✗ | pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO) { | |
2065 | uint8_t buf[HLS_MAX_ID3_TAGS_DATA_LEN]; | ||
2066 | ✗ | if ((ret = avio_read(&pls->pb.pub, buf, HLS_MAX_ID3_TAGS_DATA_LEN)) < 0) { | |
2067 | /* Fail if error was not end of file */ | ||
2068 | ✗ | if (ret != AVERROR_EOF) { | |
2069 | ✗ | avformat_free_context(pls->ctx); | |
2070 | ✗ | pls->ctx = NULL; | |
2071 | ✗ | return ret; | |
2072 | } | ||
2073 | } | ||
2074 | ✗ | ret = 0; | |
2075 | /* Reset reading */ | ||
2076 | ✗ | ff_format_io_close(pls->parent, &pls->input); | |
2077 | ✗ | pls->input = NULL; | |
2078 | ✗ | pls->input_read_done = 0; | |
2079 | ✗ | ff_format_io_close(pls->parent, &pls->input_next); | |
2080 | ✗ | pls->input_next = NULL; | |
2081 | ✗ | pls->input_next_requested = 0; | |
2082 | ✗ | pls->cur_seg_offset = 0; | |
2083 | ✗ | pls->cur_init_section = NULL; | |
2084 | /* Reset EOF flag */ | ||
2085 | ✗ | pls->pb.pub.eof_reached = 0; | |
2086 | /* Clear any buffered data */ | ||
2087 | ✗ | pls->pb.pub.buf_end = pls->pb.pub.buf_ptr = pls->pb.pub.buffer; | |
2088 | /* Reset the position */ | ||
2089 | ✗ | pls->pb.pub.pos = 0; | |
2090 | } | ||
2091 | |||
2092 | /* | ||
2093 | * If encryption scheme is SAMPLE-AES and audio setup information is present in external audio track, | ||
2094 | * use that information to find the media format, otherwise probe input data | ||
2095 | */ | ||
2096 |
2/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 | if (seg && seg->key_type == KEY_SAMPLE_AES && pls->is_id3_timestamped && |
2097 | ✗ | pls->audio_setup_info.codec_id != AV_CODEC_ID_NONE) { | |
2098 | ✗ | void *iter = NULL; | |
2099 | ✗ | while ((in_fmt = av_demuxer_iterate(&iter))) | |
2100 | ✗ | if (in_fmt->raw_codec_id == pls->audio_setup_info.codec_id) | |
2101 | ✗ | break; | |
2102 | } else { | ||
2103 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4; |
2104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE; |
2105 | 12 | pls->ctx->interrupt_callback = s->interrupt_callback; | |
2106 | 12 | url = av_strdup(pls->segments[0]->url); | |
2107 | 12 | ret = av_probe_input_buffer(&pls->pb.pub, &in_fmt, url, NULL, 0, 0); | |
2108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) { |
2109 | /* Free the ctx - it isn't initialized properly at this point, | ||
2110 | * so avformat_close_input shouldn't be called. If | ||
2111 | * avformat_open_input fails below, it frees and zeros the | ||
2112 | * context, so it doesn't need any special treatment like this. */ | ||
2113 | ✗ | av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url); | |
2114 | ✗ | avformat_free_context(pls->ctx); | |
2115 | ✗ | pls->ctx = NULL; | |
2116 | ✗ | av_free(url); | |
2117 | ✗ | return ret; | |
2118 | } | ||
2119 | 12 | av_free(url); | |
2120 | } | ||
2121 | |||
2122 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
12 | if (seg && seg->key_type == KEY_SAMPLE_AES) { |
2123 | ✗ | if (strstr(in_fmt->name, "mov")) { | |
2124 | char key[33]; | ||
2125 | ✗ | ff_data_to_hex(key, pls->key, sizeof(pls->key), 0); | |
2126 | ✗ | av_dict_set(&options, "decryption_key", key, 0); | |
2127 | ✗ | } else if (!c->crypto_ctx.aes_ctx) { | |
2128 | ✗ | c->crypto_ctx.aes_ctx = av_aes_alloc(); | |
2129 | ✗ | if (!c->crypto_ctx.aes_ctx) { | |
2130 | ✗ | avformat_free_context(pls->ctx); | |
2131 | ✗ | pls->ctx = NULL; | |
2132 | ✗ | return AVERROR(ENOMEM); | |
2133 | } | ||
2134 | } | ||
2135 | } | ||
2136 | |||
2137 | 12 | pls->ctx->pb = &pls->pb.pub; | |
2138 | 12 | pls->ctx->io_open = nested_io_open; | |
2139 | 12 | pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO; | |
2140 | |||
2141 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0) |
2142 | ✗ | return ret; | |
2143 | |||
2144 | 12 | av_dict_copy(&options, c->seg_format_opts, 0); | |
2145 | |||
2146 | 12 | ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &options); | |
2147 | 12 | av_dict_free(&options); | |
2148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
2149 | ✗ | return ret; | |
2150 | |||
2151 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | if (pls->id3_deferred_extra && pls->ctx->nb_streams == 1) { |
2152 | ✗ | ff_id3v2_parse_apic(pls->ctx, pls->id3_deferred_extra); | |
2153 | ✗ | avformat_queue_attached_pictures(pls->ctx); | |
2154 | ✗ | ff_id3v2_parse_priv(pls->ctx, pls->id3_deferred_extra); | |
2155 | ✗ | ff_id3v2_free_extra_meta(&pls->id3_deferred_extra); | |
2156 | } | ||
2157 | |||
2158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (pls->is_id3_timestamped == -1) |
2159 | ✗ | av_log(s, AV_LOG_WARNING, "No expected HTTP requests have been made\n"); | |
2160 | |||
2161 | /* | ||
2162 | * For ID3 timestamped raw audio streams we need to detect the packet | ||
2163 | * durations to calculate timestamps in fill_timing_for_id3_timestamped_stream(), | ||
2164 | * but for other streams we can rely on our user calling avformat_find_stream_info() | ||
2165 | * on us if they want to. | ||
2166 | */ | ||
2167 |
2/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
12 | if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) { |
2168 | ✗ | if (seg && seg->key_type == KEY_SAMPLE_AES && pls->audio_setup_info.setup_data_length > 0 && | |
2169 | ✗ | pls->ctx->nb_streams == 1) | |
2170 | ✗ | ret = ff_hls_senc_parse_audio_setup_info(pls->ctx->streams[0], &pls->audio_setup_info); | |
2171 | else | ||
2172 | ✗ | ret = avformat_find_stream_info(pls->ctx, NULL); | |
2173 | |||
2174 | ✗ | if (ret < 0) | |
2175 | ✗ | return ret; | |
2176 | } | ||
2177 | |||
2178 | 12 | pls->has_noheader_flag = !!(pls->ctx->ctx_flags & AVFMTCTX_NOHEADER); | |
2179 | |||
2180 | /* Create new AVStreams for each stream in this playlist */ | ||
2181 | 12 | ret = update_streams_from_subdemuxer(s, pls); | |
2182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
2183 | ✗ | return ret; | |
2184 | |||
2185 | /* | ||
2186 | * Copy any metadata from playlist to main streams, but do not set | ||
2187 | * event flags. | ||
2188 | */ | ||
2189 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (pls->n_main_streams) |
2190 | 12 | av_dict_copy(&pls->main_streams[0]->metadata, pls->ctx->metadata, 0); | |
2191 | |||
2192 | 12 | add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_AUDIO); | |
2193 | 12 | add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_VIDEO); | |
2194 | 12 | add_metadata_from_renditions(s, pls, AVMEDIA_TYPE_SUBTITLE); | |
2195 | } | ||
2196 | |||
2197 | 12 | update_noheader_flag(s); | |
2198 | |||
2199 | 12 | return 0; | |
2200 | } | ||
2201 | |||
2202 | 6343 | static int recheck_discard_flags(AVFormatContext *s, int first) | |
2203 | { | ||
2204 | 6343 | HLSContext *c = s->priv_data; | |
2205 | 6343 | int i, changed = 0; | |
2206 | int cur_needed; | ||
2207 | |||