Skip to content

Commit a484f11

Browse files
committed
[fix] fixed multi sequence headers bugs in gop cache.
SPS & PPS may change during the lifetime of a stream.
1 parent 2adfaee commit a484f11

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

ngx_rtmp_gop_cache_module.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ ngx_rtmp_gop_cache_free_cache(ngx_rtmp_session_t *s,
325325
ngx_rtmp_gop_cache_free_frame(s, frame);
326326
}
327327

328+
cache->video_seq_header = NULL;
329+
cache->audio_seq_header = NULL;
330+
328331
cache->video_frame_in_this = 0;
329332
cache->audio_frame_in_this = 0;
330333

@@ -356,8 +359,6 @@ ngx_rtmp_gop_cache_cleanup(ngx_rtmp_session_t *s)
356359
ngx_rtmp_gop_cache_free_cache(s, cache);
357360
}
358361

359-
ctx->video_seq_header = NULL;
360-
ctx->audio_seq_header = NULL;
361362
ctx->meta = NULL;
362363

363364
if (ctx->cache_head) {
@@ -447,9 +448,9 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
447448

448449
// audio only
449450
if (ctx->video_frame_in_all == 0 && ch->type == NGX_RTMP_MSG_AUDIO) {
450-
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
451-
"gop cache: drop audio frame timestamp=%uD",
452-
ch->timestamp);
451+
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
452+
"gop cache: drop audio frame timestamp=%uD",
453+
ch->timestamp);
453454

454455
return;
455456
}
@@ -461,25 +462,29 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
461462
}
462463

463464
// save video seq header.
464-
if (codec_ctx->avc_header && ctx->video_seq_header == NULL) {
465+
if (codec_ctx->avc_header &&
466+
(ctx->cache_tail && ctx->cache_tail->video_seq_header == NULL))
467+
{
465468
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
466-
"gop cache: video seq header is comming");
467-
ctx->video_seq_header = codec_ctx->avc_header;
469+
"gop cache: add video seq header in new cache");
470+
ctx->cache_tail->video_seq_header = codec_ctx->avc_header;
468471
}
469472

470473
// save audio seq header.
471-
if (codec_ctx->aac_header && ctx->audio_seq_header == NULL) {
474+
if (codec_ctx->aac_header &&
475+
(ctx->cache_tail && ctx->cache_tail->audio_seq_header == NULL))
476+
{
472477
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
473-
"gop cache: audio seq header is comming");
474-
ctx->audio_seq_header = codec_ctx->aac_header;
478+
"gop cache: add audio seq header in new cache");
479+
ctx->cache_tail->audio_seq_header = codec_ctx->aac_header;
475480
}
476481

477482
// save metadata.
478483
if (codec_ctx->meta &&
479484
(ctx->meta == NULL || codec_ctx->meta_version != ctx->meta_version))
480485
{
481486
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
482-
"gop cache: meta is comming");
487+
"gop cache: update meta");
483488
ctx->meta_version = codec_ctx->meta_version;
484489
ctx->meta = codec_ctx->meta;
485490
}
@@ -669,20 +674,23 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
669674

670675
if (!cs->active) {
671676
if (mandatory) {
677+
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
678+
"gop cache: skipping header");
679+
672680
continue;
673681
}
674682

675683
switch (gf->h.type) {
676684
case NGX_RTMP_MSG_VIDEO:
677-
header = gctx->video_seq_header;
685+
header = cache->video_seq_header;
678686
if (lacf->interleave) {
679-
coheader = gctx->audio_seq_header;
687+
coheader = cache->audio_seq_header;
680688
}
681689
break;
682690
default:
683-
header = gctx->audio_seq_header;
691+
header = cache->audio_seq_header;
684692
if (lacf->interleave) {
685-
coheader = gctx->video_seq_header;
693+
coheader = cache->video_seq_header;
686694
}
687695
}
688696

@@ -850,27 +858,22 @@ ngx_rtmp_gop_cache_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
850858

851859
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module);
852860
if (ctx == NULL) {
853-
ctx = ngx_palloc(s->connection->pool,
854-
sizeof(ngx_rtmp_gop_cache_ctx_t));
861+
ctx = ngx_pcalloc(s->connection->pool,
862+
sizeof(ngx_rtmp_gop_cache_ctx_t));
855863
if (ctx == NULL) {
856864
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
857865
"gop cache publish: failed to allocate for ctx");
858866

859867
return NGX_ERROR;
860868
}
861869

862-
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_gop_cache_module);
863-
}
864-
865-
ngx_memzero(ctx, sizeof(*ctx));
866-
867-
if (ctx->pool == NULL) {
868870
ctx->pool = ngx_create_pool(NGX_GOP_CACHE_POOL_CREATE_SIZE,
869871
s->connection->log);
870-
871872
if (ctx->pool == NULL) {
872873
return NGX_ERROR;
873874
}
875+
876+
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_gop_cache_module);
874877
}
875878

876879
next:

ngx_rtmp_gop_cache_module.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ struct ngx_rtmp_gop_cache_s {
2727
ngx_rtmp_gop_frame_t *frame_head;
2828
ngx_rtmp_gop_frame_t *frame_tail;
2929
ngx_rtmp_gop_cache_t *next;
30+
31+
ngx_chain_t *video_seq_header;
32+
ngx_chain_t *audio_seq_header;
33+
3034
ngx_int_t video_frame_in_this;
3135
ngx_int_t audio_frame_in_this;
3236
};
@@ -48,8 +52,6 @@ typedef struct ngx_rtmp_gop_cache_ctx_s {
4852
ngx_rtmp_gop_cache_t *free_cache;
4953
ngx_rtmp_gop_frame_t *free_frame;
5054

51-
ngx_chain_t *video_seq_header;
52-
ngx_chain_t *audio_seq_header;
5355
ngx_chain_t *meta;
5456
ngx_chain_t *free;
5557

0 commit comments

Comments
 (0)