-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_flv_module.c
1395 lines (1059 loc) · 38 KB
/
ngx_http_flv_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
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Roman Arutyunyan
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define NGX_FLV_HEADER_SIGVER 0x464c5601
#define NGX_FLV_HEADER_VIDEO 0x01
#define NGX_FLV_HEADER_AUDIO 0x04
#define NGX_FLV_TAG_TYPE_AUDIO 0x08
#define NGX_FLV_TAG_TYPE_VIDEO 0x09
#define NGX_FLV_TAG_TYPE_META 0x12
#define NGX_FLV_TAG_TYPE_MASK 0x1f
#define NGX_FLV_PACKET_AVC 0x07
#define NGX_FLV_PACKET_AAC 0x0a
#define NGX_FLV_AUDIO_CODECID_MASK 0xf0
#define NGX_FLV_VIDEO_CODECID_MASK 0x0f
#define NGX_FLV_VIDEO_FRAMETYPE_MASK 0xf0
typedef struct {
u_char signature[3]; /* "FLV" */
u_char version[1]; /* 0x01 [version 1] */
u_char av_flags[1]; /* 0b00000 1[a] 0 1[v] */
u_char length[4]; /* 0x00000009 */
u_char tag_size[4]; /* 0x00000000 */
} ngx_flv_header_t;
typedef struct {
u_char type[1]; /* 0b00 0[filter] 00000[type] */
u_char size[3]; /* message length */
u_char timestamp[3]; /* pts in milliseconds */
u_char timestamp_ext[1]; /* upper 8 bits of timestamp */
u_char stream_id[3]; /* 0x000000 */
/* Audio: 0b0000[codec_id] 00[rate] 0[size] 0[type]
* Video: 0b0000[type] 0000[codec_id] */
u_char av_header[1];
/* Audio or Video: 0x00 is sequence_header */
u_char av_type[1];
} ngx_flv_tag_header_t;
/* NGX_FLV_HEADER_SIZE == NGX_FLV_TAG_HEADER_SIZE + NGX_FLV_PREV_TAG_SIZE */
#define NGX_FLV_HEADER_SIZE sizeof(ngx_flv_header_t)
#define NGX_FLV_PREV_TAG_SIZE 4
#define NGX_FLV_TAG_HEADER_SIZE 11
#define NGX_FLV_HEADER_LENGTH (NGX_FLV_HEADER_SIZE - NGX_FLV_PREV_TAG_SIZE)
#define ngx_flv_get_tag_size(p) \
( ((uint32_t) ((u_char *) (p))[0] << 16) \
+ ( ((u_char *) (p))[1] << 8) \
+ ( ((u_char *) (p))[2]) )
#define ngx_flv_get_timestamp(p) \
( ((uint32_t) ((u_char *) (p))[3] << 24) \
+ ( ((u_char *) (p))[0] << 16) \
+ ( ((u_char *) (p))[1] << 8) \
+ ( ((u_char *) (p))[2]) )
#define ngx_flv_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_flv_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_flv_validate_signature_version(p) \
(ngx_flv_get_32value(p) == NGX_FLV_HEADER_SIGVER)
#define ngx_flv_header_has_audio(p) \
(((u_char *) (p))[0] & NGX_FLV_HEADER_AUDIO)
#define ngx_flv_header_has_video(p) \
(((u_char *) (p))[0] & NGX_FLV_HEADER_VIDEO)
#define ngx_flv_tag_type(p) \
(((u_char *) (p))[0] & NGX_FLV_TAG_TYPE_MASK)
#define ngx_flv_tag_is_video(p) \
(ngx_flv_tag_type(p) == NGX_FLV_TAG_TYPE_VIDEO)
#define ngx_flv_tag_is_audio(p) \
(ngx_flv_tag_type(p) == NGX_FLV_TAG_TYPE_AUDIO)
#define ngx_flv_tag_is_meta(p) \
(ngx_flv_tag_type(p) == NGX_FLV_TAG_TYPE_META)
#define ngx_flv_is_sequence_header(p) (((u_char *) (p))[0] == 0x00)
#define ngx_flv_codec_is_avc(p) \
((((u_char *) (p))[0] & NGX_FLV_VIDEO_CODECID_MASK) == NGX_FLV_PACKET_AVC)
#define ngx_flv_video_frametype(p) \
((((u_char *) (p))[0] & NGX_FLV_VIDEO_FRAMETYPE_MASK) >> 4)
#define ngx_flv_video_is_keyframe(p) (ngx_flv_video_frametype(p) == 0x01)
#define ngx_flv_codec_is_aac(p) \
(((((u_char *) (p))[0] & NGX_FLV_AUDIO_CODECID_MASK) >> 4) \
== NGX_FLV_PACKET_AAC)
/* basic types */
#define NGX_FLV_AMF_NUMBER 0x00
#define NGX_FLV_AMF_BOOLEAN 0x01
#define NGX_FLV_AMF_STRING 0x02
#define NGX_FLV_AMF_OBJECT 0x03
#define NGX_FLV_AMF_NULL 0x05
#define NGX_FLV_AMF_ARRAY_NULL 0x06
#define NGX_FLV_AMF_MIXED_ARRAY 0x08
#define NGX_FLV_AMF_END 0x09
#define NGX_FLV_AMF_ARRAY 0x0a
/* extended types */
#define NGX_FLV_AMF_INT8 0x0100
#define NGX_FLV_AMF_INT16 0x0101
#define NGX_FLV_AMF_INT32 0x0102
#define NGX_FLV_AMF_VARIANT_ 0x0103
/* r/w flags */
#define NGX_FLV_AMF_OPTIONAL 0x1000
#define NGX_FLV_AMF_TYPELESS 0x2000
#define NGX_FLV_AMF_CONTEXT 0x4000
static ngx_inline double
ngx_flv_get_amf_number(u_char *p)
{
union {
uint64_t i;
double f;
} uint2double;
uint2double.i = ngx_flv_get_64value(p);
return uint2double.f;
}
typedef struct {
ngx_int_t type;
ngx_str_t name;
void *data;
size_t len;
} ngx_flv_amf_elt_t;
typedef struct {
size_t buffer_size;
size_t max_buffer_size;
ngx_flag_t time_offset;
ngx_flag_t with_metadata;
} ngx_http_flv_conf_t;
typedef struct {
ngx_file_t file;
ngx_http_request_t *request;
u_char *buffer;
u_char *buffer_start;
u_char *buffer_pos;
u_char *buffer_end;
size_t buffer_size;
off_t offset;
off_t file_end;
off_t sequence_end;
off_t necessary_len;
off_t start_offset;
off_t end_offset;
ngx_uint_t start;
ngx_uint_t end;
unsigned has_audio:1;
unsigned has_video:1;
unsigned met_audio:1;
unsigned met_video:1;
unsigned aac_audio:1;
unsigned avc_video:1;
unsigned with_metadata:1;
ngx_chain_t *necessary;
ngx_chain_t metadata;
ngx_chain_t audio_sequence_header;
ngx_chain_t video_sequence_header;
ngx_buf_t metadata_buf;
ngx_buf_t audio_sequence_buf;
ngx_buf_t video_sequence_buf;
u_char *filepositions;
u_char *times;
uint32_t filepositions_nelts;
uint32_t times_nelts;
} ngx_http_flv_file_t;
#define ngx_http_flv_tag_next(flv, n) \
flv->buffer_pos += (size_t) n; \
flv->offset += n
static ngx_int_t ngx_http_flv_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_flv_atofp(u_char *line, size_t n, size_t point);
static ngx_int_t ngx_http_flv_process(ngx_http_flv_file_t *flv);
static ngx_int_t ngx_http_flv_read(ngx_http_flv_file_t *flv, size_t size);
static ngx_int_t ngx_http_flv_read_header(ngx_http_flv_file_t *flv);
static ngx_int_t ngx_http_flv_read_metadata(ngx_http_flv_file_t *flv);
static ngx_int_t ngx_http_flv_read_tags(ngx_http_flv_file_t *flv);
static ngx_int_t ngx_http_flv_parse_metadata(ngx_http_flv_file_t *flv);
static off_t ngx_http_flv_timestamp_to_offset(ngx_http_flv_file_t *flv,
ngx_uint_t timestamp, ngx_uint_t start);
static void *ngx_flv_amf_reverse_copy(void *dst, void *src, size_t len);
static ngx_int_t ngx_flv_amf_is_compatible_type(uint8_t t1, uint8_t t2);
static ngx_int_t ngx_flv_amf_get(ngx_http_flv_file_t *flv, void *p, size_t n);
static ngx_int_t ngx_flv_amf_read(ngx_http_flv_file_t *flv,
ngx_flv_amf_elt_t *elts, size_t nelts);
static ngx_int_t ngx_flv_amf_read_object(ngx_http_flv_file_t *flv,
ngx_flv_amf_elt_t *elts, size_t nelts);
static ngx_int_t ngx_flv_amf_read_array(ngx_http_flv_file_t *flv,
ngx_flv_amf_elt_t *elts, size_t nelts);
static char *ngx_http_flv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_flv_create_conf(ngx_conf_t *cf);
static char *ngx_http_flv_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_command_t ngx_http_flv_commands[] = {
{ ngx_string("flv"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_flv,
0,
0,
NULL },
{ ngx_string("flv_time_offset"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_flv_conf_t, time_offset),
NULL },
{ ngx_string("flv_with_metadata"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_flv_conf_t, with_metadata),
NULL },
{ ngx_string("flv_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_flv_conf_t, buffer_size),
NULL },
{ ngx_string("flv_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_flv_conf_t, max_buffer_size),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_flv_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_flv_create_conf, /* create location configuration */
ngx_http_flv_merge_conf /* merge location configuration */
};
ngx_module_t ngx_http_flv_module = {
NGX_MODULE_V1,
&ngx_http_flv_module_ctx, /* module context */
ngx_http_flv_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 u_char ngx_flv_header[] = "FLV\x1\x5\0\0\0\x9\0\0\0\0";
static ngx_int_t
ngx_http_flv_handler(ngx_http_request_t *r)
{
off_t start, end, len;
u_char *last;
size_t root;
ngx_str_t path, value;
ngx_log_t *log;
ngx_buf_t *b;
ngx_int_t rc, time_start, time_end;
ngx_uint_t level, i, time_offset;
ngx_chain_t out[2];
ngx_http_flv_file_t *flv;
ngx_http_flv_conf_t *conf;
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 flv 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 = clcf->directio;
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;
conf = ngx_http_get_module_loc_conf(r, ngx_http_flv_module);
time_offset = (ngx_uint_t) conf->time_offset;
time_start = -1;
time_end = 0;
start = 0;
end = of.size;
len = of.size;
i = 1;
flv = NULL;
if (r->args.len) {
if (ngx_http_arg(r, (u_char *) "time_offset", 11, &value) == NGX_OK) {
time_offset = 1;
}
if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {
if (time_offset) {
time_start = ngx_http_flv_atofp(value.data, value.len, 3);
} else {
start = ngx_atoof(value.data, value.len);
if (start == NGX_ERROR || start >= len) {
start = 0;
}
if (start) {
len = sizeof(ngx_flv_header) - 1 + len - start;
i = 0;
}
}
}
if (time_offset
&& ngx_http_arg(r, (u_char *) "end", 3, &value) == NGX_OK)
{
time_end = ngx_http_flv_atofp(value.data, value.len, 3);
if (time_end >= 0 && time_start < 0) {
time_start = 0;
}
}
}
if (time_start >= 0) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"http flv time_offset time_start=%i time_end=%i",
time_start, time_end);
if (time_end < time_start) {
/* denotes the file end */
time_end = NGX_MAX_INT_T_VALUE;
}
flv = ngx_pcalloc(r->pool, sizeof(ngx_http_flv_file_t));
if (flv == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
flv->file.fd = of.fd;
flv->file.name = path;
flv->file.log = r->connection->log;
flv->file_end = of.size;
flv->buffer_size = conf->buffer_size;
flv->with_metadata = conf->with_metadata;
flv->start = time_start;
flv->end = time_end;
flv->request = r;
switch (ngx_http_flv_process(flv)) {
case NGX_DECLINED:
if (flv->buffer) {
ngx_pfree(r->pool, flv->buffer);
}
ngx_pfree(r->pool, flv);
flv = NULL;
break;
case NGX_OK:
start = flv->start_offset;
end = flv->end_offset;
len = end - start;
if (start > 0) {
len += sizeof(ngx_flv_header) - 1 + flv->necessary_len;
i = 0;
}
break;
default: /* NGX_ERROR */
if (flv->buffer) {
ngx_pfree(r->pool, flv->buffer);
}
ngx_pfree(r->pool, flv);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
log->action = "sending flv to client";
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = len;
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 (i == 0) {
b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->pos = ngx_flv_header;
b->last = ngx_flv_header + sizeof(ngx_flv_header) - 1;
b->memory = 1;
out[0].buf = b;
out[0].next = &out[1];
if (flv && flv->necessary) {
out[0].next = flv->necessary;
while (flv->necessary->next) {
flv->necessary= flv->necessary->next;
}
flv->necessary->next = &out[1];
}
}
b = ngx_calloc_buf(r->pool);
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;
}
r->allow_ranges = 1;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
b->file_pos = start;
b->file_last = end;
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[1].buf = b;
out[1].next = NULL;
return ngx_http_output_filter(r, &out[i]);
}
static ngx_int_t
ngx_http_flv_atofp(u_char *line, size_t n, size_t point)
{
ngx_int_t value, cutoff, cutlim;
ngx_uint_t dot;
if (n == 0) {
return NGX_ERROR;
}
cutoff = NGX_MAX_INT_T_VALUE / 10;
cutlim = NGX_MAX_INT_T_VALUE % 10;
dot = 0;
for (value = 0; n--; line++) {
if (*line == '.') {
if (dot) {
return NGX_ERROR;
}
dot = 1;
continue;
}
if (*line < '0' || *line > '9') {
return NGX_ERROR;
}
if (dot && point == 0) {
continue;
}
if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
return NGX_ERROR;
}
value = value * 10 + (*line - '0');
point -= dot;
}
while (point--) {
if (value > cutoff) {
return NGX_ERROR;
}
value = value * 10;
}
return value;
}
static ngx_int_t
ngx_http_flv_process(ngx_http_flv_file_t *flv)
{
ngx_int_t rc;
ngx_chain_t **prev;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, flv->file.log, 0,
"flv start:%ui, end:%ui", flv->start, flv->end);
rc = ngx_http_flv_read_header(flv);
if (rc != NGX_OK) {
return rc;
}
rc = ngx_http_flv_read_metadata(flv);
if (rc != NGX_OK) {
return rc;
}
rc = ngx_http_flv_read_tags(flv);
if (rc != NGX_OK) {
return rc;
}
rc = ngx_http_flv_parse_metadata(flv);
if (rc != NGX_OK) {
return rc;
}
flv->start_offset = ngx_http_flv_timestamp_to_offset(flv, flv->start, 1);
flv->end_offset = ngx_http_flv_timestamp_to_offset(flv, flv->end, 0);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, flv->file.log, 0,
"flv start_offset:%O end_offset:%O",
flv->start_offset, flv->end_offset);
if (flv->end_offset <= flv->start_offset) {
return NGX_DECLINED;
}
prev = &flv->necessary;
if (flv->with_metadata && flv->metadata.buf) {
*prev = &flv->metadata;
prev = &flv->metadata.next;
flv->necessary_len += ngx_buf_size(flv->metadata.buf);
}
if (flv->audio_sequence_header.buf) {
*prev = &flv->audio_sequence_header;
prev = &flv->audio_sequence_header.next;
flv->necessary_len += ngx_buf_size(flv->audio_sequence_header.buf);
}
if (flv->video_sequence_header.buf) {
*prev = &flv->video_sequence_header;
flv->necessary_len += ngx_buf_size(flv->video_sequence_header.buf);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_read(ngx_http_flv_file_t *flv, size_t size)
{
ssize_t n;
if (flv->buffer_pos + size <= flv->buffer_end) {
return NGX_OK;
}
if (flv->offset + (off_t) flv->buffer_size > flv->file_end) {
flv->buffer_size = (size_t) (flv->file_end - flv->offset);
}
if (flv->buffer_size < size) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"\"%s\" flv file truncated", flv->file.name.data);
return NGX_ERROR;
}
if (flv->buffer == NULL) {
flv->buffer = ngx_palloc(flv->request->pool, flv->buffer_size);
if (flv->buffer == NULL) {
return NGX_ERROR;
}
flv->buffer_start = flv->buffer;
}
n = ngx_read_file(&flv->file, flv->buffer_start, flv->buffer_size,
flv->offset);
if (n == NGX_ERROR) {
return NGX_ERROR;
}
if ((size_t) n != flv->buffer_size) {
ngx_log_error(NGX_LOG_CRIT, flv->file.log, 0,
ngx_read_file_n " read only %z of %z from \"%s\"",
n, flv->buffer_size, flv->file.name.data);
return NGX_ERROR;
}
flv->buffer_pos = flv->buffer_start;
flv->buffer_end = flv->buffer_start + flv->buffer_size;
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_read_header(ngx_http_flv_file_t *flv)
{
ngx_flv_header_t *hdr;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, flv->file.log, 0, "flv header");
if (ngx_http_flv_read(flv, NGX_FLV_HEADER_SIZE) != NGX_OK) {
return NGX_ERROR;
}
if (!ngx_flv_validate_signature_version(flv->buffer_pos)) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv wrong signature or version");
return NGX_ERROR;
}
hdr = (ngx_flv_header_t *) flv->buffer_pos;
if (ngx_flv_get_32value(hdr->length) != NGX_FLV_HEADER_LENGTH) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv wrong header length");
return NGX_ERROR;
}
if (ngx_flv_get_32value(hdr->tag_size) != 0) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv wrong header prev_tag size");
return NGX_ERROR;
}
flv->has_video = ngx_flv_header_has_video(hdr->av_flags);
flv->has_audio = ngx_flv_header_has_audio(hdr->av_flags);
ngx_http_flv_tag_next(flv, NGX_FLV_HEADER_SIZE);
return NGX_OK;
}
/*
* Small excess buffer to process tags after metadata.
*/
#define NGX_HTTP_FLV_META_BUFFER_EXCESS (16 * 1024)
static ngx_int_t
ngx_http_flv_read_metadata(ngx_http_flv_file_t *flv)
{
uint32_t size;
ngx_buf_t *buf;
ngx_http_flv_conf_t *conf;
ngx_flv_tag_header_t *tag;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, flv->file.log, 0, "flv metadata tag");
if (ngx_http_flv_read(flv, NGX_FLV_HEADER_SIZE) != NGX_OK) {
return NGX_ERROR;
}
tag = (ngx_flv_tag_header_t *) flv->buffer_pos;
size = NGX_FLV_TAG_HEADER_SIZE + ngx_flv_get_tag_size(tag->size)
+ NGX_FLV_PREV_TAG_SIZE;
if (!ngx_flv_tag_is_meta(tag->type)) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv first tag is not metadata");
return NGX_DECLINED;
}
if (size + NGX_HTTP_FLV_META_BUFFER_EXCESS > flv->buffer_size) {
conf = ngx_http_get_module_loc_conf(flv->request, ngx_http_flv_module);
if (size > conf->max_buffer_size) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"\"%s\" flv metadata tag is too large:%D, "
"you may want to increase flv_max_buffer_size",
flv->file.name.data, size);
return NGX_ERROR;
}
ngx_pfree(flv->request->pool, flv->buffer);
flv->buffer = NULL;
flv->buffer_pos = NULL;
flv->buffer_end = NULL;
flv->buffer_size = (size_t) size + NGX_HTTP_FLV_META_BUFFER_EXCESS;
}
if (ngx_http_flv_read(flv, (size_t) size) != NGX_OK) {
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, flv->file.log, 0,
"flv metadata_len:%D", size);
buf = &flv->metadata_buf;
buf->temporary = 1;
buf->pos = flv->buffer_pos;
buf->last = flv->buffer_pos + size;
buf->start = buf->pos;
buf->end = buf->last;
flv->metadata.buf = buf;
ngx_http_flv_tag_next(flv, size);
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_read_tags(ngx_http_flv_file_t *flv)
{
uint32_t size;
ngx_buf_t *buf;
ngx_flv_tag_header_t *tag;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, flv->file.log, 0, "flv av tag");
while (flv->buffer_pos + NGX_FLV_HEADER_SIZE < flv->buffer_end) {
if (ngx_http_flv_read(flv, NGX_FLV_HEADER_SIZE) != NGX_OK) {
return NGX_ERROR;
}
tag = (ngx_flv_tag_header_t *) flv->buffer_pos;
size = NGX_FLV_TAG_HEADER_SIZE + ngx_flv_get_tag_size(tag->size)
+ NGX_FLV_PREV_TAG_SIZE;
if (flv->buffer_pos + size > flv->buffer_end) {
break;
}
switch ngx_flv_tag_type(tag->type) {
case NGX_FLV_TAG_TYPE_AUDIO:
if (flv->met_audio) {
break;
}
flv->met_audio = 1;
flv->aac_audio = ngx_flv_codec_is_aac(tag->av_header);
if (!flv->aac_audio) {
break;
}
if (!ngx_flv_is_sequence_header(tag->av_type)) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv first audio tag is not sequence header");
return NGX_ERROR;
}
flv->sequence_end = flv->offset + size;
buf = &flv->audio_sequence_buf;
buf->temporary = 1;
buf->pos = flv->buffer_pos;
buf->last = flv->buffer_pos + size;
flv->audio_sequence_header.buf = buf;
break;
case NGX_FLV_TAG_TYPE_VIDEO:
if (flv->met_video) {
break;
}
flv->met_video = 1;
flv->avc_video = ngx_flv_codec_is_avc(tag->av_header);
if (!flv->avc_video) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv does not support time offset, avc required");
return NGX_DECLINED;
}
if (!ngx_flv_is_sequence_header(tag->av_type)) {
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv first video tag is not sequence header");
return NGX_ERROR;
}
flv->sequence_end = flv->offset + size;
buf = &flv->video_sequence_buf;
buf->temporary = 1;
buf->pos = flv->buffer_pos;
buf->last = flv->buffer_pos + size;
flv->video_sequence_header.buf = buf;
break;
default:
break;
}
if (flv->met_audio && flv->met_video) {
return NGX_OK;
}
ngx_http_flv_tag_next(flv, size);
}
ngx_log_error(NGX_LOG_ERR, flv->file.log, 0,
"flv no a/v tag found within %DB after metadata",
NGX_HTTP_FLV_META_BUFFER_EXCESS);
return NGX_DECLINED;
}
static ngx_int_t
ngx_http_flv_parse_metadata(ngx_http_flv_file_t *flv)
{
static u_char *filepositions_pos;
static u_char *times_pos;
static ngx_flv_amf_elt_t in_keyframes[] = {
{ NGX_FLV_AMF_ARRAY | NGX_FLV_AMF_CONTEXT,
ngx_string("filepositions"),
&filepositions_pos, 0 },
{ NGX_FLV_AMF_ARRAY | NGX_FLV_AMF_CONTEXT,