added sending metadata to clients

This commit is contained in:
Roman Arutyunyan 2012-07-02 13:42:56 +04:00
parent 51647d03b3
commit bf487dba0b
4 changed files with 168 additions and 10 deletions

View file

@ -11,11 +11,6 @@
static ngx_int_t ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf);
/* Global header version is used to identify
* incoming AAC/AVC header */
static ngx_uint_t header_version;
static ngx_rtmp_module_t ngx_rtmp_codec_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_codec_postconfiguration, /* postconfiguration */
@ -97,6 +92,20 @@ ngx_rtmp_get_video_codec_name(ngx_uint_t id)
}
static ngx_uint_t
ngx_rtmp_codec_get_next_version()
{
ngx_uint_t v;
static ngx_uint_t version;
do {
v = ++version;
} while (v == 0);
return v;
}
static ngx_int_t
ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
@ -131,6 +140,11 @@ ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ctx->aac_pheader = NULL;
}
if (ctx->meta) {
ngx_rtmp_free_shared_chain(cscf, ctx->meta);
ctx->meta = NULL;
}
return NGX_OK;
}
@ -229,9 +243,131 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_prepare_message(s, &ch, &lh, *pheader);
/* don't want zero as version value */
do {
*version = ++header_version;
} while (*version == 0);
*version = ngx_rtmp_codec_get_next_version();
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_codec_update_meta(ngx_rtmp_session_t *s)
{
ngx_rtmp_codec_ctx_t *ctx;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_int_t rc;
ngx_rtmp_header_t h;
static struct {
double width;
double height;
double duration;
double frame_rate;
double video_data_rate;
double video_codec_id;
double audio_data_rate;
double audio_codec_id;
u_char profile[32];
u_char level[32];
} v;
static ngx_rtmp_amf_elt_t out_inf[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_string("width"),
&v.width, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("height"),
&v.height, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("duration"),
&v.duration, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("framerate"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("fps"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("videodatarate"),
&v.video_data_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("videocodecid"),
&v.video_codec_id, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("audiodatarate"),
&v.audio_data_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("audiocodecid"),
&v.audio_codec_id, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_string("profile"),
&v.profile, sizeof(v.profile) },
{ NGX_RTMP_AMF_STRING,
ngx_string("level"),
&v.level, sizeof(v.level) },
};
static ngx_rtmp_amf_elt_t out_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
"onMetaData", 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
NULL, 0 },
{ NGX_RTMP_AMF_OBJECT,
ngx_null_string,
out_inf, sizeof(out_inf) },
};
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (ctx == NULL) {
return NGX_OK;
}
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;
}
v.width = ctx->width;
v.height = ctx->height;
v.duration = ctx->duration;
v.frame_rate = ctx->frame_rate;
v.video_data_rate = ctx->video_data_rate;
v.video_codec_id = ctx->video_codec_id;
v.audio_data_rate = ctx->audio_data_rate;
v.audio_codec_id = ctx->audio_codec_id;
ngx_memcpy(v.profile, ctx->profile, sizeof(ctx->profile));
ngx_memcpy(v.level, ctx->level, sizeof(ctx->level));
rc = ngx_rtmp_append_amf(s, &ctx->meta, NULL, out_elts,
sizeof(out_elts) / sizeof(out_elts[0]));
if (rc != NGX_OK || ctx->meta == NULL) {
return NGX_ERROR;
}
ngx_memzero(&h, sizeof(h));
h.csid = 5;
h.msid = NGX_RTMP_LIVE_MSID;
h.type = NGX_RTMP_MSG_AMF_META;
ngx_rtmp_prepare_message(s, &h, NULL, ctx->meta);
ctx->meta_version = ngx_rtmp_codec_get_next_version();
return NGX_OK;
}
@ -385,6 +521,8 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_get_audio_codec_name(ctx->audio_codec_id),
ctx->audio_codec_id);
ngx_rtmp_codec_update_meta(s);
return NGX_OK;
}

View file

@ -70,6 +70,9 @@ typedef struct {
/* prepared headers (for live streaming) */
ngx_chain_t *avc_pheader;
ngx_chain_t *aac_pheader;
ngx_chain_t *meta;
ngx_uint_t meta_version;
} ngx_rtmp_codec_ctx_t;

View file

@ -281,7 +281,8 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
{
ngx_rtmp_live_ctx_t *ctx, *pctx;
ngx_rtmp_codec_ctx_t *codec_ctx;
ngx_chain_t *out, *peer_out, *header_out, *pheader_out;
ngx_chain_t *out, *peer_out, *header_out,
*pheader_out, *meta;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_session_t *ss;
@ -289,7 +290,7 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_uint_t prio, peer_prio;
ngx_uint_t peers, dropped_peers;
size_t header_offset;
ngx_uint_t header_version;
ngx_uint_t header_version, meta_version;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
@ -353,6 +354,8 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
pheader_out = NULL;
header_offset = 0;
header_version = 0;
meta = NULL;
meta_version = 0;
if (codec_ctx) {
if (h->type == NGX_RTMP_MSG_AUDIO) {
if (codec_ctx->aac_pheader) {
@ -369,6 +372,10 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
header_version = codec_ctx->avc_version;
}
}
if (codec_ctx->meta) {
meta = codec_ctx->meta;
meta_version = codec_ctx->meta_version;
}
}
/* broadcast to all subscribers */
@ -413,6 +420,15 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
}
}
/* send metadata if newer exists */
if (meta && meta_version != pctx->meta_version) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: sending metadata");
if (ngx_rtmp_send_message(ss, meta, prio) == NGX_OK) {
pctx->meta_version = meta_version;
}
}
/* push buffered data */
peer_prio = prio;
if (ngx_rtmp_send_message(ss, out, peer_prio) != NGX_OK) {

View file

@ -39,6 +39,7 @@ struct ngx_rtmp_live_ctx_s {
uint32_t last_video;
ngx_uint_t aac_version;
ngx_uint_t avc_version;
ngx_uint_t meta_version;
};