-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngx_http_mp4_module.c
3545 lines (2708 loc) · 104 KB
/
ngx_http_mp4_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
/*
* ngx_http_mp4_module.c mainly base on ngx_http_mp4_module.c from Nginx(1.6.2)
*
* Copyright (C) 2014 NadiaF0rever
* Copyright (C) 2014 Beijing Datafrog Technology Co., Ltd.
* Copyright (C) 2002-2014 Igor Sysoev
* Copyright (C) 2011-2014 Nginx, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "fgvideo_youku_handler.h"
#include "fgvideo_pptv_handler.h"
#include "ngx_http_mp4_handler.h"
#define NGX_HTTP_MP4_TRAK_ATOM 0
#define NGX_HTTP_MP4_TKHD_ATOM 1
#define NGX_HTTP_MP4_MDIA_ATOM 2
#define NGX_HTTP_MP4_MDHD_ATOM 3
#define NGX_HTTP_MP4_HDLR_ATOM 4
#define NGX_HTTP_MP4_MINF_ATOM 5
#define NGX_HTTP_MP4_VMHD_ATOM 6
#define NGX_HTTP_MP4_SMHD_ATOM 7
#define NGX_HTTP_MP4_DINF_ATOM 8
#define NGX_HTTP_MP4_STBL_ATOM 9
#define NGX_HTTP_MP4_STSD_ATOM 10
#define NGX_HTTP_MP4_STTS_ATOM 11
#define NGX_HTTP_MP4_STTS_DATA 12
#define NGX_HTTP_MP4_STSS_ATOM 13
#define NGX_HTTP_MP4_STSS_DATA 14
#define NGX_HTTP_MP4_CTTS_ATOM 15
#define NGX_HTTP_MP4_CTTS_DATA 16
#define NGX_HTTP_MP4_STSC_ATOM 17
#define NGX_HTTP_MP4_STSC_START 18
#define NGX_HTTP_MP4_STSC_DATA 19
#define NGX_HTTP_MP4_STSC_END 20
#define NGX_HTTP_MP4_STSZ_ATOM 21
#define NGX_HTTP_MP4_STSZ_DATA 22
#define NGX_HTTP_MP4_STCO_ATOM 23
#define NGX_HTTP_MP4_STCO_DATA 24
#define NGX_HTTP_MP4_CO64_ATOM 25
#define NGX_HTTP_MP4_CO64_DATA 26
#define NGX_HTTP_MP4_LAST_ATOM NGX_HTTP_MP4_CO64_DATA
typedef struct {
size_t buffer_size;
size_t max_buffer_size;
} ngx_http_mp4_conf_t;
typedef struct {
u_char chunk[4];
u_char samples[4];
u_char id[4];
} ngx_mp4_stsc_entry_t;
typedef struct {
uint32_t timescale;
uint32_t time_to_sample_entries;
uint32_t sample_to_chunk_entries;
uint32_t sync_samples_entries;
uint32_t composition_offset_entries;
uint32_t sample_sizes_entries;
uint32_t chunks;
ngx_uint_t start_sample;
ngx_uint_t end_sample;
ngx_uint_t start_chunk;
ngx_uint_t end_chunk;
ngx_uint_t start_chunk_samples;
ngx_uint_t end_chunk_samples;
uint64_t start_chunk_samples_size;
uint64_t end_chunk_samples_size;
off_t start_offset;
off_t end_offset;
size_t tkhd_size;
size_t mdhd_size;
size_t hdlr_size;
size_t vmhd_size;
size_t smhd_size;
size_t dinf_size;
size_t size;
ngx_chain_t out[NGX_HTTP_MP4_LAST_ATOM + 1];
ngx_buf_t trak_atom_buf;
ngx_buf_t tkhd_atom_buf;
ngx_buf_t mdia_atom_buf;
ngx_buf_t mdhd_atom_buf;
ngx_buf_t hdlr_atom_buf;
ngx_buf_t minf_atom_buf;
ngx_buf_t vmhd_atom_buf;
ngx_buf_t smhd_atom_buf;
ngx_buf_t dinf_atom_buf;
ngx_buf_t stbl_atom_buf;
ngx_buf_t stsd_atom_buf;
ngx_buf_t stts_atom_buf;
ngx_buf_t stts_data_buf;
ngx_buf_t stss_atom_buf;
ngx_buf_t stss_data_buf;
ngx_buf_t ctts_atom_buf;
ngx_buf_t ctts_data_buf;
ngx_buf_t stsc_atom_buf;
ngx_buf_t stsc_start_chunk_buf;
ngx_buf_t stsc_end_chunk_buf;
ngx_buf_t stsc_data_buf;
ngx_buf_t stsz_atom_buf;
ngx_buf_t stsz_data_buf;
ngx_buf_t stco_atom_buf;
ngx_buf_t stco_data_buf;
ngx_buf_t co64_atom_buf;
ngx_buf_t co64_data_buf;
ngx_mp4_stsc_entry_t stsc_start_chunk_entry;
ngx_mp4_stsc_entry_t stsc_end_chunk_entry;
} ngx_http_mp4_trak_t;
typedef struct {
ngx_file_t file;
u_char *buffer;
u_char *buffer_start;
u_char *buffer_pos;
u_char *buffer_end;
size_t buffer_size;
off_t offset;
off_t end;
off_t content_length;
ngx_uint_t start;
ngx_uint_t length;
uint32_t timescale;
ngx_http_request_t *request;
ngx_array_t trak;
ngx_http_mp4_trak_t traks[2];
size_t ftyp_size;
size_t moov_size;
ngx_chain_t *out;
ngx_chain_t ftyp_atom;
ngx_chain_t moov_atom;
ngx_chain_t mvhd_atom;
ngx_chain_t mdat_atom;
ngx_chain_t mdat_data;
ngx_buf_t ftyp_atom_buf;
ngx_buf_t moov_atom_buf;
ngx_buf_t mvhd_atom_buf;
ngx_buf_t mdat_atom_buf;
ngx_buf_t mdat_data_buf;
u_char moov_atom_header[8];
u_char mdat_atom_header[16];
} ngx_http_mp4_file_t;
typedef struct {
char *name;
ngx_int_t (*handler)(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
} ngx_http_mp4_atom_handler_t;
#define ngx_mp4_atom_header(mp4) (mp4->buffer_pos - 8)
#define ngx_mp4_atom_data(mp4) mp4->buffer_pos
#define ngx_mp4_atom_data_size(t) (uint64_t) (sizeof(t) - 8)
#define ngx_mp4_atom_next(mp4, n) \
mp4->buffer_pos += (size_t) n; \
mp4->offset += n
#define ngx_mp4_set_atom_name(p, n1, n2, n3, n4) \
((u_char *) (p))[4] = n1; \
((u_char *) (p))[5] = n2; \
((u_char *) (p))[6] = n3; \
((u_char *) (p))[7] = n4
#define ngx_mp4_get_32value(p) \
( ((uint32_t) ((u_char *) (p))[0] << 24) \
+ ( ((u_char *) (p))[1] << 16) \
+ ( ((u_char *) (p))[2] << 8) \
+ ( ((u_char *) (p))[3]) )
#define ngx_mp4_set_32value(p, n) \
((u_char *) (p))[0] = (u_char) ((n) >> 24); \
((u_char *) (p))[1] = (u_char) ((n) >> 16); \
((u_char *) (p))[2] = (u_char) ((n) >> 8); \
((u_char *) (p))[3] = (u_char) (n)
#define ngx_mp4_get_64value(p) \
( ((uint64_t) ((u_char *) (p))[0] << 56) \
+ ((uint64_t) ((u_char *) (p))[1] << 48) \
+ ((uint64_t) ((u_char *) (p))[2] << 40) \
+ ((uint64_t) ((u_char *) (p))[3] << 32) \
+ ((uint64_t) ((u_char *) (p))[4] << 24) \
+ ( ((u_char *) (p))[5] << 16) \
+ ( ((u_char *) (p))[6] << 8) \
+ ( ((u_char *) (p))[7]) )
#define ngx_mp4_set_64value(p, n) \
((u_char *) (p))[0] = (u_char) ((uint64_t) (n) >> 56); \
((u_char *) (p))[1] = (u_char) ((uint64_t) (n) >> 48); \
((u_char *) (p))[2] = (u_char) ((uint64_t) (n) >> 40); \
((u_char *) (p))[3] = (u_char) ((uint64_t) (n) >> 32); \
((u_char *) (p))[4] = (u_char) ( (n) >> 24); \
((u_char *) (p))[5] = (u_char) ( (n) >> 16); \
((u_char *) (p))[6] = (u_char) ( (n) >> 8); \
((u_char *) (p))[7] = (u_char) (n)
#define ngx_mp4_last_trak(mp4) \
&((ngx_http_mp4_trak_t *) mp4->trak.elts)[mp4->trak.nelts - 1]
static ngx_int_t ngx_http_mp4_process(ngx_http_mp4_file_t *mp4);
static ngx_int_t ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size);
static ngx_int_t ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static size_t ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4,
off_t start_offset, off_t end_offset);
static ngx_int_t ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static void ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static void ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static ngx_int_t ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static void ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, int32_t adjustment);
static ngx_int_t ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
static void ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak, off_t adjustment);
static char *ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_mp4_create_conf(ngx_conf_t *cf);
static char *ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_mp4_handler_dispatcher(ngx_http_request_t *r);
static ngx_command_t ngx_http_mp4_commands[] = {
{ ngx_string("mp4"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_mp4,
0,
0,
NULL },
{ ngx_string("mp4_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mp4_conf_t, buffer_size),
NULL },
{ ngx_string("mp4_max_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mp4_conf_t, max_buffer_size),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_mp4_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_mp4_create_conf, /* create location configuration */
ngx_http_mp4_merge_conf /* merge location configuration */
};
ngx_module_t ngx_http_mp4_module = {
NGX_MODULE_V1,
&ngx_http_mp4_module_ctx, /* module context */
ngx_http_mp4_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_http_mp4_atom_handler_t ngx_http_mp4_atoms[] = {
{ "ftyp", ngx_http_mp4_read_ftyp_atom },
{ "moov", ngx_http_mp4_read_moov_atom },
{ "mdat", ngx_http_mp4_read_mdat_atom },
{ NULL, NULL }
};
static ngx_http_mp4_atom_handler_t ngx_http_mp4_moov_atoms[] = {
{ "mvhd", ngx_http_mp4_read_mvhd_atom },
{ "trak", ngx_http_mp4_read_trak_atom },
{ "cmov", ngx_http_mp4_read_cmov_atom },
{ NULL, NULL }
};
static ngx_http_mp4_atom_handler_t ngx_http_mp4_trak_atoms[] = {
{ "tkhd", ngx_http_mp4_read_tkhd_atom },
{ "mdia", ngx_http_mp4_read_mdia_atom },
{ NULL, NULL }
};
static ngx_http_mp4_atom_handler_t ngx_http_mp4_mdia_atoms[] = {
{ "mdhd", ngx_http_mp4_read_mdhd_atom },
{ "hdlr", ngx_http_mp4_read_hdlr_atom },
{ "minf", ngx_http_mp4_read_minf_atom },
{ NULL, NULL }
};
static ngx_http_mp4_atom_handler_t ngx_http_mp4_minf_atoms[] = {
{ "vmhd", ngx_http_mp4_read_vmhd_atom },
{ "smhd", ngx_http_mp4_read_smhd_atom },
{ "dinf", ngx_http_mp4_read_dinf_atom },
{ "stbl", ngx_http_mp4_read_stbl_atom },
{ NULL, NULL }
};
static ngx_http_mp4_atom_handler_t ngx_http_mp4_stbl_atoms[] = {
{ "stsd", ngx_http_mp4_read_stsd_atom },
{ "stts", ngx_http_mp4_read_stts_atom },
{ "stss", ngx_http_mp4_read_stss_atom },
{ "ctts", ngx_http_mp4_read_ctts_atom },
{ "stsc", ngx_http_mp4_read_stsc_atom },
{ "stsz", ngx_http_mp4_read_stsz_atom },
{ "stco", ngx_http_mp4_read_stco_atom },
{ "co64", ngx_http_mp4_read_co64_atom },
{ NULL, NULL }
};
ngx_int_t
ngx_http_mp4_handler(ngx_http_request_t *r)
{
u_char *last;
size_t root;
ngx_int_t rc, start, end;
ngx_uint_t level, length;
ngx_str_t path, value;
ngx_log_t *log;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_mp4_file_t *mp4;
ngx_open_file_info_t of;
ngx_http_core_loc_conf_t *clcf;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->uri.data[r->uri.len - 1] == '/') {
return NGX_DECLINED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
last = ngx_http_map_uri_to_path(r, &path, &root, 0);
if (last == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
log = r->connection->log;
path.len = last - path.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http mp4 filename: \"%V\"", &path);
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_memzero(&of, sizeof(ngx_open_file_info_t));
of.read_ahead = clcf->read_ahead;
of.directio = NGX_MAX_OFF_T_VALUE;
of.valid = clcf->open_file_cache_valid;
of.min_uses = clcf->open_file_cache_min_uses;
of.errors = clcf->open_file_cache_errors;
of.events = clcf->open_file_cache_events;
if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
!= NGX_OK)
{
switch (of.err) {
case 0:
return NGX_HTTP_INTERNAL_SERVER_ERROR;
case NGX_ENOENT:
case NGX_ENOTDIR:
case NGX_ENAMETOOLONG:
level = NGX_LOG_ERR;
rc = NGX_HTTP_NOT_FOUND;
break;
case NGX_EACCES:
#if (NGX_HAVE_OPENAT)
case NGX_EMLINK:
case NGX_ELOOP:
#endif
level = NGX_LOG_ERR;
rc = NGX_HTTP_FORBIDDEN;
break;
default:
level = NGX_LOG_CRIT;
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
}
if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
ngx_log_error(level, log, of.err,
"%s \"%s\" failed", of.failed, path.data);
}
return rc;
}
if (!of.is_file) {
if (ngx_close_file(of.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_close_file_n " \"%s\" failed", path.data);
}
return NGX_DECLINED;
}
r->root_tested = !r->error_page;
r->allow_ranges = 1;
start = -1;
length = 0;
r->headers_out.content_length_n = of.size;
mp4 = NULL;
b = NULL;
if (r->args.len) {
if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {
/*
* A Flash player may send start value with a lot of digits
* after dot so strtod() is used instead of atofp(). NaNs and
* infinities become negative numbers after (int) conversion.
*/
ngx_set_errno(0);
start = (int) (strtod((char *) value.data, NULL) * 1000);
if (ngx_errno != 0) {
start = -1;
}
}
if (ngx_http_arg(r, (u_char *) "end", 3, &value) == NGX_OK) {
ngx_set_errno(0);
end = (int) (strtod((char *) value.data, NULL) * 1000);
if (ngx_errno != 0) {
end = -1;
}
if (end > 0) {
if (start < 0) {
start = 0;
}
if (end > start) {
length = end - start;
}
}
}
}
if (start >= 0) {
/*
* FIXME:
* the mp4 process code get from nginx-1.6.2 and for nginx-1.6.2
* this flag is useless, so just comment it.
* r->single_range = 1;
*/
mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
if (mp4 == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
mp4->file.fd = of.fd;
mp4->file.name = path;
mp4->file.log = r->connection->log;
mp4->end = of.size;
mp4->start = (ngx_uint_t) start;
mp4->length = length;
mp4->request = r;
switch (ngx_http_mp4_process(mp4)) {
case NGX_DECLINED:
if (mp4->buffer) {
ngx_pfree(r->pool, mp4->buffer);
}
ngx_pfree(r->pool, mp4);
mp4 = NULL;
break;
case NGX_OK:
r->headers_out.content_length_n = mp4->content_length;
break;
default: /* NGX_ERROR */
if (mp4->buffer) {
ngx_pfree(r->pool, mp4->buffer);
}
ngx_pfree(r->pool, mp4);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
log->action = "sending mp4 to client";
if (clcf->directio <= of.size) {
/*
* DIRECTIO is set on transfer only
* to allow kernel to cache "moov" atom
*/
if (ngx_directio_on(of.fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_directio_on_n " \"%s\" failed", path.data);
}
of.is_directio = 1;
if (mp4) {
mp4->file.directio = 1;
}
}
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.last_modified_time = of.mtime;
if (ngx_http_set_etag(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (mp4 == NULL) {
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
if (b->file == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
if (mp4) {
return ngx_http_output_filter(r, mp4->out);
}
b->file_pos = 0;
b->file_last = of.size;
b->in_file = b->file_last ? 1 : 0;
b->last_buf = (r == r->main) ? 1 : 0;
b->last_in_chain = 1;
b->file->fd = of.fd;
b->file->name = path;
b->file->log = log;
b->file->directio = of.is_directio;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static ngx_int_t
ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
{
off_t start_offset, end_offset, adjustment;
ngx_int_t rc;
ngx_uint_t i, j;
ngx_chain_t **prev;
ngx_http_mp4_trak_t *trak;
ngx_http_mp4_conf_t *conf;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mp4 start:%ui, length:%ui", mp4->start, mp4->length);
conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);
mp4->buffer_size = conf->buffer_size;
rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_atoms, mp4->end);
if (rc != NGX_OK) {
return rc;
}
if (mp4->trak.nelts == 0) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"no mp4 trak atoms were found in \"%s\"",
mp4->file.name.data);
return NGX_ERROR;
}
if (mp4->mdat_atom.buf == NULL) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"no mp4 mdat atom was found in \"%s\"",
mp4->file.name.data);
return NGX_ERROR;
}
prev = &mp4->out;
if (mp4->ftyp_atom.buf) {
*prev = &mp4->ftyp_atom;
prev = &mp4->ftyp_atom.next;
}
*prev = &mp4->moov_atom;
prev = &mp4->moov_atom.next;
if (mp4->mvhd_atom.buf) {
mp4->moov_size += mp4->mvhd_atom_buf.last - mp4->mvhd_atom_buf.pos;
*prev = &mp4->mvhd_atom;
prev = &mp4->mvhd_atom.next;
}
start_offset = mp4->end;
end_offset = 0;
trak = mp4->trak.elts;
for (i = 0; i < mp4->trak.nelts; i++) {
if (ngx_http_mp4_update_stts_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_http_mp4_update_stss_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
ngx_http_mp4_update_ctts_atom(mp4, &trak[i]);
if (ngx_http_mp4_update_stsc_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_http_mp4_update_stsz_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
if (ngx_http_mp4_update_co64_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
} else {
if (ngx_http_mp4_update_stco_atom(mp4, &trak[i]) != NGX_OK) {
return NGX_ERROR;
}
}
ngx_http_mp4_update_stbl_atom(mp4, &trak[i]);
ngx_http_mp4_update_minf_atom(mp4, &trak[i]);
trak[i].size += trak[i].mdhd_size;
trak[i].size += trak[i].hdlr_size;
ngx_http_mp4_update_mdia_atom(mp4, &trak[i]);
trak[i].size += trak[i].tkhd_size;
ngx_http_mp4_update_trak_atom(mp4, &trak[i]);
mp4->moov_size += trak[i].size;
if (start_offset > trak[i].start_offset) {
start_offset = trak[i].start_offset;
}
if (end_offset < trak[i].end_offset) {
end_offset = trak[i].end_offset;
}
*prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM];
prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next;
for (j = 0; j < NGX_HTTP_MP4_LAST_ATOM + 1; j++) {
if (trak[i].out[j].buf) {
*prev = &trak[i].out[j];
prev = &trak[i].out[j].next;
}
}
}
if (end_offset < start_offset) {
end_offset = start_offset;
}
mp4->moov_size += 8;
ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size);
ngx_mp4_set_atom_name(mp4->moov_atom_header, 'm', 'o', 'o', 'v');
mp4->content_length += mp4->moov_size;
*prev = &mp4->mdat_atom;
if (start_offset > mp4->mdat_data.buf->file_last) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"start time is out mp4 mdat atom in \"%s\"",
mp4->file.name.data);
return NGX_ERROR;
}
adjustment = mp4->ftyp_size + mp4->moov_size
+ ngx_http_mp4_update_mdat_atom(mp4, start_offset, end_offset)
- start_offset;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mp4 adjustment:%O", adjustment);
for (i = 0; i < mp4->trak.nelts; i++) {
if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
ngx_http_mp4_adjust_co64_atom(mp4, &trak[i], adjustment);
} else {
ngx_http_mp4_adjust_stco_atom(mp4, &trak[i], (int32_t) adjustment);
}
}
return NGX_OK;
}
typedef struct {
u_char size[4];
u_char name[4];
} ngx_mp4_atom_header_t;
typedef struct {
u_char size[4];
u_char name[4];
u_char size64[8];
} ngx_mp4_atom_header64_t;
static ngx_int_t
ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size)
{
off_t end;
size_t atom_header_size;
u_char *atom_header, *atom_name;
uint64_t atom_size;
ngx_int_t rc;
ngx_uint_t n;
end = mp4->offset + atom_data_size;
while (mp4->offset < end) {
if (ngx_http_mp4_read(mp4, sizeof(uint32_t)) != NGX_OK) {
return NGX_ERROR;
}
atom_header = mp4->buffer_pos;
atom_size = ngx_mp4_get_32value(atom_header);
atom_header_size = sizeof(ngx_mp4_atom_header_t);
if (atom_size == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mp4 atom end");
return NGX_OK;
}
if (atom_size < sizeof(ngx_mp4_atom_header_t)) {
if (atom_size == 1) {
if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header64_t))
!= NGX_OK)
{
return NGX_ERROR;
}
/* 64-bit atom size */
atom_header = mp4->buffer_pos;
atom_size = ngx_mp4_get_64value(atom_header + 8);
atom_header_size = sizeof(ngx_mp4_atom_header64_t);
} else {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"\"%s\" mp4 atom is too small:%uL",
mp4->file.name.data, atom_size);
return NGX_ERROR;
}
}
if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header_t)) != NGX_OK) {
return NGX_ERROR;
}
atom_header = mp4->buffer_pos;
atom_name = atom_header + sizeof(uint32_t);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mp4 atom: %*s @%O:%uL",
4, atom_name, mp4->offset, atom_size);
if (atom_size > (uint64_t) (NGX_MAX_OFF_T_VALUE - mp4->offset)
|| mp4->offset + (off_t) atom_size > end)
{
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"\"%s\" mp4 atom too large:%uL",
mp4->file.name.data, atom_size);
return NGX_ERROR;
}
for (n = 0; atom[n].name; n++) {
if (ngx_strncmp(atom_name, atom[n].name, 4) == 0) {
ngx_mp4_atom_next(mp4, atom_header_size);
rc = atom[n].handler(mp4, atom_size - atom_header_size);
if (rc != NGX_OK) {
return rc;
}
goto next;
}
}
ngx_mp4_atom_next(mp4, atom_size);
next:
continue;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size)
{
ssize_t n;
if (mp4->buffer_pos + size <= mp4->buffer_end) {
return NGX_OK;
}
if (mp4->offset + (off_t) mp4->buffer_size > mp4->end) {
mp4->buffer_size = (size_t) (mp4->end - mp4->offset);
}
if (mp4->buffer_size < size) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"\"%s\" mp4 file truncated", mp4->file.name.data);