You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1667 lines
59KB

  1. /*
  2. * Apple HTTP Live Streaming segmenter
  3. * Copyright (c) 2012, Luca Barbato
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <float.h>
  23. #include <stdint.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #if CONFIG_GCRYPT
  28. #include <gcrypt.h>
  29. #elif CONFIG_OPENSSL
  30. #include <openssl/rand.h>
  31. #endif
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/parseutils.h"
  35. #include "libavutil/avstring.h"
  36. #include "libavutil/intreadwrite.h"
  37. #include "libavutil/random_seed.h"
  38. #include "libavutil/opt.h"
  39. #include "libavutil/log.h"
  40. #include "libavutil/time_internal.h"
  41. #include "avformat.h"
  42. #include "avio_internal.h"
  43. #include "internal.h"
  44. #include "os_support.h"
  45. typedef enum {
  46. HLS_START_SEQUENCE_AS_START_NUMBER = 0,
  47. HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH = 1,
  48. HLS_START_SEQUENCE_AS_FORMATTED_DATETIME = 2, // YYYYMMDDhhmmss
  49. } StartSequenceSourceType;
  50. #define KEYSIZE 16
  51. #define LINE_BUFFER_SIZE 1024
  52. #define HLS_MICROSECOND_UNIT 1000000
  53. typedef struct HLSSegment {
  54. char filename[1024];
  55. char sub_filename[1024];
  56. double duration; /* in seconds */
  57. int discont;
  58. int64_t pos;
  59. int64_t size;
  60. char key_uri[LINE_BUFFER_SIZE + 1];
  61. char iv_string[KEYSIZE*2 + 1];
  62. struct HLSSegment *next;
  63. } HLSSegment;
  64. typedef enum HLSFlags {
  65. // Generate a single media file and use byte ranges in the playlist.
  66. HLS_SINGLE_FILE = (1 << 0),
  67. HLS_DELETE_SEGMENTS = (1 << 1),
  68. HLS_ROUND_DURATIONS = (1 << 2),
  69. HLS_DISCONT_START = (1 << 3),
  70. HLS_OMIT_ENDLIST = (1 << 4),
  71. HLS_SPLIT_BY_TIME = (1 << 5),
  72. HLS_APPEND_LIST = (1 << 6),
  73. HLS_PROGRAM_DATE_TIME = (1 << 7),
  74. HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), // include segment index in segment filenames when use_localtime e.g.: %%03d
  75. HLS_SECOND_LEVEL_SEGMENT_DURATION = (1 << 9), // include segment duration (microsec) in segment filenames when use_localtime e.g.: %%09t
  76. HLS_SECOND_LEVEL_SEGMENT_SIZE = (1 << 10), // include segment size (bytes) in segment filenames when use_localtime e.g.: %%014s
  77. HLS_TEMP_FILE = (1 << 11),
  78. } HLSFlags;
  79. typedef enum {
  80. PLAYLIST_TYPE_NONE,
  81. PLAYLIST_TYPE_EVENT,
  82. PLAYLIST_TYPE_VOD,
  83. PLAYLIST_TYPE_NB,
  84. } PlaylistType;
  85. typedef struct HLSContext {
  86. const AVClass *class; // Class for private options.
  87. unsigned number;
  88. int64_t sequence;
  89. int64_t start_sequence;
  90. uint32_t start_sequence_source_type; // enum StartSequenceSourceType
  91. AVOutputFormat *oformat;
  92. AVOutputFormat *vtt_oformat;
  93. AVFormatContext *avf;
  94. AVFormatContext *vtt_avf;
  95. float time; // Set by a private option.
  96. float init_time; // Set by a private option.
  97. int max_nb_segments; // Set by a private option.
  98. #if FF_API_HLS_WRAP
  99. int wrap; // Set by a private option.
  100. #endif
  101. uint32_t flags; // enum HLSFlags
  102. uint32_t pl_type; // enum PlaylistType
  103. char *segment_filename;
  104. int use_localtime; ///< flag to expand filename with localtime
  105. int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
  106. int allowcache;
  107. int64_t recording_time;
  108. int has_video;
  109. int has_subtitle;
  110. int new_start;
  111. double dpp; // duration per packet
  112. int64_t start_pts;
  113. int64_t end_pts;
  114. double duration; // last segment duration computed so far, in seconds
  115. int64_t start_pos; // last segment starting position
  116. int64_t size; // last segment size
  117. int64_t max_seg_size; // every segment file max size
  118. int nb_entries;
  119. int discontinuity_set;
  120. int discontinuity;
  121. HLSSegment *segments;
  122. HLSSegment *last_segment;
  123. HLSSegment *old_segments;
  124. char *basename;
  125. char *vtt_basename;
  126. char *vtt_m3u8_name;
  127. char *baseurl;
  128. char *format_options_str;
  129. char *vtt_format_options_str;
  130. char *subtitle_filename;
  131. AVDictionary *format_options;
  132. int encrypt;
  133. char *key;
  134. char *key_url;
  135. char *iv;
  136. char *key_basename;
  137. char *key_info_file;
  138. char key_file[LINE_BUFFER_SIZE + 1];
  139. char key_uri[LINE_BUFFER_SIZE + 1];
  140. char key_string[KEYSIZE*2 + 1];
  141. char iv_string[KEYSIZE*2 + 1];
  142. AVDictionary *vtt_format_options;
  143. char *method;
  144. double initial_prog_date_time;
  145. char current_segment_final_filename_fmt[1024]; // when renaming segments
  146. } HLSContext;
  147. static int get_int_from_double(double val)
  148. {
  149. return (int)((val - (int)val) >= 0.001) ? (int)(val + 1) : (int)val;
  150. }
  151. static int mkdir_p(const char *path) {
  152. int ret = 0;
  153. char *temp = av_strdup(path);
  154. char *pos = temp;
  155. char tmp_ch = '\0';
  156. if (!path || !temp) {
  157. return -1;
  158. }
  159. if (!strncmp(temp, "/", 1) || !strncmp(temp, "\\", 1)) {
  160. pos++;
  161. } else if (!strncmp(temp, "./", 2) || !strncmp(temp, ".\\", 2)) {
  162. pos += 2;
  163. }
  164. for ( ; *pos != '\0'; ++pos) {
  165. if (*pos == '/' || *pos == '\\') {
  166. tmp_ch = *pos;
  167. *pos = '\0';
  168. ret = mkdir(temp, 0755);
  169. *pos = tmp_ch;
  170. }
  171. }
  172. if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
  173. ret = mkdir(temp, 0755);
  174. }
  175. av_free(temp);
  176. return ret;
  177. }
  178. static int replace_int_data_in_filename(char *buf, int buf_size, const char *filename, char placeholder, int64_t number)
  179. {
  180. const char *p;
  181. char *q, buf1[20], c;
  182. int nd, len, addchar_count;
  183. int found_count = 0;
  184. q = buf;
  185. p = filename;
  186. for (;;) {
  187. c = *p;
  188. if (c == '\0')
  189. break;
  190. if (c == '%' && *(p+1) == '%') // %%
  191. addchar_count = 2;
  192. else if (c == '%' && (av_isdigit(*(p+1)) || *(p+1) == placeholder)) {
  193. nd = 0;
  194. addchar_count = 1;
  195. while (av_isdigit(*(p + addchar_count))) {
  196. nd = nd * 10 + *(p + addchar_count) - '0';
  197. addchar_count++;
  198. }
  199. if (*(p + addchar_count) == placeholder) {
  200. len = snprintf(buf1, sizeof(buf1), "%0*"PRId64, (number < 0) ? nd : nd++, number);
  201. if (len < 1) // returned error or empty buf1
  202. goto fail;
  203. if ((q - buf + len) > buf_size - 1)
  204. goto fail;
  205. memcpy(q, buf1, len);
  206. q += len;
  207. p += (addchar_count + 1);
  208. addchar_count = 0;
  209. found_count++;
  210. }
  211. } else
  212. addchar_count = 1;
  213. while (addchar_count--)
  214. if ((q - buf) < buf_size - 1)
  215. *q++ = *p++;
  216. else
  217. goto fail;
  218. }
  219. *q = '\0';
  220. return found_count;
  221. fail:
  222. *q = '\0';
  223. return -1;
  224. }
  225. static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls) {
  226. HLSSegment *segment, *previous_segment = NULL;
  227. float playlist_duration = 0.0f;
  228. int ret = 0, path_size, sub_path_size;
  229. char *dirname = NULL, *p, *sub_path;
  230. char *path = NULL;
  231. AVDictionary *options = NULL;
  232. AVIOContext *out = NULL;
  233. const char *proto = NULL;
  234. segment = hls->segments;
  235. while (segment) {
  236. playlist_duration += segment->duration;
  237. segment = segment->next;
  238. }
  239. segment = hls->old_segments;
  240. while (segment) {
  241. playlist_duration -= segment->duration;
  242. previous_segment = segment;
  243. segment = previous_segment->next;
  244. if (playlist_duration <= -previous_segment->duration) {
  245. previous_segment->next = NULL;
  246. break;
  247. }
  248. }
  249. if (segment && !hls->use_localtime_mkdir) {
  250. if (hls->segment_filename) {
  251. dirname = av_strdup(hls->segment_filename);
  252. } else {
  253. dirname = av_strdup(hls->avf->filename);
  254. }
  255. if (!dirname) {
  256. ret = AVERROR(ENOMEM);
  257. goto fail;
  258. }
  259. p = (char *)av_basename(dirname);
  260. *p = '\0';
  261. }
  262. while (segment) {
  263. av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
  264. segment->filename);
  265. path_size = (hls->use_localtime_mkdir ? 0 : strlen(dirname)) + strlen(segment->filename) + 1;
  266. path = av_malloc(path_size);
  267. if (!path) {
  268. ret = AVERROR(ENOMEM);
  269. goto fail;
  270. }
  271. if (hls->use_localtime_mkdir)
  272. av_strlcpy(path, segment->filename, path_size);
  273. else { // segment->filename contains basename only
  274. av_strlcpy(path, dirname, path_size);
  275. av_strlcat(path, segment->filename, path_size);
  276. }
  277. proto = avio_find_protocol_name(s->filename);
  278. if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
  279. av_dict_set(&options, "method", "DELETE", 0);
  280. if ((ret = hls->avf->io_open(hls->avf, &out, path, AVIO_FLAG_WRITE, &options)) < 0)
  281. goto fail;
  282. ff_format_io_close(hls->avf, &out);
  283. } else if (unlink(path) < 0) {
  284. av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
  285. path, strerror(errno));
  286. }
  287. if ((segment->sub_filename[0] != '\0')) {
  288. sub_path_size = strlen(segment->sub_filename) + 1 + (dirname ? strlen(dirname) : 0);
  289. sub_path = av_malloc(sub_path_size);
  290. if (!sub_path) {
  291. ret = AVERROR(ENOMEM);
  292. goto fail;
  293. }
  294. av_strlcpy(sub_path, dirname, sub_path_size);
  295. av_strlcat(sub_path, segment->sub_filename, sub_path_size);
  296. if (hls->method || (proto && !av_strcasecmp(proto, "http"))) {
  297. av_dict_set(&options, "method", "DELETE", 0);
  298. if ((ret = hls->avf->io_open(hls->avf, &out, sub_path, AVIO_FLAG_WRITE, &options)) < 0) {
  299. av_free(sub_path);
  300. goto fail;
  301. }
  302. ff_format_io_close(hls->avf, &out);
  303. } else if (unlink(sub_path) < 0) {
  304. av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
  305. sub_path, strerror(errno));
  306. }
  307. av_free(sub_path);
  308. }
  309. av_freep(&path);
  310. previous_segment = segment;
  311. segment = previous_segment->next;
  312. av_free(previous_segment);
  313. }
  314. fail:
  315. av_free(path);
  316. av_free(dirname);
  317. return ret;
  318. }
  319. static int randomize(uint8_t *buf, int len)
  320. {
  321. #if CONFIG_GCRYPT
  322. gcry_randomize(buf, len, GCRY_VERY_STRONG_RANDOM);
  323. return 0;
  324. #elif CONFIG_OPENSSL
  325. if (RAND_bytes(buf, len))
  326. return 0;
  327. #else
  328. return AVERROR(ENOSYS);
  329. #endif
  330. return AVERROR(EINVAL);
  331. }
  332. static int do_encrypt(AVFormatContext *s)
  333. {
  334. HLSContext *hls = s->priv_data;
  335. int ret;
  336. int len;
  337. AVIOContext *pb;
  338. uint8_t key[KEYSIZE];
  339. len = strlen(hls->basename) + 4 + 1;
  340. hls->key_basename = av_mallocz(len);
  341. if (!hls->key_basename)
  342. return AVERROR(ENOMEM);
  343. av_strlcpy(hls->key_basename, s->filename, len);
  344. av_strlcat(hls->key_basename, ".key", len);
  345. if (hls->key_url) {
  346. strncpy(hls->key_file, hls->key_url, sizeof(hls->key_file));
  347. strncpy(hls->key_uri, hls->key_url, sizeof(hls->key_uri));
  348. } else {
  349. strncpy(hls->key_file, hls->key_basename, sizeof(hls->key_file));
  350. strncpy(hls->key_uri, hls->key_basename, sizeof(hls->key_uri));
  351. }
  352. if (!*hls->iv_string) {
  353. uint8_t iv[16] = { 0 };
  354. char buf[33];
  355. if (!hls->iv) {
  356. AV_WB64(iv + 8, hls->sequence);
  357. } else {
  358. memcpy(iv, hls->iv, sizeof(iv));
  359. }
  360. ff_data_to_hex(buf, iv, sizeof(iv), 0);
  361. buf[32] = '\0';
  362. memcpy(hls->iv_string, buf, sizeof(hls->iv_string));
  363. }
  364. if (!*hls->key_uri) {
  365. av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
  366. return AVERROR(EINVAL);
  367. }
  368. if (!*hls->key_file) {
  369. av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
  370. return AVERROR(EINVAL);
  371. }
  372. if (!*hls->key_string) {
  373. if (!hls->key) {
  374. if ((ret = randomize(key, sizeof(key))) < 0) {
  375. av_log(s, AV_LOG_ERROR, "Cannot generate a strong random key\n");
  376. return ret;
  377. }
  378. } else {
  379. memcpy(key, hls->key, sizeof(key));
  380. }
  381. ff_data_to_hex(hls->key_string, key, sizeof(key), 0);
  382. if ((ret = s->io_open(s, &pb, hls->key_file, AVIO_FLAG_WRITE, NULL)) < 0)
  383. return ret;
  384. avio_seek(pb, 0, SEEK_CUR);
  385. avio_write(pb, key, KEYSIZE);
  386. avio_close(pb);
  387. }
  388. return 0;
  389. }
  390. static int hls_encryption_start(AVFormatContext *s)
  391. {
  392. HLSContext *hls = s->priv_data;
  393. int ret;
  394. AVIOContext *pb;
  395. uint8_t key[KEYSIZE];
  396. if ((ret = s->io_open(s, &pb, hls->key_info_file, AVIO_FLAG_READ, NULL)) < 0) {
  397. av_log(hls, AV_LOG_ERROR,
  398. "error opening key info file %s\n", hls->key_info_file);
  399. return ret;
  400. }
  401. ff_get_line(pb, hls->key_uri, sizeof(hls->key_uri));
  402. hls->key_uri[strcspn(hls->key_uri, "\r\n")] = '\0';
  403. ff_get_line(pb, hls->key_file, sizeof(hls->key_file));
  404. hls->key_file[strcspn(hls->key_file, "\r\n")] = '\0';
  405. ff_get_line(pb, hls->iv_string, sizeof(hls->iv_string));
  406. hls->iv_string[strcspn(hls->iv_string, "\r\n")] = '\0';
  407. ff_format_io_close(s, &pb);
  408. if (!*hls->key_uri) {
  409. av_log(hls, AV_LOG_ERROR, "no key URI specified in key info file\n");
  410. return AVERROR(EINVAL);
  411. }
  412. if (!*hls->key_file) {
  413. av_log(hls, AV_LOG_ERROR, "no key file specified in key info file\n");
  414. return AVERROR(EINVAL);
  415. }
  416. if ((ret = s->io_open(s, &pb, hls->key_file, AVIO_FLAG_READ, NULL)) < 0) {
  417. av_log(hls, AV_LOG_ERROR, "error opening key file %s\n", hls->key_file);
  418. return ret;
  419. }
  420. ret = avio_read(pb, key, sizeof(key));
  421. ff_format_io_close(s, &pb);
  422. if (ret != sizeof(key)) {
  423. av_log(hls, AV_LOG_ERROR, "error reading key file %s\n", hls->key_file);
  424. if (ret >= 0 || ret == AVERROR_EOF)
  425. ret = AVERROR(EINVAL);
  426. return ret;
  427. }
  428. ff_data_to_hex(hls->key_string, key, sizeof(key), 0);
  429. return 0;
  430. }
  431. static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
  432. {
  433. int len = ff_get_line(s, buf, maxlen);
  434. while (len > 0 && av_isspace(buf[len - 1]))
  435. buf[--len] = '\0';
  436. return len;
  437. }
  438. static int hls_mux_init(AVFormatContext *s)
  439. {
  440. HLSContext *hls = s->priv_data;
  441. AVFormatContext *oc;
  442. AVFormatContext *vtt_oc = NULL;
  443. int i, ret;
  444. ret = avformat_alloc_output_context2(&hls->avf, hls->oformat, NULL, NULL);
  445. if (ret < 0)
  446. return ret;
  447. oc = hls->avf;
  448. oc->filename[0] = '\0';
  449. oc->oformat = hls->oformat;
  450. oc->interrupt_callback = s->interrupt_callback;
  451. oc->max_delay = s->max_delay;
  452. oc->opaque = s->opaque;
  453. oc->io_open = s->io_open;
  454. oc->io_close = s->io_close;
  455. av_dict_copy(&oc->metadata, s->metadata, 0);
  456. if(hls->vtt_oformat) {
  457. ret = avformat_alloc_output_context2(&hls->vtt_avf, hls->vtt_oformat, NULL, NULL);
  458. if (ret < 0)
  459. return ret;
  460. vtt_oc = hls->vtt_avf;
  461. vtt_oc->oformat = hls->vtt_oformat;
  462. av_dict_copy(&vtt_oc->metadata, s->metadata, 0);
  463. }
  464. for (i = 0; i < s->nb_streams; i++) {
  465. AVStream *st;
  466. AVFormatContext *loc;
  467. if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
  468. loc = vtt_oc;
  469. else
  470. loc = oc;
  471. if (!(st = avformat_new_stream(loc, NULL)))
  472. return AVERROR(ENOMEM);
  473. avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  474. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  475. st->time_base = s->streams[i]->time_base;
  476. av_dict_copy(&st->metadata, s->streams[i]->metadata, 0);
  477. }
  478. hls->start_pos = 0;
  479. hls->new_start = 1;
  480. return 0;
  481. }
  482. static HLSSegment *find_segment_by_filename(HLSSegment *segment, const char *filename)
  483. {
  484. while (segment) {
  485. if (!av_strcasecmp(segment->filename,filename))
  486. return segment;
  487. segment = segment->next;
  488. }
  489. return (HLSSegment *) NULL;
  490. }
  491. static int sls_flags_filename_process(struct AVFormatContext *s, HLSContext *hls, HLSSegment *en, double duration,
  492. int64_t pos, int64_t size)
  493. {
  494. if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
  495. strlen(hls->current_segment_final_filename_fmt)) {
  496. av_strlcpy(hls->avf->filename, hls->current_segment_final_filename_fmt, sizeof(hls->avf->filename));
  497. if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
  498. char * filename = av_strdup(hls->avf->filename); // %%s will be %s after strftime
  499. if (!filename) {
  500. av_free(en);
  501. return AVERROR(ENOMEM);
  502. }
  503. if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),
  504. filename, 's', pos + size) < 1) {
  505. av_log(hls, AV_LOG_ERROR,
  506. "Invalid second level segment filename template '%s', "
  507. "you can try to remove second_level_segment_size flag\n",
  508. filename);
  509. av_free(filename);
  510. av_free(en);
  511. return AVERROR(EINVAL);
  512. }
  513. av_free(filename);
  514. }
  515. if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
  516. char * filename = av_strdup(hls->avf->filename); // %%t will be %t after strftime
  517. if (!filename) {
  518. av_free(en);
  519. return AVERROR(ENOMEM);
  520. }
  521. if (replace_int_data_in_filename(hls->avf->filename, sizeof(hls->avf->filename),
  522. filename, 't', (int64_t)round(duration * HLS_MICROSECOND_UNIT)) < 1) {
  523. av_log(hls, AV_LOG_ERROR,
  524. "Invalid second level segment filename template '%s', "
  525. "you can try to remove second_level_segment_time flag\n",
  526. filename);
  527. av_free(filename);
  528. av_free(en);
  529. return AVERROR(EINVAL);
  530. }
  531. av_free(filename);
  532. }
  533. }
  534. return 0;
  535. }
  536. static int sls_flag_check_duration_size_index(HLSContext *hls)
  537. {
  538. int ret = 0;
  539. if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
  540. av_log(hls, AV_LOG_ERROR,
  541. "second_level_segment_duration hls_flag requires use_localtime to be true\n");
  542. ret = AVERROR(EINVAL);
  543. }
  544. if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
  545. av_log(hls, AV_LOG_ERROR,
  546. "second_level_segment_size hls_flag requires use_localtime to be true\n");
  547. ret = AVERROR(EINVAL);
  548. }
  549. if (hls->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) {
  550. av_log(hls, AV_LOG_ERROR,
  551. "second_level_segment_index hls_flag requires use_localtime to be true\n");
  552. ret = AVERROR(EINVAL);
  553. }
  554. return ret;
  555. }
  556. static int sls_flag_check_duration_size(HLSContext *hls)
  557. {
  558. const char *proto = avio_find_protocol_name(hls->basename);
  559. int segment_renaming_ok = proto && !strcmp(proto, "file");
  560. int ret = 0;
  561. if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) && !segment_renaming_ok) {
  562. av_log(hls, AV_LOG_ERROR,
  563. "second_level_segment_duration hls_flag works only with file protocol segment names\n");
  564. ret = AVERROR(EINVAL);
  565. }
  566. if ((hls->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) && !segment_renaming_ok) {
  567. av_log(hls, AV_LOG_ERROR,
  568. "second_level_segment_size hls_flag works only with file protocol segment names\n");
  569. ret = AVERROR(EINVAL);
  570. }
  571. return ret;
  572. }
  573. static void sls_flag_file_rename(HLSContext *hls, char *old_filename) {
  574. if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
  575. strlen(hls->current_segment_final_filename_fmt)) {
  576. ff_rename(old_filename, hls->avf->filename, hls);
  577. }
  578. }
  579. static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c)
  580. {
  581. if (c->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) {
  582. char * filename = av_strdup(oc->filename); // %%d will be %d after strftime
  583. if (!filename)
  584. return AVERROR(ENOMEM);
  585. if (replace_int_data_in_filename(oc->filename, sizeof(oc->filename),
  586. #if FF_API_HLS_WRAP
  587. filename, 'd', c->wrap ? c->sequence % c->wrap : c->sequence) < 1) {
  588. #else
  589. filename, 'd', c->sequence) < 1) {
  590. #endif
  591. av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
  592. "you can try to remove second_level_segment_index flag\n",
  593. filename);
  594. av_free(filename);
  595. return AVERROR(EINVAL);
  596. }
  597. av_free(filename);
  598. }
  599. if (c->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) {
  600. av_strlcpy(c->current_segment_final_filename_fmt, oc->filename,
  601. sizeof(c->current_segment_final_filename_fmt));
  602. if (c->flags & HLS_SECOND_LEVEL_SEGMENT_SIZE) {
  603. char * filename = av_strdup(oc->filename); // %%s will be %s after strftime
  604. if (!filename)
  605. return AVERROR(ENOMEM);
  606. if (replace_int_data_in_filename(oc->filename, sizeof(oc->filename), filename, 's', 0) < 1) {
  607. av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
  608. "you can try to remove second_level_segment_size flag\n",
  609. filename);
  610. av_free(filename);
  611. return AVERROR(EINVAL);
  612. }
  613. av_free(filename);
  614. }
  615. if (c->flags & HLS_SECOND_LEVEL_SEGMENT_DURATION) {
  616. char * filename = av_strdup(oc->filename); // %%t will be %t after strftime
  617. if (!filename)
  618. return AVERROR(ENOMEM);
  619. if (replace_int_data_in_filename(oc->filename, sizeof(oc->filename), filename, 't', 0) < 1) {
  620. av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', "
  621. "you can try to remove second_level_segment_time flag\n",
  622. filename);
  623. av_free(filename);
  624. return AVERROR(EINVAL);
  625. }
  626. av_free(filename);
  627. }
  628. }
  629. return 0;
  630. }
  631. /* Create a new segment and append it to the segment list */
  632. static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration,
  633. int64_t pos, int64_t size)
  634. {
  635. HLSSegment *en = av_malloc(sizeof(*en));
  636. const char *filename;
  637. int ret;
  638. if (!en)
  639. return AVERROR(ENOMEM);
  640. ret = sls_flags_filename_process(s, hls, en, duration, pos, size);
  641. if (ret < 0) {
  642. return ret;
  643. }
  644. filename = av_basename(hls->avf->filename);
  645. if (hls->use_localtime_mkdir) {
  646. filename = hls->avf->filename;
  647. }
  648. if (find_segment_by_filename(hls->segments, filename)
  649. || find_segment_by_filename(hls->old_segments, filename)) {
  650. av_log(hls, AV_LOG_WARNING, "Duplicated segment filename detected: %s\n", filename);
  651. }
  652. av_strlcpy(en->filename, filename, sizeof(en->filename));
  653. if(hls->has_subtitle)
  654. av_strlcpy(en->sub_filename, av_basename(hls->vtt_avf->filename), sizeof(en->sub_filename));
  655. else
  656. en->sub_filename[0] = '\0';
  657. en->duration = duration;
  658. en->pos = pos;
  659. en->size = size;
  660. en->next = NULL;
  661. en->discont = 0;
  662. if (hls->discontinuity) {
  663. en->discont = 1;
  664. hls->discontinuity = 0;
  665. }
  666. if (hls->key_info_file || hls->encrypt) {
  667. av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));
  668. av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));
  669. }
  670. if (!hls->segments)
  671. hls->segments = en;
  672. else
  673. hls->last_segment->next = en;
  674. hls->last_segment = en;
  675. // EVENT or VOD playlists imply sliding window cannot be used
  676. if (hls->pl_type != PLAYLIST_TYPE_NONE)
  677. hls->max_nb_segments = 0;
  678. if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
  679. en = hls->segments;
  680. hls->initial_prog_date_time += en->duration;
  681. hls->segments = en->next;
  682. if (en && hls->flags & HLS_DELETE_SEGMENTS &&
  683. #if FF_API_HLS_WRAP
  684. !(hls->flags & HLS_SINGLE_FILE || hls->wrap)) {
  685. #else
  686. !(hls->flags & HLS_SINGLE_FILE)) {
  687. #endif
  688. en->next = hls->old_segments;
  689. hls->old_segments = en;
  690. if ((ret = hls_delete_old_segments(s, hls)) < 0)
  691. return ret;
  692. } else
  693. av_free(en);
  694. } else
  695. hls->nb_entries++;
  696. if (hls->max_seg_size > 0) {
  697. return 0;
  698. }
  699. hls->sequence++;
  700. return 0;
  701. }
  702. static int parse_playlist(AVFormatContext *s, const char *url)
  703. {
  704. HLSContext *hls = s->priv_data;
  705. AVIOContext *in;
  706. int ret = 0, is_segment = 0;
  707. int64_t new_start_pos;
  708. char line[1024];
  709. const char *ptr;
  710. if ((ret = ffio_open_whitelist(&in, url, AVIO_FLAG_READ,
  711. &s->interrupt_callback, NULL,
  712. s->protocol_whitelist, s->protocol_blacklist)) < 0)
  713. return ret;
  714. read_chomp_line(in, line, sizeof(line));
  715. if (strcmp(line, "#EXTM3U")) {
  716. ret = AVERROR_INVALIDDATA;
  717. goto fail;
  718. }
  719. hls->discontinuity = 0;
  720. while (!avio_feof(in)) {
  721. read_chomp_line(in, line, sizeof(line));
  722. if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {
  723. int64_t tmp_sequence = strtoll(ptr, NULL, 10);
  724. if (tmp_sequence < hls->sequence)
  725. av_log(hls, AV_LOG_VERBOSE,
  726. "Found playlist sequence number was smaller """
  727. "than specified start sequence number: %"PRId64" < %"PRId64", "
  728. "omitting\n", tmp_sequence, hls->start_sequence);
  729. else {
  730. av_log(hls, AV_LOG_DEBUG, "Found playlist sequence number: %"PRId64"\n", tmp_sequence);
  731. hls->sequence = tmp_sequence;
  732. }
  733. } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) {
  734. is_segment = 1;
  735. hls->discontinuity = 1;
  736. } else if (av_strstart(line, "#EXTINF:", &ptr)) {
  737. is_segment = 1;
  738. hls->duration = atof(ptr);
  739. } else if (av_strstart(line, "#", NULL)) {
  740. continue;
  741. } else if (line[0]) {
  742. if (is_segment) {
  743. is_segment = 0;
  744. new_start_pos = avio_tell(hls->avf->pb);
  745. hls->size = new_start_pos - hls->start_pos;
  746. av_strlcpy(hls->avf->filename, line, sizeof(line));
  747. ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
  748. if (ret < 0)
  749. goto fail;
  750. hls->start_pos = new_start_pos;
  751. }
  752. }
  753. }
  754. fail:
  755. avio_close(in);
  756. return ret;
  757. }
  758. static void hls_free_segments(HLSSegment *p)
  759. {
  760. HLSSegment *en;
  761. while(p) {
  762. en = p;
  763. p = p->next;
  764. av_free(en);
  765. }
  766. }
  767. static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
  768. {
  769. const char *proto = avio_find_protocol_name(s->filename);
  770. int http_base_proto = proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
  771. if (c->method) {
  772. av_dict_set(options, "method", c->method, 0);
  773. } else if (http_base_proto) {
  774. av_log(c, AV_LOG_WARNING, "No HTTP method set, hls muxer defaulting to method PUT.\n");
  775. av_dict_set(options, "method", "PUT", 0);
  776. }
  777. }
  778. static void write_m3u8_head_block(HLSContext *hls, AVIOContext *out, int version,
  779. int target_duration, int64_t sequence)
  780. {
  781. avio_printf(out, "#EXTM3U\n");
  782. avio_printf(out, "#EXT-X-VERSION:%d\n", version);
  783. if (hls->allowcache == 0 || hls->allowcache == 1) {
  784. avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
  785. }
  786. avio_printf(out, "#EXT-X-TARGETDURATION:%d\n", target_duration);
  787. avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
  788. av_log(hls, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
  789. }
  790. static void hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
  791. {
  792. size_t len = strlen(oc->filename);
  793. char final_filename[sizeof(oc->filename)];
  794. av_strlcpy(final_filename, oc->filename, len);
  795. final_filename[len-4] = '\0';
  796. ff_rename(oc->filename, final_filename, s);
  797. oc->filename[len-4] = '\0';
  798. }
  799. static int hls_window(AVFormatContext *s, int last)
  800. {
  801. HLSContext *hls = s->priv_data;
  802. HLSSegment *en;
  803. int target_duration = 0;
  804. int ret = 0;
  805. AVIOContext *out = NULL;
  806. AVIOContext *sub_out = NULL;
  807. char temp_filename[1024];
  808. int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
  809. int version = 3;
  810. const char *proto = avio_find_protocol_name(s->filename);
  811. int use_rename = proto && !strcmp(proto, "file");
  812. static unsigned warned_non_file;
  813. char *key_uri = NULL;
  814. char *iv_string = NULL;
  815. AVDictionary *options = NULL;
  816. double prog_date_time = hls->initial_prog_date_time;
  817. int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
  818. if (byterange_mode) {
  819. version = 4;
  820. sequence = 0;
  821. }
  822. if (!use_rename && !warned_non_file++)
  823. av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporary partial files\n");
  824. set_http_options(s, &options, hls);
  825. snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
  826. if ((ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, &options)) < 0)
  827. goto fail;
  828. for (en = hls->segments; en; en = en->next) {
  829. if (target_duration <= en->duration)
  830. target_duration = get_int_from_double(en->duration);
  831. }
  832. hls->discontinuity_set = 0;
  833. write_m3u8_head_block(hls, out, version, target_duration, sequence);
  834. if (hls->pl_type == PLAYLIST_TYPE_EVENT) {
  835. avio_printf(out, "#EXT-X-PLAYLIST-TYPE:EVENT\n");
  836. } else if (hls->pl_type == PLAYLIST_TYPE_VOD) {
  837. avio_printf(out, "#EXT-X-PLAYLIST-TYPE:VOD\n");
  838. }
  839. if((hls->flags & HLS_DISCONT_START) && sequence==hls->start_sequence && hls->discontinuity_set==0 ){
  840. avio_printf(out, "#EXT-X-DISCONTINUITY\n");
  841. hls->discontinuity_set = 1;
  842. }
  843. for (en = hls->segments; en; en = en->next) {
  844. if ((hls->encrypt || hls->key_info_file) && (!key_uri || strcmp(en->key_uri, key_uri) ||
  845. av_strcasecmp(en->iv_string, iv_string))) {
  846. avio_printf(out, "#EXT-X-KEY:METHOD=AES-128,URI=\"%s\"", en->key_uri);
  847. if (*en->iv_string)
  848. avio_printf(out, ",IV=0x%s", en->iv_string);
  849. avio_printf(out, "\n");
  850. key_uri = en->key_uri;
  851. iv_string = en->iv_string;
  852. }
  853. if (en->discont) {
  854. avio_printf(out, "#EXT-X-DISCONTINUITY\n");
  855. }
  856. if (hls->flags & HLS_ROUND_DURATIONS)
  857. avio_printf(out, "#EXTINF:%ld,\n", lrint(en->duration));
  858. else
  859. avio_printf(out, "#EXTINF:%f,\n", en->duration);
  860. if (byterange_mode)
  861. avio_printf(out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
  862. en->size, en->pos);
  863. if (hls->flags & HLS_PROGRAM_DATE_TIME) {
  864. time_t tt, wrongsecs;
  865. int milli;
  866. struct tm *tm, tmpbuf;
  867. char buf0[128], buf1[128];
  868. tt = (int64_t)prog_date_time;
  869. milli = av_clip(lrint(1000*(prog_date_time - tt)), 0, 999);
  870. tm = localtime_r(&tt, &tmpbuf);
  871. strftime(buf0, sizeof(buf0), "%Y-%m-%dT%H:%M:%S", tm);
  872. if (!strftime(buf1, sizeof(buf1), "%z", tm) || buf1[1]<'0' ||buf1[1]>'2') {
  873. int tz_min, dst = tm->tm_isdst;
  874. tm = gmtime_r(&tt, &tmpbuf);
  875. tm->tm_isdst = dst;
  876. wrongsecs = mktime(tm);
  877. tz_min = (abs(wrongsecs - tt) + 30) / 60;
  878. snprintf(buf1, sizeof(buf1),
  879. "%c%02d%02d",
  880. wrongsecs <= tt ? '+' : '-',
  881. tz_min / 60,
  882. tz_min % 60);
  883. }
  884. avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
  885. prog_date_time += en->duration;
  886. }
  887. if (hls->baseurl)
  888. avio_printf(out, "%s", hls->baseurl);
  889. avio_printf(out, "%s\n", en->filename);
  890. }
  891. if (last && (hls->flags & HLS_OMIT_ENDLIST)==0)
  892. avio_printf(out, "#EXT-X-ENDLIST\n");
  893. if( hls->vtt_m3u8_name ) {
  894. if ((ret = s->io_open(s, &sub_out, hls->vtt_m3u8_name, AVIO_FLAG_WRITE, &options)) < 0)
  895. goto fail;
  896. write_m3u8_head_block(hls, sub_out, version, target_duration, sequence);
  897. for (en = hls->segments; en; en = en->next) {
  898. avio_printf(sub_out, "#EXTINF:%f,\n", en->duration);
  899. if (byterange_mode)
  900. avio_printf(sub_out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
  901. en->size, en->pos);
  902. if (hls->baseurl)
  903. avio_printf(sub_out, "%s", hls->baseurl);
  904. avio_printf(sub_out, "%s\n", en->sub_filename);
  905. }
  906. if (last)
  907. avio_printf(sub_out, "#EXT-X-ENDLIST\n");
  908. }
  909. fail:
  910. av_dict_free(&options);
  911. ff_format_io_close(s, &out);
  912. ff_format_io_close(s, &sub_out);
  913. if (ret >= 0 && use_rename)
  914. ff_rename(temp_filename, s->filename, s);
  915. return ret;
  916. }
  917. static int hls_start(AVFormatContext *s)
  918. {
  919. HLSContext *c = s->priv_data;
  920. AVFormatContext *oc = c->avf;
  921. AVFormatContext *vtt_oc = c->vtt_avf;
  922. AVDictionary *options = NULL;
  923. char *filename, iv_string[KEYSIZE*2 + 1];
  924. int err = 0;
  925. if (c->flags & HLS_SINGLE_FILE) {
  926. av_strlcpy(oc->filename, c->basename,
  927. sizeof(oc->filename));
  928. if (c->vtt_basename)
  929. av_strlcpy(vtt_oc->filename, c->vtt_basename,
  930. sizeof(vtt_oc->filename));
  931. } else if (c->max_seg_size > 0) {
  932. if (replace_int_data_in_filename(oc->filename, sizeof(oc->filename),
  933. #if FF_API_HLS_WRAP
  934. c->basename, 'd', c->wrap ? c->sequence % c->wrap : c->sequence) < 1) {
  935. #else
  936. c->basename, 'd', c->sequence) < 1) {
  937. #endif
  938. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -use_localtime 1 with it\n", c->basename);
  939. return AVERROR(EINVAL);
  940. }
  941. } else {
  942. if (c->use_localtime) {
  943. time_t now0;
  944. struct tm *tm, tmpbuf;
  945. time(&now0);
  946. tm = localtime_r(&now0, &tmpbuf);
  947. if (!strftime(oc->filename, sizeof(oc->filename), c->basename, tm)) {
  948. av_log(oc, AV_LOG_ERROR, "Could not get segment filename with use_localtime\n");
  949. return AVERROR(EINVAL);
  950. }
  951. err = sls_flag_use_localtime_filename(oc, c);
  952. if (err < 0) {
  953. return AVERROR(ENOMEM);
  954. }
  955. if (c->use_localtime_mkdir) {
  956. const char *dir;
  957. char *fn_copy = av_strdup(oc->filename);
  958. if (!fn_copy) {
  959. return AVERROR(ENOMEM);
  960. }
  961. dir = av_dirname(fn_copy);
  962. if (mkdir_p(dir) == -1 && errno != EEXIST) {
  963. av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
  964. av_free(fn_copy);
  965. return AVERROR(errno);
  966. }
  967. av_free(fn_copy);
  968. }
  969. } else if (replace_int_data_in_filename(oc->filename, sizeof(oc->filename),
  970. #if FF_API_HLS_WRAP
  971. c->basename, 'd', c->wrap ? c->sequence % c->wrap : c->sequence) < 1) {
  972. #else
  973. c->basename, 'd', c->sequence) < 1) {
  974. #endif
  975. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -use_localtime 1 with it\n", c->basename);
  976. return AVERROR(EINVAL);
  977. }
  978. if( c->vtt_basename) {
  979. if (replace_int_data_in_filename(vtt_oc->filename, sizeof(vtt_oc->filename),
  980. #if FF_API_HLS_WRAP
  981. c->vtt_basename, 'd', c->wrap ? c->sequence % c->wrap : c->sequence) < 1) {
  982. #else
  983. c->vtt_basename, 'd', c->sequence) < 1) {
  984. #endif
  985. av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->vtt_basename);
  986. return AVERROR(EINVAL);
  987. }
  988. }
  989. }
  990. c->number++;
  991. set_http_options(s, &options, c);
  992. if (c->flags & HLS_TEMP_FILE) {
  993. av_strlcat(oc->filename, ".tmp", sizeof(oc->filename));
  994. }
  995. if (c->key_info_file || c->encrypt) {
  996. if (c->key_info_file && c->encrypt) {
  997. av_log(s, AV_LOG_WARNING, "Cannot use both -hls_key_info_file and -hls_enc,"
  998. " will use -hls_key_info_file priority\n");
  999. }
  1000. if (c->key_info_file) {
  1001. if ((err = hls_encryption_start(s)) < 0)
  1002. goto fail;
  1003. } else {
  1004. if ((err = do_encrypt(s)) < 0)
  1005. goto fail;
  1006. }
  1007. if ((err = av_dict_set(&options, "encryption_key", c->key_string, 0))
  1008. < 0)
  1009. goto fail;
  1010. err = av_strlcpy(iv_string, c->iv_string, sizeof(iv_string));
  1011. if (!err)
  1012. snprintf(iv_string, sizeof(iv_string), "%032"PRIx64, c->sequence);
  1013. if ((err = av_dict_set(&options, "encryption_iv", iv_string, 0)) < 0)
  1014. goto fail;
  1015. filename = av_asprintf("crypto:%s", oc->filename);
  1016. if (!filename) {
  1017. err = AVERROR(ENOMEM);
  1018. goto fail;
  1019. }
  1020. err = s->io_open(s, &oc->pb, filename, AVIO_FLAG_WRITE, &options);
  1021. av_free(filename);
  1022. av_dict_free(&options);
  1023. if (err < 0)
  1024. return err;
  1025. } else
  1026. if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, &options)) < 0)
  1027. goto fail;
  1028. if (c->vtt_basename) {
  1029. set_http_options(s, &options, c);
  1030. if ((err = s->io_open(s, &vtt_oc->pb, vtt_oc->filename, AVIO_FLAG_WRITE, &options)) < 0)
  1031. goto fail;
  1032. }
  1033. av_dict_free(&options);
  1034. /* We only require one PAT/PMT per segment. */
  1035. if (oc->oformat->priv_class && oc->priv_data) {
  1036. char period[21];
  1037. snprintf(period, sizeof(period), "%d", (INT_MAX / 2) - 1);
  1038. av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
  1039. av_opt_set(oc->priv_data, "sdt_period", period, 0);
  1040. av_opt_set(oc->priv_data, "pat_period", period, 0);
  1041. }
  1042. if (c->vtt_basename) {
  1043. err = avformat_write_header(vtt_oc,NULL);
  1044. if (err < 0)
  1045. return err;
  1046. }
  1047. return 0;
  1048. fail:
  1049. av_dict_free(&options);
  1050. return err;
  1051. }
  1052. static const char * get_default_pattern_localtime_fmt(void)
  1053. {
  1054. char b[21];
  1055. time_t t = time(NULL);
  1056. struct tm *p, tmbuf;
  1057. p = localtime_r(&t, &tmbuf);
  1058. // no %s support when strftime returned error or left format string unchanged
  1059. // also no %s support on MSVC, which invokes the invalid parameter handler on unsupported format strings, instead of returning an error
  1060. return (HAVE_LIBC_MSVCRT || !strftime(b, sizeof(b), "%s", p) || !strcmp(b, "%s")) ? "-%Y%m%d%H%M%S.ts" : "-%s.ts";
  1061. }
  1062. static int hls_write_header(AVFormatContext *s)
  1063. {
  1064. HLSContext *hls = s->priv_data;
  1065. int ret, i;
  1066. char *p;
  1067. const char *pattern = "%d.ts";
  1068. const char *pattern_localtime_fmt = get_default_pattern_localtime_fmt();
  1069. const char *vtt_pattern = "%d.vtt";
  1070. AVDictionary *options = NULL;
  1071. int basename_size;
  1072. int vtt_basename_size;
  1073. if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
  1074. (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
  1075. time_t t = time(NULL); // we will need it in either case
  1076. if (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) {
  1077. hls->start_sequence = (int64_t)t;
  1078. } else if (hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME) {
  1079. char b[15];
  1080. struct tm *p, tmbuf;
  1081. if (!(p = localtime_r(&t, &tmbuf)))
  1082. return AVERROR(ENOMEM);
  1083. if (!strftime(b, sizeof(b), "%Y%m%d%H%M%S", p))
  1084. return AVERROR(ENOMEM);
  1085. hls->start_sequence = strtoll(b, NULL, 10);
  1086. }
  1087. av_log(hls, AV_LOG_DEBUG, "start_number evaluated to %"PRId64"\n", hls->start_sequence);
  1088. }
  1089. hls->sequence = hls->start_sequence;
  1090. hls->recording_time = (hls->init_time ? hls->init_time : hls->time) * AV_TIME_BASE;
  1091. hls->start_pts = AV_NOPTS_VALUE;
  1092. hls->current_segment_final_filename_fmt[0] = '\0';
  1093. if (hls->flags & HLS_PROGRAM_DATE_TIME) {
  1094. time_t now0;
  1095. time(&now0);
  1096. hls->initial_prog_date_time = now0;
  1097. }
  1098. if (hls->format_options_str) {
  1099. ret = av_dict_parse_string(&hls->format_options, hls->format_options_str, "=", ":", 0);
  1100. if (ret < 0) {
  1101. av_log(s, AV_LOG_ERROR, "Could not parse format options list '%s'\n", hls->format_options_str);
  1102. goto fail;
  1103. }
  1104. }
  1105. for (i = 0; i < s->nb_streams; i++) {
  1106. hls->has_video +=
  1107. s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
  1108. hls->has_subtitle +=
  1109. s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE;
  1110. }
  1111. if (hls->has_video > 1)
  1112. av_log(s, AV_LOG_WARNING,
  1113. "More than a single video stream present, "
  1114. "expect issues decoding it.\n");
  1115. hls->oformat = av_guess_format("mpegts", NULL, NULL);
  1116. if (!hls->oformat) {
  1117. ret = AVERROR_MUXER_NOT_FOUND;
  1118. goto fail;
  1119. }
  1120. if(hls->has_subtitle) {
  1121. hls->vtt_oformat = av_guess_format("webvtt", NULL, NULL);
  1122. if (!hls->oformat) {
  1123. ret = AVERROR_MUXER_NOT_FOUND;
  1124. goto fail;
  1125. }
  1126. }
  1127. if (hls->segment_filename) {
  1128. hls->basename = av_strdup(hls->segment_filename);
  1129. if (!hls->basename) {
  1130. ret = AVERROR(ENOMEM);
  1131. goto fail;
  1132. }
  1133. } else {
  1134. if (hls->flags & HLS_SINGLE_FILE)
  1135. pattern = ".ts";
  1136. if (hls->use_localtime) {
  1137. basename_size = strlen(s->filename) + strlen(pattern_localtime_fmt) + 1;
  1138. } else {
  1139. basename_size = strlen(s->filename) + strlen(pattern) + 1;
  1140. }
  1141. hls->basename = av_malloc(basename_size);
  1142. if (!hls->basename) {
  1143. ret = AVERROR(ENOMEM);
  1144. goto fail;
  1145. }
  1146. av_strlcpy(hls->basename, s->filename, basename_size);
  1147. p = strrchr(hls->basename, '.');
  1148. if (p)
  1149. *p = '\0';
  1150. if (hls->use_localtime) {
  1151. av_strlcat(hls->basename, pattern_localtime_fmt, basename_size);
  1152. } else {
  1153. av_strlcat(hls->basename, pattern, basename_size);
  1154. }
  1155. }
  1156. if (!hls->use_localtime) {
  1157. ret = sls_flag_check_duration_size_index(hls);
  1158. if (ret < 0) {
  1159. goto fail;
  1160. }
  1161. } else {
  1162. ret = sls_flag_check_duration_size(hls);
  1163. if (ret < 0) {
  1164. goto fail;
  1165. }
  1166. }
  1167. if(hls->has_subtitle) {
  1168. if (hls->flags & HLS_SINGLE_FILE)
  1169. vtt_pattern = ".vtt";
  1170. vtt_basename_size = strlen(s->filename) + strlen(vtt_pattern) + 1;
  1171. hls->vtt_basename = av_malloc(vtt_basename_size);
  1172. if (!hls->vtt_basename) {
  1173. ret = AVERROR(ENOMEM);
  1174. goto fail;
  1175. }
  1176. hls->vtt_m3u8_name = av_malloc(vtt_basename_size);
  1177. if (!hls->vtt_m3u8_name ) {
  1178. ret = AVERROR(ENOMEM);
  1179. goto fail;
  1180. }
  1181. av_strlcpy(hls->vtt_basename, s->filename, vtt_basename_size);
  1182. p = strrchr(hls->vtt_basename, '.');
  1183. if (p)
  1184. *p = '\0';
  1185. if( hls->subtitle_filename ) {
  1186. strcpy(hls->vtt_m3u8_name, hls->subtitle_filename);
  1187. } else {
  1188. strcpy(hls->vtt_m3u8_name, hls->vtt_basename);
  1189. av_strlcat(hls->vtt_m3u8_name, "_vtt.m3u8", vtt_basename_size);
  1190. }
  1191. av_strlcat(hls->vtt_basename, vtt_pattern, vtt_basename_size);
  1192. }
  1193. if ((ret = hls_mux_init(s)) < 0)
  1194. goto fail;
  1195. if (hls->flags & HLS_APPEND_LIST) {
  1196. parse_playlist(s, s->filename);
  1197. hls->discontinuity = 1;
  1198. if (hls->init_time > 0) {
  1199. av_log(s, AV_LOG_WARNING, "append_list mode does not support hls_init_time,"
  1200. " hls_init_time value will have no effect\n");
  1201. hls->init_time = 0;
  1202. hls->recording_time = hls->time * AV_TIME_BASE;
  1203. }
  1204. }
  1205. if ((ret = hls_start(s)) < 0)
  1206. goto fail;
  1207. av_dict_copy(&options, hls->format_options, 0);
  1208. ret = avformat_write_header(hls->avf, &options);
  1209. if (av_dict_count(options)) {
  1210. av_log(s, AV_LOG_ERROR, "Some of provided format options in '%s' are not recognized\n", hls->format_options_str);
  1211. ret = AVERROR(EINVAL);
  1212. goto fail;
  1213. }
  1214. //av_assert0(s->nb_streams == hls->avf->nb_streams);
  1215. for (i = 0; i < s->nb_streams; i++) {
  1216. AVStream *inner_st;
  1217. AVStream *outer_st = s->streams[i];
  1218. if (hls->max_seg_size > 0) {
  1219. if ((outer_st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
  1220. (outer_st->codecpar->bit_rate > hls->max_seg_size)) {
  1221. av_log(s, AV_LOG_WARNING, "Your video bitrate is bigger than hls_segment_size, "
  1222. "(%"PRId64 " > %"PRId64 "), the result maybe not be what you want.",
  1223. outer_st->codecpar->bit_rate, hls->max_seg_size);
  1224. }
  1225. }
  1226. if (outer_st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
  1227. inner_st = hls->avf->streams[i];
  1228. else if (hls->vtt_avf)
  1229. inner_st = hls->vtt_avf->streams[0];
  1230. else {
  1231. /* We have a subtitle stream, when the user does not want one */
  1232. inner_st = NULL;
  1233. continue;
  1234. }
  1235. avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
  1236. }
  1237. fail:
  1238. av_dict_free(&options);
  1239. if (ret < 0) {
  1240. av_freep(&hls->basename);
  1241. av_freep(&hls->vtt_basename);
  1242. av_freep(&hls->key_basename);
  1243. if (hls->avf)
  1244. avformat_free_context(hls->avf);
  1245. if (hls->vtt_avf)
  1246. avformat_free_context(hls->vtt_avf);
  1247. }
  1248. return ret;
  1249. }
  1250. static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
  1251. {
  1252. HLSContext *hls = s->priv_data;
  1253. AVFormatContext *oc = NULL;
  1254. AVStream *st = s->streams[pkt->stream_index];
  1255. int64_t end_pts = hls->recording_time * hls->number;
  1256. int is_ref_pkt = 1;
  1257. int ret, can_split = 1;
  1258. int stream_index = 0;
  1259. if (hls->sequence - hls->nb_entries > hls->start_sequence && hls->init_time > 0) {
  1260. /* reset end_pts, hls->recording_time at end of the init hls list */
  1261. int init_list_dur = hls->init_time * hls->nb_entries * AV_TIME_BASE;
  1262. int after_init_list_dur = (hls->sequence - hls->nb_entries ) * hls->time * AV_TIME_BASE;
  1263. hls->recording_time = hls->time * AV_TIME_BASE;
  1264. end_pts = init_list_dur + after_init_list_dur ;
  1265. }
  1266. if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {
  1267. oc = hls->vtt_avf;
  1268. stream_index = 0;
  1269. } else {
  1270. oc = hls->avf;
  1271. stream_index = pkt->stream_index;
  1272. }
  1273. if (hls->start_pts == AV_NOPTS_VALUE) {
  1274. hls->start_pts = pkt->pts;
  1275. hls->end_pts = pkt->pts;
  1276. }
  1277. if (hls->has_video) {
  1278. can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  1279. ((pkt->flags & AV_PKT_FLAG_KEY) || (hls->flags & HLS_SPLIT_BY_TIME));
  1280. is_ref_pkt = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
  1281. }
  1282. if (pkt->pts == AV_NOPTS_VALUE)
  1283. is_ref_pkt = can_split = 0;
  1284. if (is_ref_pkt) {
  1285. if (hls->new_start) {
  1286. hls->new_start = 0;
  1287. hls->duration = (double)(pkt->pts - hls->end_pts)
  1288. * st->time_base.num / st->time_base.den;
  1289. hls->dpp = (double)(pkt->duration) * st->time_base.num / st->time_base.den;
  1290. } else {
  1291. if (pkt->duration) {
  1292. hls->duration += (double)(pkt->duration) * st->time_base.num / st->time_base.den;
  1293. } else {
  1294. av_log(s, AV_LOG_WARNING, "pkt->duration = 0, maybe the hls segment duration will not precise\n");
  1295. hls->duration = (double)(pkt->pts - hls->end_pts) * st->time_base.num / st->time_base.den;
  1296. }
  1297. }
  1298. }
  1299. if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
  1300. end_pts, AV_TIME_BASE_Q) >= 0) {
  1301. int64_t new_start_pos;
  1302. char *old_filename = av_strdup(hls->avf->filename);
  1303. int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
  1304. if (!old_filename) {
  1305. return AVERROR(ENOMEM);
  1306. }
  1307. av_write_frame(oc, NULL); /* Flush any buffered data */
  1308. new_start_pos = avio_tell(hls->avf->pb);
  1309. hls->size = new_start_pos - hls->start_pos;
  1310. if (!byterange_mode) {
  1311. ff_format_io_close(s, &oc->pb);
  1312. if (hls->vtt_avf) {
  1313. ff_format_io_close(s, &hls->vtt_avf->pb);
  1314. }
  1315. }
  1316. if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
  1317. if (!(hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size <= 0))
  1318. if (hls->avf->oformat->priv_class && hls->avf->priv_data)
  1319. av_opt_set(hls->avf->priv_data, "mpegts_flags", "resend_headers", 0);
  1320. hls_rename_temp_file(s, oc);
  1321. }
  1322. ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
  1323. hls->start_pos = new_start_pos;
  1324. if (ret < 0) {
  1325. av_free(old_filename);
  1326. return ret;
  1327. }
  1328. hls->end_pts = pkt->pts;
  1329. hls->duration = 0;
  1330. if (hls->flags & HLS_SINGLE_FILE) {
  1331. hls->number++;
  1332. } else if (hls->max_seg_size > 0) {
  1333. if (hls->start_pos >= hls->max_seg_size) {
  1334. hls->sequence++;
  1335. sls_flag_file_rename(hls, old_filename);
  1336. ret = hls_start(s);
  1337. hls->start_pos = 0;
  1338. /* When split segment by byte, the duration is short than hls_time,
  1339. * so it is not enough one segment duration as hls_time, */
  1340. hls->number--;
  1341. }
  1342. hls->number++;
  1343. } else {
  1344. sls_flag_file_rename(hls, old_filename);
  1345. ret = hls_start(s);
  1346. }
  1347. if (ret < 0) {
  1348. av_free(old_filename);
  1349. return ret;
  1350. }
  1351. if ((ret = hls_window(s, 0)) < 0) {
  1352. av_free(old_filename);
  1353. return ret;
  1354. }
  1355. }
  1356. ret = ff_write_chained(oc, stream_index, pkt, s, 0);
  1357. return ret;
  1358. }
  1359. static int hls_write_trailer(struct AVFormatContext *s)
  1360. {
  1361. HLSContext *hls = s->priv_data;
  1362. AVFormatContext *oc = hls->avf;
  1363. AVFormatContext *vtt_oc = hls->vtt_avf;
  1364. char *old_filename = av_strdup(hls->avf->filename);
  1365. if (!old_filename) {
  1366. return AVERROR(ENOMEM);
  1367. }
  1368. av_write_trailer(oc);
  1369. if (oc->pb) {
  1370. hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
  1371. ff_format_io_close(s, &oc->pb);
  1372. if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0]) {
  1373. hls_rename_temp_file(s, oc);
  1374. }
  1375. /* after av_write_trailer, then duration + 1 duration per packet */
  1376. hls_append_segment(s, hls, hls->duration + hls->dpp, hls->start_pos, hls->size);
  1377. }
  1378. sls_flag_file_rename(hls, old_filename);
  1379. if (vtt_oc) {
  1380. if (vtt_oc->pb)
  1381. av_write_trailer(vtt_oc);
  1382. hls->size = avio_tell(hls->vtt_avf->pb) - hls->start_pos;
  1383. ff_format_io_close(s, &vtt_oc->pb);
  1384. }
  1385. av_freep(&hls->basename);
  1386. av_freep(&hls->key_basename);
  1387. avformat_free_context(oc);
  1388. hls->avf = NULL;
  1389. hls_window(s, 1);
  1390. if (vtt_oc) {
  1391. av_freep(&hls->vtt_basename);
  1392. av_freep(&hls->vtt_m3u8_name);
  1393. avformat_free_context(vtt_oc);
  1394. }
  1395. hls_free_segments(hls->segments);
  1396. hls_free_segments(hls->old_segments);
  1397. av_free(old_filename);
  1398. return 0;
  1399. }
  1400. #define OFFSET(x) offsetof(HLSContext, x)
  1401. #define E AV_OPT_FLAG_ENCODING_PARAM
  1402. static const AVOption options[] = {
  1403. {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
  1404. {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
  1405. {"hls_init_time", "set segment length in seconds at init list", OFFSET(init_time), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, FLT_MAX, E},
  1406. {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
  1407. {"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},
  1408. {"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},
  1409. #if FF_API_HLS_WRAP
  1410. {"hls_wrap", "set number after which the index wraps (will be deprecated)", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
  1411. #endif
  1412. {"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},
  1413. {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  1414. {"hls_segment_filename", "filename template for segment files", OFFSET(segment_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  1415. {"hls_segment_size", "maximum size per segment file, (in bytes)", OFFSET(max_seg_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
  1416. {"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},
  1417. {"hls_enc", "enable AES128 encryption support", OFFSET(encrypt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
  1418. {"hls_enc_key", "hex-coded 16 byte key to encrypt the segments", OFFSET(key), AV_OPT_TYPE_STRING, .flags = E},
  1419. {"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},
  1420. {"hls_enc_iv", "hex-coded 16 byte initialization vector", OFFSET(iv), AV_OPT_TYPE_STRING, .flags = E},
  1421. {"hls_subtitle_path", "set path of hls subtitles", OFFSET(subtitle_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  1422. {"hls_flags", "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
  1423. {"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"},
  1424. {"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"},
  1425. {"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"},
  1426. {"round_durations", "round durations in m3u8 to whole numbers", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_ROUND_DURATIONS }, 0, UINT_MAX, E, "flags"},
  1427. {"discont_start", "start the playlist with a discontinuity tag", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_DISCONT_START }, 0, UINT_MAX, E, "flags"},
  1428. {"omit_endlist", "Do not append an endlist when ending stream", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_OMIT_ENDLIST }, 0, UINT_MAX, E, "flags"},
  1429. {"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"},
  1430. {"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"},
  1431. {"program_date_time", "add EXT-X-PROGRAM-DATE-TIME", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PROGRAM_DATE_TIME }, 0, UINT_MAX, E, "flags"},
  1432. {"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"},
  1433. {"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"},
  1434. {"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"},
  1435. {"use_localtime", "set filename expansion with strftime at segment creation", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  1436. {"use_localtime_mkdir", "create last directory component in strftime-generated filename", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
  1437. {"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" },
  1438. {"event", "EVENT playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_EVENT }, INT_MIN, INT_MAX, E, "pl_type" },
  1439. {"vod", "VOD playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_VOD }, INT_MIN, INT_MAX, E, "pl_type" },
  1440. {"method", "set the HTTP method(default: PUT)", OFFSET(method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  1441. {"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" },
  1442. {"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" },
  1443. {"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" },
  1444. {"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" },
  1445. { NULL },
  1446. };
  1447. static const AVClass hls_class = {
  1448. .class_name = "hls muxer",
  1449. .item_name = av_default_item_name,
  1450. .option = options,
  1451. .version = LIBAVUTIL_VERSION_INT,
  1452. };
  1453. AVOutputFormat ff_hls_muxer = {
  1454. .name = "hls",
  1455. .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
  1456. .extensions = "m3u8",
  1457. .priv_data_size = sizeof(HLSContext),
  1458. .audio_codec = AV_CODEC_ID_AAC,
  1459. .video_codec = AV_CODEC_ID_H264,
  1460. .subtitle_codec = AV_CODEC_ID_WEBVTT,
  1461. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  1462. .write_header = hls_write_header,
  1463. .write_packet = hls_write_packet,
  1464. .write_trailer = hls_write_trailer,
  1465. .priv_class = &hls_class,
  1466. };