@@ -259,6 +259,11 @@ ngx_hls_http_send_header(ngx_http_request_t *r, ngx_uint_t status, ngx_keyval_t
259
259
r -> headers_out .status = status ;
260
260
r -> keepalive = 0 ; /* set Connection to closed */
261
261
262
+ //set eTag
263
+ if (ngx_http_set_etag (r ) != NGX_OK ) {
264
+ return NGX_HTTP_INTERNAL_SERVER_ERROR ;
265
+ }
266
+
262
267
while (h && h -> key .len ) {
263
268
rc = ngx_http_set_header_out (r , & h -> key , & h -> value );
264
269
if (rc != NGX_OK ) {
@@ -332,7 +337,7 @@ ngx_hls_http_redirect_handler(ngx_http_request_t *r,
332
337
r -> headers_out .content_length_n = 0 ;
333
338
r -> header_only = 1 ;
334
339
335
- rc = ngx_hls_http_send_header (r , NGX_HTTP_MOVED_TEMPORARILY , ngx_m3u8_headers );
340
+ rc = ngx_hls_http_send_header (r , NGX_HTTP_MOVED_TEMPORARILY , NULL );
336
341
if (rc != NGX_OK ) {
337
342
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
338
343
"hls-http: redirect_handler| "
@@ -397,23 +402,22 @@ ngx_hls_http_cleanup(void *data)
397
402
{
398
403
ngx_http_request_t * r ;
399
404
ngx_hls_http_ctx_t * ctx ;
400
- // ngx_chain_t *cl;
405
+ ngx_chain_t * cl ;
401
406
402
407
r = data ;
403
408
ctx = ngx_http_get_module_ctx (r , ngx_hls_http_module );
404
409
405
410
if (!ctx ) {
406
411
return ;
407
412
}
408
- /*
413
+
409
414
cl = ctx -> out_chain ;
410
415
while (cl ) {
411
416
ctx -> out_chain = cl -> next ;
412
417
ngx_put_chainbuf (cl );
413
418
cl = ctx -> out_chain ;
414
419
}
415
420
ctx -> out_chain = NULL ;
416
- */
417
421
418
422
if (ctx -> session ) {
419
423
ctx -> session -> request = NULL ;
@@ -619,8 +623,6 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
619
623
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
620
624
}
621
625
622
- buf = ngx_create_temp_buf (r -> connection -> pool , 1024 * 512 );
623
-
624
626
s = ngx_hls_live_fetch_session (& ctx -> serverid , & ctx -> stream , & ctx -> sid );
625
627
if (s == NULL ) {
626
628
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
@@ -634,8 +636,6 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
634
636
635
637
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
636
638
}
637
- s -> log -> connection = r -> connection -> number ;
638
- s -> number = r -> connection -> number ;
639
639
s -> sockaddr = ngx_pcalloc (s -> pool , sizeof (struct sockaddr ));
640
640
ngx_memcpy (s -> sockaddr , r -> connection -> sockaddr , sizeof (struct sockaddr ));
641
641
}
@@ -644,27 +644,33 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
644
644
645
645
ctx -> session = s ;
646
646
647
- out = ngx_pcalloc (s -> pool , sizeof (ngx_chain_t ));
648
- out -> buf = buf ;
647
+ if (!ctx -> m3u8 ) {
648
+ ctx -> m3u8 = ngx_pcalloc (s -> pool , sizeof (ngx_chain_t ));
649
+ ctx -> m3u8 -> buf = ngx_create_temp_buf (r -> connection -> pool , 1024 * 512 );
650
+ }
649
651
652
+ out = ctx -> m3u8 ;
653
+ buf = out -> buf ;
650
654
buf -> last = buf -> pos = buf -> start ;
651
655
buf -> memory = 1 ;
652
656
buf -> flush = 1 ;
653
- buf -> last_in_chain = 1 ;
654
- buf -> last_buf = 1 ;
657
+ // buf->last_in_chain = 1;
658
+ // buf->last_buf = 1;
655
659
656
- rc = ngx_hls_live_write_playlist (s , buf );
660
+ rc = ngx_hls_live_write_playlist (s , buf , & r -> headers_out . last_modified_time );
657
661
if (rc != NGX_OK ) {
658
662
goto again ;
659
663
}
660
664
661
665
r -> headers_out .content_length_n = buf -> last - buf -> pos ;
662
666
663
- rc = ngx_hls_http_send_header (r , NGX_HTTP_OK , ngx_m3u8_headers );
664
- if (rc != NGX_OK ) {
665
- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
666
- "hls-http: m3u8_handler| send http header failed" );
667
- return rc ;
667
+ if (!r -> header_sent ) {
668
+ rc = ngx_hls_http_send_header (r , NGX_HTTP_OK , ngx_m3u8_headers );
669
+ if (rc != NGX_OK ) {
670
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
671
+ "hls-http: m3u8_handler| send http header failed" );
672
+ return rc ;
673
+ }
668
674
}
669
675
670
676
rc = ngx_http_output_filter (r , out );
@@ -769,7 +775,7 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
769
775
}
770
776
771
777
if (ctx -> out_chain == NULL ) {
772
- ctx -> out_chain = ngx_hls_live_prepare_out_chain (s , ctx -> frag , 1 );
778
+ ctx -> out_chain = ngx_hls_live_prepare_out_chain (s , ctx -> frag , 4 );
773
779
}
774
780
775
781
rc = NGX_OK ;
@@ -818,7 +824,7 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
818
824
break ;
819
825
}
820
826
821
- ctx -> out_chain = ngx_hls_live_prepare_out_chain (s , ctx -> frag , 1 );
827
+ ctx -> out_chain = ngx_hls_live_prepare_out_chain (s , ctx -> frag , 4 );
822
828
}
823
829
824
830
if (wev -> active ) {
@@ -871,6 +877,7 @@ ngx_hls_http_ts_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf)
871
877
ctx -> frag = frag ;
872
878
873
879
r -> headers_out .content_length_n = frag -> length ;
880
+ r -> headers_out .last_modified_time = frag -> last_modified_time ;
874
881
rc = ngx_hls_http_send_header (r , NGX_HTTP_OK , ngx_ts_headers );
875
882
if (rc != NGX_OK ) {
876
883
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
@@ -880,7 +887,7 @@ ngx_hls_http_ts_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf)
880
887
881
888
ngx_rtmp_shared_acquire_frag (frag );
882
889
883
- if (1 ) {
890
+ if (0 ) {
884
891
r -> count ++ ;
885
892
r -> write_event_handler = ngx_hls_http_write_handler ;
886
893
@@ -904,6 +911,7 @@ ngx_hls_http_handler(ngx_http_request_t *r)
904
911
ngx_int_t rc ;
905
912
ngx_http_cleanup_t * cln ;
906
913
ngx_str_t sstr ;
914
+ ngx_hls_http_ctx_t * ctx ;
907
915
908
916
hlcf = ngx_http_get_module_loc_conf (r , ngx_hls_http_module );
909
917
@@ -919,14 +927,16 @@ ngx_hls_http_handler(ngx_http_request_t *r)
919
927
return NGX_DECLINED ;
920
928
}
921
929
922
- cln = ngx_http_cleanup_add (r , 0 );
923
- if (cln == NULL ) {
924
-
925
- return NGX_HTTP_INTERNAL_SERVER_ERROR ;
930
+ ctx = ngx_http_get_module_ctx (r , ngx_hls_http_module );
931
+ if (!ctx ) {
932
+ cln = ngx_http_cleanup_add (r , 0 );
933
+ if (cln == NULL ) {
934
+ return NGX_HTTP_INTERNAL_SERVER_ERROR ;
935
+ }
936
+ cln -> handler = ngx_hls_http_cleanup ;
937
+ cln -> data = r ;
938
+ r -> read_event_handler = ngx_http_test_reading ;
926
939
}
927
- cln -> handler = ngx_hls_http_cleanup ;
928
- cln -> data = r ;
929
- r -> read_event_handler = ngx_http_test_reading ;
930
940
931
941
if (!ngx_strncmp (r -> uri .data + r -> uri .len - 5 , ".m3u8" , 5 )) {
932
942
@@ -963,22 +973,19 @@ ngx_hls_http_m3u8(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
963
973
ctx = ngx_http_get_module_ctx (r , ngx_hls_http_module );
964
974
965
975
if (!ctx -> m3u8 ) {
966
- ctx -> m3u8 = ngx_get_chainbuf (1024 * 512 , 1 );
976
+ ctx -> m3u8 = ngx_pcalloc (s -> pool , sizeof (ngx_chain_t ));
977
+ ctx -> m3u8 -> buf = ngx_create_temp_buf (r -> connection -> pool , 1024 * 512 );
967
978
}
968
979
969
980
out = ctx -> m3u8 ;
970
-
971
- out -> next = NULL ;
972
-
973
981
buf = out -> buf ;
974
-
975
982
buf -> last = buf -> pos = buf -> start ;
976
983
buf -> memory = 1 ;
977
984
buf -> flush = 1 ;
978
- buf -> last_in_chain = 1 ;
979
- buf -> last_buf = 1 ;
985
+ // buf->last_in_chain = 1;
986
+ // buf->last_buf = 1;
980
987
981
- ngx_hls_live_write_playlist (s , buf );
988
+ ngx_hls_live_write_playlist (s , buf , & r -> headers_out . last_modified_time );
982
989
983
990
r -> headers_out .content_length_n = buf -> last - buf -> pos ;
984
991
@@ -996,7 +1003,7 @@ ngx_hls_http_m3u8(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
996
1003
return rc ;
997
1004
}
998
1005
999
- return rc ;
1006
+ return NGX_OK ;
1000
1007
}
1001
1008
1002
1009
static ngx_int_t
0 commit comments