Skip to content

Commit

Permalink
[fix] fixed incorrect meta processing in gop cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
winshining committed Mar 3, 2024
1 parent a484f11 commit 42891da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
64 changes: 43 additions & 21 deletions ngx_rtmp_gop_cache_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,15 @@ static ngx_rtmp_gop_cache_t *
ngx_rtmp_gop_cache_free_cache(ngx_rtmp_session_t *s,
ngx_rtmp_gop_cache_t *cache)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_gop_cache_ctx_t *ctx;
ngx_rtmp_gop_frame_t *frame;

cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
if (cscf == NULL) {
return NULL;
}

ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module);
if (ctx == NULL) {
return NULL;
Expand All @@ -325,8 +331,21 @@ ngx_rtmp_gop_cache_free_cache(ngx_rtmp_session_t *s,
ngx_rtmp_gop_cache_free_frame(s, frame);
}

cache->video_seq_header = NULL;
cache->audio_seq_header = NULL;
if (cache->video_seq_header) {
ngx_rtmp_free_shared_chain(cscf, cache->video_seq_header);
cache->video_seq_header = NULL;
}

if (cache->audio_seq_header) {
ngx_rtmp_free_shared_chain(cscf, cache->audio_seq_header);
cache->audio_seq_header = NULL;
}

if (cache->meta) {
ngx_rtmp_free_shared_chain(cscf, cache->meta);
cache->meta_version = 0;
cache->meta = NULL;
}

cache->video_frame_in_this = 0;
cache->audio_frame_in_this = 0;
Expand Down Expand Up @@ -359,8 +378,6 @@ ngx_rtmp_gop_cache_cleanup(ngx_rtmp_session_t *s)
ngx_rtmp_gop_cache_free_cache(s, cache);
}

ctx->meta = NULL;

if (ctx->cache_head) {
ctx->cache_head->next = ctx->free_cache;
ctx->free_cache = ctx->cache_head;
Expand Down Expand Up @@ -468,6 +485,7 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: add video seq header in new cache");
ctx->cache_tail->video_seq_header = codec_ctx->avc_header;
ngx_rtmp_acquire_shared_chain(ctx->cache_tail->video_seq_header);
}

// save audio seq header.
Expand All @@ -477,16 +495,19 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: add audio seq header in new cache");
ctx->cache_tail->audio_seq_header = codec_ctx->aac_header;
ngx_rtmp_acquire_shared_chain(ctx->cache_tail->audio_seq_header);
}

// save metadata.
if (codec_ctx->meta &&
(ctx->meta == NULL || codec_ctx->meta_version != ctx->meta_version))
(ctx->cache_tail && ctx->cache_tail->meta == NULL))
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: update meta");
ctx->meta_version = codec_ctx->meta_version;
ctx->meta = codec_ctx->meta;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: add meta in new cache, version=%ui",
codec_ctx->meta_version);
ctx->cache_tail->meta_version = codec_ctx->meta_version;
ctx->cache_tail->meta = codec_ctx->meta;
ngx_rtmp_acquire_shared_chain(ctx->cache_tail->meta);
}

gf = ngx_rtmp_gop_cache_alloc_frame(s);
Expand Down Expand Up @@ -569,7 +590,6 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
acopkt = NULL;
header = NULL;
coheader = NULL;
meta = NULL;
meta_version = 0;

pub_ctx = ctx->stream->pub_ctx;
Expand Down Expand Up @@ -609,22 +629,24 @@ 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;
}
}
meta = NULL;

if (cache->meta) {
if (meta_version != cache->meta_version) {
meta = handler->meta_message_pt(s, cache->meta);
if (meta == NULL) {
ngx_rtmp_finalize_session(s);
return;
}

if (meta) {
meta_version = gctx->meta_version;
meta_version = cache->meta_version;
}
}

/* send metadata */
if (meta && meta_version != ctx->meta_version) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache send: meta");
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache send: meta, version=%ui", meta_version);

if (handler->send_message_pt(s, meta, 0) == NGX_ERROR) {
ngx_rtmp_finalize_session(s);
Expand Down
8 changes: 3 additions & 5 deletions ngx_rtmp_gop_cache_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct ngx_rtmp_gop_cache_s {

ngx_chain_t *video_seq_header;
ngx_chain_t *audio_seq_header;
ngx_chain_t *meta;

ngx_uint_t meta_version;

ngx_int_t video_frame_in_this;
ngx_int_t audio_frame_in_this;
Expand All @@ -52,11 +55,6 @@ typedef struct ngx_rtmp_gop_cache_ctx_s {
ngx_rtmp_gop_cache_t *free_cache;
ngx_rtmp_gop_frame_t *free_frame;

ngx_chain_t *meta;
ngx_chain_t *free;

ngx_uint_t meta_version;

size_t gop_cache_count;
size_t video_frame_in_all;
size_t audio_frame_in_all;
Expand Down

0 comments on commit 42891da

Please sign in to comment.