Skip to content

Commit

Permalink
http-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
im-pingo committed Sep 25, 2019
1 parent 2f1f259 commit b3a9d5b
Show file tree
Hide file tree
Showing 13 changed files with 1,026 additions and 239 deletions.
8 changes: 5 additions & 3 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RTMP_CORE_MODULES=" \
ngx_rtmp_shared_module \
ngx_rtmp_gop_module \
ngx_rtmp_monitor_module \
ngx_mpegts_mux_module \
ngx_mpegts_live_module \
ngx_mpegts_gop_module \
ngx_hls_live_module \
"
Expand All @@ -37,6 +37,7 @@ RTMP_HTTP_MODULES=" \
ngx_rtmp_control_module \
ngx_http_flv_live_module \
ngx_hls_http_module \
ngx_mpegts_http_module \
"


Expand All @@ -63,7 +64,7 @@ RTMP_DEPS=" \
$ngx_addon_dir/ngx_live_record.h \
$ngx_addon_dir/ngx_rtmp_dynamic.h \
$ngx_addon_dir/ngx_rtmp_variables.h \
$ngx_addon_dir/mpegts/ngx_mpegts_mux_module.h \
$ngx_addon_dir/mpegts/ngx_mpegts_live_module.h \
$ngx_addon_dir/mpegts/ngx_hls_live_module.h \
$ngx_addon_dir/mpegts/ngx_mpegts_gop_module.h \
"
Expand Down Expand Up @@ -108,7 +109,7 @@ RTMP_CORE_SRCS=" \
$ngx_addon_dir/ngx_rtmp_monitor_module.c \
$ngx_addon_dir/ngx_rtmp_dynamic.c \
$ngx_addon_dir/ngx_rtmp_variables.c \
$ngx_addon_dir/mpegts/ngx_mpegts_mux_module.c \
$ngx_addon_dir/mpegts/ngx_mpegts_live_module.c \
$ngx_addon_dir/mpegts/ngx_hls_live_module.c \
$ngx_addon_dir/mpegts/ngx_mpegts_gop_module.c \
"
Expand All @@ -121,6 +122,7 @@ RTMP_HTTP_SRCS=" \
$ngx_addon_dir/http/ngx_http_flv_live_module.c \
$ngx_addon_dir/http/ngx_http_set_header.c \
$ngx_addon_dir/mpegts/ngx_hls_http_module.c \
$ngx_addon_dir/mpegts/ngx_mpegts_http_module.c \
"

if [ -f auto/module ] ; then
Expand Down
24 changes: 11 additions & 13 deletions mpegts/ngx_hls_http_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <math.h>
#include <ngx_rtmp_cmd_module.h>
#include "ngx_http_set_header.h"
#include "ngx_mpegts_mux_module.h"
#include "ngx_mpegts_live_module.h"
#include "ngx_hls_live_module.h"
#include "ngx_rbuf.h"
#include "ngx_rtmp_dynamic.h"
Expand Down Expand Up @@ -397,23 +397,23 @@ ngx_hls_http_cleanup(void *data)
{
ngx_http_request_t *r;
ngx_hls_http_ctx_t *ctx;
ngx_chain_t *cl;
// ngx_chain_t *cl;

r = data;
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);

if (!ctx || !ctx->session) {
return;
}

/*
cl = ctx->out_chain;
while (cl) {
ctx->out_chain = cl->next;
ngx_put_chainbuf(cl);
cl = ctx->out_chain;
}
ctx->out_chain = NULL;

*/
ctx->session->request = NULL;
ctx->session->connection = NULL;

Expand Down Expand Up @@ -592,7 +592,7 @@ ngx_hls_http_create_session(ngx_http_request_t *r)
return NULL;
}

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

return s;
}
Expand All @@ -604,7 +604,7 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
ngx_hls_http_ctx_t *ctx;
ngx_int_t rc;
ngx_rtmp_session_t *s;
ngx_chain_t out;
ngx_chain_t *out;
ngx_buf_t *buf;

ctx = ngx_hls_http_create_ctx(r, addr_conf);
Expand Down Expand Up @@ -638,8 +638,8 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf

ctx->session = s;

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

buf->last = buf->pos = buf->start;
buf->memory = 1;
Expand All @@ -661,7 +661,7 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
return rc;
}

