Skip to content

Commit b3a9d5b

Browse files
committed
http-ts
1 parent 2f1f259 commit b3a9d5b

13 files changed

+1026
-239
lines changed

config

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ RTMP_CORE_MODULES=" \
2525
ngx_rtmp_shared_module \
2626
ngx_rtmp_gop_module \
2727
ngx_rtmp_monitor_module \
28-
ngx_mpegts_mux_module \
28+
ngx_mpegts_live_module \
2929
ngx_mpegts_gop_module \
3030
ngx_hls_live_module \
3131
"
@@ -37,6 +37,7 @@ RTMP_HTTP_MODULES=" \
3737
ngx_rtmp_control_module \
3838
ngx_http_flv_live_module \
3939
ngx_hls_http_module \
40+
ngx_mpegts_http_module \
4041
"
4142

4243

@@ -63,7 +64,7 @@ RTMP_DEPS=" \
6364
$ngx_addon_dir/ngx_live_record.h \
6465
$ngx_addon_dir/ngx_rtmp_dynamic.h \
6566
$ngx_addon_dir/ngx_rtmp_variables.h \
66-
$ngx_addon_dir/mpegts/ngx_mpegts_mux_module.h \
67+
$ngx_addon_dir/mpegts/ngx_mpegts_live_module.h \
6768
$ngx_addon_dir/mpegts/ngx_hls_live_module.h \
6869
$ngx_addon_dir/mpegts/ngx_mpegts_gop_module.h \
6970
"
@@ -108,7 +109,7 @@ RTMP_CORE_SRCS=" \
108109
$ngx_addon_dir/ngx_rtmp_monitor_module.c \
109110
$ngx_addon_dir/ngx_rtmp_dynamic.c \
110111
$ngx_addon_dir/ngx_rtmp_variables.c \
111-
$ngx_addon_dir/mpegts/ngx_mpegts_mux_module.c \
112+
$ngx_addon_dir/mpegts/ngx_mpegts_live_module.c \
112113
$ngx_addon_dir/mpegts/ngx_hls_live_module.c \
113114
$ngx_addon_dir/mpegts/ngx_mpegts_gop_module.c \
114115
"
@@ -121,6 +122,7 @@ RTMP_HTTP_SRCS=" \
121122
$ngx_addon_dir/http/ngx_http_flv_live_module.c \
122123
$ngx_addon_dir/http/ngx_http_set_header.c \
123124
$ngx_addon_dir/mpegts/ngx_hls_http_module.c \
125+
$ngx_addon_dir/mpegts/ngx_mpegts_http_module.c \
124126
"
125127

126128
if [ -f auto/module ] ; then

mpegts/ngx_hls_http_module.c

+11-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <math.h>
1313
#include <ngx_rtmp_cmd_module.h>
1414
#include "ngx_http_set_header.h"
15-
#include "ngx_mpegts_mux_module.h"
15+
#include "ngx_mpegts_live_module.h"
1616
#include "ngx_hls_live_module.h"
1717
#include "ngx_rbuf.h"
1818
#include "ngx_rtmp_dynamic.h"
@@ -397,23 +397,23 @@ ngx_hls_http_cleanup(void *data)
397397
{
398398
ngx_http_request_t *r;
399399
ngx_hls_http_ctx_t *ctx;
400-
ngx_chain_t *cl;
400+
// ngx_chain_t *cl;
401401

402402
r = data;
403403
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
404404

405405
if (!ctx || !ctx->session) {
406406
return;
407407
}
408-
408+
/*
409409
cl = ctx->out_chain;
410410
while (cl) {
411411
ctx->out_chain = cl->next;
412412
ngx_put_chainbuf(cl);
413413
cl = ctx->out_chain;
414414
}
415415
ctx->out_chain = NULL;
416-
416+
*/
417417
ctx->session->request = NULL;
418418
ctx->session->connection = NULL;
419419

@@ -592,7 +592,7 @@ ngx_hls_http_create_session(ngx_http_request_t *r)
592592
return NULL;
593593
}
594594

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

