-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
ngx_http_flv_live_module.c
2542 lines (1953 loc) · 67.9 KB
/
ngx_http_flv_live_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) Winshining
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "ngx_http_flv_live_module.h"
#include "ngx_rtmp_bandwidth.h"
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_close_stream_pt next_close_stream;
ngx_rtmp_play_pt http_flv_live_next_play;
ngx_rtmp_close_stream_pt http_flv_live_next_close_stream;
static ngx_int_t ngx_http_flv_live_init(ngx_conf_t *cf);
static void *ngx_http_flv_live_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_flv_live_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_flv_live_handler(ngx_http_request_t *r);
static void ngx_http_flv_live_cleanup(void *data);
static ngx_int_t ngx_http_flv_live_init_process(ngx_cycle_t *cycle);
static void ngx_http_flv_live_send_tail(ngx_rtmp_session_t *s);
static ngx_int_t ngx_http_flv_live_send_message(ngx_rtmp_session_t *s,
ngx_chain_t *out, ngx_uint_t priority);
static ngx_chain_t *ngx_http_flv_live_meta_message(ngx_rtmp_session_t *,
ngx_chain_t *in);
static ngx_chain_t *ngx_http_flv_live_append_message(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_rtmp_header_t *lh, ngx_chain_t *in);
static void ngx_http_flv_live_free_message(ngx_rtmp_session_t *s,
ngx_chain_t *in);
static ngx_int_t ngx_http_flv_live_join(ngx_rtmp_session_t *s, u_char *name,
unsigned int publisher);
static ngx_chain_t *ngx_http_flv_live_append_shared_bufs(
ngx_rtmp_core_srv_conf_t *cscf, ngx_rtmp_header_t *h, ngx_chain_t *in,
ngx_flag_t chunked);
static void ngx_http_flv_live_close_http_request(ngx_rtmp_session_t *s);
static ngx_int_t ngx_http_flv_live_headers_filter(ngx_rtmp_session_t *s);
static ngx_int_t ngx_http_flv_live_header_filter(ngx_rtmp_session_t *s);
#if (nginx_version <= 1003014)
static void ngx_http_do_free_request(ngx_http_request_t *r, ngx_int_t rc);
static void ngx_http_do_log_request(ngx_http_request_t *r);
#endif
typedef struct ngx_http_header_val_s ngx_http_header_val_t;
typedef ngx_int_t (*ngx_http_set_header_pt)(ngx_http_request_t *r,
ngx_http_header_val_t *hv, ngx_str_t *value);
typedef struct {
ngx_str_t name;
ngx_uint_t offset;
ngx_http_set_header_pt handler;
} ngx_http_set_header_t;
struct ngx_http_header_val_s {
ngx_http_complex_value_t value;
ngx_str_t key;
ngx_http_set_header_pt handler;
ngx_uint_t offset;
#if (nginx_version >= 1007005)
ngx_uint_t always; /* unsigned always:1 */
#endif
};
typedef enum {
NGX_HTTP_EXPIRES_OFF,
} ngx_http_expires_t;
typedef struct {
ngx_http_expires_t expires;
time_t expires_time;
#if (nginx_version >= 1007009)
ngx_http_complex_value_t *expires_value;
#endif
ngx_array_t *headers;
} ngx_http_headers_conf_t;
extern ngx_module_t ngx_http_headers_filter_module;
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
#if (nginx_version >= 1011010)
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
#endif
static ngx_str_t ngx_http_status_lines[] = {
ngx_string("200 OK"),
ngx_null_string, /* "201 Created" */
ngx_null_string, /* "202 Accepted" */
ngx_null_string, /* "203 Non-Authoritative Information" */
ngx_null_string, /* "204 No Content" */
ngx_null_string, /* "205 Reset Content" */
ngx_null_string, /* "206 Partial Content" */
/* ngx_null_string, */ /* "207 Multi-Status" */
#define NGX_HTTP_LAST_2XX 207
#define NGX_HTTP_OFF_3XX (NGX_HTTP_LAST_2XX - 200)
/* ngx_null_string, */ /* "300 Multiple Choices" */
ngx_string("301 Moved Permanently"),
ngx_string("302 Moved Temporarily"),
ngx_null_string, /* "303 See Other" */
ngx_null_string, /* "304 Not Modified" */
ngx_null_string, /* "305 Use Proxy" */
ngx_null_string, /* "306 unused" */
ngx_string("307 Temporary Redirect"),
#define NGX_HTTP_LAST_3XX 308
#define NGX_HTTP_OFF_4XX (NGX_HTTP_LAST_3XX - 301 + NGX_HTTP_OFF_3XX)
ngx_string("400 Bad Request"),
ngx_null_string, /* "401 Unauthorized" */
ngx_null_string, /* "402 Payment Required" */
ngx_string("403 Forbidden"),
ngx_string("404 Not Found"),
ngx_string("405 Not Allowed"),
ngx_null_string, /* "406 Not Acceptable" */
ngx_null_string, /* "407 Proxy Authentication Required" */
ngx_null_string, /* "408 Request Time-out" */
ngx_null_string, /* "409 Conflict" */
ngx_null_string, /* "410 Gone" */
ngx_null_string, /* "411 Length Required" */
ngx_null_string, /* "412 Precondition Failed" */
ngx_null_string, /* "413 Request Entity Too Large" */
ngx_null_string, /* "414 Request-URI Too Large" */
ngx_null_string, /* "415 Unsupported Media Type" */
ngx_null_string, /* "416 Requested Range Not Satisfiable" */
ngx_null_string, /* "417 Expectation Failed" */
ngx_null_string, /* "418 unused" */
ngx_null_string, /* "419 unused" */
ngx_null_string, /* "420 unused" */
ngx_null_string, /* "421 Misdirected Request" */
/* ngx_null_string, */ /* "422 Unprocessable Entity" */
/* ngx_null_string, */ /* "423 Locked" */
/* ngx_null_string, */ /* "424 Failed Dependency" */
#define NGX_HTTP_LAST_4XX 422
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string("500 Internal Server Error"),
ngx_null_string, /* "501 Not Implemented" */
ngx_null_string, /* "502 Bad Gateway" */
ngx_string("503 Service Temporarily Unavailable"),
ngx_null_string, /* "504 Gateway Time-out" */
ngx_null_string, /* "505 HTTP Version Not Supported" */
ngx_null_string, /* "506 Variant Also Negotiates" */
ngx_null_string, /* "507 Insufficient Storage" */
/* ngx_null_string, */ /* "508 unused" */
/* ngx_null_string, */ /* "509 unused" */
/* ngx_null_string, */ /* "510 Not Extended" */
#define NGX_HTTP_LAST_5XX 508
};
extern ngx_rtmp_live_proc_handler_t ngx_rtmp_live_proc_handler;
static ngx_rtmp_live_proc_handler_t ngx_http_flv_live_proc_handler = {
NULL,
NULL,
NULL,
NULL,
ngx_http_flv_live_send_message,
ngx_http_flv_live_meta_message,
ngx_http_flv_live_append_message,
ngx_http_flv_live_free_message
};
ngx_rtmp_live_proc_handler_t *ngx_rtmp_live_proc_handlers[] = {
&ngx_rtmp_live_proc_handler,
&ngx_http_flv_live_proc_handler
};
static ngx_int_t ngx_http_flv_live_init_handlers(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_flv_live_request(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in);
static void ngx_http_flv_live_free_request(ngx_rtmp_session_t *s);
static void ngx_http_flv_live_read_handler(ngx_event_t *rev);
static void ngx_http_flv_live_write_handler(ngx_event_t *wev);
static ngx_int_t ngx_http_flv_live_send(ngx_rtmp_session_t *s);
static void ngx_http_flv_live_correct_timestamp(ngx_rtmp_session_t *s,
ngx_flag_t correct);
static ngx_int_t ngx_http_flv_live_preprocess(ngx_http_request_t *r,
ngx_rtmp_connection_t *rconn);
static ngx_rtmp_session_t *ngx_http_flv_live_init_connection(
ngx_http_request_t *r, ngx_rtmp_connection_t *rconn);
static ngx_rtmp_session_t *ngx_http_flv_live_init_session(
ngx_http_request_t *r, ngx_rtmp_addr_conf_t *add_conf);
static ngx_int_t ngx_http_flv_live_connect_init(ngx_rtmp_session_t *s,
ngx_str_t *app, ngx_str_t *stream);
static ngx_http_module_t ngx_http_flv_live_module_ctx = {
NULL,
ngx_http_flv_live_init, /* postconfiguration */
NULL,
NULL,
NULL,
NULL,
ngx_http_flv_live_create_loc_conf, /* create location configuration */
ngx_http_flv_live_merge_loc_conf /* merge location configuration */
};
static ngx_command_t ngx_http_flv_live_commands[] = {
{ ngx_string("flv_live"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_flv_live_conf_t, flv_live),
NULL },
ngx_null_command
};
ngx_module_t ngx_http_flv_live_module = {
NGX_MODULE_V1,
&ngx_http_flv_live_module_ctx,
ngx_http_flv_live_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
ngx_http_flv_live_init_process,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_flv_live_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
/* insert in the NGX_HTTP_CONTENT_PHASE */
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_flv_live_handler;
return NGX_OK;
}
static void *
ngx_http_flv_live_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_flv_live_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_flv_live_conf_t));
if (conf == NULL) {
return NULL;
}
conf->flv_live = NGX_CONF_UNSET;
return (void *) conf;
}
static char *
ngx_http_flv_live_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child)
{
ngx_http_flv_live_conf_t *prev = parent;
ngx_http_flv_live_conf_t *conf = child;
ngx_conf_merge_value(conf->flv_live, prev->flv_live, 0);
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_flv_live_init_handlers(ngx_cycle_t *cycle)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
cmcf = ngx_rtmp_cycle_get_module_main_conf(cycle, ngx_rtmp_core_module);
if (cmcf == NULL) {
return NGX_OK;
}
/* rtmp live conf aready exsits, so add additional event handlers */
h = ngx_array_push(&cmcf->events[NGX_HTTP_FLV_LIVE_REQUEST]);
*h = ngx_http_flv_live_request;
next_play = http_flv_live_next_play;
next_close_stream = http_flv_live_next_close_stream;
http_flv_live_next_play = NULL;
http_flv_live_next_close_stream = NULL;
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_live_init_process(ngx_cycle_t *cycle)
{
return ngx_http_flv_live_init_handlers(cycle);
}
/*
* chunk format:
* hex1\r\n
* content1(hex1)\r\n
* hex2\r\n
* content2(hex2)\r\n
* ...
* 0\r\n\r\n
*/
ngx_int_t
ngx_http_flv_live_send_header(ngx_rtmp_session_t *s)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_http_core_loc_conf_t *clcf;
ngx_http_request_t *r;
ngx_rtmp_live_ctx_t *live_ctx;
ngx_rtmp_codec_ctx_t *codec_ctx;
ngx_list_part_t *part;
ngx_table_elt_t *e, *header;
u_char *p;
ngx_chain_t cl_flv_hdr, *pkt;
ngx_buf_t buf_flv_hdr;
ngx_uint_t i;
ngx_str_t chunked_flv_header;
ngx_str_t consec_flv_header;
u_char chunked_flv_header_data[18];
ngx_flag_t connection_header;
/**
* |F|L|V|ver|00000101|header_size|0|0|0|0|, ngx_http_flv_module.c
* for more details, please refer to http://www.adobe.com/devnet/f4v.html
**/
u_char flv_header[] = "FLV\x1\0\0\0\0\x9\0\0\0\0";
r = s->data;
r->headers_out.status = NGX_HTTP_OK;
ngx_str_set(&r->headers_out.content_type, "video/x-flv");
/* fill HTTP header 'Connection' according to headers_in */
r->keepalive = 0;
connection_header = 0;
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
if (ngx_strcasecmp(header[i].key.data, (u_char *) "connection") == 0) {
connection_header = 1;
if (ngx_strcasecmp(header[i].value.data, (u_char *) "keep-alive")
== 0)
{
r->keepalive = 1;
}
break;
}
}
if (!connection_header && r->http_version == NGX_HTTP_VERSION_11) {
r->keepalive = 1;
}
live_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (live_ctx && !live_ctx->active) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"flv live: try to send header when session not active");
return NGX_ERROR;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
codec_ctx = ngx_rtmp_get_module_ctx(s->publisher, ngx_rtmp_codec_module);
if (codec_ctx->video_codec_id != 0) {
flv_header[4] |= 0x1;
}
if (codec_ctx->audio_codec_id != 0
&& codec_ctx->audio_codec_id != NGX_RTMP_AUDIO_UNCOMPRESSED)
{
flv_header[4] |= (0x1 << 2);
}
if (clcf->chunked_transfer_encoding &&
r->http_version == NGX_HTTP_VERSION_11)
{
r->chunked = 1;
p = chunked_flv_header_data;
*p++ = 'd';
*p++ = CR;
*p++ = LF;
ngx_memmove(p, flv_header, 13);
p += 13;
*p++ = CR;
*p++ = LF;
chunked_flv_header.data = chunked_flv_header_data;
chunked_flv_header.len = 18;
buf_flv_hdr.pos = chunked_flv_header.data;
buf_flv_hdr.last = chunked_flv_header.data + chunked_flv_header.len;
} else {
consec_flv_header.data = flv_header;
consec_flv_header.len = 13;
buf_flv_hdr.pos = consec_flv_header.data;
buf_flv_hdr.last = consec_flv_header.data + consec_flv_header.len;
}
e = r->headers_out.expires;
if (e == NULL) {
e = ngx_list_push(&r->headers_out.headers);
if (e == NULL) {
return NGX_ERROR;
}
r->headers_out.expires = e;
e->hash = 1;
ngx_str_set(&e->key, "Expires");
}
e->value.data = (u_char *) "-1";
e->value.len = ngx_strlen("-1");
if (ngx_http_flv_live_headers_filter(s) == NGX_ERROR) {
return NGX_ERROR;
}
buf_flv_hdr.start = buf_flv_hdr.pos;
buf_flv_hdr.end = buf_flv_hdr.last;
cl_flv_hdr.buf = &buf_flv_hdr;
cl_flv_hdr.next = NULL;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
pkt = ngx_rtmp_append_shared_bufs(cscf, NULL, &cl_flv_hdr);
if (pkt == NULL) {
return NGX_ERROR;
}
ngx_http_flv_live_send_message(s, pkt, 0);
ngx_rtmp_free_shared_chain(cscf, pkt);
return NGX_OK;
}
/**
* for adding non-standard HTTP headers
**/
static ngx_int_t
ngx_http_flv_live_headers_filter(ngx_rtmp_session_t *s)
{
ngx_str_t value;
ngx_uint_t i;
#if (nginx_version >= 1007005)
ngx_uint_t safe_status;
#endif
ngx_http_header_val_t *h;
ngx_http_headers_conf_t *conf;
ngx_http_request_t *r;
r = s->data;
conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_filter_module);
/* force */
conf->expires = NGX_HTTP_EXPIRES_OFF;
if (conf->headers == NULL) {
return ngx_http_flv_live_header_filter(s);
}
#if (nginx_version >= 1007005)
switch (r->headers_out.status) {
case NGX_HTTP_OK:
case NGX_HTTP_CREATED:
case NGX_HTTP_NO_CONTENT:
case NGX_HTTP_PARTIAL_CONTENT:
case NGX_HTTP_MOVED_PERMANENTLY:
case NGX_HTTP_MOVED_TEMPORARILY:
case NGX_HTTP_SEE_OTHER:
case NGX_HTTP_NOT_MODIFIED:
case NGX_HTTP_TEMPORARY_REDIRECT:
safe_status = 1;
break;
default:
safe_status = 0;
}
#endif
if (conf->headers) {
h = conf->headers->elts;
for (i = 0; i < conf->headers->nelts; i++) {
#if (nginx_version >= 1007005)
if (!safe_status && !h[i].always) {
continue;
}
#endif
if (ngx_http_complex_value(r, &h[i].value, &value) != NGX_OK) {
return NGX_ERROR;
}
if (h[i].handler(r, &h[i], &value) != NGX_OK) {
return NGX_ERROR;
}
}
}
return ngx_http_flv_live_header_filter(s);
}
static ngx_int_t
ngx_http_flv_live_header_filter(ngx_rtmp_session_t *s)
{
u_char *p;
size_t len;
ngx_str_t *status_line;
ngx_buf_t *b;
ngx_uint_t status, i;
ngx_chain_t out, *pkt;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_http_core_loc_conf_t *clcf;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_http_request_t *r;
r = s->data;
if (r->header_sent) {
return NGX_OK;
}
r->header_sent = 1;
if (r->chunked && r->http_version < NGX_HTTP_VERSION_11) {
ngx_log_error(NGX_LOG_WARN, s->connection->log, 0,
"flv live: chunked only supported by HTTP/1.1");
r->chunked = 0;
}
len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
/* the end of the header */
+ sizeof(CRLF) - 1;
/* status line */
if (r->headers_out.status_line.len) {
len += r->headers_out.status_line.len;
status_line = &r->headers_out.status_line;
#if (NGX_SUPPRESS_WARN)
status = 0;
#endif
} else {
status = r->headers_out.status;
if (status >= NGX_HTTP_OK
&& status < NGX_HTTP_LAST_2XX)
{
/* 2XX */
status -= NGX_HTTP_OK;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_MOVED_PERMANENTLY
&& status < NGX_HTTP_LAST_3XX)
{
/* 3XX */
status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_BAD_REQUEST
&& status < NGX_HTTP_LAST_4XX)
{
/* 4XX */
status = status - NGX_HTTP_BAD_REQUEST
+ NGX_HTTP_OFF_4XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR
&& status < NGX_HTTP_LAST_5XX)
{
/* 5XX */
status = status - NGX_HTTP_INTERNAL_SERVER_ERROR
+ NGX_HTTP_OFF_5XX;
status_line = &ngx_http_status_lines[status];
len += ngx_http_status_lines[status].len;
} else {
len += NGX_INT_T_LEN + 1 /* SP */;
status_line = NULL;
}
if (status_line && status_line->len == 0) {
status = r->headers_out.status;
len += NGX_INT_T_LEN + 1 /* SP */;
status_line = NULL;
}
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->headers_out.server == NULL) {
#if (nginx_version >= 1011010)
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
len += sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
len += sizeof(ngx_http_server_build_string) - 1;
} else {
len += sizeof(ngx_http_server_string) - 1;
}
#else
len += clcf->server_tokens ? sizeof(ngx_http_server_full_string) - 1 :
sizeof(ngx_http_server_string) - 1;
#endif
}
if (r->headers_out.date == NULL) {
len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
}
if (r->headers_out.content_type.len) {
len += sizeof("Content-Type: ") - 1
+ r->headers_out.content_type.len + 2;
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
}
}
if (r->headers_out.content_length == NULL
&& r->headers_out.content_length_n >= 0)
{
len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;
}
if (r->headers_out.last_modified == NULL
&& r->headers_out.last_modified_time != -1)
{
len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;
}
if (r->chunked) {
len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;
}
if (r->keepalive) {
len += sizeof("Connection: keep-alive" CRLF) - 1;
/*
* MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
* MSIE keeps the connection alive for about 60-65 seconds.
* Opera keeps the connection alive very long.
* Mozilla keeps the connection alive for N plus about 1-10 seconds.
* Konqueror keeps the connection alive for about N seconds.
*/
if (clcf->keepalive_header) {
len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;
}
} else {
len += sizeof("Connection: close" CRLF) - 1;
}
part = &r->headers_out.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len
+ sizeof(CRLF) - 1;
}
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_ERROR;
}
/* "HTTP/1.x " */
if (r->http_version == NGX_HTTP_VERSION_10) {
b->last = ngx_cpymem(b->last, "HTTP/1.0 ", sizeof("HTTP/1.x ") - 1);
} else {
b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);
}
/* status line */
if (status_line) {
b->last = ngx_copy(b->last, status_line->data, status_line->len);
} else {
b->last = ngx_sprintf(b->last, "%03ui ", status);
}
*b->last++ = CR; *b->last++ = LF;
if (r->headers_out.server == NULL) {
#if (nginx_version >= 1011010)
if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
p = ngx_http_server_full_string;
len = sizeof(ngx_http_server_full_string) - 1;
} else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
p = ngx_http_server_build_string;
len = sizeof(ngx_http_server_build_string) - 1;
} else {
#else
if (clcf->server_tokens) {
p = (u_char *) ngx_http_server_full_string;
len = sizeof(ngx_http_server_full_string) - 1;
} else {
#endif
p = ngx_http_server_string;
len = sizeof(ngx_http_server_string) - 1;
}
b->last = ngx_cpymem(b->last, p, len);
}
if (r->headers_out.date == NULL) {
b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);
b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,
ngx_cached_http_time.len);
*b->last++ = CR; *b->last++ = LF;
}
if (r->headers_out.content_type.len) {
b->last = ngx_cpymem(b->last, "Content-Type: ",
sizeof("Content-Type: ") - 1);
p = b->last;
b->last = ngx_copy(b->last, r->headers_out.content_type.data,
r->headers_out.content_type.len);
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
b->last = ngx_cpymem(b->last, "; charset=",
sizeof("; charset=") - 1);
b->last = ngx_copy(b->last, r->headers_out.charset.data,
r->headers_out.charset.len);
/* update r->headers_out.content_type for possible logging */
r->headers_out.content_type.len = b->last - p;
r->headers_out.content_type.data = p;
}
*b->last++ = CR; *b->last++ = LF;
}
if (r->chunked) {
b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,
sizeof("Transfer-Encoding: chunked" CRLF) - 1);
}
if (r->keepalive) {
b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,
sizeof("Connection: keep-alive" CRLF) - 1);
if (clcf->keepalive_header) {
b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,
clcf->keepalive_header);
}
} else {
b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
sizeof("Connection: close" CRLF) - 1);
}
part = &r->headers_out.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
*b->last++ = ':'; *b->last++ = ' ';
b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
*b->last++ = CR; *b->last++ = LF;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"%*s", (size_t) (b->last - b->pos), b->pos);
/* the end of HTTP header */
*b->last++ = CR;
*b->last++ = LF;
r->header_size = b->last - b->pos;
out.buf = b;
out.next = NULL;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
pkt = ngx_rtmp_append_shared_bufs(cscf, NULL, &out);
if (pkt == NULL) {
return NGX_ERROR;
}
ngx_http_flv_live_send_message(s, pkt, 0);
ngx_rtmp_free_shared_chain(cscf, pkt);
return NGX_OK;
}
static void
ngx_http_flv_live_send_tail(ngx_rtmp_session_t *s)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_chain_t cl_resp_hdr, *pkt;
ngx_buf_t buf_resp_hdr;
const ngx_str_t response_tail = ngx_string("0" CRLF CRLF);
buf_resp_hdr.pos = response_tail.data;
buf_resp_hdr.last = response_tail.data + response_tail.len;
buf_resp_hdr.start = buf_resp_hdr.pos;
buf_resp_hdr.end = buf_resp_hdr.last;
cl_resp_hdr.buf = &buf_resp_hdr;
cl_resp_hdr.next = NULL;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
pkt = ngx_rtmp_append_shared_bufs(cscf, NULL, &cl_resp_hdr);
ngx_http_flv_live_send_message(s, pkt, 0);
ngx_rtmp_free_shared_chain(cscf, pkt);
}
static ngx_int_t
ngx_http_flv_live_send_message(ngx_rtmp_session_t *s,
ngx_chain_t *out, ngx_uint_t priority)
{
ngx_uint_t nmsg;
nmsg = (s->out_last + s->out_queue - s->out_pos) % s->out_queue + 1;
if (priority > 3) {
priority = 3;
}
/* drop packet?
* Note we always leave 1 slot free */
if (nmsg + priority * s->out_queue / 4 >= s->out_queue) {
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"flv live: HTTP drop message bufs=%ui, priority=%ui",
nmsg, priority);
return NGX_AGAIN;
}
s->out[s->out_last++] = out;
s->out_last %= s->out_queue;
ngx_rtmp_acquire_shared_chain(out);
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"flv live: HTTP send nmsg=%ui, priority=%ui #%ui",
nmsg, priority, s->out_last);
if (priority && s->out_buffer && nmsg < s->out_cork) {
return NGX_OK;
}
if (!s->connection->write->active) {
ngx_http_flv_live_write_handler(s->connection->write);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_flv_live_request(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
static ngx_rtmp_play_t v;
ngx_int_t rc;
ngx_http_request_t *r;
ngx_http_flv_live_ctx_t *ctx;
r = s->data;
ctx = ngx_http_get_module_ctx(r, ngx_http_flv_live_module);