FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hlsenc.c
Date: 2026-09-19 14:27:34
Exec Total Coverage
Lines: 948 1856 51.1%
Functions: 31 47 66.0%
Branches: 622 1370 45.4%

Line Branch Exec Source
1 /*
2 * Apple HTTP Live Streaming segmenter
3 * Copyright (c) 2012, Luca Barbato
4 * Copyright (c) 2017 Akamai Technologies, Inc.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24 #include "config_components.h"
25 #include <stdint.h>
26 #include <time.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include "libavutil/attributes_internal.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/macros.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/bprint.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mem.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/log.h"
42 #include "libavutil/random_seed.h"
43 #include "libavutil/time.h"
44 #include "libavutil/time_internal.h"
45
46 #include "libavcodec/defs.h"
47
48 #include "avformat.h"
49 #include "avio_internal.h"
50 #if CONFIG_HTTP_PROTOCOL
51 #include "http.h"
52 #endif
53 #include "hlsplaylist.h"
54 #include "internal.h"
55 #include "mux.h"
56 #if CONFIG_MP4_MUXER
57 #include "movenc.h"
58 #endif
59 #include "os_support.h"
60 #include "url.h"
61
62 typedef enum {
63 HLS_START_SEQUENCE_AS_START_NUMBER = 0,
64 HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH = 1,
65 HLS_START_SEQUENCE_AS_FORMATTED_DATETIME = 2, // YYYYMMDDhhmmss
66 HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH = 3,
67 HLS_START_SEQUENCE_LAST, // unused
68 } StartSequenceSourceType;
69
70 typedef enum {
71 CODEC_ATTRIBUTE_WRITTEN = 0,
72 CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN,
73 } CodecAttributeStatus;
74
75 #define KEYSIZE 16
76 #define LINE_BUFFER_SIZE MAX_URL_SIZE
77 #define HLS_MICROSECOND_UNIT 1000000
78 #define BUFSIZE (16 * 1024)
79 #define POSTFIX_PATTERN "_%d"
80
81 typedef struct HLSSegment {
82 const char *filename;
83 const char *sub_filename;
84 double duration; /* in seconds */
85 int discont;
86 int64_t pos;
87 int64_t size;
88 unsigned var_stream_idx;
89
90 const char *key_uri;
91 char iv_string[KEYSIZE*2 + 1];
92
93 struct HLSSegment *next;
94 double discont_program_date_time;
95
96 char buf[]; /* for filename, sub_filename and key_uri */
97 } HLSSegment;
98
99 typedef enum HLSFlags {
100 // Generate a single media file and use byte ranges in the playlist.
101 HLS_SINGLE_FILE = (1 << 0),
102 HLS_DELETE_SEGMENTS = (1 << 1),
103 HLS_ROUND_DURATIONS = (1 << 2),
104 HLS_DISCONT_START = (1 << 3),
105 HLS_OMIT_ENDLIST = (1 << 4),
106 HLS_SPLIT_BY_TIME = (1 << 5),
107 HLS_APPEND_LIST = (1 << 6),
108 HLS_PROGRAM_DATE_TIME = (1 << 7),
109 HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), // include segment index in segment filenames when use_localtime e.g.: %%03d
110 HLS_SECOND_LEVEL_SEGMENT_DURATION = (1 << 9), // include segment duration (microsec) in segment filenames when use_localtime e.g.: %%09t
111 HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), // include segment size (bytes) in segment filenames when use_localtime e.g.: %%014s
112 HLS_TEMP_FILE = (1 << 11),
113 HLS_PERIODIC_REKEY = (1 << 12),
114 HLS_INDEPENDENT_SEGMENTS = (1 << 13),
115 HLS_I_FRAMES_ONLY = (1 << 14),
116 } HLSFlags;
117
118 typedef enum {
119 SEGMENT_TYPE_MPEGTS,
120 SEGMENT_TYPE_FMP4,
121 } SegmentType;
122
123 typedef struct VariantStream {
124 unsigned var_stream_idx;
125 unsigned number;
126 int64_t sequence;
127 const AVOutputFormat *oformat;
128 const AVOutputFormat *vtt_oformat;
129 AVIOContext *out;
130 AVIOContext *out_single_file;
131 int packets_written;
132 int init_range_length;
133 uint8_t *temp_buffer;
134 uint8_t *init_buffer;
135
136 AVFormatContext *avf;
137 AVFormatContext *vtt_avf;
138
139 int has_video;
140 int has_subtitle;
141 int new_start;
142 int start_pts_from_audio;
143 double dpp; // duration per packet
144 int64_t start_pts;
145 int64_t end_pts;
146 int64_t video_lastpos;
147 double duration; // last segment duration computed so far, in seconds
148 int64_t start_pos; // last segment starting position
149 int64_t size; // last segment size
150 int nb_entries;
151 int discontinuity_set;
152 int discontinuity;
153 int reference_stream_index;
154
155 int64_t total_size;
156 double total_duration;
157 int64_t avg_bitrate;
158 int64_t max_bitrate;
159
160 HLSSegment *segments;
161 HLSSegment *last_segment;
162 HLSSegment *old_segments;
163
164 char *basename_tmp;
165 char *basename;
166 char *vtt_basename;
167 char *vtt_m3u8_name;
168 char *m3u8_name;
169
170 double initial_prog_date_time;
171 char current_segment_final_filename_fmt[MAX_URL_SIZE]; // when renaming segments
172
173 char *fmp4_init_filename;
174 char *base_output_dirname;
175
176 int encrypt_started;
177
178 char key_file[LINE_BUFFER_SIZE + 1];
179 char key_uri[LINE_BUFFER_SIZE + 1];
180 char key_string[KEYSIZE*2 + 1];
181 char iv_string[KEYSIZE*2 + 1];
182
183 AVStream **streams;
184 char codec_attr[128];
185 CodecAttributeStatus attr_status;
186 unsigned int nb_streams;
187 int m3u8_created; /* status of media play-list creation */
188 int is_default; /* default status of audio group */
189 const char *language; /* audio language name */
190 const char *agroup; /* audio group name */
191 const char *sgroup; /* subtitle group name */
192 const char *ccgroup; /* closed caption group name */
193 const char *varname; /* variant name */
194 const char *subtitle_varname; /* subtitle variant name */
195 } VariantStream;
196
197 typedef struct ClosedCaptionsStream {
198 const char *ccgroup; /* closed caption group name */
199 const char *instreamid; /* closed captions INSTREAM-ID */
200 const char *language; /* closed captions language */
201 } ClosedCaptionsStream;
202
203 typedef struct HLSContext {
204 const AVClass *class; // Class for private options.
205 int64_t start_sequence;
206 uint32_t start_sequence_source_type; // enum StartSequenceSourceType
207
208 int64_t time; // Set by a private option.
209 int64_t init_time; // Set by a private option.
210 int max_nb_segments; // Set by a private option.
211 int hls_delete_threshold; // Set by a private option.
212 uint32_t flags; // enum HLSFlags
213 uint32_t pl_type; // enum PlaylistType
214 char *segment_filename;
215 char *fmp4_init_filename;
216 int segment_type;
217 int resend_init_file; ///< resend init file into disk after refresh m3u8
218
219 int use_localtime; ///< flag to expand filename with localtime
220 int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
221 int allowcache;
222 int64_t recording_time;
223 int64_t max_seg_size; // every segment file max size
224
225 char *baseurl;
226 char *vtt_format_options_str;
227 char *subtitle_filename;
228 AVDictionary *format_options;
229
230 int encrypt;
231 char *key;
232 char *key_url;
233 char *iv;
234 char *key_basename;
235 int encrypt_started;
236
237 char *key_info_file;
238 char key_file[LINE_BUFFER_SIZE + 1];
239 char key_uri[LINE_BUFFER_SIZE + 1];
240 char key_string[KEYSIZE*2 + 1];
241 char iv_string[KEYSIZE*2 + 1];
242 AVDictionary *vtt_format_options;
243
244 char *method;
245 char *user_agent;
246
247 VariantStream *var_streams;
248 unsigned int nb_varstreams;
249 ClosedCaptionsStream *cc_streams;
250 unsigned int nb_ccstreams;
251
252 int master_m3u8_created; /* status of master play-list creation */
253 char *master_m3u8_url; /* URL of the master m3u8 file */
254 int version; /* HLS version */
255 char *var_stream_map; /* user specified variant stream map string */
256 char *cc_stream_map; /* user specified closed caption streams map string */
257 char *master_pl_name;
258 unsigned int master_publish_rate;
259 int http_persistent;
260 AVIOContext *m3u8_out;
261 AVIOContext *sub_m3u8_out;
262 AVIOContext *http_delete;
263 int64_t timeout;
264 int ignore_io_errors;
265 char *headers;
266 int has_default_key; /* has DEFAULT field of var_stream_map */
267 int has_video_m3u8; /* has video stream m3u8 list */
268 } HLSContext;
269
270 static int strftime_expand(const char *fmt, char **dest)
271 {
272 int r = 1;
273 time_t now0;
274 struct tm *tm, tmpbuf;
275 char *buf;
276
277 buf = av_mallocz(MAX_URL_SIZE);
278 if (!buf)
279 return AVERROR(ENOMEM);
280
281 time(&now0);
282 tm = localtime_r(&now0, &tmpbuf);
283 r = strftime(buf, MAX_URL_SIZE, fmt, tm);
284 if (!r) {
285 av_free(buf);
286 return AVERROR(EINVAL);
287 }
288 *dest = buf;
289
290 return r;
291 }
292
293 194 static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, const char *filename,
294 AVDictionary **options)
295 {
296 194 HLSContext *hls = s->priv_data;
297
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
194 int http_base_proto = filename ? ff_is_http_proto(filename) : 0;
298 194 int err = AVERROR_MUXER_NOT_FOUND;
299
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
194 if (!*pb || !http_base_proto || !hls->http_persistent) {
300 194 err = s->io_open(s, pb, filename, AVIO_FLAG_WRITE, options);
301 #if CONFIG_HTTP_PROTOCOL
302 } else {
303 URLContext *http_url_context = ffio_geturlcontext(*pb);
304 av_assert0(http_url_context);
305 err = ff_http_do_new_request(http_url_context, filename);
306 if (err < 0)
307 ff_format_io_close(s, pb);
308
309 #endif
310 }
311 194 return err;
312 }
313
314 297 static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename)
315 {
316 297 HLSContext *hls = s->priv_data;
317
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 102 times.
297 int http_base_proto = filename ? ff_is_http_proto(filename) : 0;
318 297 int ret = 0;
319
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 193 times.
297 if (!*pb)
320 104 return ret;
321
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 193 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.
193 if (!http_base_proto || !hls->http_persistent || hls->key_info_file || hls->encrypt) {
322 193 ff_format_io_close(s, pb);
323 #if CONFIG_HTTP_PROTOCOL
324 } else {
325 URLContext *http_url_context = ffio_geturlcontext(*pb);
326 av_assert0(http_url_context);
327 avio_flush(*pb);
328 ret = ffurl_shutdown(http_url_context, AVIO_FLAG_WRITE);
329 #endif
330 }
331 193 return ret;
332 }
333
334 192 static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
335 {
336 192 int http_base_proto = ff_is_http_proto(s->url);
337
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (c->method) {
339 av_dict_set(options, "method", c->method, 0);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 } else if (http_base_proto) {
341 av_dict_set(options, "method", "PUT", 0);
342 }
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (c->user_agent)
344 av_dict_set(options, "user_agent", c->user_agent, 0);
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (c->http_persistent)
346 av_dict_set_int(options, "multiple_requests", 1, 0);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (c->timeout >= 0)
348 av_dict_set_int(options, "timeout", c->timeout, 0);
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (c->headers)
350 av_dict_set(options, "headers", c->headers, 0);
351 192 }
352
353 23 static void write_codec_attr(AVStream *st, VariantStream *vs)
354 {
355 23 int codec_strlen = strlen(vs->codec_attr);
356 char attr[32];
357 AVBPrint buffer;
358
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
360 return;
361
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22 times.
23 if (vs->attr_status == CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN)
362 1 return;
363
364 22 av_bprint_init_for_buffer(&buffer, attr, sizeof(attr));
365
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 13 times.
22 if (ff_make_codec_str(vs->avf, st->codecpar, &st->avg_frame_rate,
366 &buffer) < 0)
367 9 goto fail;
368
369 // Don't write the same attribute multiple times
370
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 if (!av_stristr(vs->codec_attr, attr)) {
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 snprintf(vs->codec_attr + codec_strlen,
372 sizeof(vs->codec_attr) - codec_strlen,
373 "%s%s", codec_strlen ? "," : "", attr);
374 }
375 13 return;
376
377 9 fail:
378 9 vs->codec_attr[0] = '\0';
379 9 vs->attr_status = CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN;
380 9 return;
381 }
382
383 static int replace_str_data_in_filename(char **s, const char *filename, char placeholder, const char *datastring)
384 {
385 const char *p;
386 char c;
387 int addchar_count;
388 int found_count = 0;
389 AVBPrint buf;
390 int ret;
391
392 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
393
394 p = filename;
395 for (;;) {
396 c = *p;
397 if (c == '\0')
398 break;
399 if (c == '%' && *(p+1) == '%') // %%
400 addchar_count = 2;
401 else if (c == '%' && *(p+1) == placeholder) {
402 av_bprintf(&buf, "%s", datastring);
403 p += 2;
404 addchar_count = 0;
405 found_count ++;
406 } else
407 addchar_count = 1;
408
409 if (addchar_count > 0) {
410 av_bprint_append_data(&buf, p, addchar_count);
411 p += addchar_count;
412 }
413 }
414 if (!av_bprint_is_complete(&buf)) {
415 av_bprint_finalize(&buf, NULL);
416 return AVERROR(ENOMEM);
417 }
418 if ((ret = av_bprint_finalize(&buf, s)) < 0)
419 return ret;
420 return found_count;
421 }
422
423 86 static int replace_int_data_in_filename(char **s, const char *filename, char placeholder, int64_t number)
424 {
425 const char *p;
426 char c;
427 int nd, addchar_count;
428 86 int found_count = 0;
429 AVBPrint buf;
430 int ret;
431
432 86 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
433
434 86 p = filename;
435 for (;;) {
436 7551 c = *p;
437
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 7465 times.
7551 if (c == '\0')
438 86 break;
439
3/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 7377 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
7465 if (c == '%' && *(p+1) == '%') // %%
440 addchar_count = 2;
441
6/6
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 7377 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 63 times.
✓ Branch 5 taken 2 times.
7465 else if (c == '%' && (av_isdigit(*(p+1)) || *(p+1) == placeholder)) {
442 86 nd = 0;
443 86 addchar_count = 1;
444
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 86 times.
132 while (av_isdigit(*(p + addchar_count))) {
445 46 nd = nd * 10 + *(p + addchar_count) - '0';
446 46 addchar_count++;
447 }
448
449
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (*(p + addchar_count) == placeholder) {
450
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 av_bprintf(&buf, "%0*"PRId64, (number < 0) ? nd : nd++, number);
451 86 p += (addchar_count + 1);
452 86 addchar_count = 0;
453 86 found_count++;
454 }
455
456 } else
457 7379 addchar_count = 1;
458
459 7465 av_bprint_append_data(&buf, p, addchar_count);
460 7465 p += addchar_count;
461 }
462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if (!av_bprint_is_complete(&buf)) {
463 av_bprint_finalize(&buf, NULL);
464 return AVERROR(ENOMEM);
465 }
466
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if ((ret = av_bprint_finalize(&buf, s)) < 0)
467 return ret;
468 86 return found_count;
469 }
470
471 6 static void write_styp(AVIOContext *pb)
472 {
473 6 avio_wb32(pb, 24);
474 6 ffio_wfourcc(pb, "styp");
475 6 ffio_wfourcc(pb, "msdh");
476 6 avio_wb32(pb, 0); /* minor */
477 6 ffio_wfourcc(pb, "msdh");
478 6 ffio_wfourcc(pb, "msix");
479 6 }
480
481 98 static int flush_dynbuf(VariantStream *vs, int *range_length)
482 {
483 98 AVFormatContext *ctx = vs->avf;
484
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (!ctx->pb) {
486 return AVERROR(EINVAL);
487 }
488
489 // flush
490 98 av_write_frame(ctx, NULL);
491
492 // write out to file
493 98 *range_length = avio_close_dyn_buf(ctx->pb, &vs->temp_buffer);
494 98 ctx->pb = NULL;
495 98 avio_write(vs->out, vs->temp_buffer, *range_length);
496 98 avio_flush(vs->out);
497
498 // re-open buffer
499 98 return avio_open_dyn_buf(&ctx->pb);
500 }
501
502 static void reflush_dynbuf(VariantStream *vs, int *range_length)
503 {
504 // re-open buffer
505 avio_write(vs->out, vs->temp_buffer, *range_length);
506 }
507
508 static int hls_delete_file(HLSContext *hls, AVFormatContext *avf,
509 char *path, const char *proto)
510 {
511 if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
512 AVDictionary *opt = NULL;
513 int ret;
514
515 set_http_options(avf, &opt, hls);
516 av_dict_set(&opt, "method", "DELETE", 0);
517
518 ret = hlsenc_io_open(avf, &hls->http_delete, path, &opt);
519 av_dict_free(&opt);
520 if (ret < 0)
521 return hls->ignore_io_errors ? 1 : ret;
522
523 //Nothing to write
524 hlsenc_io_close(avf, &hls->http_delete, path);
525 } else if (unlink(path) < 0) {
526 av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
527 path, strerror(errno));
528 }
529 return 0;
530 }
531
532 static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
533 VariantStream *vs)
534 {
535
536 HLSSegment *segment, *previous_segment = NULL;
537 float playlist_duration = 0.0f;
538 int ret = 0;
539 int segment_cnt = 0;
540 AVBPrint path;
541 const char *dirname = NULL;
542 char *dirname_r = NULL;
543 char *dirname_repl = NULL;
544 const char *vtt_dirname = NULL;
545 char *vtt_dirname_r = NULL;
546 const char *proto = NULL;
547
548 av_bprint_init(&path, 0, AV_BPRINT_SIZE_UNLIMITED);
549
550 segment = vs->segments;
551 while (segment) {
552 playlist_duration += segment->duration;
553 segment = segment->next;
554 }
555
556 segment = vs->old_segments;
557 segment_cnt = 0;
558 while (segment) {
559 playlist_duration -= segment->duration;
560 previous_segment = segment;
561 segment = previous_segment->next;
562 segment_cnt++;
563 if (playlist_duration <= -previous_segment->duration) {
564 previous_segment->next = NULL;
565 break;
566 }
567 if (segment_cnt >= hls->hls_delete_threshold) {
568 previous_segment->next = NULL;
569 break;
570 }
571 }
572
573 if (segment && !hls->use_localtime_mkdir) {
574 dirname_r = hls->segment_filename ? av_strdup(hls->segment_filename): av_strdup(vs->avf->url);
575 dirname = av_dirname(dirname_r);
576 }
577
578 /* if %v is present in the file's directory
579 * all segment belongs to the same variant, so do it only once before the loop*/
580 if (dirname && av_stristr(dirname, "%v")) {
581 if (!vs->varname) {
582 if (replace_int_data_in_filename(&dirname_repl, dirname, 'v', segment->var_stream_idx) < 1) {
583 ret = AVERROR(EINVAL);
584 goto fail;
585 }
586 } else {
587 if (replace_str_data_in_filename(&dirname_repl, dirname, 'v', vs->varname) < 1) {
588 ret = AVERROR(EINVAL);
589 goto fail;
590 }
591 }
592
593 dirname = dirname_repl;
594 }
595
596 while (segment) {
597 av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
598 segment->filename);
599 if (!hls->use_localtime_mkdir) // segment->filename contains basename only
600 av_bprintf(&path, "%s/", dirname);
601 av_bprintf(&path, "%s", segment->filename);
602
603 if (!av_bprint_is_complete(&path)) {
604 ret = AVERROR(ENOMEM);
605 goto fail;
606 }
607
608 proto = avio_find_protocol_name(s->url);
609 if (ret = hls_delete_file(hls, s, path.str, proto))
610 goto fail;
611
612 if ((segment->sub_filename[0] != '\0')) {
613 vtt_dirname_r = av_strdup(vs->vtt_avf->url);
614 vtt_dirname = av_dirname(vtt_dirname_r);
615
616 av_bprint_clear(&path);
617 av_bprintf(&path, "%s/%s", vtt_dirname, segment->sub_filename);
618 av_freep(&vtt_dirname_r);
619
620 if (!av_bprint_is_complete(&path)) {
621 ret = AVERROR(ENOMEM);
622 goto fail;
623 }
624
625 if (ret = hls_delete_file(hls, s, path.str, proto))
626 goto fail;
627 }
628 av_bprint_clear(&path);
629 previous_segment = segment;
630 segment = previous_segment->next;
631 av_freep(&previous_segment);
632 }
633
634 fail:
635 av_bprint_finalize(&path, NULL);
636 av_freep(&dirname_r);
637 av_freep(&dirname_repl);
638
639 return ret;
640 }
641
642 static int do_encrypt(AVFormatContext *s, VariantStream *vs)
643 {
644 HLSContext *hls = s->priv_data;
645 int ret;
646 int len;
647 AVIOContext *pb;
648 uint8_t key[KEYSIZE];
649 char * key_basename_source = (hls->master_m3u8_url) ? hls->master_m3u8_url : s->url;
650
651 len = strlen(key_basename_source) + 4 + 1;
652 hls->key_basename = av_mallocz(len);
653 if (!hls->key_basename)
654 return AVERROR(ENOMEM);
655
656 av_strlcpy(hls->key_basename, key_basename_source, len);
657 av_strlcat(hls->key_basename, ".key", len);
658
659 if (hls->key_url) {
660 av_strlcpy(hls->key_file, hls->key_url, sizeof(hls->key_file));
661 av_strlcpy(hls->key_uri, hls->key_url, sizeof(hls->key_uri));
662 } else {
663 av_strlcpy(hls->key_file, hls->key_basename, sizeof(hls->key_file));
664 av_strlcpy(hls->key_uri, hls->key_basename, sizeof(hls->key_uri));
665 }
666
667 if (!*hls->iv_string) {
668 uint8_t iv[16] = { 0 };
669 char buf[33];
670
671 if (!hls->iv) {
672 AV_WB64(iv + 8, vs->sequence);
673 } else if (strlen(hls->iv) < sizeof(iv)) {
674 av_log(hls, AV_LOG_ERROR, "hls_enc_iv is shorter than %zu bytes\n", sizeof(iv));
675 return AVERROR(EINVAL);
676 } else {
677 memcpy(iv, hls->iv, sizeof(iv));
678 }
679 ff_data_to_hex(buf, iv, sizeof(iv), 0);
680 memcpy(hls->iv_string, buf, sizeof(hls->iv_string));
681 }
682
683 if (!*hls->key_uri) {
684 av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
685 return AVERROR(EINVAL);
686 }
687
688 if (!*hls->key_file) {
689 av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
690 return AVERROR(EINVAL);
691 }
692
693 if (!*hls->key_string) {
694 AVDictionary *options = NULL;
695 if (!hls->key) {
696 if ((ret = av_random_bytes(key, sizeof(key))) < 0) {
697 av_log(s, AV_LOG_ERROR, "Cannot generate a strong random key\n");
698 return ret;
699 }
700 } else if (strlen(hls->key) < sizeof(key)) {
701 av_log(hls, AV_LOG_ERROR, "hls_enc_key is shorter than %zu bytes\n", sizeof(key));
702 return AVERROR(EINVAL);
703 } else {
704 memcpy(key, hls->key, sizeof(key));
705 }
706
707 ff_data_to_hex(hls->key_string, key, sizeof(key), 0);
708 set_http_options(s, &options, hls);
709 ret = s->io_open(s, &pb, hls->key_file, AVIO_FLAG_WRITE, &options);
710 av_dict_free(&options);
711 if (ret < 0)
712 return ret;
713 avio_seek(pb, 0, SEEK_CUR);
714 avio_write(pb, key, KEYSIZE);
715 ff_format_io_close(s, &pb);
716 }
717 return 0;
718 }
719
720
721 static int hls_encryption_start(AVFormatContext *s, VariantStream *vs)
722 {
723 HLSContext *hls = s->priv_data;
724 int ret;
725 AVIOContext *pb;
726 uint8_t key[KEYSIZE];
727 AVDictionary *options = NULL;
728
729 set_http_options(s, &options, hls);
730 ret = s->io_open(s, &pb, hls->key_info_file, AVIO_FLAG_READ, &options);
731 av_dict_free(&options);
732 if (ret < 0) {
733 av_log(hls, AV_LOG_ERROR,
734 "error opening key info file %s\n", hls->key_info_file);
735 return ret;
736 }
737
738 ff_get_line(pb, vs->key_uri, sizeof(vs->key_uri));
739 vs->key_uri[strcspn(vs->key_uri, "\r\n")] = '\0';
740
741 ff_get_line(pb, vs->key_file, sizeof(vs->key_file));
742 vs->key_file[strcspn(vs->key_file, "\r\n")] = '\0';
743
744 ff_get_line(pb, vs->iv_string, sizeof(vs->iv_string));
745 vs->iv_string[strcspn(vs->iv_string, "\r\n")] = '\0';
746
747 ff_format_io_close(s, &pb);
748
749 if (!*vs->key_uri) {
750 av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
751 return AVERROR(EINVAL);
752 }
753
754 if (!*vs->key_file) {
755 av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
756 return AVERROR(EINVAL);
757 }
758
759 set_http_options(s, &options, hls);
760 ret = s->io_open(s, &pb, vs->key_file, AVIO_FLAG_READ, &options);
761 av_dict_free(&options);
762 if (ret < 0) {
763 av_log(hls, AV_LOG_ERROR, "error opening key file %s\n", vs->key_file);
764 return ret;
765 }
766
767 ret = avio_read(pb, key, sizeof(key));
768 ff_format_io_close(s, &pb);
769 if (ret != sizeof(key)) {
770 av_log(hls, AV_LOG_ERROR, "error reading key file %s\n", vs->key_file);
771 if (ret >= 0 || ret == AVERROR_EOF)
772 ret = AVERROR(EINVAL);
773 return ret;
774 }
775 ff_data_to_hex(vs->key_string, key, sizeof(key), 0);
776
777 return 0;
778 }
779
780 22 static int hls_mux_init(AVFormatContext *s, VariantStream *vs)
781 {
782 22 AVDictionary *options = NULL;
783 22 HLSContext *hls = s->priv_data;
784 AVFormatContext *oc;
785 22 AVFormatContext *vtt_oc = NULL;
786
4/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18 times.
22 int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
787 int remaining_options;
788 int i, ret;
789
790 22 ret = avformat_alloc_output_context2(&vs->avf, vs->oformat, NULL, NULL);
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
792 return ret;
793 22 oc = vs->avf;
794
795 22 oc->url = av_strdup("");
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!oc->url)
797 return AVERROR(ENOMEM);
798
799 22 oc->interrupt_callback = s->interrupt_callback;
800 22 oc->max_delay = s->max_delay;
801 22 oc->opaque = s->opaque;
802 22 oc->io_open = s->io_open;
803 22 oc->io_close2 = s->io_close2;
804 22 oc->strict_std_compliance = s->strict_std_compliance;
805 22 oc->flags = s->flags;
806 22 av_dict_copy(&oc->metadata, s->metadata, 0);
807
808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vs->vtt_oformat) {
809 ret = avformat_alloc_output_context2(&vs->vtt_avf, vs->vtt_oformat, NULL, NULL);
810 if (ret < 0)
811 return ret;
812 vtt_oc = vs->vtt_avf;
813 vtt_oc->flags = s->flags;
814 av_dict_copy(&vtt_oc->metadata, s->metadata, 0);
815 }
816
817
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
44 for (i = 0; i < vs->nb_streams; i++) {
818 AVStream *st;
819 AVFormatContext *loc;
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vs->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
821 loc = vtt_oc;
822 else
823 22 loc = oc;
824
825
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if (!(st = avformat_new_stream(loc, NULL)))
826 return AVERROR(ENOMEM);
827 22 ret = avcodec_parameters_copy(st->codecpar, vs->streams[i]->codecpar);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
829 return ret;
830
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (!oc->oformat->codec_tag ||
831
3/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
5 av_codec_get_id (oc->oformat->codec_tag, vs->streams[i]->codecpar->codec_tag) == st->codecpar->codec_id ||
832 2 av_codec_get_tag(oc->oformat->codec_tag, vs->streams[i]->codecpar->codec_id) <= 0) {
833 20 st->codecpar->codec_tag = vs->streams[i]->codecpar->codec_tag;
834 } else {
835 2 st->codecpar->codec_tag = 0;
836 }
837
838 22 st->sample_aspect_ratio = vs->streams[i]->sample_aspect_ratio;
839 22 st->time_base = vs->streams[i]->time_base;
840 22 av_dict_copy(&st->metadata, vs->streams[i]->metadata, 0);
841 22 st->id = vs->streams[i]->id;
842 }
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 for (i = 0; i < s->nb_programs; i++) {
845 ret = av_program_copy(oc, (const AVFormatContext *)s, s->programs[i]->id, 0);
846 if (ret < 0) {
847 av_log(s, AV_LOG_ERROR, "unable to transfer program %d to child muxer\n", s->programs[i]->id);
848 return ret;
849 }
850 }
851
852 22 vs->start_pos = 0;
853 22 vs->new_start = 1;
854
855
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4 && hls->max_seg_size > 0) {
856 if (hls->http_persistent > 0) {
857 //TODO: Support fragment fmp4 for http persistent in HLS muxer.
858 av_log(s, AV_LOG_WARNING, "http persistent mode is currently unsupported for fragment mp4 in the HLS muxer.\n");
859 }
860 if (hls->max_seg_size > 0) {
861 av_log(s, AV_LOG_WARNING, "Multi-file byterange mode is currently unsupported in the HLS muxer.\n");
862 return AVERROR_PATCHWELCOME;
863 }
864 }
865
866
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = avio_open_dyn_buf(&oc->pb)) < 0)
867 return ret;
868
869
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
870 3 set_http_options(s, &options, hls);
871
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (byterange_mode) {
872 1 ret = hlsenc_io_open(s, &vs->out, vs->basename, &options);
873 } else {
874 2 ret = hlsenc_io_open(s, &vs->out, vs->base_output_dirname, &options);
875 }
876 3 av_dict_free(&options);
877 }
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
879 av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", vs->fmp4_init_filename);
880 return ret;
881 }
882
883 22 av_dict_copy(&options, hls->format_options, 0);
884
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
885 3 av_dict_set(&options, "fflags", "-autobsf", 0);
886 3 av_dict_set(&options, "movflags", "+frag_custom+dash+delay_moov", AV_DICT_APPEND);
887 } else {
888 /* We only require one PAT/PMT per segment. */
889 char period[21];
890 19 snprintf(period, sizeof(period), "%d", (INT_MAX / 2) - 1);
891 19 av_dict_set(&options, "sdt_period", period, AV_DICT_DONT_OVERWRITE);
892 19 av_dict_set(&options, "pat_period", period, AV_DICT_DONT_OVERWRITE);
893 }
894 22 ret = avformat_init_output(oc, &options);
895 22 remaining_options = av_dict_count(options);
896 22 av_dict_free(&options);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
898 return ret;
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (remaining_options) {
900 av_log(s, AV_LOG_ERROR, "Some of the provided format options are not recognized\n");
901 return AVERROR(EINVAL);
902 }
903 22 avio_flush(oc->pb);
904 22 return 0;
905 }
906
907 193 static HLSSegment *find_segment_by_filename(HLSSegment *segment, const char *filename)
908 {
909
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 174 times.
396 while (segment) {
910
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 203 times.
222 if (!av_strcasecmp(segment->filename,filename))
911 19 return segment;
912 203 segment = segment->next;
913 }
914 174 return (HLSSegment *) NULL;
915 }
916
917 106 static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls,
918 VariantStream *vs,
919 double duration, int64_t pos, int64_t size)
920 {
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
922 strlen(vs->current_segment_final_filename_fmt)) {
923 char * new_url = av_strdup(vs->current_segment_final_filename_fmt);
924 if (!new_url) {
925 return AVERROR(ENOMEM);
926 }
927 ff_format_set_url(vs->avf, new_url);
928 if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
929 char *filename = NULL;
930 if (replace_int_data_in_filename(&filename, vs->avf->url, 's', pos + size) < 1) {
931 av_log(hls, AV_LOG_ERROR,
932 "Invalid second level segment filename template '%s', "
933 "you can try to remove second_level_segment_size flag\n",
934 vs->avf->url);
935 av_freep(&filename);
936 return AVERROR(EINVAL);
937 }
938 ff_format_set_url(vs->avf, filename);
939 }
940 if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
941 char *filename = NULL;
942 if (replace_int_data_in_filename(&filename, vs->avf->url,
943 't', (int64_t)round(duration * HLS_MICROSECOND_UNIT)) < 1) {
944 av_log(hls, AV_LOG_ERROR,
945 "Invalid second level segment filename template '%s', "
946 "you can try to remove second_level_segment_duration flag\n",
947 vs->avf->url);
948 av_freep(&filename);
949 return AVERROR(EINVAL);
950 }
951 ff_format_set_url(vs->avf, filename);
952 }
953 }
954 106 return 0;
955 }
956
957 22 static int sls_flag_check_duration_size_index(HLSContext *hls)
958 {
959 22 int ret = 0;
960
961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
962 av_log(hls, AV_LOG_ERROR,
963 "second_level_segment_duration hls_flag requires strftime to be true\n");
964 ret = AVERROR(EINVAL);
965 }
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
967 av_log(hls, AV_LOG_ERROR,
968 "second_level_segment_size hls_flag requires strfime to be true\n");
969 ret = AVERROR(EINVAL);
970 }
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) {
972 av_log(hls, AV_LOG_ERROR,
973 "second_level_segment_index hls_flag requires strftime to be true\n");
974 ret = AVERROR(EINVAL);
975 }
976
977 22 return ret;
978 }
979
980 static int sls_flag_check_duration_size(HLSContext *hls, VariantStream *vs)
981 {
982 const char *proto = avio_find_protocol_name(vs->basename);
983 int segment_renaming_ok = proto && !strcmp(proto, "file");
984 int ret = 0;
985
986 if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) && !segment_renaming_ok) {
987 av_log(hls, AV_LOG_ERROR,
988 "second_level_segment_duration hls_flag works only with file protocol segment names\n");
989 ret = AVERROR(EINVAL);
990 }
991 if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) && !segment_renaming_ok) {
992 av_log(hls, AV_LOG_ERROR,
993 "second_level_segment_size hls_flag works only with file protocol segment names\n");
994 ret = AVERROR(EINVAL);
995 }
996
997 return ret;
998 }
999
1000 85 static void sls_flag_file_rename(HLSContext *hls, VariantStream *vs, char *old_filename) {
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
1002 strlen(vs->current_segment_final_filename_fmt)) {
1003 ff_rename(old_filename, vs->avf->url, hls);
1004 }
1005 85 }
1006
1007 static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, VariantStream *vs)
1008 {
1009 if (c->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) {
1010 char *filename = NULL;
1011 if (replace_int_data_in_filename(&filename,
1012 oc->url, 'd', vs->sequence) < 1) {
1013 av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
1014 "you can try to remove second_level_segment_index flag\n",
1015 oc->url);
1016 av_freep(&filename);
1017 return AVERROR(EINVAL);
1018 }
1019 ff_format_set_url(oc, filename);
1020 }
1021 if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
1022 av_strlcpy(vs->current_segment_final_filename_fmt, oc->url,
1023 sizeof(vs->current_segment_final_filename_fmt));
1024 if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
1025 char *filename = NULL;
1026 if (replace_int_data_in_filename(&filename, oc->url, 's', 0) < 1) {
1027 av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
1028 "you can try to remove second_level_segment_size flag\n",
1029 oc->url);
1030 av_freep(&filename);
1031 return AVERROR(EINVAL);
1032 }
1033 ff_format_set_url(oc, filename);
1034 }
1035 if (c->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
1036 char *filename = NULL;
1037 if (replace_int_data_in_filename(&filename, oc->url, 't', 0) < 1) {
1038 av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
1039 "you can try to remove second_level_segment_duration flag\n",
1040 oc->url);
1041 av_freep(&filename);
1042 return AVERROR(EINVAL);
1043 }
1044 ff_format_set_url(oc, filename);
1045 }
1046 }
1047 return 0;
1048 }
1049
1050 /* Create a new segment and append it to the segment list */
1051 106 static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
1052 VariantStream *vs, double duration, int64_t pos,
1053 int64_t size)
1054 {
1055 106 HLSSegment en0 = {0};
1056 106 HLSSegment *en = &en0;
1057 const char *filename;
1058
4/4
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 80 times.
106 int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
1059 int ret;
1060 AVBPrint bp;
1061
1062 106 vs->total_size += size;
1063 106 vs->total_duration += duration;
1064
1/2
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
106 if (duration > 0.5) {
1065 // Don't include the final, possibly very short segment in the
1066 // calculation of the max bitrate.
1067 106 int cur_bitrate = (int)(8 * size / duration);
1068
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 48 times.
106 if (cur_bitrate > vs->max_bitrate)
1069 58 vs->max_bitrate = cur_bitrate;
1070 }
1071
1/2
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
106 if (vs->total_duration > 0)
1072 106 vs->avg_bitrate = (int)(8 * vs->total_size / vs->total_duration);
1073
1074 106 en->var_stream_idx = vs->var_stream_idx;
1075 106 ret = sls_flags_filename_process(s, hls, vs, duration, pos, size);
1076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (ret < 0)
1077 return ret;
1078
1079 106 filename = av_basename(vs->avf->url);
1080
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (hls->use_localtime_mkdir) {
1082 filename = vs->avf->url;
1083 }
1084
4/6
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 19 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 87 times.
106 if (vs->nb_entries <= 5000 && (find_segment_by_filename(vs->segments, filename) || find_segment_by_filename(vs->old_segments, filename))
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 && !byterange_mode) {
1086 av_log(hls, AV_LOG_WARNING, "Duplicated segment filename detected: %s\n", filename);
1087 }
1088
1089 106 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1090 106 av_bprintf(&bp, "%s%c", filename, 0);
1091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 av_bprintf(&bp, "%s%c", vs->has_subtitle ? av_basename(vs->vtt_avf->url) : "", 0);
1092
1093 106 en->duration = duration;
1094 106 en->pos = pos;
1095 106 en->size = size;
1096
1097
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 105 times.
106 if (vs->discontinuity) {
1098 1 en->discont = 1;
1099 1 vs->discontinuity = 0;
1100 }
1101
1102
2/4
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 106 times.
106 if (hls->key_info_file || hls->encrypt) {
1103 av_bprintf(&bp, "%s%c", vs->key_uri, 0);
1104 av_strlcpy(en->iv_string, vs->iv_string, sizeof(en->iv_string));
1105 } else {
1106 106 av_bprint_chars(&bp, 0, 1);
1107 }
1108
1109 106 en = ff_bprint_finalize_as_fam(&bp, &en0, offsetof(HLSSegment, buf));
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (!en)
1111 return AVERROR(ENOMEM);
1112 #define NEXT(s) ((s) + strlen(s) + 1)
1113 106 en->filename = en->buf;
1114 106 en->sub_filename = NEXT(en->filename);
1115 106 en->key_uri = NEXT(en->sub_filename);
1116 #undef NEXT
1117
1118
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 84 times.
106 if (!vs->segments)
1119 22 vs->segments = en;
1120 else
1121 84 vs->last_segment->next = en;
1122
1123 106 vs->last_segment = en;
1124
1125 // EVENT or VOD playlists imply sliding window cannot be used
1126
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 93 times.
106 if (hls->pl_type != PLAYLIST_TYPE_NONE)
1127 13 hls->max_nb_segments = 0;
1128
1129
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 23 times.
106 if (hls->max_nb_segments && vs->nb_entries >= hls->max_nb_segments) {
1130 8 en = vs->segments;
1131
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if (!en->next->discont_program_date_time && !en->discont_program_date_time)
1132 8 vs->initial_prog_date_time += en->duration;
1133 8 vs->segments = en->next;
1134
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (en && hls->flags & HLS_DELETE_SEGMENTS &&
1135 !(hls->flags & HLS_SINGLE_FILE)) {
1136 en->next = vs->old_segments;
1137 vs->old_segments = en;
1138 if ((ret = hls_delete_old_segments(s, hls, vs)) < 0)
1139 return ret;
1140 } else
1141 8 av_freep(&en);
1142 } else
1143 98 vs->nb_entries++;
1144
1145
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 96 times.
106 if (hls->max_seg_size > 0) {
1146 10 return 0;
1147 }
1148 96 vs->sequence++;
1149
1150 96 return 0;
1151 }
1152
1153 static int extract_segment_number(const char *filename)
1154 {
1155 const char *dot = strrchr(filename, '.');
1156 const char *num_start;
1157 char *end;
1158 long value;
1159
1160 if (!dot)
1161 return -1;
1162 if (dot == filename)
1163 return -1;
1164
1165 num_start = dot;
1166 while (num_start > filename &&
1167 num_start[-1] >= '0' && num_start[-1] <= '9')
1168 num_start--;
1169 if (num_start == dot)
1170 return -1;
1171
1172 errno = 0;
1173 value = strtol(num_start, &end, 10);
1174 if (errno == ERANGE || end != dot || value > INT_MAX)
1175 return -1;
1176
1177 return (int)value;
1178 }
1179
1180 1 static int parse_playlist(AVFormatContext *s, const char *url, VariantStream *vs)
1181 {
1182 1 HLSContext *hls = s->priv_data;
1183 AVIOContext *in;
1184 1 int ret = 0, is_segment = 0;
1185 int64_t new_start_pos;
1186 char line[MAX_URL_SIZE];
1187 const char *ptr;
1188 const char *end;
1189 1 double discont_program_date_time = 0;
1190
1191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = s->io_open(s, &in, url, AVIO_FLAG_READ, NULL)) < 0)
1192 return ret;
1193
1194 1 ff_get_chomp_line(in, line, sizeof(line));
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (strcmp(line, "#EXTM3U")) {
1196 ret = AVERROR_INVALIDDATA;
1197 goto fail;
1198 }
1199
1200 1 vs->discontinuity = 0;
1201
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
11 while (!avio_feof(in)) {
1202 10 ff_get_chomp_line(in, line, sizeof(line));
1203
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
10 if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
1204 1 int64_t tmp_sequence = strtoll(ptr, NULL, 10);
1205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (tmp_sequence < vs->sequence)
1206 av_log(hls, AV_LOG_VERBOSE,
1207 "Found playlist sequence number was smaller """
1208 "than specified start sequence number: %"PRId64" < %"PRId64", "
1209 "omitting\n", tmp_sequence, hls->start_sequence);
1210 else {
1211 1 av_log(hls, AV_LOG_DEBUG, "Found playlist sequence number: %"PRId64"\n", tmp_sequence);
1212 1 vs->sequence = tmp_sequence;
1213 }
1214
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) {
1215 is_segment = 1;
1216 vs->discontinuity = 1;
1217
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7 times.
9 } else if (av_strstart(line, "#EXTINF:", &ptr)) {
1218 2 is_segment = 1;
1219 2 vs->duration = atof(ptr);
1220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 } else if (av_stristart(line, "#EXT-X-KEY:", &ptr)) {
1221 ptr = av_stristr(line, "URI=\"");
1222 if (ptr) {
1223 ptr += strlen("URI=\"");
1224 end = strchr(ptr, '"');
1225 if (end) {
1226 av_strlcpy(vs->key_uri, ptr,
1227 FFMIN(end - ptr + 1, sizeof(vs->key_uri)));
1228 } else {
1229 ret = AVERROR_INVALIDDATA;
1230 goto fail;
1231 }
1232 }
1233
1234 ptr = av_stristr(line, "IV=0x");
1235 if (ptr) {
1236 ptr += strlen("IV=0x");
1237 end = av_stristr(ptr, ",");
1238 if (end) {
1239 av_strlcpy(vs->iv_string, ptr, FFMIN(end - ptr + 1, sizeof(vs->iv_string)));
1240 } else {
1241 av_strlcpy(vs->iv_string, ptr, sizeof(vs->iv_string));
1242 }
1243 }
1244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 } else if (av_strstart(line, "#EXT-X-PROGRAM-DATE-TIME:", &ptr)) {
1245 struct tm program_date_time = { 0 };
1246 double ms = 0;
1247 char *q = av_small_strptime(ptr, "%Y-%m-%dT%H:%M:%S", &program_date_time);
1248
1249 if (!q) {
1250 ret = AVERROR_INVALIDDATA;
1251 goto fail;
1252 }
1253 if (*q == '.')
1254 ms = atof(q + 1);
1255 program_date_time.tm_isdst = -1;
1256
1257 errno = 0;
1258 time_t t = mktime(&program_date_time);
1259 if (t == (time_t)-1 && errno == EOVERFLOW) {
1260 ret = AVERROR_INVALIDDATA;
1261 goto fail;
1262 }
1263 discont_program_date_time = t;
1264
1265 discont_program_date_time += (double)(ms / 1000);
1266
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
7 } else if (av_strstart(line, "#", NULL)) {
1267 4 continue;
1268
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 } else if (line[0]) {
1269
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (is_segment) {
1270 2 char *new_file = av_strdup(line);
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!new_file) {
1272 ret = AVERROR(ENOMEM);
1273 goto fail;
1274 }
1275 2 ff_format_set_url(vs->avf, new_file);
1276
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (vs->has_subtitle) {
1278 int vtt_index = extract_segment_number(line);
1279 const char *vtt_basename = av_basename(vs->vtt_basename);
1280 char *vtt_file = NULL;
1281 ret = replace_int_data_in_filename(&vtt_file, vtt_basename, 'd', vtt_index);
1282 if (ret < 0 || !vtt_file) {
1283 ret = AVERROR(ENOMEM);
1284 goto fail;
1285 }
1286
1287 ff_format_set_url(vs->vtt_avf, vtt_file);
1288 }
1289
1290 2 is_segment = 0;
1291 2 new_start_pos = avio_tell(vs->avf->pb);
1292 2 vs->size = new_start_pos - vs->start_pos;
1293 2 ret = hls_append_segment(s, hls, vs, vs->duration, vs->start_pos, vs->size);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (discont_program_date_time) {
1295 vs->last_segment->discont_program_date_time = discont_program_date_time;
1296 discont_program_date_time += vs->duration;
1297 }
1298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1299 goto fail;
1300 2 vs->start_pos = new_start_pos;
1301 }
1302 }
1303 }
1304
1305 1 fail:
1306 1 ff_format_io_close(s, &in);
1307 1 return ret;
1308 }
1309
1310 44 static void hls_free_segments(HLSSegment *p)
1311 {
1312 HLSSegment *en;
1313
1314
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 44 times.
142 while (p) {
1315 98 en = p;
1316 98 p = p->next;
1317 98 av_freep(&en);
1318 }
1319 44 }
1320
1321 static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
1322 {
1323 size_t len = strlen(oc->url);
1324 char *final_filename = av_strdup(oc->url);
1325 int ret;
1326
1327 if (!final_filename)
1328 return AVERROR(ENOMEM);
1329 final_filename[len-4] = '\0';
1330 ret = ff_rename(oc->url, final_filename, s);
1331 oc->url[len-4] = '\0';
1332 av_freep(&final_filename);
1333 return ret;
1334 }
1335
1336 9 static const char* get_relative_url(const char *master_url, const char *media_url)
1337 {
1338 9 const char *p = strrchr(master_url, '/');
1339 9 size_t base_len = 0;
1340
1341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!p) p = strrchr(master_url, '\\');
1342
1343
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (p) {
1344 9 base_len = p - master_url;
1345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (av_strncasecmp(master_url, media_url, base_len)) {
1346 av_log(NULL, AV_LOG_WARNING, "Unable to find relative url\n");
1347 return NULL;
1348 }
1349 } else {
1350 return media_url;
1351 }
1352
1353 9 return media_url + base_len + 1;
1354 }
1355
1356 3 static int64_t get_stream_bit_rate(AVStream *stream)
1357 {
1358 3 const AVPacketSideData *sd = av_packet_side_data_get(
1359 3 stream->codecpar->coded_side_data, stream->codecpar->nb_coded_side_data,
1360 AV_PKT_DATA_CPB_PROPERTIES
1361 );
1362
1363
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (stream->codecpar->bit_rate)
1364 3 return stream->codecpar->bit_rate;
1365 else if (sd) {
1366 AVCPBProperties *props = (AVCPBProperties*)sd->data;
1367 return props->max_bitrate;
1368 }
1369
1370 return 0;
1371 }
1372
1373 6 static int create_master_playlist(AVFormatContext *s,
1374 VariantStream * const input_vs,
1375 int final)
1376 {
1377 6 HLSContext *hls = s->priv_data;
1378 VariantStream *vs, *temp_vs;
1379 AVStream *vid_st, *aud_st;
1380 6 AVDictionary *options = NULL;
1381 unsigned int i, j;
1382 int ret, bandwidth, avg_bandwidth;
1383 6 const char *m3u8_rel_name = NULL;
1384 6 const char *vtt_m3u8_rel_name = NULL;
1385 const char *ccgroup;
1386 6 const char *sgroup = NULL;
1387 ClosedCaptionsStream *ccs;
1388 6 const char *proto = avio_find_protocol_name(hls->master_m3u8_url);
1389
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 int is_file_proto = proto && !strcmp(proto, "file");
1390
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 int use_temp_file = is_file_proto && ((hls->flags & HLS_TEMP_FILE) || hls->master_publish_rate);
1391 char temp_filename[MAX_URL_SIZE];
1392 int nb_channels;
1393
1394 6 input_vs->m3u8_created = 1;
1395
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!hls->master_m3u8_created) {
1396 /* For the first time, wait until all the media playlists are created */
1397
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < hls->nb_varstreams; i++)
1398
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!hls->var_streams[i].m3u8_created)
1399 1 return 0;
1400 } else {
1401 /* Keep publishing the master playlist at the configured rate */
1402
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 if ((&hls->var_streams[0] != input_vs || !hls->master_publish_rate ||
1403
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 input_vs->number % hls->master_publish_rate) && !final)
1404 2 return 0;
1405 }
1406
1407 3 set_http_options(s, &options, hls);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 snprintf(temp_filename, sizeof(temp_filename), use_temp_file ? "%s.tmp" : "%s", hls->master_m3u8_url);
1409 3 ret = hlsenc_io_open(s, &hls->m3u8_out, temp_filename, &options);
1410 3 av_dict_free(&options);
1411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0) {
1412 av_log(s, AV_LOG_ERROR, "Failed to open master play list file '%s'\n",
1413 temp_filename);
1414 goto fail;
1415 }
1416
1417 3 ff_hls_write_playlist_version(hls->m3u8_out, hls->version);
1418
1419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 for (i = 0; i < hls->nb_ccstreams; i++) {
1420 ccs = &(hls->cc_streams[i]);
1421 avio_printf(hls->m3u8_out, "#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS");
1422 avio_printf(hls->m3u8_out, ",GROUP-ID=\"%s\"", ccs->ccgroup);
1423 avio_printf(hls->m3u8_out, ",NAME=\"%s\"", ccs->instreamid);
1424 if (ccs->language)
1425 avio_printf(hls->m3u8_out, ",LANGUAGE=\"%s\"", ccs->language);
1426 avio_printf(hls->m3u8_out, ",INSTREAM-ID=\"%s\"\n", ccs->instreamid);
1427 }
1428
1429 /* For audio only variant streams add #EXT-X-MEDIA tag with attributes*/
1430
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = 0; i < hls->nb_varstreams; i++) {
1431 6 vs = &(hls->var_streams[i]);
1432
1433
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
6 if (vs->has_video || vs->has_subtitle || !vs->agroup)
1434 3 continue;
1435
1436 3 m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!m3u8_rel_name) {
1438 av_log(s, AV_LOG_ERROR, "Unable to find relative URL\n");
1439 goto fail;
1440 }
1441 3 nb_channels = 0;
1442
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (j = 0; j < vs->nb_streams; j++)
1443
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1444
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (vs->streams[j]->codecpar->ch_layout.nb_channels > nb_channels)
1445 3 nb_channels = vs->streams[j]->codecpar->ch_layout.nb_channels;
1446
1447
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 ff_hls_write_audio_rendition(hls->m3u8_out, vs->agroup, m3u8_rel_name, vs->language, i, hls->has_default_key ? vs->is_default : 1, nb_channels);
1448 }
1449
1450 /* For variant streams with video add #EXT-X-STREAM-INF tag with attributes*/
1451
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = 0; i < hls->nb_varstreams; i++) {
1452 6 vs = &(hls->var_streams[i]);
1453
1454 6 m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->m3u8_name);
1455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!m3u8_rel_name) {
1456 av_log(s, AV_LOG_ERROR, "Unable to find relative URL\n");
1457 goto fail;
1458 }
1459
1460 6 vid_st = NULL;
1461 6 aud_st = NULL;
1462
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (j = 0; j < vs->nb_streams; j++) {
1463
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1464 3 vid_st = vs->streams[j];
1465
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else if (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1466 3 aud_st = vs->streams[j];
1467 }
1468
1469
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6 if (!vid_st && !aud_st) {
1470 av_log(s, AV_LOG_WARNING, "Media stream not found\n");
1471 continue;
1472 }
1473
1474 /**
1475 * Traverse through the list of audio only rendition streams and find
1476 * the rendition which has highest bitrate in the same audio group
1477 */
1478
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (vs->agroup) {
1479
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (j = 0; j < hls->nb_varstreams; j++) {
1480 12 temp_vs = &(hls->var_streams[j]);
1481
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 if (!temp_vs->has_video && !temp_vs->has_subtitle &&
1482
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 temp_vs->agroup &&
1483 6 !av_strcasecmp(temp_vs->agroup, vs->agroup)) {
1484
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!aud_st)
1485 3 aud_st = temp_vs->streams[0];
1486 6 if (temp_vs->streams[0]->codecpar->bit_rate >
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 aud_st->codecpar->bit_rate)
1488 aud_st = temp_vs->streams[0];
1489 }
1490 }
1491 }
1492
1493
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (final) {
1494 4 bandwidth = vs->max_bitrate;
1495 4 avg_bandwidth = vs->avg_bitrate;
1496 } else {
1497 2 bandwidth = 0;
1498 2 avg_bandwidth = 0;
1499
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (vid_st)
1500 1 bandwidth += get_stream_bit_rate(vid_st);
1501
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (aud_st)
1502 2 bandwidth += get_stream_bit_rate(aud_st);
1503 2 bandwidth += bandwidth / 10;
1504 }
1505
1506 6 ccgroup = NULL;
1507
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6 if (vid_st && vs->ccgroup) {
1508 /* check if this group name is available in the cc map string */
1509 for (j = 0; j < hls->nb_ccstreams; j++) {
1510 ccs = &(hls->cc_streams[j]);
1511 if (!av_strcasecmp(ccs->ccgroup, vs->ccgroup)) {
1512 ccgroup = vs->ccgroup;
1513 break;
1514 }
1515 }
1516 if (j == hls->nb_ccstreams)
1517 av_log(s, AV_LOG_WARNING, "mapping ccgroup %s not found\n",
1518 vs->ccgroup);
1519 }
1520
1521
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
6 if (vid_st && vs->sgroup) {
1522 sgroup = vs->sgroup;
1523 vtt_m3u8_rel_name = get_relative_url(hls->master_m3u8_url, vs->vtt_m3u8_name);
1524 if (!vtt_m3u8_rel_name) {
1525 av_log(s, AV_LOG_WARNING, "Unable to find relative subtitle URL\n");
1526 break;
1527 }
1528
1529 ff_hls_write_subtitle_rendition(hls->m3u8_out, sgroup, vtt_m3u8_rel_name, vs->language,
1530 vs->subtitle_varname, i, hls->has_default_key ? vs->is_default : 1);
1531 }
1532
1533
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!hls->has_default_key || !hls->has_video_m3u8) {
1534 ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, avg_bandwidth, m3u8_rel_name,
1535 aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup, sgroup);
1536 } else {
1537
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (vid_st) {
1538 3 ff_hls_write_stream_info(vid_st, hls->m3u8_out, bandwidth, avg_bandwidth, m3u8_rel_name,
1539
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 aud_st ? vs->agroup : NULL, vs->codec_attr, ccgroup, sgroup);
1540 }
1541 }
1542 }
1543 3 fail:
1544
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ret >=0)
1545 3 hls->master_m3u8_created = 1;
1546 3 hlsenc_io_close(s, &hls->m3u8_out, temp_filename);
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (use_temp_file)
1548 ff_rename(temp_filename, hls->master_m3u8_url, s);
1549
1550 3 return ret;
1551 }
1552
1553 102 static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
1554 {
1555 102 HLSContext *hls = s->priv_data;
1556 HLSSegment *en;
1557 102 int target_duration = 0;
1558 102 int ret = 0;
1559 char temp_filename[MAX_URL_SIZE];
1560 char temp_vtt_filename[MAX_URL_SIZE];
1561 102 int64_t sequence = FFMAX(hls->start_sequence, vs->sequence - vs->nb_entries);
1562 102 const char *proto = avio_find_protocol_name(vs->m3u8_name);
1563
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
102 int is_file_proto = proto && !strcmp(proto, "file");
1564
4/6
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✓ Branch 5 taken 1 times.
102 int use_temp_file = is_file_proto && ((hls->flags & HLS_TEMP_FILE) || !(hls->pl_type == PLAYLIST_TYPE_VOD));
1565 static unsigned warned_non_file;
1566 102 const char *key_uri = NULL;
1567 102 char *iv_string = NULL;
1568 102 AVDictionary *options = NULL;
1569 102 double prog_date_time = vs->initial_prog_date_time;
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 double *prog_date_time_p = (hls->flags & HLS_PROGRAM_DATE_TIME) ? &prog_date_time : NULL;
1571
4/4
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 76 times.
102 int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
1572
1573 102 hls->version = 2;
1574
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 3 times.
102 if (!(hls->flags & HLS_ROUND_DURATIONS)) {
1575 99 hls->version = 3;
1576 }
1577
1578
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 76 times.
102 if (byterange_mode) {
1579 26 hls->version = 4;
1580 26 sequence = 0;
1581 }
1582
1583
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 96 times.
102 if (hls->flags & HLS_I_FRAMES_ONLY) {
1584 6 hls->version = 4;
1585 }
1586
1587
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 99 times.
102 if (hls->flags & HLS_INDEPENDENT_SEGMENTS) {
1588 3 hls->version = 6;
1589 }
1590
1591
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 93 times.
102 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
1592 9 hls->version = 7;
1593 }
1594
1595
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
102 if (!is_file_proto && (hls->flags & HLS_TEMP_FILE) && !warned_non_file++)
1596 av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporary partial files\n");
1597
1598 102 set_http_options(s, &options, hls);
1599
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 1 times.
102 snprintf(temp_filename, sizeof(temp_filename), use_temp_file ? "%s.tmp" : "%s", vs->m3u8_name);
1600
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 76 times.
102 ret = hlsenc_io_open(s, byterange_mode ? &hls->m3u8_out : &vs->out, temp_filename, &options);
1601 102 av_dict_free(&options);
1602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ret < 0) {
1603 goto fail;
1604 }
1605
1606
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 102 times.
457 for (en = vs->segments; en; en = en->next) {
1607
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 85 times.
355 if (target_duration <= en->duration)
1608 270 target_duration = lrint(en->duration);
1609 }
1610
1611 102 vs->discontinuity_set = 0;
1612 102 ff_hls_write_playlist_header(byterange_mode ? hls->m3u8_out : vs->out, hls->version, hls->allowcache,
1613
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 76 times.
102 target_duration, sequence, hls->pl_type, hls->flags & HLS_I_FRAMES_ONLY);
1614
1615
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
102 if ((hls->flags & HLS_DISCONT_START) && sequence==hls->start_sequence && vs->discontinuity_set==0) {
1616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "#EXT-X-DISCONTINUITY\n");
1617 3 vs->discontinuity_set = 1;
1618 }
1619
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22 times.
102 if (vs->has_video && (hls->flags & HLS_INDEPENDENT_SEGMENTS)) {
1620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "#EXT-X-INDEPENDENT-SEGMENTS\n");
1621 }
1622
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 102 times.
457 for (en = vs->segments; en; en = en->next) {
1623
2/10
✓ Branch 0 taken 355 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 355 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
355 if ((hls->encrypt || hls->key_info_file) && (!key_uri || strcmp(en->key_uri, key_uri) ||
1624 av_strcasecmp(en->iv_string, iv_string))) {
1625 avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"", en->key_uri);
1626 if (*en->iv_string)
1627 avio_printf(byterange_mode ? hls->m3u8_out : vs->out, ",IV=0x%s", en->iv_string);
1628 avio_printf(byterange_mode ? hls->m3u8_out : vs->out, "\n");
1629 key_uri = en->key_uri;
1630 iv_string = en->iv_string;
1631 }
1632
1633
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 333 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 13 times.
355 if ((hls->segment_type == SEGMENT_TYPE_FMP4) && (en == vs->segments)) {
1634
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 ff_hls_write_init_file(byterange_mode ? hls->m3u8_out : vs->out, (hls->flags & HLS_SINGLE_FILE) ? en->filename : vs->fmp4_init_filename,
1635
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 hls->flags & HLS_SINGLE_FILE, vs->init_range_length, 0);
1636 }
1637
1638 710 ret = ff_hls_write_file_entry(byterange_mode ? hls->m3u8_out : vs->out, en->discont, byterange_mode,
1639 355 en->duration, hls->flags & HLS_ROUND_DURATIONS,
1640
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 233 times.
355 en->size, en->pos, hls->baseurl,
1641 en->filename,
1642 355 en->discont_program_date_time ? &en->discont_program_date_time : prog_date_time_p,
1643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 hls->flags & HLS_I_FRAMES_ONLY);
1644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 if (en->discont_program_date_time)
1645 en->discont_program_date_time -= en->duration;
1646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 if (ret < 0) {
1647 av_log(s, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
1648 }
1649 }
1650
1651
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
102 if (last && (hls->flags & HLS_OMIT_ENDLIST)==0)
1652
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 ff_hls_write_end_list(byterange_mode ? hls->m3u8_out : vs->out);
1653
1654
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (vs->vtt_m3u8_name) {
1655 set_http_options(vs->vtt_avf, &options, hls);
1656 snprintf(temp_vtt_filename, sizeof(temp_vtt_filename), use_temp_file ? "%s.tmp" : "%s", vs->vtt_m3u8_name);
1657 ret = hlsenc_io_open(s, &hls->sub_m3u8_out, temp_vtt_filename, &options);
1658 av_dict_free(&options);
1659 if (ret < 0) {
1660 goto fail;
1661 }
1662 ff_hls_write_playlist_header(hls->sub_m3u8_out, hls->version, hls->allowcache,
1663 target_duration, sequence, PLAYLIST_TYPE_NONE, 0);
1664 for (en = vs->segments; en; en = en->next) {
1665 ret = ff_hls_write_file_entry(hls->sub_m3u8_out, en->discont, byterange_mode,
1666 en->duration, 0, en->size, en->pos,
1667 hls->baseurl, en->sub_filename, NULL, 0);
1668 if (ret < 0) {
1669 av_log(s, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
1670 }
1671 }
1672
1673 if (last && !(hls->flags & HLS_OMIT_ENDLIST))
1674 ff_hls_write_end_list(hls->sub_m3u8_out);
1675
1676 }
1677
1678 102 fail:
1679 102 av_dict_free(&options);
1680
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 76 times.
102 ret = hlsenc_io_close(s, byterange_mode ? &hls->m3u8_out : &vs->out, temp_filename);
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (ret < 0) {
1682 return ret;
1683 }
1684 102 hlsenc_io_close(s, &hls->sub_m3u8_out, vs->vtt_m3u8_name);
1685
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 1 times.
102 if (use_temp_file) {
1686 101 ff_rename(temp_filename, vs->m3u8_name, s);
1687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
101 if (vs->vtt_m3u8_name)
1688 ff_rename(temp_vtt_filename, vs->vtt_m3u8_name, s);
1689 }
1690
3/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 96 times.
102 if (ret >= 0 && hls->master_pl_name)
1691
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (create_master_playlist(s, vs, last) < 0)
1692 av_log(s, AV_LOG_WARNING, "Master playlist creation failed\n");
1693
1694 102 return ret;
1695 }
1696
1697 85 static int hls_start(AVFormatContext *s, VariantStream *vs)
1698 {
1699 85 HLSContext *c = s->priv_data;
1700 85 AVFormatContext *oc = vs->avf;
1701 85 AVFormatContext *vtt_oc = vs->vtt_avf;
1702 85 AVDictionary *options = NULL;
1703 85 const char *proto = NULL;
1704 85 int use_temp_file = 0;
1705 char iv_string[KEYSIZE*2 + 1];
1706 85 int err = 0;
1707
1708
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 82 times.
85 if (c->flags & HLS_SINGLE_FILE) {
1709 3 char *new_name = av_strdup(vs->basename);
1710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!new_name)
1711 return AVERROR(ENOMEM);
1712 3 ff_format_set_url(oc, new_name);
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (vs->vtt_basename) {
1714 new_name = av_strdup(vs->vtt_basename);
1715 if (!new_name)
1716 return AVERROR(ENOMEM);
1717 ff_format_set_url(vtt_oc, new_name);
1718 }
1719
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 78 times.
82 } else if (c->max_seg_size > 0) {
1720 4 char *filename = NULL;
1721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (replace_int_data_in_filename(&filename,
1722 4 vs->basename, 'd', vs->sequence) < 1) {
1723 av_freep(&filename);
1724 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -strftime 1 with it\n", vs->basename);
1725 return AVERROR(EINVAL);
1726 }
1727 4 ff_format_set_url(oc, filename);
1728 } else {
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (c->use_localtime) {
1730 int r;
1731 char *expanded = NULL;
1732
1733 r = strftime_expand(vs->basename, &expanded);
1734 if (r < 0) {
1735 av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
1736 return r;
1737 }
1738 ff_format_set_url(oc, expanded);
1739
1740 err = sls_flag_use_localtime_filename(oc, c, vs);
1741 if (err < 0) {
1742 return AVERROR(ENOMEM);
1743 }
1744
1745 if (c->use_localtime_mkdir) {
1746 const char *dir;
1747 char *fn_copy = av_strdup(oc->url);
1748 if (!fn_copy)
1749 return AVERROR(ENOMEM);
1750 dir = av_dirname(fn_copy);
1751 if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
1752 av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
1753 av_freep(&fn_copy);
1754 return AVERROR(errno);
1755 }
1756 av_freep(&fn_copy);
1757 }
1758 } else {
1759 78 char *filename = NULL;
1760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (replace_int_data_in_filename(&filename,
1761 78 vs->basename, 'd', vs->sequence) < 1) {
1762 av_freep(&filename);
1763 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -strftime 1 with it\n", vs->basename);
1764 return AVERROR(EINVAL);
1765 }
1766 78 ff_format_set_url(oc, filename);
1767 }
1768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (vs->vtt_basename) {
1769 char *filename = NULL;
1770 if (replace_int_data_in_filename(&filename,
1771 vs->vtt_basename, 'd', vs->sequence) < 1) {
1772 av_freep(&filename);
1773 av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", vs->vtt_basename);
1774 return AVERROR(EINVAL);
1775 }
1776 ff_format_set_url(vtt_oc, filename);
1777 }
1778 }
1779
1780 85 proto = avio_find_protocol_name(oc->url);
1781
3/6
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 85 times.
85 use_temp_file = proto && !strcmp(proto, "file") && (c->flags & HLS_TEMP_FILE);
1782
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (use_temp_file) {
1784 char *new_name = av_asprintf("%s.tmp", oc->url);
1785 if (!new_name)
1786 return AVERROR(ENOMEM);
1787 ff_format_set_url(oc, new_name);
1788 }
1789
1790
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 85 times.
85 if (c->key_info_file || c->encrypt) {
1791 if (c->segment_type == SEGMENT_TYPE_FMP4) {
1792 av_log(s, AV_LOG_ERROR, "Encrypted fmp4 not yet supported\n");
1793 return AVERROR_PATCHWELCOME;
1794 }
1795
1796 if (c->key_info_file && c->encrypt) {
1797 av_log(s, AV_LOG_WARNING, "Cannot use both -hls_key_info_file and -hls_enc,"
1798 " ignoring -hls_enc\n");
1799 }
1800
1801 if (!vs->encrypt_started || (c->flags & HLS_PERIODIC_REKEY)) {
1802 if (c->key_info_file) {
1803 if ((err = hls_encryption_start(s, vs)) < 0)
1804 goto fail;
1805 } else {
1806 if (!c->encrypt_started) {
1807 if ((err = do_encrypt(s, vs)) < 0)
1808 goto fail;
1809 c->encrypt_started = 1;
1810 }
1811 av_strlcpy(vs->key_uri, c->key_uri, sizeof(vs->key_uri));
1812 av_strlcpy(vs->key_string, c->key_string, sizeof(vs->key_string));
1813 av_strlcpy(vs->iv_string, c->iv_string, sizeof(vs->iv_string));
1814 }
1815 vs->encrypt_started = 1;
1816 }
1817 err = av_strlcpy(iv_string, vs->iv_string, sizeof(iv_string));
1818 if (!err) {
1819 snprintf(iv_string, sizeof(iv_string), "%032"PRIx64, vs->sequence);
1820 memset(vs->iv_string, 0, sizeof(vs->iv_string));
1821 memcpy(vs->iv_string, iv_string, sizeof(iv_string));
1822 }
1823 }
1824
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 7 times.
85 if (c->segment_type != SEGMENT_TYPE_FMP4) {
1825
2/4
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
78 if (oc->oformat->priv_class && oc->priv_data) {
1826 78 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
1827 }
1828
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 76 times.
78 if (c->flags & HLS_SINGLE_FILE) {
1829
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (c->key_info_file || c->encrypt) {
1830 av_dict_set(&options, "encryption_key", vs->key_string, 0);
1831 av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
1832
1833 /* Write temp file with cryption content */
1834 av_freep(&vs->basename_tmp);
1835 vs->basename_tmp = av_asprintf("crypto:%s.tmp", oc->url);
1836
1837 /* append temp file content into single file */
1838 av_freep(&vs->basename);
1839 vs->basename = av_asprintf("%s", oc->url);
1840 } else {
1841 2 vs->basename_tmp = vs->basename;
1842 }
1843 2 set_http_options(s, &options, c);
1844
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!vs->out_single_file)
1845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((err = hlsenc_io_open(s, &vs->out_single_file, vs->basename, &options)) < 0) {
1846 if (c->ignore_io_errors)
1847 err = 0;
1848 goto fail;
1849 }
1850
1851
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((err = hlsenc_io_open(s, &vs->out, vs->basename_tmp, &options)) < 0) {
1852 if (c->ignore_io_errors)
1853 err = 0;
1854 goto fail;
1855 }
1856
1857 }
1858 }
1859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (vs->vtt_basename) {
1860 set_http_options(s, &options, c);
1861 if ((err = hlsenc_io_open(s, &vtt_oc->pb, vtt_oc->url, &options)) < 0) {
1862 if (c->ignore_io_errors)
1863 err = 0;
1864 goto fail;
1865 }
1866 }
1867 85 av_dict_free(&options);
1868
1869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (vs->vtt_basename) {
1870 err = avformat_write_header(vtt_oc,NULL);
1871 if (err < 0)
1872 return err;
1873 }
1874
1875 85 return 0;
1876 fail:
1877 av_dict_free(&options);
1878
1879 return err;
1880 }
1881
1882 static const char * get_default_pattern_localtime_fmt(AVFormatContext *s)
1883 {
1884 HLSContext *hls = s->priv_data;
1885 #if HAVE_LIBC_MSVCRT
1886 // no %s support on MSVC, which invokes the invalid parameter handler
1887 // on unsupported format strings, instead of returning an error
1888 int strftime_s_supported = 0;
1889 #else
1890 char b[21];
1891 time_t t = time(NULL);
1892 struct tm tmbuf, *p = localtime_r(&t, &tmbuf);
1893 // no %s support when strftime returned error or left format string unchanged
1894 int strftime_s_supported = strftime(b, sizeof(b), "%s", p) && strcmp(b, "%s");
1895 #endif
1896
1897 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
1898 return strftime_s_supported ? "-%s.m4s" : "-%Y%m%d%H%M%S.m4s";
1899 }
1900 return strftime_s_supported ? "-%s.ts" : "-%Y%m%d%H%M%S.ts";
1901 }
1902
1903 static int append_postfix(char *name, int name_buf_len, int i)
1904 {
1905 char *p;
1906 char extension[10] = {'\0'};
1907
1908 p = strrchr(name, '.');
1909 if (p) {
1910 av_strlcpy(extension, p, sizeof(extension));
1911 *p = '\0';
1912 }
1913
1914 snprintf(name + strlen(name), name_buf_len - strlen(name), POSTFIX_PATTERN, i);
1915
1916 if (strlen(extension))
1917 av_strlcat(name, extension, name_buf_len);
1918
1919 return 0;
1920 }
1921
1922 45 static int validate_name(int nb_vs, const char *fn)
1923 {
1924 const char *filename, *subdir_name;
1925 45 char *fn_dup = NULL;
1926 45 int ret = 0;
1927
1928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!fn)
1929 return AVERROR(EINVAL);
1930
1931 45 fn_dup = av_strdup(fn);
1932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!fn_dup)
1933 return AVERROR(ENOMEM);
1934 45 filename = av_basename(fn);
1935 45 subdir_name = av_dirname(fn_dup);
1936
1937
3/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
45 if (nb_vs > 1 && !av_stristr(filename, "%v") && !av_stristr(subdir_name, "%v")) {
1938 av_log(NULL, AV_LOG_ERROR, "More than 1 variant streams are present, %%v is expected "
1939 "either in the filename or in the sub-directory name of file %s\n", fn);
1940 ret = AVERROR(EINVAL);
1941 goto fail;
1942 }
1943
1944
3/4
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
45 if (av_stristr(filename, "%v") && av_stristr(subdir_name, "%v")) {
1945 av_log(NULL, AV_LOG_ERROR, "%%v is expected either in the filename or "
1946 "in the sub-directory name of file %s, but only in one of them\n", fn);
1947 ret = AVERROR(EINVAL);
1948 goto fail;
1949 }
1950
1951 45 fail:
1952 45 av_freep(&fn_dup);
1953 45 return ret;
1954 }
1955
1956 44 static int format_name(const char *buf, char **s, int index, const char *varname)
1957 {
1958 const char *proto, *dir;
1959 44 char *orig_buf_dup = NULL, *mod_buf_dup = NULL;
1960 44 int ret = 0;
1961
1962 44 orig_buf_dup = av_strdup(buf);
1963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!orig_buf_dup)
1964 return AVERROR(ENOMEM);
1965
1966
2/2
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 4 times.
44 if (!av_stristr(buf, "%v")) {
1967 40 *s = orig_buf_dup;
1968 40 return 0;
1969 }
1970
1971
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!varname) {
1972
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (replace_int_data_in_filename(s, orig_buf_dup, 'v', index) < 1) {
1973 ret = AVERROR(EINVAL);
1974 goto fail;
1975 }
1976 } else {
1977 if (replace_str_data_in_filename(s, orig_buf_dup, 'v', varname) < 1) {
1978 ret = AVERROR(EINVAL);
1979 goto fail;
1980 }
1981 }
1982
1983 4 proto = avio_find_protocol_name(orig_buf_dup);
1984 4 dir = av_dirname(orig_buf_dup);
1985
1986 /* if %v is present in the file's directory, create sub-directory */
1987
1/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
4 if (av_stristr(dir, "%v") && proto && !strcmp(proto, "file")) {
1988 mod_buf_dup = av_strdup(*s);
1989 dir = av_dirname(mod_buf_dup);
1990 if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
1991 ret = AVERROR(errno);
1992 goto fail;
1993 }
1994 }
1995
1996 4 fail:
1997 4 av_freep(&orig_buf_dup);
1998 4 av_freep(&mod_buf_dup);
1999 4 return ret;
2000 }
2001
2002 2 static int get_nth_codec_stream_index(AVFormatContext *s,
2003 enum AVMediaType codec_type,
2004 int64_t stream_id)
2005 {
2006 unsigned int stream_index, cnt;
2007
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (stream_id < 0 || stream_id > s->nb_streams - 1)
2008 return -1;
2009 2 cnt = 0;
2010
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (stream_index = 0; stream_index < s->nb_streams; stream_index++) {
2011
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (s->streams[stream_index]->codecpar->codec_type != codec_type)
2012 1 continue;
2013
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (cnt == stream_id)
2014 2 return stream_index;
2015 cnt++;
2016 }
2017 return -1;
2018 }
2019
2020 1 static int parse_variant_stream_mapstring(AVFormatContext *s)
2021 {
2022 1 HLSContext *hls = s->priv_data;
2023 VariantStream *vs;
2024 int stream_index, i, j;
2025 enum AVMediaType codec_type;
2026 1 int nb_varstreams = 0, nb_streams;
2027 char *p, *q, *saveptr1, *saveptr2, *varstr, *keyval;
2028 const char *val;
2029
2030 /**
2031 * Expected format for var_stream_map string is as below:
2032 * "a:0,v:0 a:1,v:1"
2033 * "a:0,agroup:a0,default:1,language:ENG a:1,agroup:a1,default:0 v:0,agroup:a0 v:1,agroup:a1"
2034 * This string specifies how to group the audio, video and subtitle streams
2035 * into different variant streams. The variant stream groups are separated
2036 * by space.
2037 *
2038 * a:, v:, s: are keys to specify audio, video and subtitle streams
2039 * respectively. Allowed values are 0 to 9 digits (limited just based on
2040 * practical usage)
2041 *
2042 * agroup: is key to specify audio group. A string can be given as value.
2043 * sgroup: is key to specify subtitle group. A string can be given as value.
2044 */
2045 1 p = av_strdup(hls->var_stream_map);
2046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!p)
2047 return AVERROR(ENOMEM);
2048
2049 1 q = p;
2050
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while (av_strtok(q, " \t", &saveptr1)) {
2051 2 q = NULL;
2052 2 nb_varstreams++;
2053 }
2054 1 av_freep(&p);
2055
2056 1 hls->var_streams = av_mallocz(sizeof(*hls->var_streams) * nb_varstreams);
2057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!hls->var_streams)
2058 return AVERROR(ENOMEM);
2059 1 hls->nb_varstreams = nb_varstreams;
2060
2061 1 p = hls->var_stream_map;
2062 1 nb_varstreams = 0;
2063
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while (varstr = av_strtok(p, " \t", &saveptr1)) {
2064 2 p = NULL;
2065
2066
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (nb_varstreams < hls->nb_varstreams) {
2067 2 vs = &(hls->var_streams[nb_varstreams]);
2068 2 vs->var_stream_idx = nb_varstreams;
2069 2 vs->is_default = 0;
2070 2 nb_varstreams++;
2071 } else
2072 return AVERROR(EINVAL);
2073
2074 2 q = varstr;
2075 while (1) {
2076
5/6
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
8 if (!av_strncasecmp(q, "a:", 2) || !av_strncasecmp(q, "v:", 2) ||
2077 3 !av_strncasecmp(q, "s:", 2))
2078 2 vs->nb_streams++;
2079 5 q = strchr(q, ',');
2080
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (!q)
2081 2 break;
2082 3 q++;
2083 }
2084 2 vs->streams = av_mallocz(sizeof(AVStream *) * vs->nb_streams);
2085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vs->streams)
2086 return AVERROR(ENOMEM);
2087
2088 2 nb_streams = 0;
2089
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
7 while (keyval = av_strtok(varstr, ",", &saveptr2)) {
2090 int64_t num;
2091 char *end;
2092 5 varstr = NULL;
2093
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (av_strstart(keyval, "language:", &val)) {
2094 vs->language = val;
2095 3 continue;
2096
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 } else if (av_strstart(keyval, "default:", &val)) {
2097
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 vs->is_default = (!av_strncasecmp(val, "YES", strlen("YES")) ||
2098 (!av_strncasecmp(val, "1", strlen("1"))));
2099 1 hls->has_default_key = 1;
2100 1 continue;
2101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 } else if (av_strstart(keyval, "name:", &val)) {
2102 vs->varname = val;
2103 continue;
2104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 } else if (av_strstart(keyval, "sname:", &val)) {
2105 vs->subtitle_varname = val;
2106 continue;
2107
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 } else if (av_strstart(keyval, "agroup:", &val)) {
2108 2 vs->agroup = val;
2109 2 continue;
2110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 } else if (av_strstart(keyval, "sgroup:", &val)) {
2111 vs->sgroup = val;
2112 continue;
2113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 } else if (av_strstart(keyval, "ccgroup:", &val)) {
2114 vs->ccgroup = val;
2115 continue;
2116
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 } else if (av_strstart(keyval, "v:", &val)) {
2117 1 codec_type = AVMEDIA_TYPE_VIDEO;
2118 1 hls->has_video_m3u8 = 1;
2119
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 } else if (av_strstart(keyval, "a:", &val)) {
2120 1 codec_type = AVMEDIA_TYPE_AUDIO;
2121 } else if (av_strstart(keyval, "s:", &val)) {
2122 codec_type = AVMEDIA_TYPE_SUBTITLE;
2123 } else {
2124 av_log(s, AV_LOG_ERROR, "Invalid keyval %s\n", keyval);
2125 return AVERROR(EINVAL);
2126 }
2127
2128 2 num = strtoll(val, &end, 10);
2129
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!av_isdigit(*val) || *end != '\0') {
2130 av_log(s, AV_LOG_ERROR, "Invalid stream number: '%s'\n", val);
2131 return AVERROR(EINVAL);
2132 }
2133 2 stream_index = get_nth_codec_stream_index(s, codec_type, num);
2134
2135
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (stream_index >= 0 && nb_streams < vs->nb_streams) {
2136
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 for (i = 0; nb_streams > 0 && i < nb_streams; i++) {
2137 if (vs->streams[i] == s->streams[stream_index]) {
2138 av_log(s, AV_LOG_ERROR, "Same elementary stream found more than once inside "
2139 "variant definition #%d\n", nb_varstreams - 1);
2140 return AVERROR(EINVAL);
2141 }
2142 }
2143
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 for (j = 0; nb_varstreams > 1 && j < nb_varstreams - 1; j++) {
2144
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < hls->var_streams[j].nb_streams; i++) {
2145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (hls->var_streams[j].streams[i] == s->streams[stream_index]) {
2146 av_log(s, AV_LOG_ERROR, "Same elementary stream found more than once "
2147 "in two different variant definitions #%d and #%d\n",
2148 j, nb_varstreams - 1);
2149 return AVERROR(EINVAL);
2150 }
2151 }
2152 }
2153 2 vs->streams[nb_streams++] = s->streams[stream_index];
2154 } else {
2155 av_log(s, AV_LOG_ERROR, "Unable to map stream at %s\n", keyval);
2156 return AVERROR(EINVAL);
2157 }
2158 }
2159 }
2160 1 av_log(s, AV_LOG_DEBUG, "Number of variant streams %d\n",
2161 hls->nb_varstreams);
2162
2163 1 return 0;
2164 }
2165
2166 static int parse_cc_stream_mapstring(AVFormatContext *s)
2167 {
2168 HLSContext *hls = s->priv_data;
2169 int nb_ccstreams = 0;
2170 char *p, *q, *ccstr, *keyval;
2171 char *saveptr1 = NULL, *saveptr2 = NULL;
2172 const char *val;
2173 ClosedCaptionsStream *ccs;
2174
2175 p = av_strdup(hls->cc_stream_map);
2176 if(!p)
2177 return AVERROR(ENOMEM);
2178
2179 q = p;
2180 while (av_strtok(q, " \t", &saveptr1)) {
2181 q = NULL;
2182 nb_ccstreams++;
2183 }
2184 av_freep(&p);
2185
2186 hls->cc_streams = av_mallocz(sizeof(*hls->cc_streams) * nb_ccstreams);
2187 if (!hls->cc_streams)
2188 return AVERROR(ENOMEM);
2189 hls->nb_ccstreams = nb_ccstreams;
2190
2191 p = hls->cc_stream_map;
2192 nb_ccstreams = 0;
2193 while (ccstr = av_strtok(p, " \t", &saveptr1)) {
2194 p = NULL;
2195
2196 if (nb_ccstreams < hls->nb_ccstreams)
2197 ccs = &(hls->cc_streams[nb_ccstreams++]);
2198 else
2199 return AVERROR(EINVAL);
2200
2201 while (keyval = av_strtok(ccstr, ",", &saveptr2)) {
2202 ccstr = NULL;
2203
2204 if (av_strstart(keyval, "ccgroup:", &val)) {
2205 ccs->ccgroup = val;
2206 } else if (av_strstart(keyval, "instreamid:", &val)) {
2207 ccs->instreamid = val;
2208 } else if (av_strstart(keyval, "language:", &val)) {
2209 ccs->language = val;
2210 } else {
2211 av_log(s, AV_LOG_ERROR, "Invalid keyval %s\n", keyval);
2212 return AVERROR(EINVAL);
2213 }
2214 }
2215
2216 if (!ccs->ccgroup || !ccs->instreamid) {
2217 av_log(s, AV_LOG_ERROR, "Insufficient parameters in cc stream map string\n");
2218 return AVERROR(EINVAL);
2219 }
2220
2221 if (av_strstart(ccs->instreamid, "CC", &val)) {
2222 if (atoi(val) < 1 || atoi(val) > 4) {
2223 av_log(s, AV_LOG_ERROR, "Invalid instream ID CC index %d in %s, range 1-4\n",
2224 atoi(val), ccs->instreamid);
2225 return AVERROR(EINVAL);
2226 }
2227 } else if (av_strstart(ccs->instreamid, "SERVICE", &val)) {
2228 if (atoi(val) < 1 || atoi(val) > 63) {
2229 av_log(s, AV_LOG_ERROR, "Invalid instream ID SERVICE index %d in %s, range 1-63 \n",
2230 atoi(val), ccs->instreamid);
2231 return AVERROR(EINVAL);
2232 }
2233 } else {
2234 av_log(s, AV_LOG_ERROR, "Invalid instream ID %s, supported are CCn or SERVICEn\n",
2235 ccs->instreamid);
2236 return AVERROR(EINVAL);
2237 }
2238 }
2239
2240 return 0;
2241 }
2242
2243 21 static int update_variant_stream_info(AVFormatContext *s)
2244 {
2245 21 HLSContext *hls = s->priv_data;
2246 unsigned int i;
2247 21 int ret = 0;
2248
2249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (hls->cc_stream_map) {
2250 ret = parse_cc_stream_mapstring(s);
2251 if (ret < 0)
2252 return ret;
2253 }
2254
2255
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (hls->var_stream_map) {
2256 1 return parse_variant_stream_mapstring(s);
2257 } else {
2258 //By default, a single variant stream with all the codec streams is created
2259 20 hls->var_streams = av_mallocz(sizeof(*hls->var_streams));
2260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!hls->var_streams)
2261 return AVERROR(ENOMEM);
2262 20 hls->nb_varstreams = 1;
2263
2264 20 hls->var_streams[0].var_stream_idx = 0;
2265 20 hls->var_streams[0].nb_streams = s->nb_streams;
2266 40 hls->var_streams[0].streams = av_mallocz(sizeof(AVStream *) *
2267 20 hls->var_streams[0].nb_streams);
2268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!hls->var_streams[0].streams)
2269 return AVERROR(ENOMEM);
2270
2271 //by default, the first available ccgroup is mapped to the variant stream
2272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (hls->nb_ccstreams)
2273 hls->var_streams[0].ccgroup = hls->cc_streams[0].ccgroup;
2274
2275
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 for (i = 0; i < s->nb_streams; i++)
2276 20 hls->var_streams[0].streams[i] = s->streams[i];
2277 }
2278 20 return 0;
2279 }
2280
2281 1 static int update_master_pl_info(AVFormatContext *s)
2282 {
2283 1 HLSContext *hls = s->priv_data;
2284 const char *dir;
2285 1 char *fn1= NULL, *fn2 = NULL;
2286 1 int ret = 0;
2287
2288 1 fn1 = av_strdup(s->url);
2289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fn1)
2290 return AVERROR(ENOMEM);
2291 1 dir = av_dirname(fn1);
2292
2293 /**
2294 * if output file's directory has %v, variants are created in sub-directories
2295 * then master is created at the sub-directories level
2296 */
2297
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (dir && av_stristr(av_basename(dir), "%v")) {
2298 fn2 = av_strdup(dir);
2299 if (!fn2) {
2300 ret = AVERROR(ENOMEM);
2301 goto fail;
2302 }
2303 dir = av_dirname(fn2);
2304 }
2305
2306
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (dir && strcmp(dir, "."))
2307 1 hls->master_m3u8_url = av_append_path_component(dir, hls->master_pl_name);
2308 else
2309 hls->master_m3u8_url = av_strdup(hls->master_pl_name);
2310
2311
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!hls->master_m3u8_url) {
2312 ret = AVERROR(ENOMEM);
2313 goto fail;
2314 }
2315
2316 1 fail:
2317 1 av_freep(&fn1);
2318 1 av_freep(&fn2);
2319
2320 1 return ret;
2321 }
2322
2323 21 static int hls_write_header(AVFormatContext *s)
2324 {
2325 21 HLSContext *hls = s->priv_data;
2326 int ret, i, j;
2327 21 VariantStream *vs = NULL;
2328
2329
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
43 for (i = 0; i < hls->nb_varstreams; i++) {
2330 22 int subtitle_streams = 0;
2331 22 vs = &hls->var_streams[i];
2332
2333 22 ret = avformat_write_header(vs->avf, NULL);
2334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
2335 return ret;
2336 //av_assert0(s->nb_streams == hls->avf->nb_streams);
2337
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
44 for (j = 0; j < vs->nb_streams; j++) {
2338 AVStream *inner_st;
2339 22 AVStream *outer_st = vs->streams[j];
2340
2341
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 if (hls->max_seg_size > 0) {
2342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if ((outer_st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
2343 (outer_st->codecpar->bit_rate > hls->max_seg_size)) {
2344 av_log(s, AV_LOG_WARNING, "Your video bitrate is bigger than hls_segment_size, "
2345 "(%"PRId64 " > %"PRId64 "), the result maybe not be what you want.",
2346 outer_st->codecpar->bit_rate, hls->max_seg_size);
2347 }
2348 }
2349
2350
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (outer_st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
2351 22 inner_st = vs->avf->streams[j - subtitle_streams];
2352 else if (vs->vtt_avf) {
2353 inner_st = vs->vtt_avf->streams[0];
2354 subtitle_streams++;
2355 } else {
2356 /* We have a subtitle stream, when the user does not want one */
2357 inner_st = NULL;
2358 continue;
2359 }
2360 22 avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
2361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (outer_st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
2362 outer_st->codecpar->codec_tag != MKTAG('h','v','c','1')) {
2363 av_log(s, AV_LOG_WARNING, "Stream HEVC is not hvc1, you should use tag:v hvc1 to set it.\n");
2364 }
2365 22 write_codec_attr(outer_st, vs);
2366
2367 }
2368 /* Update the Codec Attr string for the mapped audio groups */
2369
4/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8 times.
22 if (vs->has_video && vs->agroup) {
2370
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (j = 0; j < hls->nb_varstreams; j++) {
2371 2 VariantStream *vs_agroup = &(hls->var_streams[j]);
2372
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 if (!vs_agroup->has_video && !vs_agroup->has_subtitle &&
2373
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 vs_agroup->agroup &&
2374 1 !av_strcasecmp(vs_agroup->agroup, vs->agroup)) {
2375 1 write_codec_attr(vs_agroup->streams[0], vs);
2376 }
2377 }
2378 }
2379 }
2380
2381 21 return 0;
2382 }
2383
2384 static int hls_init_file_resend(AVFormatContext *s, VariantStream *vs)
2385 {
2386 HLSContext *hls = s->priv_data;
2387 AVDictionary *options = NULL;
2388 int ret = 0;
2389
2390 set_http_options(s, &options, hls);
2391 ret = hlsenc_io_open(s, &vs->out, vs->base_output_dirname, &options);
2392 av_dict_free(&options);
2393 if (ret < 0)
2394 return ret;
2395 avio_write(vs->out, vs->init_buffer, vs->init_range_length);
2396 hlsenc_io_close(s, &vs->out, hls->fmp4_init_filename);
2397
2398 return ret;
2399 }
2400
2401 static int64_t append_single_file(AVFormatContext *s, VariantStream *vs)
2402 {
2403 int64_t ret = 0;
2404 int64_t read_byte = 0;
2405 int64_t total_size = 0;
2406 char *filename = NULL;
2407 char buf[BUFSIZE];
2408 AVFormatContext *oc = vs->avf;
2409
2410 hlsenc_io_close(s, &vs->out, vs->basename_tmp);
2411 filename = av_asprintf("%s.tmp", oc->url);
2412 ret = s->io_open(s, &vs->out, filename, AVIO_FLAG_READ, NULL);
2413 if (ret < 0) {
2414 av_free(filename);
2415 return ret;
2416 }
2417
2418 do {
2419 read_byte = avio_read(vs->out, buf, BUFSIZE);
2420 if (read_byte > 0) {
2421 avio_write(vs->out_single_file, buf, read_byte);
2422 total_size += read_byte;
2423 ret = total_size;
2424 }
2425 } while (read_byte > 0);
2426
2427 hlsenc_io_close(s, &vs->out, filename);
2428 av_free(filename);
2429
2430 return ret;
2431 }
2432 7380 static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
2433 {
2434 7380 HLSContext *hls = s->priv_data;
2435 7380 AVFormatContext *oc = NULL;
2436 7380 AVStream *st = s->streams[pkt->stream_index];
2437 7380 int64_t end_pts = 0;
2438 7380 int is_ref_pkt = 1;
2439 7380 int ret = 0, can_split = 1, i, j;
2440 7380 int stream_index = 0;
2441 7380 int range_length = 0;
2442 7380 const char *proto = NULL;
2443 7380 int use_temp_file = 0;
2444 7380 VariantStream *vs = NULL;
2445 7380 char *old_filename = NULL;
2446
2447
1/2
✓ Branch 0 taken 7610 times.
✗ Branch 1 not taken.
7610 for (i = 0; i < hls->nb_varstreams; i++) {
2448 7610 int subtitle_streams = 0;
2449 7610 vs = &hls->var_streams[i];
2450
2/2
✓ Branch 0 taken 7610 times.
✓ Branch 1 taken 230 times.
7840 for (j = 0; j < vs->nb_streams; j++) {
2451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7610 times.
7610 if (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2452 subtitle_streams++;
2453 }
2454
2/2
✓ Branch 0 taken 7380 times.
✓ Branch 1 taken 230 times.
7610 if (vs->streams[j] == st) {
2455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2456 oc = vs->vtt_avf;
2457 stream_index = 0;
2458 } else {
2459 7380 oc = vs->avf;
2460 7380 stream_index = j - subtitle_streams;
2461 }
2462 7380 break;
2463 }
2464 }
2465
2466
2/2
✓ Branch 0 taken 7380 times.
✓ Branch 1 taken 230 times.
7610 if (oc)
2467 7380 break;
2468 }
2469
2470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 if (!oc) {
2471 av_log(s, AV_LOG_ERROR, "Unable to find mapping variant stream\n");
2472 return AVERROR(ENOMEM);
2473 }
2474
2475 7380 end_pts = hls->recording_time * vs->number;
2476
2477
4/4
✓ Branch 0 taken 341 times.
✓ Branch 1 taken 7039 times.
✓ Branch 2 taken 191 times.
✓ Branch 3 taken 150 times.
7380 if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) {
2478 /* reset end_pts, hls->recording_time at end of the init hls list */
2479 191 int64_t init_list_dur = hls->init_time * vs->nb_entries;
2480 191 int64_t after_init_list_dur = (vs->sequence - hls->start_sequence - vs->nb_entries) * hls->time;
2481 191 hls->recording_time = hls->time;
2482 191 end_pts = init_list_dur + after_init_list_dur ;
2483 }
2484
2485
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7358 times.
7380 if (vs->start_pts == AV_NOPTS_VALUE) {
2486 22 vs->start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
2487
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 9 times.
22 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2488 13 vs->start_pts_from_audio = 1;
2489 }
2490
3/4
✓ Branch 0 taken 7296 times.
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7296 times.
7380 if (vs->start_pts_from_audio && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2491 int64_t video_start = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
2492 if (vs->start_pts > video_start) {
2493 vs->start_pts = video_start;
2494 vs->start_pts_from_audio = 0;
2495 }
2496 }
2497
2498
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7296 times.
7380 if (vs->has_video) {
2499
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
168 can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2500
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55 times.
84 ((pkt->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));
2501
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 is_ref_pkt = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (pkt->stream_index == vs->reference_stream_index);
2502 }
2503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
2504 is_ref_pkt = can_split = 0;
2505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 if (pkt->pts == AV_NOPTS_VALUE)
2506 is_ref_pkt = can_split = 0;
2507
2508
1/2
✓ Branch 0 taken 7380 times.
✗ Branch 1 not taken.
7380 if (is_ref_pkt) {
2509
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7358 times.
7380 if (vs->end_pts == AV_NOPTS_VALUE)
2510 22 vs->end_pts = pkt->pts;
2511
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7358 times.
7380 if (vs->new_start) {
2512 22 vs->new_start = 0;
2513 22 vs->duration = (double)(pkt->pts - vs->end_pts)
2514 22 * st->time_base.num / st->time_base.den;
2515 22 vs->dpp = (double)(pkt->duration) * st->time_base.num / st->time_base.den;
2516 } else {
2517
1/2
✓ Branch 0 taken 7358 times.
✗ Branch 1 not taken.
7358 if (pkt->duration) {
2518 7358 vs->duration += (double)(pkt->duration) * st->time_base.num / st->time_base.den;
2519 } else {
2520 av_log(s, AV_LOG_WARNING, "Stream %d packet with pts %" PRId64 " has duration 0. The segment duration may not be precise.\n",
2521 pkt->stream_index, pkt->pts);
2522 vs->duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / st->time_base.den;
2523 }
2524 }
2525 }
2526
2527
4/4
✓ Branch 0 taken 7325 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 7303 times.
✓ Branch 3 taken 22 times.
7380 can_split = can_split && (pkt->pts - vs->end_pts > 0);
2528
6/6
✓ Branch 0 taken 7358 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 7303 times.
✓ Branch 3 taken 55 times.
✓ Branch 4 taken 82 times.
✓ Branch 5 taken 7221 times.
7380 if (vs->packets_written && can_split && (av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q) - vs->start_pts
2529 >= end_pts)) {
2530 int64_t new_start_pos;
2531
4/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 60 times.
82 int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
2532 double cur_duration;
2533
2534 #if CONFIG_MP4_MUXER
2535
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
82 if (hls->segment_type == SEGMENT_TYPE_FMP4 && is_ref_pkt &&
2536
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 pkt->dts != AV_NOPTS_VALUE)
2537 6 ff_mov_set_fragment_end_hint(oc, stream_index, pkt, st->time_base);
2538 #endif
2539 82 av_write_frame(oc, NULL); /* Flush any buffered data */
2540 82 new_start_pos = avio_tell(oc->pb);
2541 82 vs->size = new_start_pos - vs->start_pos;
2542 82 avio_flush(oc->pb);
2543
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 76 times.
82 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
2544
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (!vs->init_range_length) {
2545 2 range_length = avio_close_dyn_buf(oc->pb, &vs->init_buffer);
2546 2 oc->pb = NULL;
2547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (range_length <= 0)
2548 return AVERROR(EINVAL);
2549 2 avio_write(vs->out, vs->init_buffer, range_length);
2550
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!hls->resend_init_file)
2551 2 av_freep(&vs->init_buffer);
2552 2 vs->init_range_length = range_length;
2553 2 avio_open_dyn_buf(&oc->pb);
2554 2 vs->packets_written = 0;
2555 2 vs->start_pos = range_length;
2556
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!byterange_mode) {
2557 1 hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
2558 }
2559 }
2560 }
2561
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 22 times.
82 if (!byterange_mode) {
2562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (vs->vtt_avf) {
2563 hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->url);
2564 }
2565 }
2566
2567
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 69 times.
82 if (hls->flags & HLS_SINGLE_FILE) {
2568 13 ret = flush_dynbuf(vs, &range_length);
2569 13 av_freep(&vs->temp_buffer);
2570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0) {
2571 return ret;
2572 }
2573 13 vs->size = range_length;
2574
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (hls->key_info_file || hls->encrypt)
2575 vs->size = append_single_file(s, vs);
2576 } else {
2577
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 if (oc->url[0]) {
2578 69 proto = avio_find_protocol_name(oc->url);
2579
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
138 use_temp_file = proto && !strcmp(proto, "file")
2580
2/4
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
138 && (hls->flags & HLS_TEMP_FILE);
2581 }
2582
2583
6/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 6 times.
69 if ((hls->max_seg_size > 0 && (vs->size + vs->start_pos >= hls->max_seg_size)) || !byterange_mode) {
2584 63 AVDictionary *options = NULL;
2585 63 char *filename = NULL;
2586
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
63 if (hls->key_info_file || hls->encrypt) {
2587 av_dict_set(&options, "encryption_key", vs->key_string, 0);
2588 av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
2589 filename = av_asprintf("crypto:%s", oc->url);
2590 } else {
2591 63 filename = av_asprintf("%s", oc->url);
2592 }
2593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (!filename) {
2594 av_dict_free(&options);
2595 return AVERROR(ENOMEM);
2596 }
2597
2598 // look to rename the asset name
2599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (use_temp_file)
2600 av_dict_set(&options, "mpegts_flags", "resend_headers", 0);
2601
2602 63 set_http_options(s, &options, hls);
2603
2604 63 ret = hlsenc_io_open(s, &vs->out, filename, &options);
2605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (ret < 0) {
2606 av_log(s, hls->ignore_io_errors ? AV_LOG_WARNING : AV_LOG_ERROR,
2607 "Failed to open file '%s'\n", filename);
2608 av_freep(&filename);
2609 av_dict_free(&options);
2610 return hls->ignore_io_errors ? 0 : ret;
2611 }
2612
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 59 times.
63 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
2613 4 write_styp(vs->out);
2614 }
2615 63 ret = flush_dynbuf(vs, &range_length);
2616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (ret < 0) {
2617 av_freep(&filename);
2618 av_dict_free(&options);
2619 return ret;
2620 }
2621 63 vs->size = range_length;
2622 63 ret = hlsenc_io_close(s, &vs->out, filename);
2623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (ret < 0) {
2624 av_log(s, AV_LOG_WARNING, "upload segment failed,"
2625 " will retry with a new http session.\n");
2626 ff_format_io_close(s, &vs->out);
2627 ret = hlsenc_io_open(s, &vs->out, filename, &options);
2628 if (ret >= 0) {
2629 reflush_dynbuf(vs, &range_length);
2630 ret = hlsenc_io_close(s, &vs->out, filename);
2631 }
2632 }
2633 63 av_dict_free(&options);
2634 63 av_freep(&vs->temp_buffer);
2635 63 av_freep(&filename);
2636 }
2637
2638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (use_temp_file)
2639 hls_rename_temp_file(s, oc);
2640 }
2641
2642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret < 0)
2643 return ret;
2644
2645 82 old_filename = av_strdup(oc->url);
2646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (!old_filename) {
2647 return AVERROR(ENOMEM);
2648 }
2649
2650 82 cur_duration = (double)(pkt->pts - vs->end_pts) * st->time_base.num / st->time_base.den;
2651 82 ret = hls_append_segment(s, hls, vs, cur_duration, vs->start_pos, vs->size);
2652 82 vs->end_pts = pkt->pts;
2653 82 vs->duration = 0;
2654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret < 0) {
2655 av_freep(&old_filename);
2656 return ret;
2657 }
2658
2659 // if we're building a VOD playlist, skip writing the manifest multiple times, and just wait until the end
2660
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 2 times.
82 if (hls->pl_type != PLAYLIST_TYPE_VOD) {
2661
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
80 if ((ret = hls_window(s, 0, vs)) < 0) {
2662 av_log(s, AV_LOG_WARNING, "upload playlist failed, will retry with a new http session.\n");
2663 ff_format_io_close(s, &vs->out);
2664 if ((ret = hls_window(s, 0, vs)) < 0) {
2665 av_freep(&old_filename);
2666 return ret;
2667 }
2668 }
2669 }
2670
2671
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
82 if (hls->resend_init_file && hls->segment_type == SEGMENT_TYPE_FMP4) {
2672 ret = hls_init_file_resend(s, vs);
2673 if (ret < 0) {
2674 av_freep(&old_filename);
2675 return ret;
2676 }
2677 }
2678
2679
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 69 times.
82 if (hls->flags & HLS_SINGLE_FILE) {
2680 13 vs->start_pos += vs->size;
2681
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (hls->key_info_file || hls->encrypt)
2682 ret = hls_start(s, vs);
2683
4/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
13 if (hls->segment_type == SEGMENT_TYPE_MPEGTS && oc->oformat->priv_class && oc->priv_data) {
2684 11 av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
2685 }
2686
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 60 times.
69 } else if (hls->max_seg_size > 0) {
2687
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (vs->size + vs->start_pos >= hls->max_seg_size) {
2688 3 vs->sequence++;
2689 3 sls_flag_file_rename(hls, vs, old_filename);
2690 3 ret = hls_start(s, vs);
2691 3 vs->start_pos = 0;
2692 /* When split segment by byte, the duration is short than hls_time,
2693 * so it is not enough one segment duration as hls_time, */
2694 } else {
2695 6 vs->start_pos = new_start_pos;
2696 }
2697 } else {
2698 60 vs->start_pos = 0;
2699 60 sls_flag_file_rename(hls, vs, old_filename);
2700 60 ret = hls_start(s, vs);
2701 }
2702 82 vs->number++;
2703 82 av_freep(&old_filename);
2704
2705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (ret < 0) {
2706 return ret;
2707 }
2708 }
2709
2710 7380 vs->packets_written++;
2711
1/2
✓ Branch 0 taken 7380 times.
✗ Branch 1 not taken.
7380 if (oc->pb) {
2712 7380 ret = ff_write_chained(oc, stream_index, pkt, s, 0);
2713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7380 times.
7380 if (hls->ignore_io_errors)
2714 ret = 0;
2715 }
2716
2717 7380 return ret;
2718 }
2719
2720 21 static void hls_deinit(AVFormatContext *s)
2721 {
2722 21 HLSContext *hls = s->priv_data;
2723 21 int i = 0;
2724 21 VariantStream *vs = NULL;
2725
2726
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
43 for (i = 0; i < hls->nb_varstreams; i++) {
2727 22 vs = &hls->var_streams[i];
2728
2729 22 av_freep(&vs->basename);
2730 22 av_freep(&vs->base_output_dirname);
2731 22 av_freep(&vs->fmp4_init_filename);
2732 22 av_freep(&vs->vtt_basename);
2733 22 av_freep(&vs->vtt_m3u8_name);
2734
2735 22 avformat_free_context(vs->vtt_avf);
2736
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (vs->avf)
2737 22 ffio_free_dyn_buf(&vs->avf->pb);
2738 22 avformat_free_context(vs->avf);
2739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (hls->resend_init_file)
2740 av_freep(&vs->init_buffer);
2741 22 hls_free_segments(vs->segments);
2742 22 hls_free_segments(vs->old_segments);
2743 22 av_freep(&vs->m3u8_name);
2744 22 av_freep(&vs->streams);
2745 }
2746
2747 21 ff_format_io_close(s, &hls->m3u8_out);
2748 21 ff_format_io_close(s, &hls->sub_m3u8_out);
2749 21 ff_format_io_close(s, &hls->http_delete);
2750 21 av_freep(&hls->key_basename);
2751 21 av_freep(&hls->var_streams);
2752 21 av_freep(&hls->cc_streams);
2753 21 av_freep(&hls->master_m3u8_url);
2754 21 }
2755
2756 21 static int hls_write_trailer(struct AVFormatContext *s)
2757 {
2758 21 HLSContext *hls = s->priv_data;
2759 21 AVFormatContext *oc = NULL;
2760 21 AVFormatContext *vtt_oc = NULL;
2761 21 char *old_filename = NULL;
2762 21 const char *proto = NULL;
2763 21 int use_temp_file = 0;
2764 int i;
2765 21 int ret = 0;
2766 21 VariantStream *vs = NULL;
2767 21 AVDictionary *options = NULL;
2768 int range_length, byterange_mode;
2769
2770
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
43 for (i = 0; i < hls->nb_varstreams; i++) {
2771 22 char *filename = NULL;
2772 22 vs = &hls->var_streams[i];
2773 22 oc = vs->avf;
2774 22 vtt_oc = vs->vtt_avf;
2775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!oc->pb)
2776 continue;
2777 22 old_filename = av_strdup(oc->url);
2778 22 use_temp_file = 0;
2779
2780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!old_filename) {
2781 return AVERROR(ENOMEM);
2782 }
2783
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 if (hls->key_info_file || hls->encrypt) {
2784 av_dict_set(&options, "encryption_key", vs->key_string, 0);
2785 av_dict_set(&options, "encryption_iv", vs->iv_string, 0);
2786 filename = av_asprintf("crypto:%s", oc->url);
2787 } else {
2788 22 filename = av_asprintf("%s", oc->url);
2789 }
2790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (!filename) {
2791 av_dict_free(&options);
2792 av_freep(&old_filename);
2793 return AVERROR(ENOMEM);
2794 }
2795
2796
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
2797
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!vs->init_range_length) {
2798 1 uint8_t *buffer = NULL;
2799 1 av_write_frame(oc, NULL); /* Flush any buffered data */
2800
2801 1 int init_range_length = avio_close_dyn_buf(oc->pb, &buffer);
2802 1 avio_write(vs->out, buffer, init_range_length);
2803 1 av_freep(&buffer);
2804 1 vs->init_range_length = init_range_length;
2805 1 avio_open_dyn_buf(&oc->pb);
2806 1 vs->packets_written = 0;
2807 1 vs->start_pos = init_range_length;
2808
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
2809
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!byterange_mode) {
2810 1 ff_format_io_close(s, &vs->out);
2811 1 hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
2812 }
2813 }
2814 }
2815
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3 times.
22 if (!(hls->flags & HLS_SINGLE_FILE)) {
2816 19 set_http_options(s, &options, hls);
2817 19 ret = hlsenc_io_open(s, &vs->out, filename, &options);
2818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0) {
2819 av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", oc->url);
2820 goto failed;
2821 }
2822
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
19 if (hls->segment_type == SEGMENT_TYPE_FMP4)
2823 2 write_styp(vs->out);
2824 }
2825 22 ret = flush_dynbuf(vs, &range_length);
2826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
2827 goto failed;
2828
2829 22 vs->size = range_length;
2830 22 ret = hlsenc_io_close(s, &vs->out, filename);
2831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
2832 av_log(s, AV_LOG_WARNING, "upload segment failed, will retry with a new http session.\n");
2833 ff_format_io_close(s, &vs->out);
2834 ret = hlsenc_io_open(s, &vs->out, filename, &options);
2835 if (ret < 0) {
2836 av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", oc->url);
2837 goto failed;
2838 }
2839 reflush_dynbuf(vs, &range_length);
2840 ret = hlsenc_io_close(s, &vs->out, filename);
2841 if (ret < 0)
2842 av_log(s, AV_LOG_WARNING, "Failed to upload file '%s' at the end.\n", oc->url);
2843 }
2844
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3 times.
22 if (hls->flags & HLS_SINGLE_FILE) {
2845
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (hls->key_info_file || hls->encrypt) {
2846 vs->size = append_single_file(s, vs);
2847 }
2848 3 hlsenc_io_close(s, &vs->out_single_file, vs->basename);
2849 }
2850 19 failed:
2851 22 av_freep(&vs->temp_buffer);
2852 22 av_dict_free(&options);
2853 22 av_freep(&filename);
2854 22 av_write_trailer(oc);
2855
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (oc->url[0]) {
2856 22 proto = avio_find_protocol_name(oc->url);
2857
3/6
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
22 use_temp_file = proto && !strcmp(proto, "file") && (hls->flags & HLS_TEMP_FILE);
2858 }
2859
2860 // rename that segment from .tmp to the real one
2861
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22 if (use_temp_file && !(hls->flags & HLS_SINGLE_FILE)) {
2862 hls_rename_temp_file(s, oc);
2863 av_freep(&old_filename);
2864 old_filename = av_strdup(oc->url);
2865
2866 if (!old_filename) {
2867 return AVERROR(ENOMEM);
2868 }
2869 }
2870
2871 /* after av_write_trailer, then duration + 1 duration per packet */
2872 22 hls_append_segment(s, hls, vs, vs->duration + vs->dpp, vs->start_pos, vs->size);
2873
2874 22 sls_flag_file_rename(hls, vs, old_filename);
2875
2876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vtt_oc) {
2877 if (vtt_oc->pb)
2878 av_write_trailer(vtt_oc);
2879 vs->size = avio_tell(vs->vtt_avf->pb) - vs->start_pos;
2880 ff_format_io_close(s, &vtt_oc->pb);
2881 }
2882 22 ret = hls_window(s, 1, vs);
2883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0) {
2884 av_log(s, AV_LOG_WARNING, "upload playlist failed, will retry with a new http session.\n");
2885 ff_format_io_close(s, &vs->out);
2886 hls_window(s, 1, vs);
2887 }
2888 22 ffio_free_dyn_buf(&oc->pb);
2889
2890 22 av_free(old_filename);
2891 }
2892
2893 21 return 0;
2894 }
2895
2896
2897 21 static int hls_init(AVFormatContext *s)
2898 {
2899 21 int ret = 0;
2900 21 int i = 0;
2901 21 int j = 0;
2902 21 HLSContext *hls = s->priv_data;
2903 const char *pattern;
2904 21 VariantStream *vs = NULL;
2905
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18 times.
21 const char *vtt_pattern = hls->flags & HLS_SINGLE_FILE ? ".vtt" : "%d.vtt";
2906 21 int http_base_proto = ff_is_http_proto(s->url);
2907 21 int fmp4_init_filename_len = strlen(hls->fmp4_init_filename) + 1;
2908 21 double initial_program_date_time = av_gettime() / 1000000.0;
2909
2910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (hls->use_localtime) {
2911 pattern = get_default_pattern_localtime_fmt(s);
2912 } else {
2913
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18 times.
21 pattern = hls->segment_type == SEGMENT_TYPE_FMP4 ? "%d.m4s" : "%d.ts";
2914
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18 times.
21 if (hls->flags & HLS_SINGLE_FILE)
2915 3 pattern += 2;
2916 }
2917
2918 21 hls->has_default_key = 0;
2919 21 hls->has_video_m3u8 = 0;
2920 21 ret = update_variant_stream_info(s);
2921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0) {
2922 av_log(s, AV_LOG_ERROR, "Variant stream info update failed with status %x\n",
2923 ret);
2924 return ret;
2925 }
2926
2927
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
21 if (!hls->method && http_base_proto) {
2928 av_log(hls, AV_LOG_WARNING, "No HTTP method set, hls muxer defaulting to method PUT.\n");
2929 }
2930
2931 21 ret = validate_name(hls->nb_varstreams, s->url);
2932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
2933 return ret;
2934
2935
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (hls->segment_filename) {
2936 21 ret = validate_name(hls->nb_varstreams, hls->segment_filename);
2937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
2938 return ret;
2939 }
2940
2941
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 18 times.
21 if (av_strcasecmp(hls->fmp4_init_filename, "init.mp4")) {
2942 3 ret = validate_name(hls->nb_varstreams, hls->fmp4_init_filename);
2943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
2944 return ret;
2945 }
2946
2947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (hls->subtitle_filename) {
2948 ret = validate_name(hls->nb_varstreams, hls->subtitle_filename);
2949 if (ret < 0)
2950 return ret;
2951 }
2952
2953
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (hls->master_pl_name) {
2954 1 ret = update_master_pl_info(s);
2955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0) {
2956 av_log(s, AV_LOG_ERROR, "Master stream info update failed with status %x\n",
2957 ret);
2958 return ret;
2959 }
2960 }
2961
2962
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
2963
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) ||
2964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
2965 time_t t = time(NULL);
2966 if (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) {
2967 hls->start_sequence = av_gettime();
2968 } else if (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) {
2969 hls->start_sequence = (int64_t)t;
2970 } else if (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME) {
2971 char b[15];
2972 struct tm *p, tmbuf;
2973 if (!(p = localtime_r(&t, &tmbuf)))
2974 return AVERROR(errno);
2975 if (!strftime(b, sizeof(b), "%Y%m%d%H%M%S", p))
2976 return AVERROR(ENOMEM);
2977 hls->start_sequence = strtoll(b, NULL, 10);
2978 }
2979 av_log(hls, AV_LOG_DEBUG, "start_number evaluated to %"PRId64"\n", hls->start_sequence);
2980 }
2981
2982
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
21 hls->recording_time = hls->init_time && hls->max_nb_segments > 0 ? hls->init_time : hls->time;
2983
2984
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21 if (hls->flags & HLS_SPLIT_BY_TIME && hls->flags & HLS_INDEPENDENT_SEGMENTS) {
2985 // Independent segments cannot be guaranteed when splitting by time
2986 hls->flags &= ~HLS_INDEPENDENT_SEGMENTS;
2987 av_log(s, AV_LOG_WARNING,
2988 "'split_by_time' and 'independent_segments' cannot be "
2989 "enabled together. Disabling 'independent_segments' flag\n");
2990 }
2991
2992
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 21 times.
43 for (i = 0; i < hls->nb_varstreams; i++) {
2993 22 vs = &hls->var_streams[i];
2994
2995 22 ret = format_name(s->url, &vs->m3u8_name, i, vs->varname);
2996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
2997 return ret;
2998
2999 22 vs->sequence = hls->start_sequence;
3000 22 vs->start_pts = AV_NOPTS_VALUE;
3001 22 vs->end_pts = AV_NOPTS_VALUE;
3002 22 vs->current_segment_final_filename_fmt[0] = '\0';
3003 22 vs->initial_prog_date_time = initial_program_date_time;
3004
3005
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
44 for (j = 0; j < vs->nb_streams; j++) {
3006 22 vs->has_video += vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
3007 /* Get one video stream to reference for split segments
3008 * so use the first video stream index. */
3009
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
22 if ((vs->has_video == 1) && (vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) {
3010 9 vs->reference_stream_index = vs->streams[j]->index;
3011 }
3012 22 vs->has_subtitle += vs->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE;
3013 }
3014
3015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vs->has_video > 1)
3016 av_log(s, AV_LOG_WARNING, "More than a single video stream present, expect issues decoding it.\n");
3017
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
3018 #if CONFIG_MP4_MUXER
3019 EXTERN const FFOutputFormat ff_mp4_muxer;
3020 3 vs->oformat = &ff_mp4_muxer.p;
3021 #else
3022 return AVERROR_MUXER_NOT_FOUND;
3023 #endif
3024 } else {
3025 EXTERN const FFOutputFormat ff_mpegts_muxer;
3026 19 vs->oformat = &ff_mpegts_muxer.p;
3027 }
3028
3029
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (hls->segment_filename) {
3030 22 ret = format_name(hls->segment_filename, &vs->basename, i, vs->varname);
3031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
3032 return ret;
3033 } else {
3034 char *p = strrchr(vs->m3u8_name, '.');
3035 if (p)
3036 *p = '\0';
3037
3038 vs->basename = av_asprintf("%s%s", vs->m3u8_name, pattern);
3039 if (!vs->basename)
3040 return AVERROR(ENOMEM);
3041
3042 if (p)
3043 *p = '.';
3044 }
3045
3046
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 if (hls->segment_type == SEGMENT_TYPE_FMP4) {
3047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (hls->nb_varstreams > 1)
3048 fmp4_init_filename_len += strlen(POSTFIX_PATTERN);
3049
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (hls->flags & HLS_SINGLE_FILE) {
3050 1 vs->fmp4_init_filename = av_strdup(vs->basename);
3051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!vs->fmp4_init_filename)
3052 return AVERROR(ENOMEM);
3053 } else {
3054 2 vs->fmp4_init_filename = av_malloc(fmp4_init_filename_len);
3055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vs->fmp4_init_filename)
3056 return AVERROR(ENOMEM);
3057 2 av_strlcpy(vs->fmp4_init_filename, hls->fmp4_init_filename,
3058 fmp4_init_filename_len);
3059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (hls->nb_varstreams > 1) {
3060 if (av_stristr(vs->fmp4_init_filename, "%v")) {
3061 av_freep(&vs->fmp4_init_filename);
3062 ret = format_name(hls->fmp4_init_filename,
3063 &vs->fmp4_init_filename, i, vs->varname);
3064 } else {
3065 ret = append_postfix(vs->fmp4_init_filename, fmp4_init_filename_len, i);
3066 }
3067 if (ret < 0)
3068 return ret;
3069 }
3070
3071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (hls->use_localtime) {
3072 int r;
3073 char *expanded = NULL;
3074
3075 r = strftime_expand(vs->fmp4_init_filename, &expanded);
3076 if (r < 0) {
3077 av_log(s, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
3078 return r;
3079 }
3080 av_free(vs->fmp4_init_filename);
3081 vs->fmp4_init_filename = expanded;
3082 }
3083
3084 2 char *p = strrchr(vs->m3u8_name, '/');
3085
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (p) {
3086 2 char tmp = *(++p);
3087 2 *p = '\0';
3088 2 vs->base_output_dirname = av_asprintf("%s%s", vs->m3u8_name,
3089 vs->fmp4_init_filename);
3090 2 *p = tmp;
3091 } else {
3092 vs->base_output_dirname = av_strdup(vs->fmp4_init_filename);
3093 }
3094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!vs->base_output_dirname)
3095 return AVERROR(ENOMEM);
3096 }
3097 }
3098
3099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 ret = hls->use_localtime ? sls_flag_check_duration_size(hls, vs) : sls_flag_check_duration_size_index(hls);
3100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
3101 return ret;
3102
3103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (vs->has_subtitle) {
3104 EXTERN const FFOutputFormat ff_webvtt_muxer;
3105 vs->vtt_oformat = &ff_webvtt_muxer.p;
3106
3107 char *p = strrchr(vs->m3u8_name, '.');
3108 if (p)
3109 *p = '\0';
3110
3111 vs->vtt_basename = av_asprintf("%s%s", vs->m3u8_name, vtt_pattern);
3112 if (!vs->vtt_basename)
3113 return AVERROR(ENOMEM);
3114
3115 if (hls->subtitle_filename) {
3116 ret = format_name(hls->subtitle_filename, &vs->vtt_m3u8_name, i, vs->varname);
3117 if (ret < 0)
3118 return ret;
3119 } else {
3120 vs->vtt_m3u8_name = av_asprintf("%s_vtt.m3u8", vs->m3u8_name);
3121 if (!vs->vtt_m3u8_name)
3122 return AVERROR(ENOMEM);
3123 }
3124 if (p)
3125 *p = '.';
3126 }
3127
3128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = hls_mux_init(s, vs)) < 0)
3129 return ret;
3130
3131
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 if (hls->flags & HLS_APPEND_LIST) {
3132 1 parse_playlist(s, vs->m3u8_name, vs);
3133 1 vs->discontinuity = 1;
3134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (hls->init_time > 0) {
3135 av_log(s, AV_LOG_WARNING, "append_list mode does not support hls_init_time,"
3136 " hls_init_time value will have no effect\n");
3137 hls->init_time = 0;
3138 hls->recording_time = hls->time;
3139 }
3140 }
3141
3142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = hls_start(s, vs)) < 0)
3143 return ret;
3144 22 vs->number++;
3145 }
3146
3147 21 return ret;
3148 }
3149
3150 #define OFFSET(x) offsetof(HLSContext, x)
3151 #define E AV_OPT_FLAG_ENCODING_PARAM
3152 static const AVOption options[] = {
3153 {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
3154 {"hls_time", "set segment length", OFFSET(time), AV_OPT_TYPE_DURATION, {.i64 = 2000000}, 0, INT64_MAX, E},
3155 {"hls_init_time", "set segment length at init list", OFFSET(init_time), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E},
3156 {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
3157 {"hls_delete_threshold", "set number of unreferenced segments to keep before deleting", OFFSET(hls_delete_threshold), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, E},
3158 {"hls_vtt_options","set hls vtt list of options for the container format used for hls", OFFSET(vtt_format_options_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3159 {"hls_allow_cache", "explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments", OFFSET(allowcache), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, E},
3160 {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3161 {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3162 {"hls_segment_options","set segments files format options of hls", OFFSET(format_options), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, E},
3163 {"hls_segment_size", "maximum size per segment file, (in bytes)", OFFSET(max_seg_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
3164 {"hls_key_info_file", "file with key URI and key file path", OFFSET(key_info_file), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3165 {"hls_enc", "enable AES128 encryption support", OFFSET(encrypt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
3166 {"hls_enc_key", "hex-coded 16 byte key to encrypt the segments", OFFSET(key), AV_OPT_TYPE_STRING, .flags = E},
3167 {"hls_enc_key_url", "url to access the key to decrypt the segments", OFFSET(key_url), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3168 {"hls_enc_iv", "hex-coded 16 byte initialization vector", OFFSET(iv), AV_OPT_TYPE_STRING, .flags = E},
3169 {"hls_subtitle_path", "set path of hls subtitles", OFFSET(subtitle_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3170 {"hls_segment_type", "set hls segment files type", OFFSET(segment_type), AV_OPT_TYPE_INT, {.i64 = SEGMENT_TYPE_MPEGTS }, 0, SEGMENT_TYPE_FMP4, E, .unit = "segment_type"},
3171 {"mpegts", "make segment file to mpegts files in m3u8", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_TYPE_MPEGTS }, 0, UINT_MAX, E, .unit = "segment_type"},
3172 {"fmp4", "make segment file to fragment mp4 files in m3u8", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_TYPE_FMP4 }, 0, UINT_MAX, E, .unit = "segment_type"},
3173 {"hls_fmp4_init_filename", "set fragment mp4 file init filename", OFFSET(fmp4_init_filename), AV_OPT_TYPE_STRING, {.str = "init.mp4"}, 0, 0, E},
3174 {"hls_fmp4_init_resend", "resend fragment mp4 init file after refresh m3u8 every time", OFFSET(resend_init_file), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
3175 {"hls_flags", "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, .unit = "flags"},
3176 {"single_file", "generate a single media file indexed with byte ranges", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX, E, .unit = "flags"},
3177 {"temp_file", "write segment and playlist to temporary file and rename when complete", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_TEMP_FILE }, 0, UINT_MAX, E, .unit = "flags"},
3178 {"delete_segments", "delete segment files that are no longer part of the playlist", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DELETE_SEGMENTS }, 0, UINT_MAX, E, .unit = "flags"},
3179 {"round_durations", "round durations in m3u8 to whole numbers", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_ROUND_DURATIONS }, 0, UINT_MAX, E, .unit = "flags"},
3180 {"discont_start", "start the playlist with a discontinuity tag", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DISCONT_START }, 0, UINT_MAX, E, .unit = "flags"},
3181 {"omit_endlist", "Do not append an endlist when ending stream", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_OMIT_ENDLIST }, 0, UINT_MAX, E, .unit = "flags"},
3182 {"split_by_time", "split the hls segment by time which user set by hls_time", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SPLIT_BY_TIME }, 0, UINT_MAX, E, .unit = "flags"},
3183 {"append_list", "append the new segments into old hls segment list", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_APPEND_LIST }, 0, UINT_MAX, E, .unit = "flags"},
3184 {"program_date_time", "add EXT-X-PROGRAM-DATE-TIME", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PROGRAM_DATE_TIME }, 0, UINT_MAX, E, .unit = "flags"},
3185 {"second_level_segment_index", "include segment index in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_INDEX }, 0, UINT_MAX, E, .unit = "flags"},
3186 {"second_level_segment_duration", "include segment duration in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_DURATION }, 0, UINT_MAX, E, .unit = "flags"},
3187 {"second_level_segment_size", "include segment size in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_SIZE }, 0, UINT_MAX, E, .unit = "flags"},
3188 {"periodic_rekey", "reload keyinfo file periodically for re-keying", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PERIODIC_REKEY }, 0, UINT_MAX, E, .unit = "flags"},
3189 {"independent_segments", "add EXT-X-INDEPENDENT-SEGMENTS, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_INDEPENDENT_SEGMENTS }, 0, UINT_MAX, E, .unit = "flags"},
3190 {"iframes_only", "add EXT-X-I-FRAMES-ONLY, whenever applicable", 0, AV_OPT_TYPE_CONST, { .i64 = HLS_I_FRAMES_ONLY }, 0, UINT_MAX, E, .unit = "flags"},
3191 {"strftime", "set filename expansion with strftime at segment creation", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
3192 {"strftime_mkdir", "create last directory component in strftime-generated filename", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
3193 {"hls_playlist_type", "set the HLS playlist type", OFFSET(pl_type), AV_OPT_TYPE_INT, {.i64 = PLAYLIST_TYPE_NONE }, 0, PLAYLIST_TYPE_NB-1, E, .unit = "pl_type" },
3194 {"event", "EVENT playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_EVENT }, INT_MIN, INT_MAX, E, .unit = "pl_type" },
3195 {"vod", "VOD playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_VOD }, INT_MIN, INT_MAX, E, .unit = "pl_type" },
3196 {"method", "set the HTTP method(default: PUT)", OFFSET(method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3197 {"hls_start_number_source", "set source of first number in sequence", OFFSET(start_sequence_source_type), AV_OPT_TYPE_INT, {.i64 = HLS_START_SEQUENCE_AS_START_NUMBER }, 0, HLS_START_SEQUENCE_LAST-1, E, .unit = "start_sequence_source_type" },
3198 {"generic", "start_number value (default)", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_START_SEQUENCE_AS_START_NUMBER }, INT_MIN, INT_MAX, E, .unit = "start_sequence_source_type" },
3199 {"epoch", "seconds since epoch", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH }, INT_MIN, INT_MAX, E, .unit = "start_sequence_source_type" },
3200 {"epoch_us", "microseconds since epoch", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH }, INT_MIN, INT_MAX, E, .unit = "start_sequence_source_type" },
3201 {"datetime", "current datetime as YYYYMMDDhhmmss", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_START_SEQUENCE_AS_FORMATTED_DATETIME }, INT_MIN, INT_MAX, E, .unit = "start_sequence_source_type" },
3202 {"http_user_agent", "override User-Agent field in HTTP header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3203 {"var_stream_map", "Variant stream map string", OFFSET(var_stream_map), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3204 {"cc_stream_map", "Closed captions stream map string", OFFSET(cc_stream_map), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3205 {"master_pl_name", "Create HLS master playlist with this name", OFFSET(master_pl_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
3206 {"master_pl_publish_rate", "Publish master play list every after this many segment intervals", OFFSET(master_publish_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT_MAX, E},
3207 {"http_persistent", "Use persistent HTTP connections", OFFSET(http_persistent), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
3208 {"timeout", "set timeout for socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT_MAX, .flags = E },
3209 {"ignore_io_errors", "Ignore IO errors for stable long-duration runs with network output", OFFSET(ignore_io_errors), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
3210 {"headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
3211 { NULL },
3212 };
3213
3214 static const AVClass hls_class = {
3215 .class_name = "hls muxer",
3216 .item_name = av_default_item_name,
3217 .option = options,
3218 .version = LIBAVUTIL_VERSION_INT,
3219 };
3220
3221
3222 const FFOutputFormat ff_hls_muxer = {
3223 .p.name = "hls",
3224 .p.long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
3225 .p.extensions = "m3u8",
3226 .p.audio_codec = AV_CODEC_ID_AAC,
3227 .p.video_codec = AV_CODEC_ID_H264,
3228 .p.subtitle_codec = AV_CODEC_ID_WEBVTT,
3229 .p.flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NODIMENSIONS,
3230 .p.priv_class = &hls_class,
3231 .flags_internal = FF_OFMT_FLAG_ALLOW_FLUSH,
3232 .priv_data_size = sizeof(HLSContext),
3233 .init = hls_init,
3234 .write_header = hls_write_header,
3235 .write_packet = hls_write_packet,
3236 .write_trailer = hls_write_trailer,
3237 .deinit = hls_deinit,
3238 };
3239