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.

552 lines
15KB

  1. /*
  2. * Apple HTTP Live Streaming segmenter
  3. * Copyright (c) 2012, Luca Barbato
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <float.h>
  22. #include <stdint.h>
  23. #include <config.h>
  24. #if CONFIG_GCRYPT
  25. #include <gcrypt.h>
  26. #elif CONFIG_OPENSSL
  27. #include <openssl/rand.h>
  28. #endif
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/parseutils.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/random_seed.h"
  35. #include "libavutil/log.h"
  36. #include "avformat.h"
  37. #include "internal.h"
  38. typedef struct ListEntry {
  39. char name[1024];
  40. int64_t duration; // segment duration in AV_TIME_BASE units
  41. struct ListEntry *next;
  42. } ListEntry;
  43. typedef struct HLSContext {
  44. const AVClass *class; // Class for private options.
  45. unsigned number;
  46. int64_t sequence;
  47. int64_t start_sequence;
  48. AVOutputFormat *oformat;
  49. AVFormatContext *avf;
  50. float time; // Set by a private option.
  51. int size; // Set by a private option.
  52. int wrap; // Set by a private option.
  53. int version; // Set by a private option.
  54. int allowcache;
  55. int64_t recording_time;
  56. int has_video;
  57. // The following timestamps are in AV_TIME_BASE units.
  58. int64_t start_pts;
  59. int64_t end_pts;
  60. int64_t duration; // last segment duration computed so far.
  61. int nb_entries;
  62. ListEntry *list;
  63. ListEntry *end_list;
  64. char *basename;
  65. char *baseurl;
  66. int encrypt; // Set by a private option.
  67. char *key; // Set by a private option.
  68. int key_len;
  69. char *key_url; // Set by a private option.
  70. char *iv; // Set by a private option.
  71. int iv_len;
  72. char *key_basename;
  73. AVDictionary *enc_opts;
  74. } HLSContext;
  75. static int randomize(uint8_t *buf, int len)
  76. {
  77. #if CONFIG_GCRYPT
  78. gcry_randomize(buf, len, GCRY_VERY_STRONG_RANDOM);
  79. return 0;
  80. #elif CONFIG_OPENSSL
  81. if (RAND_bytes(buf, len))
  82. return 0;
  83. #else
  84. return AVERROR(ENOSYS);
  85. #endif
  86. }
  87. static void free_encryption(AVFormatContext *s)
  88. {
  89. HLSContext *hls = s->priv_data;
  90. av_dict_free(&hls->enc_opts);
  91. av_freep(&hls->key_basename);
  92. }
  93. static int dict_set_bin(AVDictionary **dict, const char *key, uint8_t *buf)
  94. {
  95. char hex[33];
  96. ff_data_to_hex(hex, buf, sizeof(buf), 0);
  97. hex[32] = '\0';
  98. return av_dict_set(dict, key, hex, 0);
  99. }
  100. static int setup_encryption(AVFormatContext *s)
  101. {
  102. HLSContext *hls = s->priv_data;
  103. AVIOContext *out = NULL;
  104. int len, ret;
  105. uint8_t buf[16];
  106. uint8_t *k;
  107. len = strlen(hls->basename) + 4 + 1;
  108. hls->key_basename = av_mallocz(len);
  109. if (!hls->key_basename)
  110. return AVERROR(ENOMEM);
  111. av_strlcpy(hls->key_basename, hls->basename + 7, len);
  112. av_strlcat(hls->key_basename, ".key", len);
  113. if (hls->key) {
  114. if (hls->key_len != 16) {
  115. av_log(s, AV_LOG_ERROR,
  116. "Invalid key size %d, expected 16-bytes hex-coded key\n",
  117. hls->key_len);
  118. return AVERROR(EINVAL);
  119. }
  120. if ((ret = dict_set_bin(&hls->enc_opts, "key", hls->key)) < 0)
  121. return ret;
  122. k = hls->key;
  123. } else {
  124. if ((ret = randomize(buf, sizeof(buf))) < 0) {
  125. av_log(s, AV_LOG_ERROR, "Cannot generate a strong random key\n");
  126. return ret;
  127. }
  128. if ((ret = dict_set_bin(&hls->enc_opts, "key", buf)) < 0)
  129. return ret;
  130. k = buf;
  131. }
  132. if (hls->iv) {
  133. if (hls->iv_len != 16) {
  134. av_log(s, AV_LOG_ERROR,
  135. "Invalid key size %d, expected 16-bytes hex-coded initialization vector\n",
  136. hls->iv_len);
  137. return AVERROR(EINVAL);
  138. }
  139. if ((ret = dict_set_bin(&hls->enc_opts, "iv", hls->iv)) < 0)
  140. return ret;
  141. }
  142. if ((ret = s->io_open(s, &out, hls->key_basename, AVIO_FLAG_WRITE, NULL)) < 0)
  143. return ret;
  144. avio_write(out, k, 16);
  145. avio_close(out);
  146. return 0;
  147. }
  148. static int hls_mux_init(AVFormatContext *s)
  149. {
  150. HLSContext *hls = s->priv_data;
  151. AVFormatContext *oc;
  152. int i;
  153. hls->avf = oc = avformat_alloc_context();
  154. if (!oc)
  155. return AVERROR(ENOMEM);
  156. oc->oformat = hls->oformat;
  157. oc->interrupt_callback = s->interrupt_callback;
  158. oc->opaque = s->opaque;
  159. oc->io_open = s->io_open;
  160. oc->io_close = s->io_close;
  161. for (i = 0; i < s->nb_streams; i++) {
  162. AVStream *st;
  163. if (!(st = avformat_new_stream(oc, NULL)))
  164. return AVERROR(ENOMEM);
  165. avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
  166. st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
  167. st->time_base = s->streams[i]->time_base;
  168. }
  169. return 0;
  170. }
  171. static int append_entry(HLSContext *hls, int64_t duration)
  172. {
  173. ListEntry *en = av_malloc(sizeof(*en));
  174. if (!en)
  175. return AVERROR(ENOMEM);
  176. av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
  177. en->duration = duration;
  178. en->next = NULL;
  179. if (!hls->list)
  180. hls->list = en;
  181. else
  182. hls->end_list->next = en;
  183. hls->end_list = en;
  184. if (hls->nb_entries >= hls->size) {
  185. en = hls->list;
  186. hls->list = en->next;
  187. av_free(en);
  188. } else
  189. hls->nb_entries++;
  190. hls->sequence++;
  191. return 0;
  192. }
  193. static void free_entries(HLSContext *hls)
  194. {
  195. ListEntry *p = hls->list, *en;
  196. while(p) {
  197. en = p;
  198. p = p->next;
  199. av_free(en);
  200. }
  201. }
  202. static int hls_window(AVFormatContext *s, int last)
  203. {
  204. HLSContext *hls = s->priv_data;
  205. ListEntry *en;
  206. int64_t target_duration = 0;
  207. int ret = 0;
  208. AVIOContext *out = NULL;
  209. char temp_filename[1024];
  210. int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
  211. snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
  212. if ((ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL)) < 0)
  213. goto fail;
  214. for (en = hls->list; en; en = en->next) {
  215. if (target_duration < en->duration)
  216. target_duration = en->duration;
  217. }
  218. avio_printf(out, "#EXTM3U\n");
  219. avio_printf(out, "#EXT-X-VERSION:%d\n", hls->version);
  220. if (hls->allowcache == 0 || hls->allowcache == 1) {
  221. avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
  222. }
  223. avio_printf(out, "#EXT-X-TARGETDURATION:%"PRId64"\n",
  224. av_rescale_rnd(target_duration, 1, AV_TIME_BASE,
  225. AV_ROUND_UP));
  226. avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
  227. av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
  228. sequence);
  229. for (en = hls->list; en; en = en->next) {
  230. if (hls->encrypt) {
  231. char *key_url;
  232. if (hls->key_url)
  233. key_url = hls->key_url;
  234. else
  235. key_url = hls->baseurl;
  236. avio_printf(out, "#EXT-X-KEY:METHOD=AES-128");
  237. avio_printf(out, ",URI=\"");
  238. if (key_url)
  239. avio_printf(out, "%s", key_url);
  240. avio_printf(out, "%s\"", av_basename(hls->key_basename));
  241. if (hls->iv)
  242. avio_printf(out, ",IV=\"0x%s\"", hls->iv);
  243. avio_printf(out, "\n");
  244. }
  245. if (hls->version > 2)
  246. avio_printf(out, "#EXTINF:%f\n",
  247. (double)en->duration / AV_TIME_BASE);
  248. else
  249. avio_printf(out, "#EXTINF:%"PRId64",\n",
  250. av_rescale(en->duration, 1, AV_TIME_BASE));
  251. if (hls->baseurl)
  252. avio_printf(out, "%s", hls->baseurl);
  253. avio_printf(out, "%s\n", en->name);
  254. }
  255. if (last)
  256. avio_printf(out, "#EXT-X-ENDLIST\n");
  257. fail:
  258. ff_format_io_close(s, &out);
  259. if (ret >= 0)
  260. ff_rename(temp_filename, s->filename);
  261. return ret;
  262. }
  263. static int hls_start(AVFormatContext *s)
  264. {
  265. HLSContext *c = s->priv_data;
  266. AVFormatContext *oc = c->avf;
  267. int err = 0;
  268. AVDictionary *opts = NULL;
  269. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  270. c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0)
  271. return AVERROR(EINVAL);
  272. c->number++;
  273. if (c->encrypt) {
  274. if ((err = av_dict_copy(&opts, c->enc_opts, 0)) < 0)
  275. return err;
  276. if (!c->iv) {
  277. uint8_t iv[16] = { 0 };
  278. char buf[33];
  279. AV_WB64(iv + 8, c->sequence);
  280. ff_data_to_hex(buf, iv, sizeof(iv), 0);
  281. buf[32] = '\0';
  282. if ((err = av_dict_set(&opts, "iv", buf, 0)) < 0)
  283. goto fail;
  284. }
  285. }
  286. if ((err = s->io_open(s, &oc->pb, oc->filename, AVIO_FLAG_WRITE, &opts)) < 0)
  287. return err;
  288. if (oc->oformat->priv_class && oc->priv_data)
  289. av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
  290. fail:
  291. av_dict_free(&opts);
  292. return err;
  293. }
  294. static int hls_setup(AVFormatContext *s)
  295. {
  296. HLSContext *hls = s->priv_data;
  297. const char *pattern = "%d.ts";
  298. int basename_size = strlen(s->filename) + strlen(pattern) + 1;
  299. char *p;
  300. if (hls->encrypt)
  301. basename_size += 7;
  302. hls->basename = av_mallocz(basename_size);
  303. if (!hls->basename)
  304. return AVERROR(ENOMEM);
  305. // TODO: support protocol nesting?
  306. if (hls->encrypt)
  307. strcpy(hls->basename, "crypto:");
  308. av_strlcat(hls->basename, s->filename, basename_size);
  309. p = strrchr(hls->basename, '.');
  310. if (p)
  311. *p = '\0';
  312. if (hls->encrypt) {
  313. int ret = setup_encryption(s);
  314. if (ret < 0)
  315. return ret;
  316. }
  317. av_strlcat(hls->basename, pattern, basename_size);
  318. return 0;
  319. }
  320. static int hls_write_header(AVFormatContext *s)
  321. {
  322. HLSContext *hls = s->priv_data;
  323. int ret, i;
  324. hls->sequence = hls->start_sequence;
  325. hls->recording_time = hls->time * AV_TIME_BASE;
  326. hls->start_pts = AV_NOPTS_VALUE;
  327. for (i = 0; i < s->nb_streams; i++)
  328. hls->has_video +=
  329. s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
  330. if (hls->has_video > 1)
  331. av_log(s, AV_LOG_WARNING,
  332. "More than a single video stream present, "
  333. "expect issues decoding it.\n");
  334. hls->oformat = av_guess_format("mpegts", NULL, NULL);
  335. if (!hls->oformat) {
  336. ret = AVERROR_MUXER_NOT_FOUND;
  337. goto fail;
  338. }
  339. if ((ret = hls_setup(s)) < 0)
  340. goto fail;
  341. if ((ret = hls_mux_init(s)) < 0)
  342. goto fail;
  343. if ((ret = hls_start(s)) < 0)
  344. goto fail;
  345. if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
  346. return ret;
  347. fail:
  348. if (ret) {
  349. av_free(hls->basename);
  350. if (hls->avf)
  351. avformat_free_context(hls->avf);
  352. free_encryption(s);
  353. }
  354. return ret;
  355. }
  356. static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
  357. {
  358. HLSContext *hls = s->priv_data;
  359. AVFormatContext *oc = hls->avf;
  360. AVStream *st = s->streams[pkt->stream_index];
  361. int64_t end_pts = hls->recording_time * hls->number;
  362. int64_t pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
  363. int ret, can_split = 1;
  364. if (hls->start_pts == AV_NOPTS_VALUE) {
  365. hls->start_pts = pts;
  366. hls->end_pts = pts;
  367. }
  368. if (hls->has_video) {
  369. can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  370. pkt->flags & AV_PKT_FLAG_KEY;
  371. }
  372. if (pkt->pts == AV_NOPTS_VALUE)
  373. can_split = 0;
  374. else
  375. hls->duration = pts - hls->end_pts;
  376. if (can_split && pts - hls->start_pts >= end_pts) {
  377. ret = append_entry(hls, hls->duration);
  378. if (ret)
  379. return ret;
  380. hls->end_pts = pts;
  381. hls->duration = 0;
  382. av_write_frame(oc, NULL); /* Flush any buffered data */
  383. ff_format_io_close(s, &oc->pb);
  384. ret = hls_start(s);
  385. if (ret)
  386. return ret;
  387. oc = hls->avf;
  388. if ((ret = hls_window(s, 0)) < 0)
  389. return ret;
  390. }
  391. ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
  392. return ret;
  393. }
  394. static int hls_write_trailer(struct AVFormatContext *s)
  395. {
  396. HLSContext *hls = s->priv_data;
  397. AVFormatContext *oc = hls->avf;
  398. av_write_trailer(oc);
  399. ff_format_io_close(s, &oc->pb);
  400. avformat_free_context(oc);
  401. av_free(hls->basename);
  402. append_entry(hls, hls->duration);
  403. hls_window(s, 1);
  404. free_entries(hls);
  405. free_encryption(s);
  406. return 0;
  407. }
  408. #define OFFSET(x) offsetof(HLSContext, x)
  409. #define E AV_OPT_FLAG_ENCODING_PARAM
  410. static const AVOption options[] = {
  411. {"start_number", "first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
  412. {"hls_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
  413. {"hls_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
  414. {"hls_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
  415. {"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},
  416. {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
  417. {"hls_version", "protocol version", OFFSET(version), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, E},
  418. {"hls_enc", "AES128 encryption support", OFFSET(encrypt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E},
  419. {"hls_enc_key", "use the specified hex-coded 16byte key to encrypt the segments", OFFSET(key), AV_OPT_TYPE_BINARY, .flags = E},
  420. {"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},
  421. {"hls_enc_iv", "use the specified hex-coded 16byte initialization vector", OFFSET(iv), AV_OPT_TYPE_BINARY, .flags = E},
  422. { NULL },
  423. };
  424. static const AVClass hls_class = {
  425. .class_name = "hls muxer",
  426. .item_name = av_default_item_name,
  427. .option = options,
  428. .version = LIBAVUTIL_VERSION_INT,
  429. };
  430. AVOutputFormat ff_hls_muxer = {
  431. .name = "hls",
  432. .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
  433. .extensions = "m3u8",
  434. .priv_data_size = sizeof(HLSContext),
  435. .audio_codec = AV_CODEC_ID_AAC,
  436. .video_codec = AV_CODEC_ID_H264,
  437. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  438. .write_header = hls_write_header,
  439. .write_packet = hls_write_packet,
  440. .write_trailer = hls_write_trailer,
  441. .priv_class = &hls_class,
  442. };