rc = ngx_http_output_filter(r, &out);
rc = ngx_http_output_filter(r, out);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"hls-http: m3u8_handler| send http content failed");
Expand Down Expand Up @@ -734,7 +734,6 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
size_t present, sent;
ngx_int_t rc;
ngx_chain_t *cl;
ngx_hls_live_frag_t *frag;

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

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

ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
s = ctx->session;
frag = ctx->frag;

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

ngx_rtmp_shared_acquire_frag(frag);

if (0) {
if (1) {
r->count++;
r->write_event_handler = ngx_hls_http_write_handler;

Expand Down Expand Up @@ -1027,7 +1025,7 @@ ngx_http_hls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_hls_http_loc_conf_t *hlcf;
ngx_str_t *value;
ngx_uint_t n;

clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_hls_http_handler;

Expand Down
125 changes: 92 additions & 33 deletions mpegts/ngx_hls_live_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include <ngx_rtmp_codec_module.h>
#include "ngx_rbuf.h"
#include "ngx_hls_live_module.h"
#include "ngx_mpegts_mux_module.h"
#include "ngx_mpegts_live_module.h"
#include "ngx_rtmp_live_module.h"
#include "ngx_mpegts_gop_module.h"

static ngx_rtmp_play_pt next_play;
Expand Down Expand Up @@ -253,7 +254,7 @@ ngx_hls_live_write_playlist(ngx_rtmp_session_t *s, ngx_buf_t *out)

for (i = 0; i < ctx->nfrags; i++) {
frag = ngx_hls_live_get_frag(s, i);
if (frag->duration > max_frag) {
if (frag && frag->duration > max_frag) {
max_frag = (ngx_uint_t) (frag->duration + .5);
}
}
Expand Down Expand Up @@ -410,10 +411,11 @@ ngx_hls_live_prepare_frag(ngx_rtmp_session_t *s, ngx_hls_live_frag_t *frag)
while (cl) {
*ll = ngx_get_chainbuf(0, 0);
*((*ll)->buf) = *(cl->buf);
(*ll)->buf->flush = 1;
(*ll)->buf->memory = 1;

if (frag->content_pos == frag->content_last && cl->next == NULL) {
(*ll)->buf->last_in_chain = 1;
(*ll)->buf->flush = 1;
}

ll = &((*ll)->next);
Expand All @@ -433,17 +435,15 @@ ngx_hls_live_fetch_session(ngx_str_t *server,
ngx_str_t *stream, ngx_str_t *session)
{
ngx_live_stream_t *live_stream;
ngx_rtmp_core_ctx_t *lctx;
ngx_hls_live_ctx_t *ctx;

live_stream = ngx_live_fetch_stream(server, stream);
if (live_stream) {
for (lctx = live_stream->hls_play_ctx; lctx; lctx = lctx->next) {
ctx = ngx_rtmp_get_module_ctx(lctx->session, ngx_hls_live_module);
for (ctx = live_stream->hls_ctx; ctx; ctx = ctx->next) {
if (session->len == ctx->sid.len &&
!ngx_strncmp(ctx->sid.data, session->data, session->len))
{
return lctx->session;
return ctx->session;
}
}
}
Expand Down Expand Up @@ -476,7 +476,7 @@ ngx_hls_live_close_fragment(ngx_rtmp_session_t *s)
return NGX_OK;
}

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

ctx->opened = 0;
Expand Down Expand Up @@ -538,7 +538,7 @@ ngx_hls_live_create_frag(ngx_rtmp_session_t *s) {
sizeof(ngx_mpegts_frame_t*) * s->out_queue);
}

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

return frag;
Expand Down Expand Up @@ -566,7 +566,7 @@ ngx_hls_live_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,

id = ngx_hls_live_get_fragment_id(s, ts);

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

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


static ngx_int_t
ngx_hls_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
ngx_hls_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
{
ngx_hls_live_app_conf_t *hacf;
ngx_hls_live_ctx_t *ctx;
ngx_hls_live_ctx_t *ctx;
ngx_live_stream_t *st;
ngx_rtmp_live_app_conf_t *lacf;
ngx_hls_live_app_conf_t *hacf;

hacf = ngx_rtmp_get_module_app_conf(s, ngx_hls_live_module);
if (hacf == NULL || !hacf->hls || s->live_type != NGX_HLS_LIVE) {
goto next;
}

if (s->interprocess) {
goto next;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
return NGX_ERROR;
}

ctx = ngx_rtmp_get_module_ctx(s, ngx_hls_live_module);
if (ctx) {
goto next;
if (ctx && ctx->stream) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->log, 0,
"mpegts-live: join| already joined");
return NGX_ERROR;
}

ctx = ngx_pcalloc(s->pool, sizeof(ngx_hls_live_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_hls_live_module);

ctx->sid.len = ngx_strlen(v->session);
ctx->sid.data = ngx_pcalloc(s->pool, ctx->sid.len);
ngx_memcpy(ctx->sid.data, v->session, ctx->sid.len);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->pool, sizeof(ngx_hls_live_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_hls_live_module);

if (ctx->frags == NULL) {
ctx->frags = ngx_pcalloc(s->pool,
sizeof(ngx_hls_live_frag_t *) * (hacf->winfrags * 2 + 1));
if (ctx->frags == NULL) {
return NGX_ERROR;
}
}

ctx->session = s;

ngx_log_error(NGX_LOG_INFO, s->log, 0,
"mpegts-live: join| join '%s'", name);

st = s->live_stream;

if (!(publisher || st->publish_ctx || lacf->idle_streams)) {
ngx_log_error(NGX_LOG_ERR, s->log, 0,
"mpegts-live: join| stream not found");

s->status = 404;

ngx_rtmp_finalize_session(s);

return NGX_ERROR;
}

ctx->stream = st;
ctx->next = st->hls_ctx;

st->hls_ctx = ctx;

ctx->ev.data = s;
ctx->ev.handler = ngx_hls_live_timeout;
ctx->ev.log = s->log;
ctx->timeout = hacf->timeout;

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

return NGX_OK;
}

static ngx_int_t
ngx_hls_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_hls_live_app_conf_t *hacf;
ngx_hls_live_ctx_t *ctx;

hacf = ngx_rtmp_get_module_app_conf(s, ngx_hls_live_module);
if (hacf == NULL || !hacf->hls || s->live_type != NGX_HLS_LIVE) {
goto next;
}

if (s->interprocess || s->live_type != NGX_HLS_LIVE) {
goto next;
}

if (ngx_hls_live_join(s, v->name, 0) == NGX_ERROR) {
return NGX_ERROR;
}

ctx = ngx_rtmp_get_module_ctx(s, ngx_hls_live_module);

ctx->sid.len = ngx_strlen(v->session);
ctx->sid.data = ngx_pcalloc(s->pool, ctx->sid.len);
ngx_memcpy(ctx->sid.data, v->session, ctx->sid.len);

next:
return next_play(s, v);
}
Expand All @@ -678,7 +727,7 @@ static ngx_int_t
ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
{
ngx_hls_live_app_conf_t *hacf;
ngx_hls_live_ctx_t *ctx;
ngx_hls_live_ctx_t *ctx, **cctx;
ngx_uint_t i;
ngx_hls_live_frag_t *frag;

Expand Down Expand Up @@ -708,6 +757,18 @@ ngx_hls_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
}
}

for (cctx = &ctx->stream->hls_ctx; *cctx; cctx = &(*cctx)->next) {
if (*cctx == ctx) {
*cctx = ctx->next;
break;
}
}

if (ctx->stream->hls_ctx) {
ctx->stream = NULL;
goto next;
}

next:
return next_close_stream(s, v);
}
Expand Down Expand Up @@ -858,7 +919,7 @@ static ngx_int_t
ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
{
ngx_live_stream_t *live_stream;
ngx_rtmp_core_ctx_t *live_ctx;
ngx_hls_live_ctx_t *ctx;
ngx_rtmp_session_t *ss;
ngx_rtmp_codec_ctx_t *codec_ctx;
ngx_hls_live_app_conf_t *hacf;
Expand All @@ -871,10 +932,8 @@ ngx_hls_live_av(ngx_rtmp_session_t *s, ngx_mpegts_frame_t *frame)
frame->dts/90, frame->type, frame->key);

live_stream = s->live_stream;
for (live_ctx = live_stream->hls_play_ctx; live_ctx;
live_ctx = live_ctx->next)
{
ss = live_ctx->session;
for (ctx = live_stream->hls_ctx; ctx; ctx = ctx->next) {
ss = ctx->session;

switch (ngx_mpegts_gop_link(s, ss, hacf->playlen, hacf->playlen)) {
case NGX_DECLINED:
Expand Down
Loading

0 comments on commit b3a9d5b

Please sign in to comment.