Skip to content

Commit bd64e5a

Browse files
committed
detach http(hls) from rtmp-session
1 parent b3a9d5b commit bd64e5a

16 files changed

+182
-149
lines changed

http/ngx_http_flv_live_module.c

+5
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ ngx_http_flv_live_handler(ngx_http_request_t *r)
520520
return NGX_HTTP_INTERNAL_SERVER_ERROR;
521521
}
522522
s->connection = r->connection;
523+
s->number = r->connection->number;
524+
s->remote_addr_text.data = ngx_pcalloc(s->pool, r->connection->addr_text.len);
525+
s->remote_addr_text.len = r->connection->addr_text.len;
526+
ngx_memcpy(s->remote_addr_text.data,
527+
r->connection->addr_text.data, r->connection->addr_text.len);
523528
ngx_rtmp_set_combined_log(s, r->connection->log->data,
524529
r->connection->log->handler);
525530
s->log->connection = r->connection->number;

http/ngx_http_ts_live_module.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,14 @@ ngx_http_ts_live_handler(ngx_http_request_t *r)
403403
return NGX_HTTP_INTERNAL_SERVER_ERROR;
404404
}
405405
s->connection = r->connection;
406+
s->log->connection = c->number;
407+
s->number = c->number;
408+
s->remote_addr_text.data = ngx_pcalloc(s->pool, r->connection->addr_text.len);
409+
s->remote_addr_text.len = r->connection->addr_text.len;
410+
ngx_memcpy(s->remote_addr_text.data,
411+
r->connection->addr_text.data, r->connection->addr_text.len);
406412
ngx_rtmp_set_combined_log(s, r->connection->log->data,
407413
r->connection->log->handler);
408-
s->log->connection = r->connection->number;
409414
ctx->session = s;
410415

411416
/* get host, app, stream name */

mpegts/ngx_hls_http_module.c

+17-7
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ ngx_hls_http_cleanup(void *data)
402402
r = data;
403403
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
404404

405-
if (!ctx || !ctx->session) {
405+
if (!ctx) {
406406
return;
407407
}
408408
/*
@@ -414,16 +414,19 @@ ngx_hls_http_cleanup(void *data)
414414
}
415415
ctx->out_chain = NULL;
416416
*/
417-
ctx->session->request = NULL;
418-
ctx->session->connection = NULL;
417+
418+
if (ctx->session) {
419+
ctx->session->request = NULL;
420+
ctx->session->connection = NULL;
421+
}
419422

420423
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
421424
"hls-http: cleanup_handler| http cleanup");
422425

423426
if (ctx->frag) {
424427
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
425428
"hls-http: cleanup_handler| free frag[%p]", ctx->frag);
426-
ngx_hls_live_free_frag(ctx->session, ctx->frag);
429+
ngx_hls_live_free_frag(ctx->frag);
427430
ctx->frag = NULL;
428431
}
429432
}
@@ -550,6 +553,8 @@ ngx_hls_http_create_session(ngx_http_request_t *r)
550553
}
551554
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
552555

556+
s->log->connection = r->connection->number;
557+
s->number = r->connection->number;
553558
s->live_type = NGX_HLS_LIVE;
554559
s->live_server = ngx_live_create_server(&s->serverid);
555560
s->remote_addr_text.data = ngx_pcalloc(s->pool, r->connection->addr_text.len);
@@ -586,13 +591,13 @@ ngx_hls_http_create_session(ngx_http_request_t *r)
586591

587592
s->stage = NGX_LIVE_PLAY;
588593
s->ptime = ngx_current_msec;
589-
s->connection = r->connection;
594+
// s->connection = r->connection;
590595

591596
if (ngx_rtmp_play_filter(s, &v) != NGX_OK) {
592597
return NULL;
593598
}
594599

595-
// ngx_add_timer(r->connection->write, s->timeout);
600+
ngx_add_timer(r->connection->write, s->timeout);
596601

597602
return s;
598603
}
@@ -631,7 +636,8 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
631636
}
632637
s->log->connection = r->connection->number;
633638
s->number = r->connection->number;
634-
s->sockaddr = r->connection->sockaddr;
639+
s->sockaddr = ngx_pcalloc(s->pool, sizeof(struct sockaddr));
640+
ngx_memcpy(s->sockaddr, r->connection->sockaddr, sizeof(struct sockaddr));
635641
}
636642

