Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix seqheader per gop #261

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ngx_rtmp_codec_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_codec_ctx_t *ctx;
ngx_chain_t **header;
ngx_uint_t *version;
uint8_t fmt;
static ngx_uint_t sample_rates[] =
{ 5512, 11025, 22050, 44100 };
Expand Down Expand Up @@ -250,13 +251,15 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
if (h->type == NGX_RTMP_MSG_AUDIO) {
if (ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC) {
header = &ctx->aac_header;
version = &ctx->ash_version;
if (ngx_rtmp_codec_parse_aac_header(s, in) == NGX_ERROR) {
return NGX_ERROR;
}
}
} else {
if (ctx->video_codec_id == NGX_RTMP_VIDEO_H264) {
header = &ctx->avc_header;
version = &ctx->vsh_version;
if (ngx_rtmp_codec_parse_avc_header(s, in) == NGX_ERROR) {
return NGX_ERROR;
}
Expand All @@ -272,6 +275,14 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
}

*header = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
*version = ngx_rtmp_codec_get_next_version();
if (h->type == NGX_RTMP_MSG_AUDIO) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: audio header version=%ui", *version);
} else {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: video header version=%ui", *version);
}

return NGX_OK;
}
Expand Down Expand Up @@ -766,6 +777,8 @@ ngx_rtmp_codec_prepare_meta(ngx_rtmp_session_t *s, uint32_t timestamp)
ngx_rtmp_prepare_message(s, &h, NULL, ctx->meta);

ctx->meta_version = ngx_rtmp_codec_get_next_version();
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: meta header version=%ui", ctx->meta_version);

return NGX_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions ngx_rtmp_codec_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ typedef struct {

ngx_chain_t *avc_header;
ngx_chain_t *aac_header;
ngx_uint_t vsh_version;
ngx_uint_t ash_version;

ngx_chain_t *meta;
ngx_uint_t meta_version;
Expand Down
121 changes: 76 additions & 45 deletions ngx_rtmp_gop_cache_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,11 @@ ngx_rtmp_gop_cache_cleanup(ngx_rtmp_session_t *s)

for (cache = ctx->cache_head; cache; cache = cache->next) {
ngx_rtmp_gop_cache_free_cache(s, cache);
cache->video_seq_header = NULL;
cache->audio_seq_header = NULL;
cache->meta = NULL;
}

ctx->video_seq_header = NULL;
ctx->audio_seq_header = NULL;
ctx->meta = NULL;

if (ctx->cache_head) {
ctx->cache_head->next = ctx->free_cache;
ctx->free_cache = ctx->cache_head;
Expand Down Expand Up @@ -460,28 +459,35 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
}
}

if (ctx->cache_tail == NULL) {
return;
}

// save video seq header.
if (codec_ctx->avc_header && ctx->video_seq_header == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: video seq header is comming");
ctx->video_seq_header = codec_ctx->avc_header;
if (codec_ctx->avc_header && ctx->cache_tail->video_seq_header == NULL) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: video seq header is comming, version=%ui",
codec_ctx->vsh_version);
ctx->cache_tail->video_seq_header = codec_ctx->avc_header;
ctx->cache_tail->vsh_version = codec_ctx->vsh_version;
}

// save audio seq header.
if (codec_ctx->aac_header && ctx->audio_seq_header == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: audio seq header is comming");
ctx->audio_seq_header = codec_ctx->aac_header;
if (codec_ctx->aac_header && ctx->cache_tail->audio_seq_header == NULL) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: audio seq header is comming, version=%ui",
codec_ctx->ash_version);
ctx->cache_tail->audio_seq_header = codec_ctx->aac_header;
ctx->cache_tail->ash_version = codec_ctx->ash_version;
}

// save metadata.
if (codec_ctx->meta &&
(ctx->meta == NULL || codec_ctx->meta_version != ctx->meta_version))
if (codec_ctx->meta && ctx->cache_tail->meta == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache: meta is comming");
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: meta is comming, version=%ui", codec_ctx->meta_version);
ctx->cache_tail->meta_version = codec_ctx->meta_version;
ctx->cache_tail->meta = codec_ctx->meta;
}

