hlsenc: Add encryption support
[libav.git] / libavformat / hlsenc.c
CommitLineData
22a0827d
LB
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>
8f8bc923 23#include <stdint.h>
22a0827d 24
0a4b9d0c
LB
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
22a0827d
LB
33#include "libavutil/mathematics.h"
34#include "libavutil/parseutils.h"
35#include "libavutil/avstring.h"
0a4b9d0c 36#include "libavutil/intreadwrite.h"
22a0827d 37#include "libavutil/opt.h"
0a4b9d0c 38#include "libavutil/random_seed.h"
22a0827d
LB
39#include "libavutil/log.h"
40
41#include "avformat.h"
42#include "internal.h"
43
44typedef struct ListEntry {
45 char name[1024];
72839fce 46 int64_t duration; // segment duration in AV_TIME_BASE units
22a0827d
LB
47 struct ListEntry *next;
48} ListEntry;
49
50typedef struct HLSContext {
51 const AVClass *class; // Class for private options.
30a76487 52 unsigned number;
27a15e0a 53 int64_t sequence;
c7603b3c 54 int64_t start_sequence;
22a0827d
LB
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.
2157df42 60 int version; // Set by a private option.
55f03d87 61 int allowcache;
22a0827d
LB
62 int64_t recording_time;
63 int has_video;
72839fce 64 // The following timestamps are in AV_TIME_BASE units.
22a0827d
LB
65 int64_t start_pts;
66 int64_t end_pts;
72839fce 67 int64_t duration; // last segment duration computed so far.
0448f26c 68 int nb_entries;
22a0827d
LB
69 ListEntry *list;
70 ListEntry *end_list;
71 char *basename;
5a70a783 72 char *baseurl;
0a4b9d0c
LB
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;
22a0827d
LB
84} HLSContext;
85
0a4b9d0c
LB
86
87static 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
100static 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
109static 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
119static 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
22a0827d
LB
179static 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;
9f61abc8
AK
191 oc->opaque = s->opaque;
192 oc->io_open = s->io_open;
193 oc->io_close = s->io_close;
22a0827d
LB
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);
9200514a 199 avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
22a0827d 200 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
28816050 201 st->time_base = s->streams[i]->time_base;
22a0827d
LB
202 }
203
204 return 0;
205}
206
72839fce 207static int append_entry(HLSContext *hls, int64_t duration)
22a0827d
LB
208{
209 ListEntry *en = av_malloc(sizeof(*en));
210
211 if (!en)
212 return AVERROR(ENOMEM);
213
ae85d6c9 214 av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
22a0827d
LB
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
0448f26c 226 if (hls->nb_entries >= hls->size) {
22a0827d
LB
227 en = hls->list;
228 hls->list = en->next;
229 av_free(en);
0448f26c
LB
230 } else
231 hls->nb_entries++;
22a0827d 232
27a15e0a
K
233 hls->sequence++;
234
22a0827d
LB
235 return 0;
236}
237
238static 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
249static int hls_window(AVFormatContext *s, int last)
250{
251 HLSContext *hls = s->priv_data;
252 ListEntry *en;
72839fce 253 int64_t target_duration = 0;
22a0827d 254 int ret = 0;
ff48ae94 255 AVIOContext *out = NULL;
3fe26bb6 256 char temp_filename[1024];
a1643103 257 int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->size);
22a0827d 258
3fe26bb6 259 snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
9f61abc8 260 if ((ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL)) < 0)
22a0827d
LB
261 goto fail;
262
7e98956e
LB
263 for (en = hls->list; en; en = en->next) {
264 if (target_duration < en->duration)
265 target_duration = en->duration;
266 }
267
ff48ae94 268 avio_printf(out, "#EXTM3U\n");
2157df42 269 avio_printf(out, "#EXT-X-VERSION:%d\n", hls->version);
55f03d87 270 if (hls->allowcache == 0 || hls->allowcache == 1) {
ff48ae94 271 avio_printf(out, "#EXT-X-ALLOW-CACHE:%s\n", hls->allowcache == 0 ? "NO" : "YES");
55f03d87 272 }
72839fce 273 avio_printf(out, "#EXT-X-TARGETDURATION:%"PRId64"\n",
2157df42
LB
274 av_rescale_rnd(target_duration, 1, AV_TIME_BASE,
275 AV_ROUND_UP));
ff48ae94 276 avio_printf(out, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
22a0827d 277
344f7b5a 278 av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
a1643103 279 sequence);
344f7b5a 280
22a0827d 281 for (en = hls->list; en; en = en->next) {
0a4b9d0c
LB
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
2157df42
LB
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));
5a70a783 306 if (hls->baseurl)
ff48ae94
HL
307 avio_printf(out, "%s", hls->baseurl);
308 avio_printf(out, "%s\n", en->name);
22a0827d
LB
309 }
310
311 if (last)
ff48ae94 312 avio_printf(out, "#EXT-X-ENDLIST\n");
22a0827d
LB
313
314fail:
9f61abc8 315 ff_format_io_close(s, &out);
3fe26bb6
HL
316 if (ret >= 0)
317 ff_rename(temp_filename, s->filename);
22a0827d
LB
318 return ret;
319}
320
321static int hls_start(AVFormatContext *s)
322{
323 HLSContext *c = s->priv_data;
324 AVFormatContext *oc = c->avf;
325 int err = 0;
0a4b9d0c
LB
326 AVDictionary *opts = NULL;
327
22a0827d 328
22a0827d 329 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
7266e24f 330 c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0)
22a0827d 331 return AVERROR(EINVAL);
9d86bfc2 332 c->number++;
22a0827d 333
0a4b9d0c
LB
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)
22a0827d
LB
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
0a4b9d0c
LB
356fail:
357 av_dict_free(&opts);
358
359 return err;
360}
361
362static 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
22a0827d
LB
395 return 0;
396}
397
398static int hls_write_header(AVFormatContext *s)
399{
400 HLSContext *hls = s->priv_data;
401 int ret, i;
22a0827d 402
c7603b3c 403 hls->sequence = hls->start_sequence;
0d8cc7a3 404 hls->recording_time = hls->time * AV_TIME_BASE;
22a0827d
LB
405 hls->start_pts = AV_NOPTS_VALUE;
406
407 for (i = 0; i < s->nb_streams; i++)
408 hls->has_video +=
9200514a 409 s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO;
22a0827d
LB
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
0a4b9d0c 423 if ((ret = hls_setup(s)) < 0)
22a0827d 424 goto fail;
22a0827d
LB
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
436fail:
437 if (ret) {
438 av_free(hls->basename);
439 if (hls->avf)
440 avformat_free_context(hls->avf);
0a4b9d0c
LB
441
442 free_encryption(s);
22a0827d
LB
443 }
444 return ret;
445}
446
447static 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;
72839fce 453 int64_t pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
cf679b94 454 int ret, can_split = 1;
22a0827d
LB
455
456 if (hls->start_pts == AV_NOPTS_VALUE) {
72839fce
LB
457 hls->start_pts = pts;
458 hls->end_pts = pts;
22a0827d 459 }
22a0827d 460
cf679b94 461 if (hls->has_video) {
9200514a 462 can_split = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
cf679b94
AK
463 pkt->flags & AV_PKT_FLAG_KEY;
464 }
09c93b1b
SS
465 if (pkt->pts == AV_NOPTS_VALUE)
466 can_split = 0;
467 else
72839fce 468 hls->duration = pts - hls->end_pts;
22a0827d 469
72839fce 470 if (can_split && pts - hls->start_pts >= end_pts) {
09c93b1b 471 ret = append_entry(hls, hls->duration);
6dd93ee6
LB
472 if (ret)
473 return ret;
474
72839fce 475 hls->end_pts = pts;
09c93b1b 476 hls->duration = 0;
22a0827d
LB
477
478 av_write_frame(oc, NULL); /* Flush any buffered data */
9f61abc8 479 ff_format_io_close(s, &oc->pb);
22a0827d
LB
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
497static 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);
9f61abc8 503 ff_format_io_close(s, &oc->pb);
22a0827d
LB
504 avformat_free_context(oc);
505 av_free(hls->basename);
09c93b1b 506 append_entry(hls, hls->duration);
22a0827d
LB
507 hls_window(s, 1);
508
509 free_entries(hls);
0a4b9d0c 510 free_encryption(s);
22a0827d
LB
511 return 0;
512}
513
514#define OFFSET(x) offsetof(HLSContext, x)
515#define E AV_OPT_FLAG_ENCODING_PARAM
516static const AVOption options[] = {
c7603b3c 517 {"start_number", "first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
22a0827d
LB
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},
55f03d87 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},
5a70a783 522 {"hls_base_url", "url to prepend to each playlist entry", OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E},
2157df42 523 {"hls_version", "protocol version", OFFSET(version), AV_OPT_TYPE_INT, {.i64 = 3}, 2, 3, E},
0a4b9d0c
LB
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},
22a0827d
LB
528 { NULL },
529};
530
531static 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
539AVOutputFormat ff_hls_muxer = {
540 .name = "hls",
adbe0307 541 .long_name = NULL_IF_CONFIG_SMALL("Apple HTTP Live Streaming"),
22a0827d
LB
542 .extensions = "m3u8",
543 .priv_data_size = sizeof(HLSContext),
34e2ce5d
MS
544 .audio_codec = AV_CODEC_ID_AAC,
545 .video_codec = AV_CODEC_ID_H264,
22a0827d
LB
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};