637643
s->request = r;
@@ -1009,6 +1015,10 @@ ngx_hls_http_close(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
10091015
return NGX_OK;
10101016
}
10111017

1018+
s->request = NULL;
1019+
s->connection = NULL;
1020+
ctx->session = NULL;
1021+
10121022
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
10131023
"hls-http: close| finalize http request");
10141024

mpegts/ngx_hls_live_module.c

+57-45
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static ngx_rtmp_close_stream_pt next_close_stream;
2222
#define ngx_hls_live_prev(s, pos) (pos == 0 ? s->out_queue - 1 : pos - 1)
2323

2424
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);
2526
static void * ngx_hls_live_create_app_conf(ngx_conf_t *cf);
2627
static char * ngx_hls_live_merge_app_conf(ngx_conf_t *cf,
2728
void *parent, void *child);
@@ -46,11 +47,16 @@ typedef struct {
4647
ngx_flag_t cleanup;
4748
ngx_array_t *variant;
4849
ngx_str_t base_url;
49-
ngx_hls_live_frag_t *free_frag;
5050
ngx_pool_t *pool;
5151
ngx_msec_t timeout;
5252
} ngx_hls_live_app_conf_t;
5353

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;
5460

5561
#define NGX_RTMP_HLS_NAMING_SEQUENTIAL 1
5662
#define NGX_RTMP_HLS_NAMING_TIMESTAMP 2
@@ -173,7 +179,7 @@ static ngx_rtmp_module_t ngx_hls_live_module_ctx = {
173179
NULL, /* preconfiguration */
174180
ngx_hls_live_postconfiguration, /* postconfiguration */
175181

176-
NULL, /* create main configuration */
182+
ngx_hls_live_create_main_conf, /* create main configuration */
177183
NULL, /* init main configuration */
178184

179185
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,
374380
while (i < nframes && frag->content_pos != frag->content_last) {
375381
frame = frag->content[frag->content_pos];
376382

377-
cl = frame->chain;
378-
while (cl) {
383+
for (cl = frame->chain; cl; cl = cl->next) {
379384
*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;
381387
(*ll)->buf->flush = 1;
382-
(*ll)->buf->memory = 1;
383388

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;
390390
}
391391

392392
*ll = NULL;
@@ -407,19 +407,13 @@ ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag)
407407
while (frag->content_pos != frag->content_last) {
408408
frame = frag->content[frag->content_pos];
409409

410-
cl = frame->chain;
411-
while (cl) {
410+
for (cl = frame->chain; cl; cl = cl->next) {
412411
*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;
420415

421-
ll = &((*ll)->next);
422-
cl = cl->next;
416+
ll = &(*ll)->next;
423417
}
424418

425419
*ll = NULL;
@@ -494,47 +488,46 @@ ngx_hls_live_close_fragment(ngx_rtmp_session_t *s)
494488

495489

496490
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)
498492
{
499493
ngx_mpegts_frame_t *frame;
500494
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);
504495

505496
frag->ref--;
506497

507-
ngx_log_error(NGX_LOG_DEBUG, s->log, 0,
508-
"hls-live: free_frag| frag[%p] ref %ui", frag, frag->ref);
509-
510498
if (frag->ref > 0) {
511499
return;
512500
}
513501

502+
ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0,
503+
"hls-live: free_frag| frag[%p] ref %ui", frag, frag->ref);
504+
514505
for (i = 0; i < frag->content_last; ++i) {
515506
frame = frag->content[i];
516507
if (frame) {
517508
ngx_rtmp_shared_free_mpegts_frame(frame);
518509
}
519510
}
520511

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;
523514
}
524515

525516

526517
static ngx_hls_live_frag_t*
527518
ngx_hls_live_create_frag(ngx_rtmp_session_t *s) {
528519
ngx_hls_live_frag_t *frag;
529-
ngx_hls_live_app_conf_t *hacf;
530520

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;
532525

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);
536528
} 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) +
538531
sizeof(ngx_mpegts_frame_t*) * s->out_queue);
539532
}
540533

@@ -571,7 +564,7 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
571564

