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