From 8f7e66f75bb83f88b048cdcb1bc04d9906a9a0f2 Mon Sep 17 00:00:00 2001 From: HeyJupiter <42520467+HeyJupiter@users.noreply.github.com> Date: Fri, 31 Aug 2018 11:57:35 +0800 Subject: [PATCH 1/6] [dev] make config file more compatible. --- config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config b/config index 8be2715..f3fbb6a 100644 --- a/config +++ b/config @@ -114,7 +114,7 @@ if [ -f auto/module ] ; then . auto/module - dynamic_modules=`eval echo '$'"${ngx_module}_MODULES" | sed -e "s/\s\{0,\}$ngx_addon_name//"` + dynamic_modules=`eval echo '$'"${ngx_module}_MODULES" | sed -e "s/ \{0,\}$ngx_addon_name//"` eval ${ngx_module}_MODULES=\"$dynamic_modules\" unset dynamic_modules From 49b26d6bc4e69c0689eb574882a21f76ecbb870b Mon Sep 17 00:00:00 2001 From: HeyJupiter Date: Sun, 2 Sep 2018 13:19:22 +0800 Subject: [PATCH 2/6] [dev] optimize GOP cache feature. --- ngx_rtmp_gop_cache_module.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ngx_rtmp_gop_cache_module.c b/ngx_rtmp_gop_cache_module.c index 412d327..98ea07b 100644 --- a/ngx_rtmp_gop_cache_module.c +++ b/ngx_rtmp_gop_cache_module.c @@ -2,6 +2,7 @@ /* * Copyright (C) Gnolizuh * Copyright (C) Winshining + * Copyright (C) HeyJupiter */ @@ -550,6 +551,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) ngx_rtmp_live_chunk_stream_t *cs; ngx_rtmp_live_proc_handler_t *handler; ngx_http_request_t *r; + ngx_flag_t error; lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module); if (lacf == NULL) { @@ -600,6 +602,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) if (meta == NULL && meta_version != gctx->meta_version) { meta = handler->meta_message_pt(s, gctx->meta); if (meta == NULL) { + ngx_rtmp_finalize_session(s); return; } } @@ -638,6 +641,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) } delta = ch.timestamp - lh.timestamp; + error = 0; if (!cs->active) { switch (gf->h.type) { @@ -651,12 +655,13 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) if (header) { apkt = handler->append_message_pt(s, &lh, NULL, header); if (apkt == NULL) { - return; + error = 1; + goto next; } } if (apkt && handler->send_message_pt(s, apkt, 0) != NGX_OK) { - continue; + goto next; } cs->timestamp = lh.timestamp; @@ -666,7 +671,8 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) pkt = handler->append_message_pt(s, &ch, &lh, gf->frame); if (pkt == NULL) { - return; + error = 1; + goto next; } if (handler->send_message_pt(s, pkt, gf->prio) != NGX_OK) { @@ -674,8 +680,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) cs->dropped += delta; - ngx_rtmp_finalize_session(s); - return; + goto next; } ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, @@ -687,6 +692,8 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) cs->timestamp += delta; s->current_time = cs->timestamp; + next: + if (pkt) { handler->free_message_pt(s, pkt); pkt = NULL; @@ -696,6 +703,11 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s) handler->free_message_pt(s, apkt); apkt = NULL; } + + if (error) { + ngx_rtmp_finalize_session(s); + return; + } } } } From 63490bcc3c62e31279910ff10a7d37bc1efc3e49 Mon Sep 17 00:00:00 2001 From: Vladimir Vainer Date: Tue, 4 Sep 2018 12:29:08 +0300 Subject: [PATCH 3/6] [dev] support for onTextData and onCuePoint (#68). --- ngx_rtmp_live_module.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/ngx_rtmp_live_module.c b/ngx_rtmp_live_module.c index eb8fee8..ca7dccc 100644 --- a/ngx_rtmp_live_module.c +++ b/ngx_rtmp_live_module.c @@ -1178,6 +1178,7 @@ static ngx_int_t ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in, ngx_rtmp_amf_elt_t *out_elts, ngx_uint_t out_elts_size) { + ngx_rtmp_live_proc_handler_t *handler; ngx_rtmp_live_ctx_t *ctx, *pctx; ngx_chain_t *data, *rpkt; ngx_rtmp_core_srv_conf_t *cscf; @@ -1190,6 +1191,7 @@ ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_uint_t peers; uint32_t delta; ngx_rtmp_live_chunk_stream_t *cs; + ngx_http_request_t *http_request; #ifdef NGX_DEBUG u_char *msg_type; @@ -1254,7 +1256,6 @@ ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, delta = ch.timestamp - cs->timestamp; rpkt = ngx_rtmp_append_shared_bufs(cscf, data, in); - ngx_rtmp_prepare_message(s, &ch, NULL, rpkt); for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) { if (pctx == ctx || pctx->paused) { @@ -1262,11 +1263,34 @@ ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, } ss = pctx->session; - - if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { - ++pctx->ndropped; - cs->dropped += delta; - continue; + handler = ngx_rtmp_live_proc_handlers[pctx->protocol]; + if (pctx->protocol == NGX_RTMP_PROTOCOL_HTTP) { + http_request = ss->data; + if (http_request == NULL || (http_request->connection && http_request->connection->destroyed)) { + continue; + } + handler->meta = handler->append_message_pt(ss,&ch,NULL,rpkt); + if (handler->meta == NULL) { + continue; + } + if (handler->meta) { + if (handler->send_message_pt(ss,handler->meta,0) != NGX_OK) { + ++pctx->ndropped; + cs->dropped += delta; + handler->free_message_pt(ss, handler->meta); + handler->meta = NULL; + continue; + } + handler->free_message_pt(ss, handler->meta); + handler->meta = NULL; + } + } else { + ngx_rtmp_prepare_message(s, &ch, NULL, rpkt); + if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { + ++pctx->ndropped; + cs->dropped += delta; + continue; + } } cs->timestamp += delta; From 0dac9b20c83c65b74fc42ac91d97e99910cd3f7e Mon Sep 17 00:00:00 2001 From: winshining Date: Tue, 4 Sep 2018 18:02:50 +0800 Subject: [PATCH 4/6] [dev] fix code style flaws. --- ngx_rtmp_live_module.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ngx_rtmp_live_module.c b/ngx_rtmp_live_module.c index ca7dccc..ac13736 100644 --- a/ngx_rtmp_live_module.c +++ b/ngx_rtmp_live_module.c @@ -1191,7 +1191,7 @@ ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_uint_t peers; uint32_t delta; ngx_rtmp_live_chunk_stream_t *cs; - ngx_http_request_t *http_request; + ngx_http_request_t *r; #ifdef NGX_DEBUG u_char *msg_type; @@ -1265,25 +1265,26 @@ ngx_rtmp_live_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ss = pctx->session; handler = ngx_rtmp_live_proc_handlers[pctx->protocol]; if (pctx->protocol == NGX_RTMP_PROTOCOL_HTTP) { - http_request = ss->data; - if (http_request == NULL || (http_request->connection && http_request->connection->destroyed)) { + r = ss->data; + if (r == NULL || (r->connection && r->connection->destroyed)) { continue; } - handler->meta = handler->append_message_pt(ss,&ch,NULL,rpkt); + + handler->meta = handler->append_message_pt(ss, &ch, NULL, rpkt); if (handler->meta == NULL) { continue; } - if (handler->meta) { - if (handler->send_message_pt(ss,handler->meta,0) != NGX_OK) { - ++pctx->ndropped; - cs->dropped += delta; - handler->free_message_pt(ss, handler->meta); - handler->meta = NULL; - continue; - } + + if (handler->send_message_pt(ss, handler->meta, 0) != NGX_OK) { + ++pctx->ndropped; + cs->dropped += delta; handler->free_message_pt(ss, handler->meta); handler->meta = NULL; + continue; } + + handler->free_message_pt(ss, handler->meta); + handler->meta = NULL; } else { ngx_rtmp_prepare_message(s, &ch, NULL, rpkt); if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) { From 8b08f5f9ed13354adb6ee77170859a580664a346 Mon Sep 17 00:00:00 2001 From: HeyJupiter <42520467+HeyJupiter@users.noreply.github.com> Date: Sun, 16 Sep 2018 09:14:14 +0800 Subject: [PATCH 5/6] [dev] sync from winshining/master. (#2) * [dev] optimize GOP cache feature. (#66) * [dev] make config file more compatible. * [dev] optimize GOP cache feature. * [dev] support for onTextData and onCuePoint (#68). * [dev] fix code style flaws. * [misc] fix a potential crash bug, remove unused codes and update version. --- ngx_rtmp_gop_cache_module.c | 34 +++++++++++++++++++--------------- ngx_rtmp_version.h | 4 ++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/ngx_rtmp_gop_cache_module.c b/ngx_rtmp_gop_cache_module.c index 98ea07b..4dc11f6 100644 --- a/ngx_rtmp_gop_cache_module.c +++ b/ngx_rtmp_gop_cache_module.c @@ -267,7 +267,6 @@ static ngx_int_t ngx_rtmp_gop_cache_alloc_cache(ngx_rtmp_session_t *s) { ngx_rtmp_codec_ctx_t *codec_ctx; - ngx_rtmp_core_srv_conf_t *cscf; ngx_rtmp_gop_cache_ctx_t *ctx; ngx_rtmp_gop_cache_t *cache, **iter; @@ -281,11 +280,6 @@ ngx_rtmp_gop_cache_alloc_cache(ngx_rtmp_session_t *s) return NGX_ERROR; } - cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); - if (cscf == NULL) { - return NGX_ERROR; - } - if (ctx->free_cache) { cache = ctx->free_cache; ctx->free_cache = cache->next; @@ -378,19 +372,18 @@ ngx_rtmp_gop_cache_cleanup(ngx_rtmp_session_t *s) ngx_rtmp_gop_cache_free_cache(s, cache); } - if (ctx->pool) { - ngx_destroy_pool(ctx->pool); - ctx->pool = NULL; - } - ctx->video_seq_header = NULL; ctx->audio_seq_header = NULL; ctx->meta = NULL; - ctx->cache_tail = ctx->cache_head = NULL; + if (ctx->cache_head) { + ctx->cache_head->next = ctx->free_cache; + ctx->free_cache = ctx->cache_head; + ctx->cache_head = NULL; + } + + ctx->cache_tail = NULL; ctx->gop_cache_count = 0; - ctx->free_cache = NULL; - ctx->free_frame = NULL; ctx->video_frame_in_all = 0; ctx->audio_frame_in_all = 0; } @@ -720,10 +713,10 @@ ngx_rtmp_gop_cache_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_live_ctx_t *ctx; ngx_rtmp_gop_cache_app_conf_t *gacf; ngx_rtmp_live_app_conf_t *lacf; + ngx_rtmp_live_chunk_stream_t *cs; ngx_rtmp_header_t ch; ngx_uint_t prio; ngx_uint_t csidx; - ngx_rtmp_live_chunk_stream_t *cs; gacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_gop_cache_module); if (gacf == NULL || !gacf->gop_cache) { @@ -851,6 +844,7 @@ ngx_rtmp_gop_cache_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v) { ngx_rtmp_live_ctx_t *ctx; + ngx_rtmp_gop_cache_ctx_t *gctx; ngx_rtmp_live_app_conf_t *lacf; ngx_rtmp_gop_cache_app_conf_t *gacf; @@ -873,6 +867,16 @@ ngx_rtmp_gop_cache_close_stream(ngx_rtmp_session_t *s, goto next; } + gctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module); + if (gctx == NULL) { + goto next; + } + + if (gctx->pool) { + ngx_destroy_pool(gctx->pool); + gctx->pool = NULL; + } + ngx_rtmp_gop_cache_cleanup(s); next: diff --git a/ngx_rtmp_version.h b/ngx_rtmp_version.h index a382c16..f3d3b56 100644 --- a/ngx_rtmp_version.h +++ b/ngx_rtmp_version.h @@ -9,8 +9,8 @@ #define _NGX_RTMP_VERSION_H_INCLUDED_ -#define nginx_rtmp_version 1002004 -#define NGINX_RTMP_VERSION "1.2.4" +#define nginx_rtmp_version 1002005 +#define NGINX_RTMP_VERSION "1.2.5" #endif /* _NGX_RTMP_VERSION_H_INCLUDED_ */ From 4a931c051016e255e9c5f1aa391b9a45bd54c9d1 Mon Sep 17 00:00:00 2001 From: HeyJupiter <42520467+HeyJupiter@users.noreply.github.com> Date: Sat, 29 Sep 2018 03:09:17 +0800 Subject: [PATCH 6/6] [fix] fix a very occasional crash bug. --- ngx_rtmp_gop_cache_module.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ngx_rtmp_gop_cache_module.c b/ngx_rtmp_gop_cache_module.c index 4dc11f6..96f5f4d 100644 --- a/ngx_rtmp_gop_cache_module.c +++ b/ngx_rtmp_gop_cache_module.c @@ -867,6 +867,8 @@ ngx_rtmp_gop_cache_close_stream(ngx_rtmp_session_t *s, goto next; } + ngx_rtmp_gop_cache_cleanup(s); + gctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module); if (gctx == NULL) { goto next; @@ -877,8 +879,6 @@ ngx_rtmp_gop_cache_close_stream(ngx_rtmp_session_t *s, gctx->pool = NULL; } - ngx_rtmp_gop_cache_cleanup(s); - next: return next_close_stream(s, v); }