gf = ngx_rtmp_gop_cache_alloc_frame(s);
Expand Down Expand Up @@ -538,7 +544,8 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
ngx_rtmp_gop_cache_t *cache;
ngx_rtmp_gop_frame_t *gf;
ngx_rtmp_header_t ch, lh, clh;
ngx_uint_t meta_version;
ngx_uint_t version, coversion, meta_version;
ngx_uint_t version_send, coversion_send;
uint32_t delta;
ngx_int_t csidx;
ngx_rtmp_live_chunk_stream_t *cs;
Expand All @@ -562,10 +569,6 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
pkt = NULL;
apkt = NULL;
acopkt = NULL;
header = NULL;
coheader = NULL;
meta = NULL;
meta_version = 0;

pub_ctx = ctx->stream->pub_ctx;
rs = pub_ctx->session;
Expand All @@ -587,6 +590,15 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
}

for (cache = gctx->cache_head; cache; cache = cache->next) {
header = NULL;
coheader = NULL;
meta = NULL;
version = 0;
coversion = 0;
meta_version = 0;
version_send = 0;
coversion_send = 0;

if (s->connection == NULL || s->connection->destroyed) {
return;
}
Expand All @@ -604,22 +616,19 @@ 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_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 Expand Up @@ -673,20 +682,31 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
}

switch (gf->h.type) {
case NGX_RTMP_MSG_VIDEO:
header = gctx->video_seq_header;
case NGX_RTMP_MSG_AUDIO:
header = cache->audio_seq_header;
version = cache->ash_version;
version_send = ctx->ash_version;
if (lacf->interleave) {
coheader = gctx->audio_seq_header;
coheader = cache->video_seq_header;
coversion = cache->vsh_version;
coversion_send = ctx->vsh_version;
}
break;
default:
header = gctx->audio_seq_header;
header = cache->video_seq_header;
version = cache->vsh_version;
version_send = ctx->vsh_version;
if (lacf->interleave) {
coheader = gctx->video_seq_header;
coheader = cache->audio_seq_header;
coversion = cache->ash_version;
coversion_send = cache->ash_version;
}
}

if (header) {
if (header && version != version_send) {
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache send: %s sequence header, version=%ui",
(gf->h.type == NGX_RTMP_MSG_AUDIO ? "audio" : "video"), version);
apkt = handler->append_message_pt(s, &lh, NULL, header);
if (apkt == NULL) {
error = 1;
Expand All @@ -698,7 +718,10 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
goto next;
}

if (coheader) {
if (coheader && coversion != coversion_send) {
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"gop cache send: %s sequence coheader, version=%ui",
(gf->h.type == NGX_RTMP_MSG_AUDIO ? "video" : "audio"), coversion);
acopkt = handler->append_message_pt(s, &clh, NULL,
coheader);
if (acopkt == NULL) {
Expand All @@ -711,6 +734,15 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
goto next;
}

if (gf->h.type == NGX_RTMP_MSG_AUDIO) {
ctx->ash_version = version;
if (coheader && coversion)
ctx->vsh_version = coversion;
} else {
ctx->vsh_version = version;
if (coheader && coversion)
ctx->ash_version = coversion;
}
cs->timestamp = lh.timestamp;
cs->active = 1;
s->current_time = cs->timestamp;
Expand Down Expand Up @@ -860,17 +892,16 @@ ngx_rtmp_gop_cache_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
}

ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_gop_cache_module);
} else if (ctx->pool) {
ngx_destroy_pool(ctx->pool);
ctx->pool = NULL;
}

ngx_memzero(ctx, sizeof(*ctx));

ctx->pool = ngx_create_pool(NGX_GOP_CACHE_POOL_CREATE_SIZE,
s->connection->log);
if (ctx->pool == NULL) {
ctx->pool = ngx_create_pool(NGX_GOP_CACHE_POOL_CREATE_SIZE,
s->connection->log);

if (ctx->pool == NULL) {
return NGX_ERROR;
}
return NGX_ERROR;
}

next:
Expand Down
13 changes: 8 additions & 5 deletions ngx_rtmp_gop_cache_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ struct ngx_rtmp_gop_cache_s {
ngx_rtmp_gop_cache_t *next;
ngx_int_t video_frame_in_this;
ngx_int_t audio_frame_in_this;

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

ngx_uint_t vsh_version;
ngx_uint_t ash_version;
ngx_uint_t meta_version;
};


Expand All @@ -48,12 +56,7 @@ 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 *video_seq_header;
ngx_chain_t *audio_seq_header;
ngx_chain_t *meta;
ngx_chain_t *free;

ngx_uint_t meta_version;

size_t gop_cache_count;
size_t video_frame_in_all;
Expand Down
Loading
Loading