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