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