** 系列文章**:
Nginx rtmp 推流(publish) 解析_fdsafwagdagadg6576的专栏-CSDN博客
Nginx rtmp 拉流播放(play)_fdsafwagdagadg6576的专栏-CSDN博客
Nginx rtmp 拉流(转发pull)_fdsafwagdagadg6576的专栏-CSDN博客
Nginx rtmp 转推_fdsafwagdagadg6576的专栏-CSDN博客
Nginx rtmp 点播流程_fdsafwagdagadg6576的专栏-CSDN博客
两种情况触发转推:
一种是在终端publish之后转推,一种是在relay server pull之后的转推.
ngx_rtmp_relay_send_publish-->ngx_rtmp_relay_play_local: 本地作为subscribe
ngx_rtmp_relay_send_play-->ngx_rtmp_relay_publish_local :pull同时转推
流程图
**流程步骤: **
1) 信令交互:
转推server的流程和终端publish的流程是一样的. (包括handshake,connect,createStream,publish都一样的流程)
右侧Nginx rtmp参见推流(publish)篇.上图绿色模块参见rela拉流(pull)篇,即转推同时也做了拉流.
2)流媒体audio/video
将远端(remote)server注册成为subscribe,接收本机ngx_rtmp_live_av转发的流。
对转发码流来说,把远端看成subscribe(play) 角色
源码分析
- 转推入口 ngx_rtmp_relay_publish
static ngx_int_t
ngx_rtmp_relay_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
{
//1.a 进程之间的relay
if (s->auto_pushed) {
goto next;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_relay_module);
//1.b //s->relay无论是转推还是relay ;init is 0,转推,relay 一次之后变成1.直接next
if (ctx && s->relay) {
goto next;
}
...
//1.c
for (n = 0; n < racf->pushes.nelts; ++n, ++t) {
...
//转推
if (ngx_rtmp_relay_push(s, &name, target) == NGX_OK) {
continue;
}
....
}
next:
//2 ngx_rtmp_live_publish
return next_publish(s, v);
}
- s->auto_pushed : worker进程之间推流,直接到ngx_rtmp_live_publish
2)s->relay: 已经转推过了(同一个流只能publish一次),直接到ngx_rtmp_live_publish
3) 遍历所有推流server推流
4) 执行ngx_rtmp_live_publish转流
- 转推信令入口
ngx_rtmp_relay_on_result {
...
//转推是将push server作为play即subsribe注册到liveav.
//具体分成两步向上游发送publish消息,准备推流;将自身设置为play即subscribe
if (ngx_rtmp_relay_send_publish(s) != NGX_OK) {
return NGX_ERROR;
}
return ngx_rtmp_relay_play_local(s);
}