forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vod_hls.c
1344 lines (1186 loc) · 38.1 KB
/
ngx_http_vod_hls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ngx_http.h>
#include <ngx_md5.h>
#include "ngx_http_vod_submodule.h"
#include "ngx_http_vod_utils.h"
#include "vod/subtitle/webvtt_builder.h"
#include "vod/hls/hls_muxer.h"
#include "vod/mp4/mp4_muxer.h"
#include "vod/mp4/mp4_fragment.h"
#include "vod/mp4/mp4_init_segment.h"
#include "vod/udrm.h"
#if (NGX_HAVE_OPENSSL_EVP)
#include "vod/dash/edash_packager.h"
#include "vod/mp4/mp4_cbcs_encrypt.h"
#include "vod/hls/aes_cbc_encrypt.h"
#endif // NGX_HAVE_OPENSSL_EVP
// constants
#define SUPPORTED_CODECS_MP4 \
(VOD_CODEC_FLAG(AVC) | \
VOD_CODEC_FLAG(HEVC) | \
VOD_CODEC_FLAG(AV1) | \
VOD_CODEC_FLAG(AAC) | \
VOD_CODEC_FLAG(AC3) | \
VOD_CODEC_FLAG(EAC3) | \
VOD_CODEC_FLAG(MP3) | \
VOD_CODEC_FLAG(DTS) | \
VOD_CODEC_FLAG(FLAC))
#define SUPPORTED_CODECS_TS \
(VOD_CODEC_FLAG(AVC) | \
VOD_CODEC_FLAG(HEVC) | \
VOD_CODEC_FLAG(AAC) | \
VOD_CODEC_FLAG(AC3) | \
VOD_CODEC_FLAG(EAC3) | \
VOD_CODEC_FLAG(MP3) | \
VOD_CODEC_FLAG(DTS))
#define SUPPORTED_CODECS (SUPPORTED_CODECS_MP4 | SUPPORTED_CODECS_TS)
#define ID3_TEXT_JSON_FORMAT "{\"timestamp\":%uL}%Z"
#define ID3_TEXT_JSON_SEQUENCE_ID_PREFIX_FORMAT "{\"timestamp\":%uL,\"sequenceId\":\""
#define ID3_TEXT_JSON_SEQUENCE_ID_SUFFIX "\"}"
// content types
static u_char m3u8_content_type[] = "application/vnd.apple.mpegurl";
static u_char encryption_key_content_type[] = "application/octet-stream";
static u_char mpeg_ts_content_type[] = "video/MP2T";
static u_char vtt_content_type[] = "text/vtt";
static const u_char ts_file_ext[] = ".ts";
static const u_char m4s_file_ext[] = ".m4s";
static const u_char vtt_file_ext[] = ".vtt";
static const u_char mp4_file_ext[] = ".mp4";
static const u_char m3u8_file_ext[] = ".m3u8";
static const u_char key_file_ext[] = ".key";
// constants
static ngx_str_t empty_string = ngx_null_string;
ngx_conf_enum_t hls_encryption_methods[] = {
{ ngx_string("none"), HLS_ENC_NONE },
{ ngx_string("aes-128"), HLS_ENC_AES_128 },
{ ngx_string("sample-aes"), HLS_ENC_SAMPLE_AES },
{ ngx_string("sample-aes-cenc"), HLS_ENC_SAMPLE_AES_CENC },
{ ngx_null_string, 0 }
};
ngx_conf_enum_t hls_container_formats[] = {
{ ngx_string("auto"), HLS_CONTAINER_AUTO },
{ ngx_string("mpegts"), HLS_CONTAINER_MPEGTS },
{ ngx_string("fmp4"), HLS_CONTAINER_FMP4 },
{ ngx_null_string, 0 }
};
static ngx_uint_t
ngx_http_vod_hls_get_container_format(
ngx_http_vod_hls_loc_conf_t* conf,
media_set_t* media_set)
{
media_track_t* track;
if (conf->m3u8_config.container_format != HLS_CONTAINER_AUTO)
{
return conf->m3u8_config.container_format;
}
track = media_set->filtered_tracks;
if ((track->media_info.media_type == MEDIA_TYPE_VIDEO && track->media_info.codec_id != VOD_CODEC_ID_AVC) ||
conf->encryption_method == HLS_ENC_SAMPLE_AES_CENC)
{
return HLS_CONTAINER_FMP4;
}
return HLS_CONTAINER_MPEGTS;
}
#if (NGX_HAVE_OPENSSL_EVP)
// some random salt to prevent the iv from being equal to key in case encryption_iv_seed is null
static u_char iv_salt[] = { 0xa7, 0xc6, 0x17, 0xab, 0x52, 0x2c, 0x40, 0x3c, 0xf6, 0x8a };
static void
ngx_http_vod_hls_init_encryption_iv(u_char* iv, uint32_t segment_index)
{
u_char* p;
// the IV is the segment index in big endian
vod_memzero(iv, AES_BLOCK_SIZE - sizeof(uint32_t));
segment_index++;
p = iv + AES_BLOCK_SIZE - sizeof(uint32_t);
*p++ = (u_char)(segment_index >> 24);
*p++ = (u_char)(segment_index >> 16);
*p++ = (u_char)(segment_index >> 8);
*p++ = (u_char)(segment_index);
}
static ngx_int_t
ngx_http_vod_hls_get_iv_seed(
ngx_http_vod_submodule_context_t* submodule_context,
media_sequence_t* sequence,
ngx_str_t* result)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
ngx_http_complex_value_t* value;
if (conf->encryption_iv_seed != NULL)
{
value = conf->encryption_iv_seed;
}
else if (conf->secret_key != NULL)
{
value = conf->secret_key;
}
else
{
*result = sequence->mapped_uri;
return NGX_OK;
}
if (ngx_http_complex_value(
submodule_context->r,
value,
result) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_get_iv_seed: ngx_http_complex_value failed");
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_init_encryption_params(
hls_encryption_params_t* encryption_params,
ngx_http_vod_submodule_context_t* submodule_context,
ngx_uint_t container_format)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
media_sequence_t* sequence;
drm_info_t* drm_info;
ngx_str_t iv_seed;
ngx_md5_t md5;
ngx_int_t rc;
encryption_params->type = conf->hls.encryption_method;
if (encryption_params->type == HLS_ENC_NONE)
{
return NGX_OK;
}
encryption_params->iv = encryption_params->iv_buf;
encryption_params->return_iv = conf->hls.output_iv;
sequence = &submodule_context->media_set.sequences[0];
if (conf->drm_enabled)
{
drm_info = sequence->drm_info;
encryption_params->key = drm_info->key;
if (drm_info->iv_set)
{
encryption_params->iv = drm_info->iv;
return NGX_OK;
}
}
else
{
encryption_params->key = sequence->encryption_key;
}
if (container_format != HLS_CONTAINER_FMP4 || encryption_params->type != HLS_ENC_AES_128)
{
ngx_http_vod_hls_init_encryption_iv(
encryption_params->iv_buf,
submodule_context->request_params.segment_index);
return NGX_OK;
}
// must generate an iv in this case
rc = ngx_http_vod_hls_get_iv_seed(
submodule_context,
sequence,
&iv_seed);
if (rc != NGX_OK)
{
return rc;
}
ngx_md5_init(&md5);
ngx_md5_update(&md5, iv_salt, sizeof(iv_salt));
ngx_md5_update(&md5, iv_seed.data, iv_seed.len);
ngx_md5_final(encryption_params->iv_buf, &md5);
encryption_params->return_iv = TRUE;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_init_segment_encryption(
ngx_http_vod_submodule_context_t* submodule_context,
segment_writer_t* segment_writer,
ngx_uint_t container_format,
hls_encryption_params_t* encryption_params)
{
aes_cbc_encrypt_context_t* encrypted_write_context;
buffer_pool_t* buffer_pool;
vod_status_t rc;
rc = ngx_http_vod_hls_init_encryption_params(encryption_params, submodule_context, container_format);
if (rc != NGX_OK)
{
return rc;
}
if (encryption_params->type != HLS_ENC_AES_128)
{
return NGX_OK;
}
if (container_format == HLS_CONTAINER_MPEGTS)
{
buffer_pool = submodule_context->request_context.output_buffer_pool;
}
else
{
// Note: should not use buffer pool for fmp4 since the buffers have varying sizes
buffer_pool = NULL;
}
rc = aes_cbc_encrypt_init(
&encrypted_write_context,
&submodule_context->request_context,
segment_writer->write_tail,
segment_writer->context,
buffer_pool,
encryption_params->key,
encryption_params->iv);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_segment_encryption: aes_cbc_encrypt_init failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
segment_writer->write_tail = (write_callback_t)aes_cbc_encrypt_write;
segment_writer->context = encrypted_write_context;
return NGX_OK;
}
#endif // NGX_HAVE_OPENSSL_EVP
static ngx_int_t
ngx_http_vod_hls_get_default_id3_data(ngx_http_vod_submodule_context_t* submodule_context, ngx_str_t* id3_data)
{
media_set_t* media_set;
vod_str_t* sequence_id;
int64_t timestamp;
u_char* p;
size_t sequence_id_escape;
size_t data_size;
media_set = &submodule_context->media_set;
sequence_id = &media_set->sequences[0].id;
if (sequence_id->len != 0)
{
sequence_id_escape = vod_escape_json(NULL, sequence_id->data, sequence_id->len);
data_size = sizeof(ID3_TEXT_JSON_SEQUENCE_ID_PREFIX_FORMAT) + VOD_INT64_LEN +
sequence_id->len + sequence_id_escape +
sizeof(ID3_TEXT_JSON_SEQUENCE_ID_SUFFIX);
}
else
{
sequence_id_escape = 0;
data_size = sizeof(ID3_TEXT_JSON_FORMAT) + VOD_INT64_LEN;
}
timestamp = media_set_get_segment_time_millis(media_set);
p = ngx_pnalloc(submodule_context->request_context.pool, data_size);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_get_default_id3_data: ngx_pnalloc failed");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_ALLOC_FAILED);
}
id3_data->data = p;
if (sequence_id->len != 0)
{
p = vod_sprintf(p, ID3_TEXT_JSON_SEQUENCE_ID_PREFIX_FORMAT, timestamp);
if (sequence_id_escape)
{
p = (u_char*)vod_escape_json(p, sequence_id->data, sequence_id->len);
}
else
{
p = vod_copy(p, sequence_id->data, sequence_id->len);
}
p = vod_copy(p, ID3_TEXT_JSON_SEQUENCE_ID_SUFFIX, sizeof(ID3_TEXT_JSON_SEQUENCE_ID_SUFFIX));
}
else
{
p = vod_sprintf(p, ID3_TEXT_JSON_FORMAT, timestamp);
}
id3_data->len = p - id3_data->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_init_muxer_conf(ngx_http_vod_submodule_context_t* submodule_context, hls_mpegts_muxer_conf_t* conf)
{
ngx_http_vod_hls_loc_conf_t* hls_conf;
hls_conf = &submodule_context->conf->hls;
conf->interleave_frames = hls_conf->interleave_frames;
conf->align_frames = hls_conf->align_frames;
conf->align_pts = hls_conf->align_pts;
if (!hls_conf->output_id3_timestamps)
{
conf->id3_data.data = NULL;
conf->id3_data.len = 0;
return NGX_OK;
}
if (hls_conf->id3_data != NULL)
{
if (ngx_http_complex_value(
submodule_context->r,
hls_conf->id3_data,
&conf->id3_data) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_muxer_conf: ngx_http_complex_value failed");
return NGX_ERROR;
}
return NGX_OK;
}
return ngx_http_vod_hls_get_default_id3_data(submodule_context, &conf->id3_data);
}
static ngx_int_t
ngx_http_vod_hls_handle_master_playlist(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
ngx_str_t base_url = ngx_null_string;
vod_status_t rc;
if (conf->hls.absolute_master_urls)
{
rc = ngx_http_vod_get_base_url(submodule_context->r, conf->base_url, &empty_string, &base_url);
if (rc != NGX_OK)
{
return rc;
}
}
rc = m3u8_builder_build_master_playlist(
&submodule_context->request_context,
&conf->hls.m3u8_config,
conf->hls.encryption_method,
&base_url,
&submodule_context->media_set,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_master_playlist: m3u8_builder_build_master_playlist failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
content_type->data = m3u8_content_type;
content_type->len = sizeof(m3u8_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_handle_index_playlist(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
hls_encryption_params_t encryption_params;
ngx_uint_t container_format;
ngx_str_t segments_base_url = ngx_null_string;
ngx_str_t base_url = ngx_null_string;
vod_status_t rc;
if (conf->hls.absolute_index_urls)
{
rc = ngx_http_vod_get_base_url(submodule_context->r, conf->base_url, &submodule_context->r->uri, &base_url);
if (rc != NGX_OK)
{
return rc;
}
if (conf->segments_base_url != NULL)
{
rc = ngx_http_vod_get_base_url(
submodule_context->r,
conf->segments_base_url,
&submodule_context->r->uri,
&segments_base_url);
if (rc != NGX_OK)
{
return rc;
}
}
else
{
segments_base_url = base_url;
}
}
container_format = ngx_http_vod_hls_get_container_format(
&conf->hls,
&submodule_context->media_set);
#if (NGX_HAVE_OPENSSL_EVP)
rc = ngx_http_vod_hls_init_encryption_params(&encryption_params, submodule_context, container_format);
if (rc != NGX_OK)
{
return rc;
}
if (encryption_params.type != HLS_ENC_NONE)
{
if (conf->hls.encryption_key_uri != NULL)
{
if (ngx_http_complex_value(
submodule_context->r,
conf->hls.encryption_key_uri,
&encryption_params.key_uri) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_index_playlist: ngx_http_complex_value failed");
return NGX_ERROR;
}
}
else
{
encryption_params.key_uri.len = 0;
}
}
#else
encryption_params.type = HLS_ENC_NONE;
#endif // NGX_HAVE_OPENSSL_EVP
rc = m3u8_builder_build_index_playlist(
&submodule_context->request_context,
&conf->hls.m3u8_config,
&base_url,
&segments_base_url,
&encryption_params,
container_format,
&submodule_context->media_set,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_index_playlist: m3u8_builder_build_index_playlist failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
content_type->data = m3u8_content_type;
content_type->len = sizeof(m3u8_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_handle_iframe_playlist(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
hls_mpegts_muxer_conf_t muxer_conf;
ngx_str_t base_url = ngx_null_string;
vod_status_t rc;
if (conf->hls.encryption_method != HLS_ENC_NONE)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_iframe_playlist: iframes playlist not supported with encryption");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_BAD_REQUEST);
}
if (submodule_context->media_set.audio_filtering_needed)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_iframe_playlist: iframes playlist not supported with audio filtering");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_BAD_REQUEST);
}
if (conf->hls.absolute_iframe_urls)
{
rc = ngx_http_vod_get_base_url(submodule_context->r, conf->base_url, &submodule_context->r->uri, &base_url);
if (rc != NGX_OK)
{
return rc;
}
}
if (ngx_http_vod_hls_get_container_format(
&conf->hls,
&submodule_context->media_set) == HLS_CONTAINER_FMP4)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_iframe_playlist: iframes playlist not supported with fmp4 container");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_BAD_REQUEST);
}
rc = ngx_http_vod_hls_init_muxer_conf(submodule_context, &muxer_conf);
if (rc != NGX_OK)
{
return rc;
}
rc = m3u8_builder_build_iframe_playlist(
&submodule_context->request_context,
&conf->hls.m3u8_config,
&muxer_conf,
&base_url,
&submodule_context->media_set,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_iframe_playlist: m3u8_builder_build_iframe_playlist failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
content_type->data = m3u8_content_type;
content_type->len = sizeof(m3u8_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_handle_encryption_key(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
u_char* encryption_key;
encryption_key = ngx_palloc(submodule_context->request_context.pool, BUFFER_CACHE_KEY_SIZE);
if (encryption_key == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_encryption_key: ngx_palloc failed");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_ALLOC_FAILED);
}
ngx_memcpy(encryption_key, submodule_context->media_set.sequences[0].encryption_key, BUFFER_CACHE_KEY_SIZE);
response->data = encryption_key;
response->len = BUFFER_CACHE_KEY_SIZE;
content_type->data = encryption_key_content_type;
content_type->len = sizeof(encryption_key_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_init_ts_frame_processor(
ngx_http_vod_submodule_context_t* submodule_context,
segment_writer_t* segment_writer,
ngx_http_vod_frame_processor_t* frame_processor,
void** frame_processor_state,
ngx_str_t* output_buffer,
size_t* response_size,
ngx_str_t* content_type)
{
hls_encryption_params_t encryption_params;
hls_mpegts_muxer_conf_t muxer_conf;
hls_muxer_state_t* state;
vod_status_t rc;
bool_t reuse_output_buffers;
#if (NGX_HAVE_OPENSSL_EVP)
rc = ngx_http_vod_hls_init_segment_encryption(
submodule_context,
segment_writer,
HLS_CONTAINER_MPEGTS,
&encryption_params);
if (rc != NGX_OK)
{
return rc;
}
if (encryption_params.type == HLS_ENC_SAMPLE_AES_CENC)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_ts_frame_processor: sample aes cenc not supported with mpeg ts container");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_BAD_REQUEST);
}
reuse_output_buffers = encryption_params.type == HLS_ENC_AES_128;
#else
encryption_params.type = HLS_ENC_NONE;
reuse_output_buffers = FALSE;
#endif // NGX_HAVE_OPENSSL_EVP
rc = ngx_http_vod_hls_init_muxer_conf(submodule_context, &muxer_conf);
if (rc != NGX_OK)
{
return rc;
}
rc = hls_muxer_init_segment(
&submodule_context->request_context,
&muxer_conf,
&encryption_params,
submodule_context->request_params.segment_index,
&submodule_context->media_set,
segment_writer->write_tail,
segment_writer->context,
reuse_output_buffers,
response_size,
output_buffer,
&state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_ts_frame_processor: hls_muxer_init failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
if (encryption_params.type == HLS_ENC_AES_128 &&
*response_size != 0)
{
*response_size = aes_round_up_to_block(*response_size);
}
*frame_processor = (ngx_http_vod_frame_processor_t)hls_muxer_process;
*frame_processor_state = state;
content_type->len = sizeof(mpeg_ts_content_type) - 1;
content_type->data = (u_char *)mpeg_ts_content_type;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_handle_mp4_init_segment(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
atom_writer_t* stsd_atom_writers = NULL;
vod_status_t rc;
#if (NGX_HAVE_OPENSSL_EVP)
aes_cbc_encrypt_context_t* encrypted_write_context;
hls_encryption_params_t encryption_params;
drm_info_t* drm_info;
rc = ngx_http_vod_hls_init_encryption_params(&encryption_params, submodule_context, HLS_CONTAINER_FMP4);
if (rc != NGX_OK)
{
return rc;
}
switch (encryption_params.type)
{
case HLS_ENC_SAMPLE_AES:
rc = mp4_init_segment_get_encrypted_stsd_writers(
&submodule_context->request_context,
&submodule_context->media_set,
SCHEME_TYPE_CBCS,
FALSE,
NULL,
encryption_params.iv,
&stsd_atom_writers);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_mp4_init_segment: mp4_init_segment_get_encrypted_stsd_writers failed %i (1)", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
break;
case HLS_ENC_SAMPLE_AES_CENC:
drm_info = (drm_info_t*)submodule_context->media_set.sequences[0].drm_info;
rc = mp4_init_segment_get_encrypted_stsd_writers(
&submodule_context->request_context,
&submodule_context->media_set,
SCHEME_TYPE_CENC,
FALSE,
drm_info->key_id,
NULL,
&stsd_atom_writers);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_mp4_init_segment: mp4_init_segment_get_encrypted_stsd_writers failed %i (2)", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
break;
default:;
}
#endif // NGX_HAVE_OPENSSL_EVP
rc = mp4_init_segment_build(
&submodule_context->request_context,
&submodule_context->media_set,
ngx_http_vod_submodule_size_only(submodule_context),
NULL,
stsd_atom_writers,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_mp4_init_segment: mp4_init_segment_build failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
#if (NGX_HAVE_OPENSSL_EVP)
if (encryption_params.type == HLS_ENC_AES_128)
{
rc = aes_cbc_encrypt_init(
&encrypted_write_context,
&submodule_context->request_context,
NULL,
NULL,
NULL,
encryption_params.key,
encryption_params.iv);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_mp4_init_segment: aes_cbc_encrypt_init failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
rc = aes_cbc_encrypt(
encrypted_write_context,
response,
response,
TRUE);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_mp4_init_segment: aes_cbc_encrypt failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
}
#endif // NGX_HAVE_OPENSSL_EVP
mp4_fragment_get_content_type(
submodule_context->media_set.track_count[MEDIA_TYPE_VIDEO],
content_type);
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_init_fmp4_frame_processor(
ngx_http_vod_submodule_context_t* submodule_context,
segment_writer_t* segment_writer,
ngx_http_vod_frame_processor_t* frame_processor,
void** frame_processor_state,
ngx_str_t* output_buffer,
size_t* response_size,
ngx_str_t* content_type)
{
dash_fragment_header_extensions_t header_extensions;
fragment_writer_state_t* state;
mp4_muxer_state_t* muxer_state;
segment_writer_t* segment_writers;
vod_status_t rc;
bool_t per_stream_writer;
bool_t reuse_input_buffers = FALSE;
bool_t size_only = ngx_http_vod_submodule_size_only(submodule_context);
#if (NGX_HAVE_OPENSSL_EVP)
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
hls_encryption_params_t encryption_params;
segment_writer_t drm_writer;
rc = ngx_http_vod_hls_init_segment_encryption(
submodule_context,
segment_writer,
HLS_CONTAINER_FMP4,
&encryption_params);
if (rc != NGX_OK)
{
return rc;
}
reuse_input_buffers = encryption_params.type != HLS_ENC_NONE;
if (conf->hls.encryption_method == HLS_ENC_SAMPLE_AES)
{
rc = mp4_cbcs_encrypt_get_writers(
&submodule_context->request_context,
&submodule_context->media_set,
segment_writer,
encryption_params.key,
encryption_params.iv,
&segment_writers);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: mp4_cbcs_encrypt_get_writers failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
per_stream_writer = TRUE;
}
else
#endif // NGX_HAVE_OPENSSL_EVP
{
segment_writers = segment_writer;
per_stream_writer = FALSE;
}
if (submodule_context->media_set.total_track_count > 1)
{
#if (NGX_HAVE_OPENSSL_EVP)
if (encryption_params.type == HLS_ENC_SAMPLE_AES_CENC)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: multiple streams not supported for sample aes cenc");
return ngx_http_vod_status_to_ngx_error(submodule_context->r, VOD_BAD_REQUEST);
}
#endif // NGX_HAVE_OPENSSL_EVP
// muxed segment
rc = mp4_muxer_init_fragment(
&submodule_context->request_context,
submodule_context->request_params.segment_index,
&submodule_context->media_set,
segment_writers,
per_stream_writer,
reuse_input_buffers,
size_only,
output_buffer,
response_size,
&muxer_state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: mp4_muxer_init_fragment failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
*frame_processor = (ngx_http_vod_frame_processor_t)mp4_muxer_process_frames;
*frame_processor_state = muxer_state;
}
else
{
#if (NGX_HAVE_OPENSSL_EVP)
if (encryption_params.type == HLS_ENC_SAMPLE_AES_CENC)
{
drm_writer = *segment_writer; // must not change segment_writer, otherwise the header will be encrypted
// encyrpted fragment
rc = edash_packager_get_fragment_writer(
&drm_writer,
&submodule_context->request_context,
&submodule_context->media_set,
submodule_context->request_params.segment_index,
conf->min_single_nalu_per_frame_segment > 0 &&
submodule_context->media_set.initial_segment_clip_relative_index >= conf->min_single_nalu_per_frame_segment - 1,
submodule_context->media_set.sequences[0].encryption_key, // iv
size_only,
output_buffer,
response_size);
switch (rc)
{
case VOD_DONE: // passthrough
break;
case VOD_OK:
segment_writers = &drm_writer;
break;
default:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: edash_packager_get_fragment_writer failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
}
else
#endif // NGX_HAVE_OPENSSL_EVP
{
// for single stream use dash segments
ngx_memzero(&header_extensions, sizeof(header_extensions));
rc = dash_packager_build_fragment_header(
&submodule_context->request_context,
&submodule_context->media_set,
submodule_context->request_params.segment_index,
0, // sample description index
&header_extensions,
size_only,
output_buffer,
response_size);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: dash_packager_build_fragment_header failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
}
// initialize the frame processor
if (!size_only || *response_size == 0)
{
rc = mp4_fragment_frame_writer_init(
&submodule_context->request_context,
submodule_context->media_set.sequences,
segment_writers[0].write_tail,
segment_writers[0].context,
reuse_input_buffers,
&state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_init_fmp4_frame_processor: mp4_fragment_frame_writer_init failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
*frame_processor = (ngx_http_vod_frame_processor_t)mp4_fragment_frame_writer_process;
*frame_processor_state = state;
}
}
#if (NGX_HAVE_OPENSSL_EVP)
if (encryption_params.type == HLS_ENC_AES_128)
{
*response_size = aes_round_up_to_block(*response_size);
}
#endif // NGX_HAVE_OPENSSL_EVP
// set the 'Content-type' header
mp4_fragment_get_content_type(
submodule_context->media_set.track_count[MEDIA_TYPE_VIDEO],
content_type);
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hls_handle_vtt_segment(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
vod_status_t rc;
rc = webvtt_builder_build(
&submodule_context->request_context,
&submodule_context->media_set,
FALSE,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hls_handle_vtt_segment: webvtt_builder_build failed %i", rc);
return ngx_http_vod_status_to_ngx_error(submodule_context->r, rc);
}
content_type->len = sizeof(vtt_content_type) - 1;