572565
ffrag = &(ctx->frags[id % (hacf->winfrags * 2 + 1)]);
573566
if (*ffrag) {
574-
ngx_hls_live_free_frag(s, *ffrag);
567+
ngx_hls_live_free_frag(*ffrag);
575568
}
576569
*ffrag = ngx_hls_live_create_frag(s);
577570

@@ -587,7 +580,6 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
587580
ctx->opened = 1;
588581
ctx->frag_ts = ts;
589582

590-
591583
ngx_memzero(&patpmt, sizeof(patpmt));
592584
patpmt.buf = ngx_create_temp_buf(s->pool, 376);
593585
patpmt.buf->last = ngx_cpymem(patpmt.buf->pos,
@@ -623,7 +615,7 @@ ngx_hls_live_timeout(ngx_event_t *ev)
623615
return;
624616
}
625617

626-
ngx_add_timer(ev, (ctx->timeout + 3000) / 3);
618+
ngx_add_timer(ev, (ctx->timeout + 3000));
627619
}
628620

629621

@@ -688,7 +680,7 @@ ngx_hls_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
688680
ctx->ev.log = s->log;
689681
ctx->timeout = hacf->timeout;
690682

691-
ngx_add_timer(&ctx->ev, (ctx->timeout + 3000) / 3);
683+
ngx_add_timer(&ctx->ev, (ctx->timeout + 3000));
692684

693685
return NGX_OK;
694686
}
@@ -753,7 +745,7 @@ ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
753745
for (i = 0; i < 2 * hacf->winfrags + 1; i++) {
754746
frag = ctx->frags[i % (hacf->winfrags * 2 + 1)];
755747
if (frag) {
756-
ngx_hls_live_free_frag(s, frag);
748+
ngx_hls_live_free_frag(frag);
757749
}
758750
}
759751

@@ -866,6 +858,9 @@ ngx_hls_live_update(ngx_rtmp_session_t *s, ngx_rtmp_codec_ctx_t *codec_ctx)
866858
ngx_hls_live_ctx_t *ctx;
867859
ngx_mpegts_frame_t *frame;
868860
ngx_int_t boundary;
861+
ngx_buf_t *b;
862+
863+
b = NULL;
869864

870865
ctx = ngx_rtmp_get_module_ctx(s, ngx_hls_live_module);
871866

@@ -888,8 +883,10 @@ ngx_hls_live_update(ngx_rtmp_session_t *s, ngx_rtmp_codec_ctx_t *codec_ctx)
888883
if (frame->type == NGX_MPEGTS_MSG_AUDIO) {
889884
boundary = codec_ctx->avc_header == NULL;
890885
} else if (frame->type == NGX_MPEGTS_MSG_VIDEO) {
886+
b = ctx->aframe;
891887
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));
893890
} else {
894891
return NGX_ERROR;
895892
}
@@ -935,7 +932,7 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
935932
for (ctx = live_stream->hls_ctx; ctx; ctx = ctx->next) {
936933
ss = ctx->session;
937934

938-
switch (ngx_mpegts_gop_link(s, ss, hacf->playlen, hacf->playlen)) {
935+
switch (ngx_mpegts_gop_link(s, ss, 0, hacf->playlen)) {
939936
case NGX_DECLINED:
940937
continue;
941938
case NGX_ERROR:
@@ -957,6 +954,21 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
957954
return NGX_ERROR;
958955
}
959956

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+
}
960972

961973
static void *
962974
ngx_hls_live_create_app_conf(ngx_conf_t *cf)

mpegts/ngx_hls_live_module.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ ngx_hls_live_frag_t* ngx_hls_live_find_frag(ngx_rtmp_session_t *s,
8080
ngx_str_t *name);
8181
ngx_chain_t* ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s,
8282
ngx_hls_live_frag_t *frag);
83-
void ngx_hls_live_free_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag);
83+
void ngx_hls_live_free_frag(ngx_hls_live_frag_t *frag);
8484
ngx_rtmp_session_t* ngx_hls_live_fetch_session(ngx_str_t *server,
8585
ngx_str_t *stream, ngx_str_t *session);
8686
void ngx_rtmp_shared_acquire_frag(ngx_hls_live_frag_t *frag);

0 commit comments

Comments
 (0)