597597
return s;
598598
}
@@ -604,7 +604,7 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
604604
ngx_hls_http_ctx_t *ctx;
605605
ngx_int_t rc;
606606
ngx_rtmp_session_t *s;
607-
ngx_chain_t out;
607+
ngx_chain_t *out;
608608
ngx_buf_t *buf;
609609

610610
ctx = ngx_hls_http_create_ctx(r, addr_conf);
@@ -638,8 +638,8 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
638638

639639
ctx->session = s;
640640

641-
out.buf = buf;
642-
out.next = NULL;
641+
out = ngx_pcalloc(s->pool, sizeof(ngx_chain_t));
642+
out->buf = buf;
643643

644644
buf->last = buf->pos = buf->start;
645645
buf->memory = 1;
@@ -661,7 +661,7 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
661661
return rc;
662662
}
663663

664-
rc = ngx_http_output_filter(r, &out);
664+
rc = ngx_http_output_filter(r, out);
665665
if (rc != NGX_OK) {
666666
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
667667
"hls-http: m3u8_handler| send http content failed");
@@ -734,7 +734,6 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
734734
size_t present, sent;
735735
ngx_int_t rc;
736736
ngx_chain_t *cl;
737-
ngx_hls_live_frag_t *frag;
738737

739738
wev = r->connection->write; //wev->handler = ngx_http_request_handler;
740739

@@ -744,7 +743,6 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
744743

745744
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
746745
s = ctx->session;
747-
frag = ctx->frag;
748746

749747
if (wev->timedout) {
750748
ngx_log_error(NGX_LOG_INFO, s->log, NGX_ETIMEDOUT,
@@ -876,7 +874,7 @@ ngx_hls_http_ts_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf)
876874

877875
ngx_rtmp_shared_acquire_frag(frag);
878876

879-
if (0) {
877+
if (1) {
880878
r->count++;
881879
r->write_event_handler = ngx_hls_http_write_handler;
882880

@@ -1027,7 +1025,7 @@ ngx_http_hls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
10271025
ngx_hls_http_loc_conf_t *hlcf;
10281026
ngx_str_t *value;
10291027
ngx_uint_t n;
1030-
1028+
10311029
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
10321030
clcf->handler = ngx_hls_http_handler;
10331031

mpegts/ngx_hls_live_module.c

+92-33
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include <ngx_rtmp_codec_module.h>
1212
#include "ngx_rbuf.h"
1313
#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"
1516
#include "ngx_mpegts_gop_module.h"
1617

1718
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)
253254

254255
for (i = 0; i < ctx->nfrags; i++) {
255256
frag = ngx_hls_live_get_frag(s, i);
256-
if (frag->duration > max_frag) {
257+
if (frag && frag->duration > max_frag) {
257258
max_frag = (ngx_uint_t) (frag->duration + .5);
258259
}
259260
}
@@ -410,10 +411,11 @@ ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag)
410411
while (cl) {
411412
*ll = ngx_get_chainbuf(0, 0);
412413
*((*ll)->buf) = *(cl->buf);
413-
(*ll)->buf->flush = 1;
414+
(*ll)->buf->memory = 1;
414415

415416
if (frag->content_pos == frag->content_last && cl->next == NULL) {
416417
(*ll)->buf->last_in_chain = 1;
418+
(*ll)->buf->flush = 1;
417419
}
418420

419421
ll = &((*ll)->next);
@@ -433,17 +435,15 @@ ngx_hls_live_fetch_session(ngx_str_t *server,
433435
ngx_str_t *stream, ngx_str_t *session)
434436
{
435437
ngx_live_stream_t *live_stream;
436-
ngx_rtmp_core_ctx_t *lctx;
437438
ngx_hls_live_ctx_t *ctx;
438439

439440
live_stream = ngx_live_fetch_stream(server, stream);
440441
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) {
443443
if (session->len == ctx->sid.len &&
444444
!ngx_strncmp(ctx->sid.data, session->data, session->len))
445445
{
446-
return lctx->session;
446+
return ctx->session;
447447
}
448448
}
449449
}
@@ -476,7 +476,7 @@ ngx_hls_live_close_fragment(ngx_rtmp_session_t *s)
476476
return NGX_OK;
477477
}
478478

479-
ngx_log_error(NGX_LOG_DEBUG, s->log, 0,
479+
ngx_log_error(NGX_LOG_INFO, s->log, 0,
480480
"hls: close fragment id=%uL", ctx->nfrag);
481481

482482
ctx->opened = 0;
@@ -538,7 +538,7 @@ ngx_hls_live_create_frag(ngx_rtmp_session_t *s) {
538538
sizeof(ngx_mpegts_frame_t*) * s->out_queue);
539539
}
540540

541-
ngx_log_error(NGX_LOG_DEBUG, s->log, 0,
541+
ngx_log_error(NGX_LOG_INFO, s->log, 0,
542542
"hls-live: create_frag| create frag[%p]", frag);
543543

544544
return frag;
@@ -566,7 +566,7 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
566566

567567
id = ngx_hls_live_get_fragment_id(s, ts);
568568

569-
ngx_log_error(NGX_LOG_DEBUG, s->log, 0,
569+
ngx_log_error(NGX_LOG_INFO, s->log, 0,
570570
"hls: open_fragment| create frag[%uL] timestamp %uL", id, ts);
571571

572572
ffrag = &(ctx->frags[id % (hacf->winfrags * 2 + 1)]);
@@ -628,47 +628,96 @@ ngx_hls_live_timeout(ngx_event_t *ev)
628628

629629

630630
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)
632632
{
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;
635637

636638
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-
}
640639

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;
643643
}
644644

645645
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;
648650
}
649651

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);
656655

