作者原创,转载请联系作者
本文主要介绍通过前文介绍的将rtmp模块编译进nginx情况下,启动nginx时rtmp模块主要做了哪些工作
Nginx的模块开发三段式
定义nginx模块需要定义三个变量:command,ctx,module。RTMP此三段式在rtmp.c文件中,模块参考代码如下:
static ngx_command_t ngx_rtmp_commands[] = {
{ ngx_string("rtmp"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_rtmp_block,
0,
0,
NULL },
ngx_null_command
};
static ngx_core_module_t ngx_rtmp_module_ctx = {
ngx_string("rtmp"),
NULL,
NULL
};
ngx_module_t ngx_rtmp_module = {
NGX_MODULE_V1,
&ngx_rtmp_module_ctx, /* module context */
ngx_rtmp_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_rtmp_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
RTMP模块如何启动?
RTMP模块的启动函数在ngx_rtmp_commands申明的ngx_rtmp_block()。下面主要讲解如何调用:
ngx_rtmp_commands的类型为ngx_command_t,其定义为:
struct ngx_command_s {
ngx_str_t name;
ngx_uint_t type;
char (set)(ngx_conf_t *cf, ngx_command_t cmd, voidconf);
ngx_uint_t conf;
ngx_uint_t offset;
void *post;
};
rtmp_block对应是set指针-
set的是在nginx启动的时候,ngx_conf_handler()中调用,其简化代码如下。可以看出,其主要根据编译时生成的ngx_modules变量,以此取出module定义的command和ctx,调用command的set函数依次启动各个模块
for (i = 0; ngx_modules[i]; i++) {
cmd = ngx_modules[i]->commands;
for ( /* void / ; cmd->name.len; cmd++) {
/ set up the directive's configuration context */
conf = NULL;
if (cmd->type & NGX_DIRECT_CONF) {
conf = ((void **) cf->ctx)[ngx_modules[i]->index];
} else if (cmd->type & NGX_MAIN_CONF) {
conf = &(((void **) cf->ctx)[ngx_modules[i]->index]);
} else if (cf->ctx) {
confp = *(void **) ((char *) cf->ctx + cmd->conf);
if (confp) {
conf = confp[ngx_modules[i]->ctx_index];
}
}
rv = cmd->set(cf, cmd, conf);
}
}
调用链如下:
static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last) ->char * ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename) -> char * ngx_conf_param(ngx_conf_t *cf) -> ngx_cycle_t * ngx_init_cycle(ngx_cycle_t *old_cycle) -> int ngx_cdecl main(int argc, char *const *argv)
RTMP启动做了什么?
RTMP启动具体做了什么其实就是ngx_rtmp_block()的功能,此处简略说明:
1、 计算RTMP模块数并设置每个模块的上下文索引
2、 为每个RTMP模块创建main_conf
3、 为每个RTMP模块创建srv_conf
4、 为每个RTMP模块创建app_conf
5、 调用各个RTMP模块preconfiguration
6、 初始化各个RTMP模块 init_main_conf
7、 初始化各个RTMP模块各个APP的merge_srv_conf
8、 初始化各个RTMP模块各个APP的merge_app_conf
9、 初始化各个RTMP服务事件
10、调用各个模块postconfiguration,主要是注册回调,具体如下
- NGX_RTMP_CONNECT
注册模块:ngx_rtmp_limit_module
注册回调:ngx_rtmp_limit_connect; - NGX_RTMP_HANDSHAKE_DONE
注册模块:ngx_rtmp_relay_module中初始化
注册回调:ngx_rtmp_relay_handshake_done - NGX_RTMP_DISCONNECT
注册模块:ngx_rtmp_cmd_module
注册回调:ngx_rtmp_cmd_disconnect_init;
注册模块:ngx_rtmp_codec_module
注册回调:ngx_rtmp_codec_disconnect;
注册模块:ngx_rtmp_limit_module
注册回调:ngx_rtmp_limit_disconnect;
注册模块:ngx_rtmp_log_module
注册回调:ngx_rtmp_log_disconnect;
注册模块:ngx_rtmp_netcall_module
注册回调:ngx_rtmp_netcall_disconnect; - NGX_RTMP_MSG_AUDIO
注册模块:ngx_rtmp_codec_module
注册回调:ngx_rtmp_codec_av
注册模块:ngx_rtmp_dash_module
注册回调:ngx_rtmp_dash_video
注册模块:ngx_rtmp_hls_module
注册回调:ngx_rtmp_hls_audio
注册模块:ngx_rtmp_live_module
注册回调:ngx_rtmp_live_av
注册模块:ngx_rtmp_record_module
注册回调:ngx_rtmp_record_av - NGX_RTMP_MSG_VIDEO
注册模块:ngx_rtmp_codec_module
注册回调:ngx_rtmp_codec_av
注册模块:ngx_rtmp_dash_module
注册回调:ngx_rtmp_dash_video
注册模块:ngx_rtmp_hls_module
注册回调:ngx_rtmp_hls_audio
注册模块:ngx_rtmp_live_module
注册回调:ngx_rtmp_live_av
注册模块:ngx_rtmp_record_module
注册回调:ngx_rtmp_record_av
11、 初始化事件处理,主要是AMF消息,特殊消息,处理回调注册
static size_t pm_events[] = {
NGX_RTMP_MSG_CHUNK_SIZE,
NGX_RTMP_MSG_ABORT,
NGX_RTMP_MSG_ACK,
NGX_RTMP_MSG_ACK_SIZE,
NGX_RTMP_MSG_BANDWIDTH
};
static size_t amf_events[] = {
NGX_RTMP_MSG_AMF_CMD,
NGX_RTMP_MSG_AMF_META,
NGX_RTMP_MSG_AMF_SHARED,
NGX_RTMP_MSG_AMF3_CMD,
NGX_RTMP_MSG_AMF3_META,
NGX_RTMP_MSG_AMF3_SHARED
};
/* init standard protocol events */
for(n = 0; n < sizeof(pm_events) / sizeof(pm_events[0]); ++n) {
eh = ngx_array_push(&cmcf->events[pm_events[n]]);
*eh = ngx_rtmp_protocol_message_handler;
}
/* init amf events */
for(n = 0; n < sizeof(amf_events) / sizeof(amf_events[0]); ++n) {
eh = ngx_array_push(&cmcf->events[amf_events[n]]);
*eh = ngx_rtmp_amf_message_handler;
}
/* init user protocol events */
eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_USER]);
*eh = ngx_rtmp_user_message_handler;
/* aggregate to audio/video map */
eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AGGREGATE]);
*eh = ngx_rtmp_aggregate_message_handler;
特别需要提出的是在ngx_rtmp_cmd_module模块,对相应的用户控制消息进行注册,代码如下:
static ngx_rtmp_amf_handler_t ngx_rtmp_cmd_map[] = {
{ ngx_string("connect"), ngx_rtmp_cmd_connect_init },
{ ngx_string("createStream"), ngx_rtmp_cmd_create_stream_init },
{ ngx_string("closeStream"), ngx_rtmp_cmd_close_stream_init },
{ ngx_string("deleteStream"), ngx_rtmp_cmd_delete_stream_init },
{ ngx_string("publish"), ngx_rtmp_cmd_publish_init },
{ ngx_string("play"), ngx_rtmp_cmd_play_init },
{ ngx_string("play2"), ngx_rtmp_cmd_play2_init },
{ ngx_string("seek"), ngx_rtmp_cmd_seek_init },
{ ngx_string("pause"), ngx_rtmp_cmd_pause_init },
{ ngx_string("pauseraw"), ngx_rtmp_cmd_pause_init },
};
......
ncalls = sizeof(ngx_rtmp_cmd_map) / sizeof(ngx_rtmp_cmd_map[0]);
ch = ngx_array_push_n(&cmcf->amf, ncalls);
bh = ngx_rtmp_cmd_map;
for(n = 0; n < ncalls; ++n, ++ch, ++bh) {
*ch = *bh;
}
......
12、开始各个RTMP服务侦听,注册连接到时时执行ngx_rtmp_init_connection的回调