Skip to content

Commit

Permalink
restructure meta
Browse files Browse the repository at this point in the history
  • Loading branch information
gnolizuh authored Sep 4, 2018
1 parent e47a542 commit 3fb55ad
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions ngx_rtmp_codec_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static void * ngx_rtmp_codec_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_codec_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf);
static ngx_int_t ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h);
static ngx_int_t ngx_rtmp_codec_construct_meta(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_codec_copy_meta(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in);
static ngx_int_t ngx_rtmp_codec_prepare_meta(ngx_rtmp_session_t *s,
Expand Down Expand Up @@ -196,11 +196,6 @@ ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ctx->meta = NULL;
}

if (ctx->meta_flv) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta_flv);
ctx->meta_flv = NULL;
}

return NGX_OK;
}

Expand Down Expand Up @@ -276,6 +271,9 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

if (header == NULL) {
return NGX_OK;
} else {
// when we got sequence header, try construct new meta if meta was not arrived.
ngx_rtmp_codec_construct_meta(s);
}

if (*header) {
Expand Down Expand Up @@ -808,6 +806,37 @@ ngx_rtmp_codec_dump_header(ngx_rtmp_session_t *s, const char *type,
#endif


static ngx_int_t
ngx_rtmp_codec_construct_meta(ngx_rtmp_session_t *s)
{
ngx_rtmp_codec_ctx_t *ctx;
ngx_rtmp_core_srv_conf_t *cscf;

ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (ctx == NULL) {
return NGX_OK;
}

// meta was arrived, we do not construct it.
if (ctx->meta_came) {
return NGX_OK;
}

ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"construct meta");

cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);

if (ctx->meta) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta);
ctx->meta = NULL;
}

// meta was not arrived, we construct depend on what we have.
return ngx_rtmp_codec_reconstruct_meta(s);
}


static ngx_int_t
ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h)
{
Expand Down Expand Up @@ -910,11 +939,6 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h)
ctx->meta = NULL;
}

if (ctx->meta_flv) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta_flv);
ctx->meta_flv = NULL;
}

v.width = ctx->width;
v.height = ctx->height;
v.duration = ctx->duration;
Expand All @@ -932,13 +956,6 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h)
return NGX_ERROR;
}

ctx->meta_flv = ngx_http_flv_append_shared_bufs(cscf, h, ctx->meta);
if (ctx->meta_flv == NULL) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta);
ctx->meta = NULL;
return NGX_ERROR;
}

return ngx_rtmp_codec_prepare_meta(s, 0);
}

Expand All @@ -963,17 +980,6 @@ ngx_rtmp_codec_copy_meta(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
return NGX_ERROR;
}

if (ctx->meta_flv) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta_flv);
}

ctx->meta_flv = ngx_http_flv_append_shared_bufs(cscf, h, in);
if (ctx->meta_flv == NULL) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta);
ctx->meta = NULL;
return NGX_ERROR;
}

return ngx_rtmp_codec_prepare_meta(s, h->timestamp);
}

Expand All @@ -991,11 +997,11 @@ ngx_rtmp_codec_prepare_meta(ngx_rtmp_session_t *s, uint32_t timestamp)
h.msid = NGX_RTMP_MSID;
h.type = NGX_RTMP_MSG_AMF_META;
h.timestamp = timestamp;
ngx_rtmp_prepare_message(s, &h, NULL, ctx->meta);

ngx_memzero(&ctx->meta_header, sizeof(ctx->meta_header));
ctx->meta_header = h;
ctx->meta_version = ngx_rtmp_codec_get_next_version();
if (!ctx->meta_came) ctx->meta_came = 1;

return NGX_OK;
}
Expand Down Expand Up @@ -1154,7 +1160,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

switch (cacf->meta) {
case NGX_RTMP_CODEC_META_ON:
return ngx_rtmp_codec_reconstruct_meta(s, h);
return ngx_rtmp_codec_reconstruct_meta(s);
case NGX_RTMP_CODEC_META_COPY:
return ngx_rtmp_codec_copy_meta(s, h, in);
}
Expand Down

0 comments on commit 3fb55ad

Please sign in to comment.