657-
if (ctx->frags == NULL) {
658656
ctx->frags = ngx_pcalloc(s->pool,
659657
sizeof(ngx_hls_live_frag_t *) * (hacf->winfrags * 2 + 1));
660658
if (ctx->frags == NULL) {
661659
return NGX_ERROR;
662660
}
663661
}
664662

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+
665686
ctx->ev.data = s;
666687
ctx->ev.handler = ngx_hls_live_timeout;
667688
ctx->ev.log = s->log;
668689
ctx->timeout = hacf->timeout;
669690

670691
ngx_add_timer(&ctx->ev, (ctx->timeout + 3000) / 3);
671692

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+
672721
next:
673722
return next_play(s, v);
674723
}
@@ -678,7 +727,7 @@ static ngx_int_t
678727
ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
679728
{
680729
ngx_hls_live_app_conf_t *hacf;
681-
ngx_hls_live_ctx_t *ctx;
730+
ngx_hls_live_ctx_t *ctx, **cctx;
682731
ngx_uint_t i;
683732
ngx_hls_live_frag_t *frag;
684733

@@ -708,6 +757,18 @@ ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
708757
}
709758
}
710759

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+
711772
next:
712773
return next_close_stream(s, v);
713774
}
@@ -858,7 +919,7 @@ static ngx_int_t
858919
ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
859920
{
860921
ngx_live_stream_t *live_stream;
861-
ngx_rtmp_core_ctx_t *live_ctx;
922+
ngx_hls_live_ctx_t *ctx;
862923
ngx_rtmp_session_t *ss;
863924
ngx_rtmp_codec_ctx_t *codec_ctx;
864925
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)
871932
frame->dts/90, frame->type, frame->key);
872933

873934
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;
878937

879938
switch (ngx_mpegts_gop_link(s, ss, hacf->playlen, hacf->playlen)) {
880939
case NGX_DECLINED:

0 commit comments

Comments
 (0)