11
11
#include <ngx_rtmp_codec_module.h>
12
12
#include "ngx_rbuf.h"
13
13
#include "ngx_hls_live_module.h"
14
- #include "ngx_mpegts_mux_module.h"
14
+ #include "ngx_mpegts_live_module.h"
15
+ #include "ngx_rtmp_live_module.h"
15
16
#include "ngx_mpegts_gop_module.h"
16
17
17
18
static ngx_rtmp_play_pt next_play ;
@@ -253,7 +254,7 @@ ngx_hls_live_write_playlist(ngx_rtmp_session_t *s, ngx_buf_t *out)
253
254
254
255
for (i = 0 ; i < ctx -> nfrags ; i ++ ) {
255
256
frag = ngx_hls_live_get_frag (s , i );
256
- if (frag -> duration > max_frag ) {
257
+ if (frag && frag -> duration > max_frag ) {
257
258
max_frag = (ngx_uint_t ) (frag -> duration + .5 );
258
259
}
259
260
}
@@ -410,10 +411,11 @@ ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag)
410
411
while (cl ) {
411
412
* ll = ngx_get_chainbuf (0 , 0 );
412
413
* ((* ll )-> buf ) = * (cl -> buf );
413
- (* ll )-> buf -> flush = 1 ;
414
+ (* ll )-> buf -> memory = 1 ;
414
415
415
416
if (frag -> content_pos == frag -> content_last && cl -> next == NULL ) {
416
417
(* ll )-> buf -> last_in_chain = 1 ;
418
+ (* ll )-> buf -> flush = 1 ;
417
419
}
418
420
419
421
ll = & ((* ll )-> next );
@@ -433,17 +435,15 @@ ngx_hls_live_fetch_session(ngx_str_t *server,
433
435
ngx_str_t * stream , ngx_str_t * session )
434
436
{
435
437
ngx_live_stream_t * live_stream ;
436
- ngx_rtmp_core_ctx_t * lctx ;
437
438
ngx_hls_live_ctx_t * ctx ;
438
439
439
440
live_stream = ngx_live_fetch_stream (server , stream );
440
441
if (live_stream ) {
441
- for (lctx = live_stream -> hls_play_ctx ; lctx ; lctx = lctx -> next ) {
442
- ctx = ngx_rtmp_get_module_ctx (lctx -> session , ngx_hls_live_module );
442
+ for (ctx = live_stream -> hls_ctx ; ctx ; ctx = ctx -> next ) {
443
443
if (session -> len == ctx -> sid .len &&
444
444
!ngx_strncmp (ctx -> sid .data , session -> data , session -> len ))
445
445
{
446
- return lctx -> session ;
446
+ return ctx -> session ;
447
447
}
448
448
}
449
449
}
@@ -476,7 +476,7 @@ ngx_hls_live_close_fragment(ngx_rtmp_session_t *s)
476
476
return NGX_OK ;
477
477
}
478
478
479
- ngx_log_error (NGX_LOG_DEBUG , s -> log , 0 ,
479
+ ngx_log_error (NGX_LOG_INFO , s -> log , 0 ,
480
480
"hls: close fragment id=%uL" , ctx -> nfrag );
481
481
482
482
ctx -> opened = 0 ;
@@ -538,7 +538,7 @@ ngx_hls_live_create_frag(ngx_rtmp_session_t *s) {
538
538
sizeof (ngx_mpegts_frame_t * ) * s -> out_queue );
539
539
}
540
540
541
- ngx_log_error (NGX_LOG_DEBUG , s -> log , 0 ,
541
+ ngx_log_error (NGX_LOG_INFO , s -> log , 0 ,
542
542
"hls-live: create_frag| create frag[%p]" , frag );
543
543
544
544
return frag ;
@@ -566,7 +566,7 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
566
566
567
567
id = ngx_hls_live_get_fragment_id (s , ts );
568
568
569
- ngx_log_error (NGX_LOG_DEBUG , s -> log , 0 ,
569
+ ngx_log_error (NGX_LOG_INFO , s -> log , 0 ,
570
570
"hls: open_fragment| create frag[%uL] timestamp %uL" , id , ts );
571
571
572
572
ffrag = & (ctx -> frags [id % (hacf -> winfrags * 2 + 1 )]);
@@ -628,47 +628,96 @@ ngx_hls_live_timeout(ngx_event_t *ev)
628
628
629
629
630
630
static ngx_int_t
631
- ngx_hls_live_play (ngx_rtmp_session_t * s , ngx_rtmp_play_t * v )
631
+ ngx_hls_live_join (ngx_rtmp_session_t * s , u_char * name , unsigned publisher )
632
632
{
633
- ngx_hls_live_app_conf_t * hacf ;
634
- ngx_hls_live_ctx_t * ctx ;
633
+ ngx_hls_live_ctx_t * ctx ;
634
+ ngx_live_stream_t * st ;
635
+ ngx_rtmp_live_app_conf_t * lacf ;
636
+ ngx_hls_live_app_conf_t * hacf ;
635
637
636
638
hacf = ngx_rtmp_get_module_app_conf (s , ngx_hls_live_module );
637
- if (hacf == NULL || !hacf -> hls || s -> live_type != NGX_HLS_LIVE ) {
638
- goto next ;
639
- }
640
639
641
- if (s -> interprocess ) {
642
- goto next ;
640
+ lacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_live_module );
641
+ if (lacf == NULL ) {
642
+ return NGX_ERROR ;
643
643
}
644
644
645
645
ctx = ngx_rtmp_get_module_ctx (s , ngx_hls_live_module );
646
- if (ctx ) {
647
- goto next ;
646
+ if (ctx && ctx -> stream ) {
647
+ ngx_log_debug0 (NGX_LOG_DEBUG_RTMP , s -> log , 0 ,
648
+ "mpegts-live: join| already joined" );
649
+ return NGX_ERROR ;
648
650
}
649
651
650
- ctx = ngx_pcalloc (s -> pool , sizeof (ngx_hls_live_ctx_t ));
651
- ngx_rtmp_set_ctx (s , ctx , ngx_hls_live_module );
652
-
653
- ctx -> sid .len = ngx_strlen (v -> session );
654
- ctx -> sid .data = ngx_pcalloc (s -> pool , ctx -> sid .len );
655
- ngx_memcpy (ctx -> sid .data , v -> session , ctx -> sid .len );
652
+ if (ctx == NULL ) {
653
+ ctx = ngx_pcalloc (s -> pool , sizeof (ngx_hls_live_ctx_t ));
654
+ ngx_rtmp_set_ctx (s , ctx , ngx_hls_live_module );
656
655
657
- if (ctx -> frags == NULL ) {
658
656
ctx -> frags = ngx_pcalloc (s -> pool ,
659
657
sizeof (ngx_hls_live_frag_t * ) * (hacf -> winfrags * 2 + 1 ));
660
658
if (ctx -> frags == NULL ) {
661
659
return NGX_ERROR ;
662
660
}
663
661
}
664
662
663
+ ctx -> session = s ;
664
+
665
+ ngx_log_error (NGX_LOG_INFO , s -> log , 0 ,
666
+ "mpegts-live: join| join '%s'" , name );
667
+
668
+ st = s -> live_stream ;
669
+
670
+ if (!(publisher || st -> publish_ctx || lacf -> idle_streams )) {
671
+ ngx_log_error (NGX_LOG_ERR , s -> log , 0 ,
672
+ "mpegts-live: join| stream not found" );
673
+
674
+ s -> status = 404 ;
675
+
676
+ ngx_rtmp_finalize_session (s );
677
+
678
+ return NGX_ERROR ;
679
+ }
680
+
681
+ ctx -> stream = st ;
682
+ ctx -> next = st -> hls_ctx ;
683
+
684
+ st -> hls_ctx = ctx ;
685
+
665
686
ctx -> ev .data = s ;
666
687
ctx -> ev .handler = ngx_hls_live_timeout ;
667
688
ctx -> ev .log = s -> log ;
668
689
ctx -> timeout = hacf -> timeout ;
669
690
670
691
ngx_add_timer (& ctx -> ev , (ctx -> timeout + 3000 ) / 3 );
671
692
693
+ return NGX_OK ;
694
+ }
695
+
696
+ static ngx_int_t
697
+ ngx_hls_live_play (ngx_rtmp_session_t * s , ngx_rtmp_play_t * v )
698
+ {
699
+ ngx_hls_live_app_conf_t * hacf ;
700
+ ngx_hls_live_ctx_t * ctx ;
701
+
702
+ hacf = ngx_rtmp_get_module_app_conf (s , ngx_hls_live_module );
703
+ if (hacf == NULL || !hacf -> hls || s -> live_type != NGX_HLS_LIVE ) {
704
+ goto next ;
705
+ }
706
+
707
+ if (s -> interprocess || s -> live_type != NGX_HLS_LIVE ) {
708
+ goto next ;
709
+ }
710
+
711
+ if (ngx_hls_live_join (s , v -> name , 0 ) == NGX_ERROR ) {
712
+ return NGX_ERROR ;
713
+ }
714
+
715
+ ctx = ngx_rtmp_get_module_ctx (s , ngx_hls_live_module );
716
+
717
+ ctx -> sid .len = ngx_strlen (v -> session );
718
+ ctx -> sid .data = ngx_pcalloc (s -> pool , ctx -> sid .len );
719
+ ngx_memcpy (ctx -> sid .data , v -> session , ctx -> sid .len );
720
+
672
721
next :
673
722
return next_play (s , v );
674
723
}
@@ -678,7 +727,7 @@ static ngx_int_t
678
727
ngx_hls_live_close_stream (ngx_rtmp_session_t * s , ngx_rtmp_close_stream_t * v )
679
728
{
680
729
ngx_hls_live_app_conf_t * hacf ;
681
- ngx_hls_live_ctx_t * ctx ;
730
+ ngx_hls_live_ctx_t * ctx , * * cctx ;
682
731
ngx_uint_t i ;
683
732
ngx_hls_live_frag_t * frag ;
684
733
@@ -708,6 +757,18 @@ ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
708
757
}
709
758
}
710
759
760
+ for (cctx = & ctx -> stream -> hls_ctx ; * cctx ; cctx = & (* cctx )-> next ) {
761
+ if (* cctx == ctx ) {
762
+ * cctx = ctx -> next ;
763
+ break ;
764
+ }
765
+ }
766
+
767
+ if (ctx -> stream -> hls_ctx ) {
768
+ ctx -> stream = NULL ;
769
+ goto next ;
770
+ }
771
+
711
772
next :
712
773
return next_close_stream (s , v );
713
774
}
@@ -858,7 +919,7 @@ static ngx_int_t
858
919
ngx_hls_live_av (ngx_rtmp_session_t * s , ngx_mpegts_frame_t * frame )
859
920
{
860
921
ngx_live_stream_t * live_stream ;
861
- ngx_rtmp_core_ctx_t * live_ctx ;
922
+ ngx_hls_live_ctx_t * ctx ;
862
923
ngx_rtmp_session_t * ss ;
863
924
ngx_rtmp_codec_ctx_t * codec_ctx ;
864
925
ngx_hls_live_app_conf_t * hacf ;
@@ -871,10 +932,8 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
871
932
frame -> dts /90 , frame -> type , frame -> key );
872
933
873
934
live_stream = s -> live_stream ;
874
- for (live_ctx = live_stream -> hls_play_ctx ; live_ctx ;
875
- live_ctx = live_ctx -> next )
876
- {
877
- ss = live_ctx -> session ;
935
+ for (ctx = live_stream -> hls_ctx ; ctx ; ctx = ctx -> next ) {
936
+ ss = ctx -> session ;
878
937
879
938
switch (ngx_mpegts_gop_link (s , ss , hacf -> playlen , hacf -> playlen )) {
880
939
case NGX_DECLINED :
0 commit comments