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