forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_http_vod_module.c
5811 lines (4917 loc) · 151 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/mp4/mp4_format.h"
#include "vod/mkv/mkv_format.h"
#include "vod/subtitle/webvtt_format.h"
#include "vod/subtitle/cap_format.h"
#include "vod/input/read_cache.h"
#include "vod/filters/audio_filter.h"
#include "vod/filters/dynamic_clip.h"
#include "vod/filters/concat_clip.h"
#include "vod/filters/rate_filter.h"
#include "vod/filters/filter.h"
#include "vod/media_set_parser.h"
#include "vod/manifest_utils.h"
#include "vod/input/silence_generator.h"
#if (NGX_HAVE_LIB_AV_CODEC)
#include "ngx_http_vod_thumb.h"
#include "ngx_http_vod_volume_map.h"
#include "vod/filters/audio_decoder.h"
#include "vod/filters/audio_encoder.h"
#include "vod/thumb/thumb_grabber.h"
#endif // NGX_HAVE_LIB_AV_CODEC
#if (NGX_HAVE_LIBXML2)
#include "vod/subtitle/dfxp_format.h"
#endif // NGX_HAVE_LIBXML2
// macros
#define DEFINE_VAR(name) \
{ ngx_string("vod_" #name), ngx_http_vod_set_##name##_var, 0 }
// constants
#define OPEN_FILE_FALLBACK_ENABLED (0x80000000)
#define MAX_STALE_RETRIES (2)
enum {
// mapping state machine
STATE_MAP_INITIAL,
STATE_MAP_OPEN,
STATE_MAP_READ,
// main state machine
STATE_READ_DRM_INFO,
STATE_READ_METADATA_INITIAL,
STATE_READ_METADATA_OPEN_FILE,
STATE_READ_METADATA_READ,
STATE_READ_FRAMES_OPEN_FILE,
STATE_READ_FRAMES_READ,
STATE_OPEN_FILE,
STATE_FILTER_FRAMES,
STATE_PROCESS_FRAMES,
STATE_DUMP_OPEN_FILE,
STATE_DUMP_FILE_PART,
};
enum {
READER_FILE,
READER_HTTP,
READER_COUNT
};
// typedefs
struct ngx_http_vod_ctx_s;
typedef struct ngx_http_vod_ctx_s ngx_http_vod_ctx_t;
typedef ngx_int_t(*ngx_http_vod_state_machine_t)(ngx_http_vod_ctx_t* ctx);
typedef ngx_int_t(*ngx_http_vod_open_file_t)(ngx_http_request_t* r, ngx_str_t* path, uint32_t flags, void** context);
typedef ngx_int_t(*ngx_http_vod_async_read_func_t)(void* context, ngx_buf_t *buf, size_t size, off_t offset);
typedef ngx_int_t(*ngx_http_vod_dump_part_t)(void* context, off_t start, off_t end);
typedef size_t(*ngx_http_vod_get_size_t)(void* context);
typedef void(*ngx_http_vod_get_path_t)(void* context, ngx_str_t* path);
typedef ngx_int_t(*ngx_http_vod_enable_directio_t)(void* context);
typedef ngx_int_t(*ngx_http_vod_dump_request_t)(void* context);
typedef ngx_int_t(*ngx_http_vod_mapping_apply_t)(ngx_http_vod_ctx_t *ctx, ngx_str_t* mapping, int* cache_index);
typedef ngx_int_t(*ngx_http_vod_mapping_get_uri_t)(ngx_http_vod_ctx_t *ctx, ngx_str_t* uri);
typedef struct {
uint32_t type;
uint32_t part_count;
} multipart_cache_header_t;
typedef struct {
size_t content_type_len;
uint32_t media_set_type;
} response_cache_header_t;
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 {
ngx_http_request_t* r;
ngx_str_t cur_remote_suburi;
ngx_str_t upstream_location;
} ngx_http_vod_http_reader_state_t;
typedef struct {
u_char cache_key[MEDIA_CLIP_KEY_SIZE];
ngx_str_t* cache_key_prefix;
ngx_buffer_cache_t** caches;
uint32_t cache_count;
uint32_t stale_retries;
// reading abstraction (over file / http)
ngx_http_vod_reader_t* reader;
void* reader_context;
size_t max_response_size;
ngx_http_vod_mapping_get_uri_t get_uri;
ngx_http_vod_mapping_apply_t apply;
} ngx_http_vod_mapping_context_t;
struct ngx_http_vod_reader_s {
ngx_http_vod_open_file_t open;
ngx_http_vod_dump_part_t dump_part;
ngx_http_vod_dump_request_t dump_request;
ngx_http_vod_get_size_t get_size;
ngx_http_vod_get_path_t get_path;
ngx_http_vod_enable_directio_t enable_directio;
ngx_http_vod_async_read_func_t read;
};
struct ngx_http_vod_ctx_s {
// base params
ngx_http_vod_submodule_context_t submodule_context;
const struct ngx_http_vod_request_s* request;
int state;
u_char request_key[BUFFER_CACHE_KEY_SIZE];
u_char child_request_key[BUFFER_CACHE_KEY_SIZE];
ngx_http_vod_state_machine_t state_machine;
// iterators
media_sequence_t* cur_sequence;
media_clip_source_t* cur_source;
media_clip_t* cur_clip;
// performance counters
int perf_counter_async_read;
ngx_perf_counters_t* perf_counters;
ngx_perf_counter_context(perf_counter_context);
ngx_perf_counter_context(total_perf_counter_context);
// mapping
ngx_http_vod_mapping_context_t mapping;
// read metadata state
ngx_buf_t read_buffer;
uint32_t read_flags;
media_format_t* format;
ngx_buf_t prefix_buffer;
off_t requested_offset;
off_t read_offset;
size_t read_size;
void* metadata_reader_context;
ngx_str_t* metadata_parts;
size_t metadata_part_count;
// read frames state
media_base_metadata_t* base_metadata;
media_format_read_request_t frames_read_req;
// clipper
media_clipper_parse_result_t* clipper_parse_result;
ngx_http_vod_reader_t* default_reader;
// read state - file
#if (NGX_THREADS)
void* async_open_context;
#endif // NGX_THREADS
// read state - http
ngx_str_t* file_key_prefix;
ngx_str_t upstream_extra_args;
// segment requests only
size_t content_length;
size_t size_limit;
read_cache_state_t read_cache_state;
ngx_http_vod_frame_processor_t frame_processor;
void* frame_processor_state;
ngx_chain_t out;
segment_writer_t segment_writer;
ngx_http_vod_write_segment_context_t write_segment_buffer_context;
media_notification_t* notification;
uint32_t frames_bytes_read;
};
// typedefs
typedef struct {
ngx_str_t name;
ngx_http_get_variable_pt handler;
uintptr_t data;
} ngx_http_vod_variable_t;
// forward declarations
static ngx_int_t ngx_http_vod_run_state_machine(ngx_http_vod_ctx_t *ctx);
static ngx_int_t ngx_http_vod_send_notification(ngx_http_vod_ctx_t *ctx);
static ngx_int_t ngx_http_vod_init_process(ngx_cycle_t *cycle);
static void ngx_http_vod_exit_process();
static ngx_int_t ngx_http_vod_init_file_reader_with_fallback(ngx_http_request_t *r, ngx_str_t* path, uint32_t flags, void** context);
static ngx_int_t ngx_http_vod_init_file_reader(ngx_http_request_t *r, ngx_str_t* path, uint32_t flags, void** context);
static ngx_int_t ngx_http_vod_dump_file(void* context);
static ngx_int_t ngx_http_vod_http_reader_open_file(ngx_http_request_t* r, ngx_str_t* path, uint32_t flags, void** context);
static ngx_int_t ngx_http_vod_dump_http_part(void* context, off_t start, off_t end);
static ngx_int_t ngx_http_vod_dump_http_request(void* context);
static void ngx_http_vod_http_reader_get_path(void* context, ngx_str_t* path);
static ngx_int_t ngx_http_vod_async_http_read(ngx_http_vod_http_reader_state_t *state, ngx_buf_t *buf, size_t size, off_t offset);
// 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 */
ngx_http_vod_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_vod_exit_process, /* exit process */
ngx_http_vod_exit_process, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t options_content_type = ngx_string("text/plain");
static ngx_str_t empty_file_string = ngx_string("empty");
static ngx_str_t empty_string = ngx_null_string;
static media_format_t* media_formats[] = {
&mp4_format,
// XXXXX add &mkv_format,
&webvtt_format,
#if (NGX_HAVE_LIBXML2)
&dfxp_format,
#endif // NGX_HAVE_LIBXML2
&cap_format,
NULL
};
static ngx_http_vod_reader_t reader_file_with_fallback = {
ngx_http_vod_init_file_reader_with_fallback,
ngx_file_reader_dump_file_part,
ngx_http_vod_dump_file,
ngx_file_reader_get_size,
ngx_file_reader_get_path,
(ngx_http_vod_enable_directio_t)ngx_file_reader_enable_directio,
(ngx_http_vod_async_read_func_t)ngx_async_file_read,
};
static ngx_http_vod_reader_t reader_file = {
ngx_http_vod_init_file_reader,
ngx_file_reader_dump_file_part,
ngx_http_vod_dump_file,
ngx_file_reader_get_size,
ngx_file_reader_get_path,
(ngx_http_vod_enable_directio_t)ngx_file_reader_enable_directio,
(ngx_http_vod_async_read_func_t)ngx_async_file_read,
};
static ngx_http_vod_reader_t reader_http = {
ngx_http_vod_http_reader_open_file,
ngx_http_vod_dump_http_part,
ngx_http_vod_dump_http_request,
NULL,
ngx_http_vod_http_reader_get_path,
NULL,
(ngx_http_vod_async_read_func_t)ngx_http_vod_async_http_read,
};
static const u_char wvm_file_magic[] = { 0x00, 0x00, 0x01, 0xba, 0x44, 0x00, 0x04, 0x00, 0x04, 0x01 };
////// Variables
static ngx_int_t
ngx_http_vod_set_status_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
// this variable is explicitly set when an error is mapped, if we got here, there's no value
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_filepath_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL ||
ctx->cur_sequence < ctx->submodule_context.media_set.sequences ||
ctx->cur_sequence >= ctx->submodule_context.media_set.sequences_end)
{
v->not_found = 1;
return NGX_OK;
}
value = &ctx->cur_sequence->mapped_uri;
if (value->len == 0)
{
v->not_found = 1;
return NGX_OK;
}
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->len = value->len;
v->data = value->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_suburi_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
media_sequence_t* sequence;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
if (ctx->cur_sequence >= ctx->submodule_context.media_set.sequences &&
ctx->cur_sequence < ctx->submodule_context.media_set.sequences_end)
{
sequence = ctx->cur_sequence;
}
else if (ctx->cur_source != NULL)
{
sequence = ctx->cur_source->sequence;
}
else
{
v->not_found = 1;
return NGX_OK;
}
value = &sequence->stripped_uri;
if (value->len == 0)
{
v->not_found = 1;
return NGX_OK;
}
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->len = value->len;
v->data = value->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_set_id_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
value = &ctx->submodule_context.media_set.id;
if (value->len == 0)
{
v->not_found = 1;
return NGX_OK;
}
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->len = value->len;
v->data = value->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_sequence_id_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
media_sequence_t* cur_sequence;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
cur_sequence = ctx->cur_sequence;
if (cur_sequence == NULL && ctx->submodule_context.media_set.sequence_count == 1)
{
cur_sequence = ctx->submodule_context.media_set.sequences;
}
if (cur_sequence < ctx->submodule_context.media_set.sequences ||
cur_sequence >= ctx->submodule_context.media_set.sequences_end)
{
v->not_found = 1;
return NGX_OK;
}
value = &cur_sequence->id;
if (value->len == 0)
{
value = &cur_sequence->stripped_uri;
if (value->len == 0)
{
v->not_found = 1;
return NGX_OK;
}
}
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->len = value->len;
v->data = value->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_clip_id_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
media_clip_source_t* clip_source;
ngx_http_vod_ctx_t *ctx;
media_clip_t* cur_clip;
media_set_t* media_set;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
goto not_found;
}
cur_clip = ctx->cur_clip;
if (cur_clip == NULL)
{
media_set = &ctx->submodule_context.media_set;
if (media_set->sequence_count == 1 && media_set->clip_count == 1)
{
cur_clip = media_set->sequences->clips[0];
}
else
{
goto not_found;
}
}
switch (cur_clip->type)
{
case MEDIA_CLIP_SOURCE:
clip_source = (media_clip_source_t*)cur_clip;
if (clip_source->id.len != 0)
{
value = &clip_source->id;
}
else
{
value = &clip_source->mapped_uri;
}
break;
case MEDIA_CLIP_DYNAMIC:
value = &((media_clip_dynamic_t*)cur_clip)->id;
break;
default:
goto not_found;
}
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
v->len = value->len;
v->data = value->data;
return NGX_OK;
not_found:
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_dynamic_mapping_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
vod_status_t rc;
ngx_str_t value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
rc = dynamic_clip_get_mapping_string(
&ctx->submodule_context.request_context,
ctx->submodule_context.media_set.dynamic_clips_head,
&value);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_set_dynamic_mapping_var: dynamic_clip_get_mapping_string failed %i", rc);
return NGX_ERROR;
}
v->data = value.data;
v->len = value.len;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_request_params_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
request_params_t* request_params;
ngx_http_vod_ctx_t *ctx;
vod_status_t rc;
ngx_str_t value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
request_params = &ctx->submodule_context.request_params;
rc = manifest_utils_build_request_params_string(
&ctx->submodule_context.request_context,
request_params->tracks_mask, // the media set may not be ready yet, include all tracks that were passed on the request
request_params->segment_index,
request_params->sequences_mask,
request_params->sequence_tracks_mask,
request_params->sequence_tracks_mask_end,
request_params->tracks_mask,
&value);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_set_request_params_var: manifest_utils_build_request_params_string failed %i", rc);
return NGX_ERROR;
}
if (value.len > 0 && value.data[0] == '-')
{
value.data++;
value.len--;
}
v->data = value.data;
v->len = value.len;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_notification_id_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
ngx_str_t* value;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL || ctx->notification == NULL)
{
v->not_found = 1;
return NGX_OK;
}
value = &ctx->notification->id;
v->data = value->data;
v->len = value->len;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_segment_time_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t* ctx;
media_set_t* media_set;
int64_t value;
u_char* p;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
media_set = &ctx->submodule_context.media_set;
if (media_set->filtered_tracks >= media_set->filtered_tracks_end)
{
v->not_found = 1;
return NGX_OK;
}
p = ngx_pnalloc(r->pool, NGX_INT64_LEN);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_set_segment_time_var: ngx_pnalloc failed");
return NGX_ERROR;
}
value = media_set_get_segment_time_millis(media_set);
v->data = p;
v->len = ngx_sprintf(p, "%L", value) - p;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_segment_duration_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
media_track_t* last_track;
media_track_t* cur_track;
media_set_t* media_set;
uint32_t timescale;
uint64_t value;
u_char* p;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
media_set = &ctx->submodule_context.media_set;
cur_track = media_set->filtered_tracks;
last_track = cur_track + media_set->total_track_count * media_set->clip_count;
if (cur_track >= last_track)
{
v->not_found = 1;
return NGX_OK;
}
p = ngx_pnalloc(r->pool, NGX_INT32_LEN);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_set_segment_duration_var: ngx_pnalloc failed");
return NGX_ERROR;
}
timescale = cur_track->media_info.timescale;
value = 0;
for (; cur_track < last_track; cur_track += media_set->total_track_count)
{
value += cur_track->total_frames_duration;
}
v->data = p;
v->len = ngx_sprintf(p, "%uD", (uint32_t)rescale_time(value, timescale, 1000)) - p;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_set_uint32_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_vod_ctx_t *ctx;
uint32_t int_value;
u_char* p;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx == NULL)
{
v->not_found = 1;
return NGX_OK;
}
p = ngx_pnalloc(r->pool, NGX_INT32_LEN);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_set_uint32_var: ngx_pnalloc failed");
return NGX_ERROR;
}
int_value = *(uint32_t*)(((u_char*)ctx) + data);
v->data = p;
v->len = ngx_sprintf(p, "%uD", int_value) - p;
v->valid = 1;
v->no_cacheable = 1;
v->not_found = 0;
return NGX_OK;
}
static ngx_http_vod_variable_t ngx_http_vod_variables[] = {
DEFINE_VAR(status),
DEFINE_VAR(filepath),
DEFINE_VAR(suburi),
DEFINE_VAR(set_id),
DEFINE_VAR(sequence_id),
DEFINE_VAR(clip_id),
DEFINE_VAR(dynamic_mapping),
DEFINE_VAR(request_params),
DEFINE_VAR(notification_id),
DEFINE_VAR(segment_time),
DEFINE_VAR(segment_duration),
{ ngx_string("vod_frames_bytes_read"), ngx_http_vod_set_uint32_var, offsetof(ngx_http_vod_ctx_t, frames_bytes_read) },
};
ngx_int_t
ngx_http_vod_preconfiguration(ngx_conf_t *cf)
{
ngx_http_vod_variable_t* vars_cur = ngx_http_vod_variables;
ngx_http_vod_variable_t* vars_end = vars_cur + vod_array_entries(ngx_http_vod_variables);
ngx_http_variable_t *var;
ngx_int_t rc;
for (; vars_cur < vars_end; vars_cur++)
{
var = ngx_http_add_variable(cf, &vars_cur->name, NGX_HTTP_VAR_NOCACHEABLE);
if (var == NULL)
{
return NGX_ERROR;
}
var->get_handler = vars_cur->handler;
var->data = vars_cur->data;
}
rc = ngx_http_get_variable_index(cf, &ngx_http_vod_variables[0].name);
if (rc == NGX_ERROR)
{
return NGX_ERROR;
}
ngx_http_vod_set_status_index(rc);
#if (NGX_HAVE_LIBXML2)
dfxp_init_process();
#endif // NGX_HAVE_LIBXML2
#if (VOD_HAVE_ICONV)
webvtt_init_process(cf->log);
#endif // VOD_HAVE_ICONV
return NGX_OK;
}
////// Perf counter wrappers
static ngx_flag_t
ngx_buffer_cache_fetch_perf(
ngx_perf_counters_t* perf_counters,
ngx_buffer_cache_t* cache,
u_char* key,
ngx_str_t* buffer,
uint32_t* token)
{
ngx_perf_counter_context(pcctx);
ngx_flag_t result;
ngx_perf_counter_start(pcctx);
result = ngx_buffer_cache_fetch(cache, key, buffer, token);
ngx_perf_counter_end(perf_counters, pcctx, PC_FETCH_CACHE);
return result;
}
static int
ngx_buffer_cache_fetch_multi_perf(
ngx_perf_counters_t* perf_counters,
ngx_buffer_cache_t** caches,
uint32_t cache_count,
u_char* key,
ngx_str_t* buffer,
uint32_t* token)
{
ngx_perf_counter_context(pcctx);
ngx_buffer_cache_t* cache;
ngx_flag_t result;
uint32_t cache_index;
ngx_perf_counter_start(pcctx);
for (cache_index = 0; cache_index < cache_count; cache_index++)
{
cache = caches[cache_index];
if (cache == NULL)
{
continue;
}
result = ngx_buffer_cache_fetch(cache, key, buffer, token);
if (!result)
{
continue;
}
ngx_perf_counter_end(perf_counters, pcctx, PC_FETCH_CACHE);
return cache_index;
}
ngx_perf_counter_end(perf_counters, pcctx, PC_FETCH_CACHE);
return -1;
}
static int
ngx_buffer_cache_fetch_copy_perf(
ngx_http_request_t* r,
ngx_perf_counters_t* perf_counters,
ngx_buffer_cache_t** caches,
uint32_t cache_count,
u_char* key,
ngx_str_t* buffer)
{
ngx_str_t original_buffer;
uint32_t token;
u_char* buffer_copy;
int result;
result = ngx_buffer_cache_fetch_multi_perf(
perf_counters,
caches,
cache_count,
key,
&original_buffer,
&token);
if (result < 0)
{
return result;
}
buffer_copy = ngx_palloc(r->pool, original_buffer.len + 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 -1;
}
ngx_memcpy(buffer_copy, original_buffer.data, original_buffer.len);
buffer_copy[original_buffer.len] = '\0';
ngx_buffer_cache_release(caches[result], key, token);
buffer->data = buffer_copy;
buffer->len = original_buffer.len;
return result;
}
static ngx_flag_t
ngx_buffer_cache_store_perf(
ngx_perf_counters_t* perf_counters,
ngx_buffer_cache_t* cache,
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(cache, 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_buffer_cache_t* cache,
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(cache, key, buffers, buffer_count);
ngx_perf_counter_end(perf_counters, pcctx, PC_STORE_CACHE);
return result;
}
////// Multipart cache functions
static ngx_flag_t
ngx_buffer_cache_store_multipart_perf(
ngx_http_vod_ctx_t *ctx,
ngx_buffer_cache_t* cache,
u_char* key,
multipart_cache_header_t* header,
ngx_str_t* parts)
{
ngx_str_t* buffers;
ngx_str_t* cur_part;
ngx_str_t* parts_end;
uint32_t part_count = header->part_count;
u_char* p;
size_t* cur_size;
p = ngx_palloc(
ctx->submodule_context.request_context.pool,
sizeof(buffers[0]) * (part_count + 1) + sizeof(*header) + sizeof(size_t) * part_count);
if (p == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->submodule_context.request_context.log, 0,
"ngx_buffer_cache_store_multipart_perf: ngx_palloc failed");
return 0;
}
buffers = (void*)p;
p += sizeof(buffers[0]) * (part_count + 1);
buffers[0].data = p;
buffers[0].len = sizeof(*header) + sizeof(size_t) * part_count;
ngx_memcpy(buffers + 1, parts, part_count * sizeof(buffers[0]));
p = ngx_copy(p, header, sizeof(*header));
cur_size = (void*)p;
parts_end = parts + part_count;
for (cur_part = parts; cur_part < parts_end; cur_part++)
{
*cur_size++ = cur_part->len;
}
return ngx_buffer_cache_store_gather_perf(
ctx->perf_counters,
cache,
key,
buffers,