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