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