forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_vod_module.c
2295 lines (1929 loc) · 65 KB
/
ngx_http_vod_module.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_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include <ngx_event.h>
#include <ngx_md5.h>
#include "ngx_http_vod_module.h"
#include "ngx_http_vod_submodule.h"
#include "ngx_http_vod_request_parse.h"
#include "ngx_child_http_request.h"
#include "ngx_http_vod_utils.h"
#include "ngx_perf_counters.h"
#include "ngx_http_vod_conf.h"
#include "ngx_file_reader.h"
#include "ngx_buffer_cache.h"
#include "vod/aes_encrypt.h"
#include "vod/mp4_parser.h"
#include "vod/read_cache.h"
// constants
#define MAX_MOOV_START_READS (3) // maximum number of attempts to find the moov atom start for non-fast-start files
// enums
enum {
STATE_INITIAL,
STATE_DRM_INFO_READ,
STATE_INITIAL_READ,
STATE_MOOV_ATOM_READ,
STATE_MOOV_ATOM_PARSED,
STATE_FRAME_DATA_READ,
};
// typedefs
typedef ngx_int_t(*ngx_http_vod_open_file_t)(ngx_http_request_t* r, ngx_str_t* path);
typedef ngx_int_t(*ngx_http_vod_async_read_func_t)(void* context, u_char *buf, size_t size, off_t offset);
typedef ngx_int_t(*ngx_http_vod_dump_request_t)(void* context);
typedef struct {
ngx_http_request_t* r;
ngx_chain_t* chain_head;
ngx_chain_t* chain_end;
size_t total_size;
} ngx_http_vod_write_segment_context_t;
typedef struct {
// base params
ngx_http_vod_submodule_context_t submodule_context;
off_t alignment;
int state;
u_char request_key[BUFFER_CACHE_KEY_SIZE];
ngx_perf_counters_t* perf_counters;
ngx_perf_counter_context(perf_counter_context);
ngx_perf_counter_context(total_perf_counter_context);
// moov read state
u_char* read_buffer;
size_t buffer_size;
off_t read_offset;
off_t atom_start_offset;
int moov_start_reads;
off_t moov_offset;
size_t moov_size;
// reading abstraction (over file / http)
ngx_http_vod_open_file_t open_file;
ngx_http_vod_async_read_func_t async_reader;
ngx_http_vod_dump_request_t dump_request;
void* async_reader_context;
ngx_flag_t file_opened;
// read state - file
ngx_file_reader_state_t file_reader;
// read state - http
ngx_child_request_buffers_t child_request_buffers;
ngx_str_t* file_key_prefix;
ngx_str_t cur_remote_suburi;
ngx_str_t upstream_extra_args;
// segment requests only
read_cache_state_t read_cache_state;
ngx_http_vod_frame_processor_t frame_processor;
void* frame_processor_state;
ngx_chain_t out;
ngx_http_vod_write_segment_context_t write_segment_buffer_context;
aes_encrypt_context_t* encrypted_write_context;
} ngx_http_vod_ctx_t;
// globals
ngx_module_t ngx_http_vod_module = {
NGX_MODULE_V1,
&ngx_http_vod_module_ctx, /* module context */
ngx_http_vod_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t options_content_type = ngx_string("text/plain");
// forward declarations
static void ngx_http_vod_finalize_request(ngx_http_vod_ctx_t *ctx, ngx_int_t rc);
static ngx_int_t ngx_http_vod_run_state_machine(ngx_http_vod_ctx_t *ctx);
////// Perf counter wrappers
static ngx_flag_t
ngx_buffer_cache_fetch_perf(
ngx_perf_counters_t* perf_counters,
ngx_shm_zone_t *shm_zone,
u_char* key,
u_char** buffer,
size_t* buffer_size)
{
ngx_perf_counter_context(pcctx);
ngx_flag_t result;
ngx_perf_counter_start(pcctx);
result = ngx_buffer_cache_fetch(shm_zone, key, buffer, buffer_size);
ngx_perf_counter_end(perf_counters, pcctx, PC_FETCH_CACHE);
return result;
}
static ngx_flag_t
ngx_buffer_cache_fetch_copy_perf(
ngx_http_request_t* r,
ngx_perf_counters_t* perf_counters,
ngx_shm_zone_t *shm_zone,
u_char* key,
u_char** buffer,
size_t* buffer_size)
{
ngx_perf_counter_context(pcctx);
ngx_flag_t result;
u_char* original_buffer;
u_char* buffer_copy;
size_t original_size;
ngx_perf_counter_start(pcctx);
result = ngx_buffer_cache_fetch(shm_zone, key, &original_buffer, &original_size);
ngx_perf_counter_end(perf_counters, pcctx, PC_FETCH_CACHE);
if (!result)
{
return 0;
}
buffer_copy = ngx_palloc(r->pool, original_size + 1);
if (buffer_copy == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_buffer_cache_fetch_copy_perf: ngx_palloc failed");
return 0;
}
ngx_memcpy(buffer_copy, original_buffer, original_size);
buffer_copy[original_size] = '\0';
*buffer = buffer_copy;
*buffer_size = original_size;
return 1;
}
static ngx_flag_t
ngx_buffer_cache_store_perf(
ngx_perf_counters_t* perf_counters,
ngx_shm_zone_t *shm_zone,
u_char* key,
u_char* source_buffer,
size_t buffer_size)
{
ngx_perf_counter_context(pcctx);
ngx_flag_t result;
ngx_perf_counter_start(pcctx);
result = ngx_buffer_cache_store(shm_zone, key, source_buffer, buffer_size);
ngx_perf_counter_end(perf_counters, pcctx, PC_STORE_CACHE);
return result;
}
static ngx_flag_t
ngx_buffer_cache_store_gather_perf(
ngx_perf_counters_t* perf_counters,
ngx_shm_zone_t *shm_zone,
u_char* key,
ngx_str_t* buffers,
size_t buffer_count)
{
ngx_perf_counter_context(pcctx);
ngx_flag_t result;
ngx_perf_counter_start(pcctx);
result = ngx_buffer_cache_store_gather(shm_zone, key, buffers, buffer_count);
ngx_perf_counter_end(perf_counters, pcctx, PC_STORE_CACHE);
return result;
}
////// Encryption support
static vod_status_t
ngx_http_vod_init_aes_encryption(ngx_http_vod_ctx_t *ctx, write_callback_t write_callback, void* callback_context)
{
ngx_pool_cleanup_t *cln;
vod_status_t rc;
ctx->encrypted_write_context = ngx_palloc(ctx->submodule_context.r->pool, sizeof(*ctx->encrypted_write_context));
if (ctx->encrypted_write_context == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_init_aes_encryption: ngx_palloc failed");
return VOD_ALLOC_FAILED;
}
cln = ngx_pool_cleanup_add(ctx->submodule_context.r->pool, 0);
if (cln == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_init_aes_encryption: ngx_pool_cleanup_add failed");
return VOD_ALLOC_FAILED;
}
cln->handler = (ngx_pool_cleanup_pt)aes_encrypt_cleanup;
cln->data = ctx->encrypted_write_context;
rc = aes_encrypt_init(
ctx->encrypted_write_context,
&ctx->submodule_context.request_context,
write_callback,
callback_context,
ctx->submodule_context.cur_suburi->file_key,
ctx->submodule_context.request_params.segment_index);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_init_aes_encryption: aes_encrypt_init failed %i", rc);
return rc;
}
return VOD_OK;
}
////// Common mp4 processing
static ngx_int_t
ngx_http_vod_read_moov_atom(ngx_http_vod_ctx_t *ctx)
{
u_char* new_read_buffer = NULL;
size_t new_buffer_size;
off_t absolute_moov_offset;
vod_status_t rc;
for (;;)
{
if (ctx->buffer_size <= (size_t)ctx->atom_start_offset)
{
ngx_log_error(NGX_LOG_ERR, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: read buffer size %uz is smaller than the atom start offset %O", ctx->buffer_size, ctx->atom_start_offset);
return ngx_http_vod_status_to_ngx_error(VOD_BAD_DATA);
}
// get moov atom offset and size
rc = mp4_parser_get_moov_atom_info(&ctx->submodule_context.request_context, ctx->read_buffer + ctx->atom_start_offset, ctx->buffer_size - ctx->atom_start_offset, &ctx->moov_offset, &ctx->moov_size);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: mp4_parser_get_moov_atom_info failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
// update moov offset to be relative to the read buffer start
ctx->moov_offset += ctx->atom_start_offset;
// check whether we found the moov atom start
if (ctx->moov_size > 0)
{
break;
}
if (ctx->moov_offset < (off_t)ctx->buffer_size)
{
ngx_log_error(NGX_LOG_ERR, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: moov start offset %O is smaller than the buffer size %uz", ctx->moov_offset, ctx->buffer_size);
return ngx_http_vod_status_to_ngx_error(VOD_UNEXPECTED);
}
if (ctx->moov_start_reads <= 0)
{
ngx_log_error(NGX_LOG_ERR, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: exhausted all moov read attempts");
return ngx_http_vod_status_to_ngx_error(VOD_BAD_DATA);
}
ctx->moov_start_reads--;
// perform another read attempt
absolute_moov_offset = ctx->read_offset + ctx->moov_offset;
ctx->read_offset = absolute_moov_offset & (~(ctx->alignment - 1));
ctx->atom_start_offset = absolute_moov_offset - ctx->read_offset;
ngx_perf_counter_start(ctx->perf_counter_context);
rc = ctx->async_reader(
ctx->async_reader_context,
ctx->read_buffer,
ctx->submodule_context.conf->initial_read_size,
ctx->read_offset);
if (rc < 0) // inc. NGX_AGAIN
{
if (rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: async_reader failed %i", rc);
}
return rc;
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_READ_FILE);
ctx->buffer_size = rc;
}
// check whether we already have the whole atom
if (ctx->moov_offset + ctx->moov_size <= ctx->buffer_size)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: already read the full moov atom");
return NGX_OK;
}
// validate the moov size
if (ctx->moov_size > ctx->submodule_context.conf->max_moov_size)
{
ngx_log_error(NGX_LOG_ERR, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: moov size %uD exceeds the max %uz", ctx->moov_size, ctx->submodule_context.conf->max_moov_size);
return ngx_http_vod_status_to_ngx_error(VOD_BAD_DATA);
}
// calculate the new buffer size (round the moov end up to alignment)
new_buffer_size = ((ctx->moov_offset + ctx->moov_size) + ctx->alignment - 1) & (~(ctx->alignment - 1));
new_read_buffer = ngx_pmemalign(ctx->submodule_context.r->pool, new_buffer_size + 1, ctx->alignment);
if (new_read_buffer == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: ngx_pmemalign failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// copy the previously read data
ngx_memcpy(new_read_buffer, ctx->read_buffer, ctx->buffer_size);
ngx_pfree(ctx->submodule_context.r->pool, ctx->read_buffer);
ctx->read_buffer = new_read_buffer;
// read the rest of the atom
ctx->submodule_context.request_context.log->action = "reading moov atom";
ctx->state = STATE_MOOV_ATOM_READ;
ngx_perf_counter_start(ctx->perf_counter_context);
rc = ctx->async_reader(
ctx->async_reader_context,
ctx->read_buffer + ctx->buffer_size,
new_buffer_size - ctx->buffer_size,
ctx->read_offset + ctx->buffer_size);
if (rc < 0)
{
if (rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_read_moov_atom: async_reader failed %i", rc);
}
return rc;
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_READ_FILE);
ctx->buffer_size += rc;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_parse_moov_atom(ngx_http_vod_ctx_t *ctx, u_char* moov_buffer, size_t moov_size)
{
mpeg_base_metadata_t mpeg_base_metadata;
const ngx_http_vod_request_t* request = ctx->submodule_context.request_params.request;
ngx_http_vod_suburi_params_t* suburi_params = ctx->submodule_context.cur_suburi;
request_context_t* request_context = &ctx->submodule_context.request_context;
segmenter_conf_t* segmenter = &ctx->submodule_context.conf->segmenter;
vod_status_t rc;
file_info_t file_info;
uint32_t segment_count;
ngx_perf_counter_start(ctx->perf_counter_context);
// init the request context
request_context->parse_type = request->parse_type;
if (request->request_class == REQUEST_CLASS_MANIFEST)
{
request_context->parse_type |= segmenter->parse_type;
}
request_context->stream_comparator = request->stream_comparator;
request_context->stream_comparator_context =
(u_char*)ctx->submodule_context.conf + request->stream_comparator_conf_offset;
file_info.file_index = suburi_params->file_index;
file_info.uri = suburi_params->uri;
file_info.drm_info = suburi_params->drm_info;
// parse the basic metadata
rc = mp4_parser_parse_basic_metadata(
&ctx->submodule_context.request_context,
suburi_params->required_tracks,
suburi_params->clip_from,
suburi_params->clip_to,
moov_buffer,
moov_size,
&file_info,
&mpeg_base_metadata);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, request_context->log, 0,
"ngx_http_vod_parse_moov_atom: mp4_parser_parse_basic_metadata failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
request_context->timescale = 1000;
if (request->request_class != REQUEST_CLASS_SEGMENT)
{
request_context->max_frame_count = 1024 * 1024;
request_context->simulation_only = TRUE;
request_context->start = suburi_params->clip_from;
if (suburi_params->clip_to == UINT_MAX)
{
request_context->end = ULLONG_MAX;
}
else
{
request_context->end = suburi_params->clip_to;
}
}
else
{
request_context->max_frame_count = 64 * 1024;
request_context->simulation_only = FALSE;
segment_count = segmenter->get_segment_count(segmenter, mpeg_base_metadata.duration_millis);
if (segment_count == INVALID_SEGMENT_COUNT)
{
ngx_log_error(NGX_LOG_ERR, request_context->log, 0,
"ngx_http_vod_parse_moov_atom: segment count is invalid");
return ngx_http_vod_status_to_ngx_error(VOD_BAD_DATA);
}
if (ctx->submodule_context.request_params.segment_index >= segment_count)
{
ngx_log_error(NGX_LOG_ERR, request_context->log, 0,
"ngx_http_vod_parse_moov_atom: requested segment index %uD exceeds the segment count %uD", ctx->submodule_context.request_params.segment_index, segment_count);
return NGX_HTTP_NOT_FOUND;
}
// get the start / end offsets
segmenter_get_start_end_offsets(
segmenter,
ctx->submodule_context.request_params.segment_index,
&request_context->start,
&request_context->end);
request_context->start += suburi_params->clip_from;
request_context->end += suburi_params->clip_from;
if (ctx->submodule_context.request_params.segment_index + 1 >= segment_count)
{
// last segment
if (suburi_params->clip_to == UINT_MAX)
{
request_context->end = ULLONG_MAX;
}
else
{
request_context->end = suburi_params->clip_to;
}
}
if (request_context->end <= request_context->start)
{
vod_log_error(VOD_LOG_ERR, request_context->log, 0,
"ngx_http_vod_parse_moov_atom: segment index %uD too big for clip from %uD and clip to %uD",
ctx->submodule_context.request_params.segment_index, suburi_params->clip_from, suburi_params->clip_to);
return NGX_HTTP_BAD_REQUEST;
}
}
// parse the frames
rc = mp4_parser_parse_frames(
&ctx->submodule_context.request_context,
&mpeg_base_metadata,
suburi_params->clip_from,
suburi_params->clip_to,
segmenter->align_to_key_frames,
&ctx->submodule_context.mpeg_metadata);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, request_context->log, 0,
"ngx_http_vod_parse_moov_atom: mp4_parser_parse_frames failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_MP4_PARSE);
return NGX_OK;
}
////// Segment request handling
static vod_status_t
ngx_http_vod_write_segment_header_buffer(void* ctx, u_char* buffer, uint32_t size, bool_t* reuse_buffer)
{
ngx_http_vod_write_segment_context_t* context = (ngx_http_vod_write_segment_context_t*)ctx;
ngx_chain_t *chain;
ngx_buf_t *b;
if (context->r->header_sent)
{
ngx_log_error(NGX_LOG_ERR, context->r->connection->log, 0,
"ngx_http_vod_write_segment_header_buffer: called after the headers were already sent");
return VOD_UNEXPECTED;
}
b = ngx_calloc_buf(context->r->pool);
if (b == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, context->r->connection->log, 0,
"ngx_http_vod_write_segment_header_buffer: ngx_pcalloc failed (1)");
return VOD_ALLOC_FAILED;
}
b->pos = buffer;
b->last = buffer + size;
b->memory = 1;
chain = ngx_alloc_chain_link(context->r->pool);
if (chain == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, context->r->connection->log, 0,
"ngx_http_vod_write_segment_header_buffer: ngx_pcalloc failed (2)");
return VOD_ALLOC_FAILED;
}
chain->buf = context->chain_head->buf;
chain->next = context->chain_head->next;
context->chain_head->buf = b;
context->chain_head->next = chain;
context->total_size += size;
return VOD_OK;
}
static vod_status_t
ngx_http_vod_write_segment_buffer(void* ctx, u_char* buffer, uint32_t size, bool_t* reuse_buffer)
{
ngx_http_vod_write_segment_context_t* context = (ngx_http_vod_write_segment_context_t*)ctx;
ngx_buf_t *b;
ngx_chain_t *chain;
ngx_chain_t out;
ngx_int_t rc;
// caller should not reuse the buffer since we keep the original buffer
*reuse_buffer = FALSE;
// create a wrapping ngx_buf_t
b = ngx_calloc_buf(context->r->pool);
if (b == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, context->r->connection->log, 0,
"ngx_http_vod_write_segment_buffer: ngx_pcalloc failed (1)");
return VOD_ALLOC_FAILED;
}
b->pos = buffer;
b->last = buffer + size;
b->memory = 1;
if (context->r->header_sent)
{
// headers already sent, output the chunk
out.buf = b;
out.next = NULL;
rc = ngx_http_output_filter(context->r, &out);
if (rc != NGX_OK && rc != NGX_AGAIN)
{
// either the connection dropped, or some allocation failed
// in case the connection dropped, the error code doesn't matter anyway
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, context->r->connection->log, 0,
"ngx_http_vod_write_segment_buffer: ngx_http_output_filter failed %i", rc);
return VOD_ALLOC_FAILED;
}
}
else
{
// headers not sent yet, add the buffer to the chain
if (context->chain_end->buf != NULL)
{
chain = ngx_alloc_chain_link(context->r->pool);
if (chain == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, context->r->connection->log, 0,
"ngx_http_vod_write_segment_buffer: ngx_pcalloc failed (2)");
return VOD_ALLOC_FAILED;
}
context->chain_end->next = chain;
context->chain_end = chain;
}
context->chain_end->buf = b;
}
context->total_size += size;
return VOD_OK;
}
static ngx_int_t
ngx_http_vod_init_frame_processing(ngx_http_vod_ctx_t *ctx)
{
ngx_http_vod_loc_conf_t* conf;
ngx_http_request_t* r = ctx->submodule_context.r;
segment_writer_t segment_writer;
ngx_str_t output_buffer = ngx_null_string;
ngx_str_t content_type;
size_t response_size = 0;
bool_t reuse_buffer;
ngx_int_t rc;
conf = ctx->submodule_context.conf;
// open the file in case the moov was loaded from cache
if (!ctx->file_opened)
{
rc = ctx->open_file(r, &ctx->submodule_context.cur_suburi->stripped_uri);
if (rc != NGX_OK)
{
if (rc != NGX_AGAIN) // NGX_AGAIN will be returned in case of fallback
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: open_file failed %i", rc);
}
return rc;
}
ctx->file_opened = 1;
}
// enable directio if enabled in the configuration (ignore errors)
// Note that directio is set on transfer only to allow the kernel to cache the "moov" atom
if (conf->request_handler != ngx_http_vod_remote_request_handler)
{
ngx_file_reader_enable_directio(&ctx->file_reader);
}
// initialize the read cache
read_cache_init(
&ctx->read_cache_state,
&ctx->submodule_context.request_context,
conf->cache_buffer_size,
ctx->alignment);
// initialize the response writer
ctx->out.buf = NULL;
ctx->out.next = NULL;
ctx->write_segment_buffer_context.r = r;
ctx->write_segment_buffer_context.chain_head = &ctx->out;
ctx->write_segment_buffer_context.chain_end = &ctx->out;
ctx->write_segment_buffer_context.total_size = 0;
if (conf->secret_key.len != 0)
{
rc = ngx_http_vod_init_aes_encryption(ctx, ngx_http_vod_write_segment_buffer, &ctx->write_segment_buffer_context);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: ngx_http_vod_init_aes_encryption failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
segment_writer.write_tail = (write_callback_t)aes_encrypt_write;
segment_writer.write_head = NULL; // cannot be supported with CBC encryption
segment_writer.context = ctx->encrypted_write_context;
}
else
{
segment_writer.write_tail = ngx_http_vod_write_segment_buffer;
segment_writer.write_head = ngx_http_vod_write_segment_header_buffer;
segment_writer.context = &ctx->write_segment_buffer_context;
}
// initialize the protocol specific frame processor
ngx_perf_counter_start(ctx->perf_counter_context);
rc = ctx->submodule_context.request_params.request->init_frame_processor(
&ctx->submodule_context,
&ctx->read_cache_state,
&segment_writer,
&ctx->frame_processor,
&ctx->frame_processor_state,
&output_buffer,
&response_size,
&content_type);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: init_frame_processor failed %i", rc);
return rc;
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_INIT_FRAME_PROCESS);
r->headers_out.content_type_len = content_type.len;
r->headers_out.content_type.len = content_type.len;
r->headers_out.content_type.data = content_type.data;
// if the frame processor can't determine the size in advance we have to build the whole response before we can start sending it
if (response_size == 0)
{
return NGX_OK;
}
// calculate the response size
if (conf->secret_key.len != 0)
{
response_size = AES_ROUND_TO_BLOCK(response_size);
}
// set the status line
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response_size;
// set the etag
rc = ngx_http_set_etag(r);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: ngx_http_set_etag failed %i", rc);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// send the response headers
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: ngx_http_send_header failed %i", rc);
return rc;
}
if (r->header_only || r->method == NGX_HTTP_HEAD)
{
return NGX_DONE;
}
// write the initial buffer if provided
if (output_buffer.len != 0)
{
rc = segment_writer.write_tail(segment_writer.context, output_buffer.data, output_buffer.len, &reuse_buffer);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_init_frame_processing: write_callback failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_finalize_segment_response(ngx_http_vod_ctx_t *ctx)
{
ngx_http_request_t *r = ctx->submodule_context.r;
ngx_int_t rc;
// flush the encryption buffer
if (ctx->encrypted_write_context != NULL)
{
rc = aes_encrypt_flush(ctx->encrypted_write_context);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: aes_encrypt_flush failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
}
// if we already sent the headers and all the buffers, just signal completion and return
if (r->header_sent)
{
if ((off_t)ctx->write_segment_buffer_context.total_size != r->headers_out.content_length_n)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: actual content length %uD is different than reported length %O",
ctx->write_segment_buffer_context.total_size, r->headers_out.content_length_n);
}
rc = ngx_http_send_special(r, NGX_HTTP_LAST);
if (rc != NGX_OK && rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: ngx_http_send_special failed %i", rc);
return rc;
}
return NGX_OK;
}
// mark the current buffer as last
ctx->write_segment_buffer_context.chain_end->next = NULL;
ctx->write_segment_buffer_context.chain_end->buf->last_buf = 1;
// set the status line
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = ctx->write_segment_buffer_context.total_size;
// set the etag
rc = ngx_http_set_etag(r);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: ngx_http_set_etag failed %i", rc);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// send the response headers
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: ngx_http_send_header failed %i", rc);
return rc;
}
if (r->header_only || r->method == NGX_HTTP_HEAD)
{
return rc;
}
// send the response buffer chain
rc = ngx_http_output_filter(r, &ctx->out);
if (rc != NGX_OK && rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_finalize_segment_response: ngx_http_output_filter failed %i", rc);
return rc;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_process_mp4_frames(ngx_http_vod_ctx_t *ctx)
{
uint64_t required_offset;
uint64_t read_offset;
u_char* read_buffer;
uint32_t read_size;
vod_status_t rc;
for (;;)
{
ngx_perf_counter_start(ctx->perf_counter_context);
rc = ctx->frame_processor(ctx->frame_processor_state, &required_offset);
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_PROCESS_FRAMES);
switch (rc)
{
case VOD_OK:
// we're done
return ngx_http_vod_finalize_segment_response(ctx);
case VOD_AGAIN:
// handled outside the switch
break;
default:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_process_mp4_frames: frame_processor failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
// get a buffer to read into
rc = read_cache_get_read_buffer(&ctx->read_cache_state, required_offset, &read_offset, &read_buffer, &read_size);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_process_mp4_frames: read_cache_get_read_buffer failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
// perform the read
ngx_perf_counter_start(ctx->perf_counter_context);
rc = ctx->async_reader(ctx->async_reader_context, read_buffer, read_size, read_offset);
if (rc < 0)
{
if (rc != NGX_AGAIN)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_http_vod_process_mp4_frames: async_reader failed %i", rc);
}
return rc;
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_READ_FILE);
// read completed synchronously, update the read cache
read_cache_read_completed(&ctx->read_cache_state, rc);
}
}
////// DRM
static void
ngx_http_vod_drm_info_request_finished(void* context, ngx_int_t rc, ngx_buf_t* response)
{
ngx_http_vod_loc_conf_t *conf;
ngx_http_vod_ctx_t *ctx;
ngx_http_request_t *r = context;
ngx_str_t drm_info;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
conf = ctx->submodule_context.conf;
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_drm_info_request_finished: upstream request failed %i", rc);
goto finalize_request;
}
ngx_perf_counter_end(ctx->perf_counters, ctx->perf_counter_context, PC_GET_DRM_INFO);
drm_info.data = response->pos;
drm_info.len = response->last - response->pos;
*response->last = '\0'; // this is ok since ngx_child_http_request always allocate the content-length + 1
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_drm_info_request_finished: result %V", &drm_info);
// parse the drm info
rc = conf->submodule.parse_drm_info(&ctx->submodule_context, &drm_info, &ctx->submodule_context.cur_suburi->drm_info);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_drm_info_request_finished: invalid drm info response %V", &drm_info);
rc = NGX_HTTP_SERVICE_UNAVAILABLE;
goto finalize_request;
}
// save to cache
if (conf->drm_info_cache_zone != NULL)
{
if (ngx_buffer_cache_store_perf(
ctx->perf_counters,
conf->drm_info_cache_zone,
ctx->submodule_context.cur_suburi->file_key,
drm_info.data,
drm_info.len))
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_drm_info_request_finished: stored in drm info cache");
}
else
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_drm_info_request_finished: failed to store drm info in cache");
}
}
ctx->state = STATE_DRM_INFO_READ;
rc = ngx_http_vod_run_state_machine(ctx);
if (rc != NGX_AGAIN)