作者原创,转载请联系作者
经过建立Netconnection和CreateStream之后,客户端就可以进行相应的流操作,本文讲解推流操作。
涉及模块
针对推流操作,NGX-RTMP处理比较复杂,涉及的模块也比较多,先罗列如下。至于回调如何注册、如何调用请参考前文。
- 模块名:ngx_rtmp_access_module
回 调:ngx_rtmp_access_publish - 模块名:ngx_rtmp_auto_push_module
回 调:ngx_rtmp_auto_push_publish - 模块名:ngx_rtmp_cfms_module
回 调:ngx_rtmp_cfms_publish - 模块名:ngx_rtmp_cmd_module
回 调:ngx_rtmp_cmd_publish - 模块名:ngx_rtmp_dash_publish
回 调:ngx_rtmp_dash_publish - 模块名:ngx_rtmp_exec_module
回 调:ngx_rtmp_exec_publish - 模块名:ngx_rtmp_hls_module
回 调:ngx_rtmp_hls_publish - 模块名:ngx_rtmp_live_module
回 调:ngx_rtmp_live_publish - 模块名:ngx_rtmp_log_module
回 调:ngx_rtmp_log_publish - 模块名:ngx_rtmp_notify_module
回 调:ngx_rtmp_notify_publish - 模块名:ngx_rtmp_record_module
回 调:ngx_rtmp_record_publish - 模块名:ngx_rtmp_relay_module
回 调:ngx_rtmp_relay_publish
具体处理
在这小节主要弄懂下面几个问题:
-
上述那些模块如何顺序调用?
在注册回调时,将原回调ngx_rtmp_publish保存起来为next_publish。在ngx_rtmp_live_publish回调处理完毕后调用next_publish,从而下一个模块继续调用。见代码:next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_live_publish; 上述那些模块调用顺序如何?
这个问题较简单,查看我的前文《RTMP添加到NGINX》中描述,主要根据编译时生成的 ngx_module_t *ngx_modules[] 变量在启动的时候一次执行-
做什么什么工作?
因为模块比较多,在此挑选ngx_rtmp_live_module进行叙述,其他模块处理细节请各位看官阅读代码,如有问题可以消息我,如有必要可以再撰文描述。合法性校验主要有:
NetStream.Play.StreamNotFound
NetStream.Publish.BadName-
设置ctx
ctx->stream = *stream; ctx->publishing = publisher; ctx->next = (*stream)->ctx; ctx->cs[0].csid = NGX_RTMP_CSID_VIDEO; ctx->cs[1].csid = NGX_RTMP_CSID_AUDIO;
设置状态ngx_rtmp_live_set_status,包括NetStream.Play.Start和NetStream.Play.PublishNotify,并发送给客户端
给pulbisher回复消息NetStream.Publish.Start
客户端收到 NetStream.Publish.Start后,给服务端发送Audio和/或Video的报文,具体如何发送详情后续再撰文叙述
-
服务端接受AUDIO/VIDEO
在nginx启动的时候,NGX-RTMP通过将处理AUDIO/VIDEO的回调注册,即NGX_RTMP_MSG_VIDEO和NGX_RTMP_MSG_AUDIO的回调,具体见下:- 模块名:ngx_rtmp_codec_module
回 调:ngx_rtmp_codec_av - 模块名:ngx_rtmp_dash_module
回 调:ngx_rtmp_dash_video&ngx_rtmp_dash_audio - 模块名:ngx_rtmp_hls_module
回 调:ngx_rtmp_hls_video&ngx_rtmp_hls_audio - 模块名:ngx_rtmp_live_module
回 调:ngx_rtmp_live_av - 模块名:ngx_rtmp_record_module
回 调:ngx_rtmp_record_av
- 模块名:ngx_rtmp_codec_module
因为模块比较多,在此挑选ngx_rtmp_live_module进行叙述,即ngx_rtmp_live_av,而ngx_rtmp_live_av其注册和调用时机请查看前文,不在叙述。其主要做了如下工作:
- 构造回应消息,主要在函数ngx_rtmp_prepare_message(...)中进行处理:
探测包大小:
mlen = 0;
nbufs = 0;
for(l = out; l; l = l->next) {
mlen += (l->buf->last - l->buf->pos);
++nbufs;
}
fmt = 0;
if (lh && lh->csid && h->msid == lh->msid) {
++fmt;
if (h->type == lh->type && mlen && mlen == lh->mlen) {
++fmt;
if (h->timestamp == lh->timestamp) {
++fmt;
}
}
timestamp = h->timestamp - lh->timestamp;
} else {
timestamp = h->timestamp;
}
构造base header
*p = (fmt << 6);
if (h->csid >= 2 && h->csid <= 63) {
*p++ |= (((uint8_t)h->csid) & 0x3f);
} else if (h->csid >= 64 && h->csid < 320) {
++p;
*p++ = (uint8_t)(h->csid - 64);
} else {
*p++ |= 1;
*p++ = (uint8_t)(h->csid - 64);
*p++ = (uint8_t)((h->csid - 64) >> 8);
}
构造消息头
if (fmt <= 2) {
pp = (u_char*)×tamp;
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
if (fmt <= 1) {
pp = (u_char*)&mlen;
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
*p++ = h->type;
if (fmt == 0) {
pp = (u_char*)&h->msid;
*p++ = pp[0];
*p++ = pp[1];
*p++ = pp[2];
*p++ = pp[3];
}
}
}
构造extended header
if (ext_timestamp) {
pp = (u_char*)&ext_timestamp;
*p++ = pp[3];
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
}
- 给所有subscribe广播
- send metadata(略)
- sync stream(略)
- send absolute codec header(略)
- send absolute packet(略)
- send relative packet(略)
- ngx_rtmp_update_bandwidth
ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen);
ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers);
ngx_rtmp_update_bandwidth(h->type == NGX_RTMP_MSG_AUDIO ?
&ctx->stream->bw_in_audio :
&ctx->stream->bw_in_video,
h->mlen);