@@ -22,6 +22,7 @@ static ngx_rtmp_close_stream_pt next_close_stream;
22
22
#define ngx_hls_live_prev (s , pos ) (pos == 0 ? s->out_queue - 1 : pos - 1)
23
23
24
24
static ngx_int_t ngx_hls_live_postconfiguration (ngx_conf_t * cf );
25
+ static void * ngx_hls_live_create_main_conf (ngx_conf_t * cf );
25
26
static void * ngx_hls_live_create_app_conf (ngx_conf_t * cf );
26
27
static char * ngx_hls_live_merge_app_conf (ngx_conf_t * cf ,
27
28
void * parent , void * child );
@@ -46,11 +47,16 @@ typedef struct {
46
47
ngx_flag_t cleanup ;
47
48
ngx_array_t * variant ;
48
49
ngx_str_t base_url ;
49
- ngx_hls_live_frag_t * free_frag ;
50
50
ngx_pool_t * pool ;
51
51
ngx_msec_t timeout ;
52
52
} ngx_hls_live_app_conf_t ;
53
53
54
+ typedef struct {
55
+ ngx_hls_live_frag_t * free_frag ;
56
+ ngx_pool_t * pool ;
57
+ } ngx_hls_live_main_conf_t ;
58
+
59
+ ngx_hls_live_main_conf_t * ngx_hls_live_main_conf = NULL ;
54
60
55
61
#define NGX_RTMP_HLS_NAMING_SEQUENTIAL 1
56
62
#define NGX_RTMP_HLS_NAMING_TIMESTAMP 2
@@ -173,7 +179,7 @@ static ngx_rtmp_module_t ngx_hls_live_module_ctx = {
173
179
NULL , /* preconfiguration */
174
180
ngx_hls_live_postconfiguration , /* postconfiguration */
175
181
176
- NULL , /* create main configuration */
182
+ ngx_hls_live_create_main_conf , /* create main configuration */
177
183
NULL , /* init main configuration */
178
184
179
185
NULL , /* create server configuration */
@@ -374,19 +380,13 @@ ngx_hls_live_prepare_out_chain(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag,
374
380
while (i < nframes && frag -> content_pos != frag -> content_last ) {
375
381
frame = frag -> content [frag -> content_pos ];
376
382
377
- cl = frame -> chain ;
378
- while (cl ) {
383
+ for (cl = frame -> chain ; cl ; cl = cl -> next ) {
379
384
* ll = ngx_get_chainbuf (0 , 0 );
380
- * ((* ll )-> buf ) = * (cl -> buf );
385
+ (* ll )-> buf -> pos = cl -> buf -> pos ;
386
+ (* ll )-> buf -> last = cl -> buf -> last ;
381
387
(* ll )-> buf -> flush = 1 ;
382
- (* ll )-> buf -> memory = 1 ;
383
388
384
- if (frag -> content_pos == frag -> content_last && cl -> next == NULL ) {
385
- (* ll )-> buf -> last_in_chain = 1 ;
386
- }
387
-
388
- ll = & ((* ll )-> next );
389
- cl = cl -> next ;
389
+ ll = & (* ll )-> next ;
390
390
}
391
391
392
392
* ll = NULL ;
@@ -407,19 +407,13 @@ ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag)
407
407
while (frag -> content_pos != frag -> content_last ) {
408
408
frame = frag -> content [frag -> content_pos ];
409
409
410
- cl = frame -> chain ;
411
- while (cl ) {
410
+ for (cl = frame -> chain ; cl ; cl = cl -> next ) {
412
411
* ll = ngx_get_chainbuf (0 , 0 );
413
- * ((* ll )-> buf ) = * (cl -> buf );
414
- (* ll )-> buf -> memory = 1 ;
415
-
416
- if (frag -> content_pos == frag -> content_last && cl -> next == NULL ) {
417
- (* ll )-> buf -> last_in_chain = 1 ;
418
- (* ll )-> buf -> flush = 1 ;
419
- }
412
+ (* ll )-> buf -> pos = cl -> buf -> pos ;
413
+ (* ll )-> buf -> last = cl -> buf -> last ;
414
+ (* ll )-> buf -> flush = 1 ;
420
415
421
- ll = & ((* ll )-> next );
422
- cl = cl -> next ;
416
+ ll = & (* ll )-> next ;
423
417
}
424
418
425
419
* ll = NULL ;
@@ -494,47 +488,46 @@ ngx_hls_live_close_fragment(ngx_rtmp_session_t *s)
494
488
495
489
496
490
void
497
- ngx_hls_live_free_frag (ngx_rtmp_session_t * s , ngx_hls_live_frag_t * frag )
491
+ ngx_hls_live_free_frag (ngx_hls_live_frag_t * frag )
498
492
{
499
493
ngx_mpegts_frame_t * frame ;
500
494
ngx_uint_t i ;
501
- ngx_hls_live_app_conf_t * hacf ;
502
-
503
- hacf = ngx_rtmp_get_module_app_conf (s , ngx_hls_live_module );
504
495
505
496
frag -> ref -- ;
506
497
507
- ngx_log_error (NGX_LOG_DEBUG , s -> log , 0 ,
508
- "hls-live: free_frag| frag[%p] ref %ui" , frag , frag -> ref );
509
-
510
498
if (frag -> ref > 0 ) {
511
499
return ;
512
500
}
513
501
502
+ ngx_log_error (NGX_LOG_DEBUG , ngx_cycle -> log , 0 ,
503
+ "hls-live: free_frag| frag[%p] ref %ui" , frag , frag -> ref );
504
+
514
505
for (i = 0 ; i < frag -> content_last ; ++ i ) {
515
506
frame = frag -> content [i ];
516
507
if (frame ) {
517
508
ngx_rtmp_shared_free_mpegts_frame (frame );
518
509
}
519
510
}
520
511
521
- frag -> next = hacf -> free_frag ;
522
- hacf -> free_frag = frag ;
512
+ frag -> next = ngx_hls_live_main_conf -> free_frag ;
513
+ ngx_hls_live_main_conf -> free_frag = frag ;
523
514
}
524
515
525
516
526
517
static ngx_hls_live_frag_t *
527
518
ngx_hls_live_create_frag (ngx_rtmp_session_t * s ) {
528
519
ngx_hls_live_frag_t * frag ;
529
- ngx_hls_live_app_conf_t * hacf ;
530
520
531
- hacf = ngx_rtmp_get_module_app_conf (s , ngx_hls_live_module );
521
+ if (ngx_hls_live_main_conf -> free_frag ) {
522
+ frag = ngx_hls_live_main_conf -> free_frag ;
523
+ ngx_hls_live_main_conf -> free_frag =
524
+ ngx_hls_live_main_conf -> free_frag -> next ;
532
525
533
- if (hacf -> free_frag ) {
534
- frag = hacf -> free_frag ;
535
- hacf -> free_frag = hacf -> free_frag -> next ;
526
+ ngx_memzero (frag , sizeof (ngx_hls_live_frag_t ) +
527
+ sizeof (ngx_mpegts_frame_t * ) * s -> out_queue );
536
528
} else {
537
- frag = ngx_pcalloc (hacf -> pool , sizeof (ngx_hls_live_frag_t ) +
529
+ frag = ngx_pcalloc (ngx_hls_live_main_conf -> pool ,
530
+ sizeof (ngx_hls_live_frag_t ) +
538
531
sizeof (ngx_mpegts_frame_t * ) * s -> out_queue );
539
532
}
540
533
@@ -571,7 +564,7 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
571
564
572
565
ffrag = & (ctx -> frags [id % (hacf -> winfrags * 2 + 1 )]);
573
566
if (* ffrag ) {
574
- ngx_hls_live_free_frag (s , * ffrag );
567
+ ngx_hls_live_free_frag (* ffrag );
575
568
}
576
569
* ffrag = ngx_hls_live_create_frag (s );
577
570
@@ -587,7 +580,6 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
587
580
ctx -> opened = 1 ;
588
581
ctx -> frag_ts = ts ;
589
582
590
-
591
583
ngx_memzero (& patpmt , sizeof (patpmt ));
592
584
patpmt .buf = ngx_create_temp_buf (s -> pool , 376 );
593
585
patpmt .buf -> last = ngx_cpymem (patpmt .buf -> pos ,
@@ -623,7 +615,7 @@ ngx_hls_live_timeout(ngx_event_t *ev)
623
615
return ;
624
616
}
625
617
626
- ngx_add_timer (ev , (ctx -> timeout + 3000 ) / 3 );
618
+ ngx_add_timer (ev , (ctx -> timeout + 3000 ));
627
619
}
628
620
629
621
@@ -688,7 +680,7 @@ ngx_hls_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
688
680
ctx -> ev .log = s -> log ;
689
681
ctx -> timeout = hacf -> timeout ;
690
682
691
- ngx_add_timer (& ctx -> ev , (ctx -> timeout + 3000 ) / 3 );
683
+ ngx_add_timer (& ctx -> ev , (ctx -> timeout + 3000 ));
692
684
693
685
return NGX_OK ;
694
686
}
@@ -753,7 +745,7 @@ ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
753
745
for (i = 0 ; i < 2 * hacf -> winfrags + 1 ; i ++ ) {
754
746
frag = ctx -> frags [i % (hacf -> winfrags * 2 + 1 )];
755
747
if (frag ) {
756
- ngx_hls_live_free_frag (s , frag );
748
+ ngx_hls_live_free_frag (frag );
757
749
}
758
750
}
759
751
@@ -866,6 +858,9 @@ ngx_hls_live_update(ngx_rtmp_session_t *s, ngx_rtmp_codec_ctx_t *codec_ctx)
866
858
ngx_hls_live_ctx_t * ctx ;
867
859
ngx_mpegts_frame_t * frame ;
868
860
ngx_int_t boundary ;
861
+ ngx_buf_t * b ;
862
+
863
+ b = NULL ;
869
864
870
865
ctx = ngx_rtmp_get_module_ctx (s , ngx_hls_live_module );
871
866
@@ -888,8 +883,10 @@ ngx_hls_live_update(ngx_rtmp_session_t *s, ngx_rtmp_codec_ctx_t *codec_ctx)
888
883
if (frame -> type == NGX_MPEGTS_MSG_AUDIO ) {
889
884
boundary = codec_ctx -> avc_header == NULL ;
890
885
} else if (frame -> type == NGX_MPEGTS_MSG_VIDEO ) {
886
+ b = ctx -> aframe ;
891
887
boundary = frame -> key &&
892
- (codec_ctx -> aac_header == NULL || !ctx -> opened );
888
+ (codec_ctx -> aac_header == NULL || !ctx -> opened ||
889
+ (b && b -> last > b -> pos ));
893
890
} else {
894
891
return NGX_ERROR ;
895
892
}
@@ -935,7 +932,7 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
935
932
for (ctx = live_stream -> hls_ctx ; ctx ; ctx = ctx -> next ) {
936
933
ss = ctx -> session ;
937
934
938
- switch (ngx_mpegts_gop_link (s , ss , hacf -> playlen , hacf -> playlen )) {
935
+ switch (ngx_mpegts_gop_link (s , ss , 0 , hacf -> playlen )) {
939
936
case NGX_DECLINED :
940
937
continue ;
941
938
case NGX_ERROR :
@@ -957,6 +954,21 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
957
954
return NGX_ERROR ;
958
955
}
959
956
957
+ static void *
958
+ ngx_hls_live_create_main_conf (ngx_conf_t * cf )
959
+ {
960
+ ngx_hls_live_main_conf_t * conf ;
961
+
962
+ conf = ngx_pcalloc (cf -> pool , sizeof (ngx_hls_live_main_conf_t ));
963
+ if (conf == NULL ) {
964
+ return NULL ;
965
+ }
966
+
967
+ conf -> pool = ngx_create_pool (1024 , ngx_cycle -> log );
968
+ ngx_hls_live_main_conf = conf ;
969
+
970
+ return conf ;
971
+ }
960
972
961
973
static void *
962
974
ngx_hls_live_create_app_conf (ngx_conf_t * cf )
0 commit comments