Nginx-rtmp之配置项的管理

1. 概述

Nginx-rtmp 对 rtmp{...} 内的配置项划分了几个级别:

  • 直接隶属于 rtmp{} 块内的配置项称为 main 配置项。
  • 直接隶属于 server{} 块内的配置项称为 srv 配置项。
  • 直接隶属于 application{} 块内的配置项称为 app 配置项。
  • 直接隶属于 record{} 块内的配置项称为 rec 配置项。

    #define NGX_RTMP_MAIN_CONF              0x02000000
    #define NGX_RTMP_SRV_CONF               0x04000000
    #define NGX_RTMP_APP_CONF               0x08000000
    #define NGX_RTMP_REC_CONF               0x10000000

对于每一个 RTMP 模块,都必须实现 ngx_rtmp_module_t 接口。

typedef struct {
    /* 在解析 rtmp{...} 内的配置项前回调 */
    ngx_int_t             (*preconfiguration)(ngx_conf_t *cf);
    /* 解析完 rtmp{...} 内的所有配置项后回调 */
    ngx_int_t             (*postconfiguration)(ngx_conf_t *cf);

    /* 创建用于存储 RTMP 全局配置项的结构体,该结构体中的成员将保存直属于 rtmp{} 块的配置项参数 */
    void                 *(*create_main_conf)(ngx_conf_t *cf);
    /* 解析完 main 配置项后回调 */
    char                 *(*init_main_conf)(ngx_conf_t *cf, void *conf);

    /* 创建用于存储可同时出现在 main、srv 级别配置项的结构体,该结构体中
     * 的成员与 server 配置是相关联的 */
    void                 *(*create_srv_conf)(ngx_conf_t *cf);
    /* create_srv_conf 产生的结构体所要解析的配置项,可能同时出现在 main、srv 级别中,merge_srv_conf 
     * 方法可以把出现在 main 级别中的配置项合并到 srv 级别配置项中 */
    char                 *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
                                    void *conf);
                                    
    /* 创建用于存储可同时出现在 main、srv、app 级别配置项的结构体,
     * 该结构体中的成员与 application 配置是相关联的 */
    void                 *(*create_app_conf)(ngx_conf_t *cf);
    /* create_app_conf 产生的结构体所要解析的配置项,可能同时出现在 main、srv、app 级别中,
     * merge_app_conf 方法可以把分别出现在 main、srv 级别的配置项值合并到 app 级别的配置项中 */
    char                 *(*merge_app_conf)(ngx_conf_t *cf, void *prev,
                                    void *conf);
} ngx_rtmp_module_t;

ngx_rtmp_module_t 完全是围绕着配置项来进行的,每一个 RTMP 模块都将根据 main、srv、app 这些不同级别的配置项来
决定自己的行为。

2. 管理 RTMP 的配置项

在处理 rtmp{} 块内的 main 级别配置项时,对每一个 RTMP 模块来说,都会调用 create_main_conf、create_srv_cof、
create_app_conf 方法建立 3 个结构体,分别用于存储 RTMP 全局配置项、server 配置项、application 配置项。问:
rtmp{} 内的配置项本来就是 main 级别的,有了 create_main_conf 生成的结构体已经足够保存全局配置项参数了,为什么
还需要调用 create_srv_conf、create_app_conf 方法建立结构体呢?这是为了把同时出现在 rtmp{}、server{}、
application{} 内的相同配置项进行合并而做的准备。

对于 server{} 块内的配置项的处理,需要调用每个 RTMP 模块的 create_srv_conf 方法、create_app_conf 方法建立两个
结构体,分别用于存储 server、application 相关的配置项,其中 create_app_conf 产生的结构体仅用于合并 application
相关的配置项。

对于 application 块内的配置项则只需要调用每个 RTMP 模块的 create_app_conf 方法建立 1 个结构体即可。

ngx_rtmp_conf_ctx_t
typedef struct {
    /* 指向一个指针数组,数组中的每个成员都是由所有 RTMP 模块的 create_main_conf 方法创建的
     * 存放全局配置项的结构体,它们存放着解析直属于 rtmp{} 块内的 main 级别的配置项参数 */
    void                  **main_conf;
    
    /* 指向一个指针数组,数组中的每个成员都是由所有 RTMP 模块的 create_srv_conf 方法创建的与 
     * server 相关的结构体,它们或存放 main 级别配置项,或存放 srv 级别配置项,这与当前的 
     * ngx_rtmp_conf_ctx_t 是在解析 rtmp{} 或者 server{} 块时创建的有关 */
    void                  **srv_conf;
    
    /* 指向一个指针数组,数组中的每个成员都是由所有 RTMP 模块的 create_app_conf 方法创建的
     * application 相关的结构体,它们可能存放着 main、srv、app 级别的配置项,这与当前的
     * ngx_rtmp_conf_ctx_t 是在解析 rtmp{}、server{} 或者 application{} 块时创建的有关 */
    void                  **app_conf;
} ngx_rtmp_conf_ctx_t;

2.1 与配置项管理相关的两个模块

2.1.1 ngx_rtmp_module

ngx_rtmp_module 模块是 RTMP 的核心模块,定义了新的模块类型 NGX_RTMP_MODULE。这样的 RTMP 模块对于 ctx 上下文
使用了不同于核心模块、事件模块的新接口 ngx_rtmp_module_t。

2.1.1.1 感兴趣的配置项

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
};

可见,ngx_rtmp_module 核心模块仅是当 nginx.conf 中配置了 rtmp{} 块时指定了 ngx_rtmp_block 方法去解析。

2.1.1.2 上下文结构体

static ngx_core_module_t  ngx_rtmp_module_ctx = {
    ngx_string("rtmp"),
    NULL,
    NULL
};

这里仅定义了该核心模块的名字。

2.1.1.3 模块的定义

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
};

该结构体中仅实现了 init process 方法,该方法也仅是初始化了一个 ngx_rtmp_init_queue 的双向链表。

static ngx_int_t ngx_rtmp_init_process(ngx_cycle_t *cycle)
{
#if (nginx_version >= 1007005)
    ngx_queue_init(&ngx_rtmp_init_queue);
#endif
    return NGX_OK;
}

2.1.2 ngx_rtmp_core_module

ngx_rtmp_core_module 模块是所有 RTMP 模块中的第一个 RTMP 模块,即它的 ctx_index 为 0。RTMP 中主要的
配置块是在该模块中进行解析的,如 server{}、application{} 等。

2.1.2.1 感兴趣的配置项

static ngx_command_t  ngx_rtmp_core_commands[] = {
    
    /* 对应 server{},代表一个虚拟主机 */
    { ngx_string("server"),
      NGX_RTMP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
      ngx_rtmp_core_server,
      0,
      0,
      NULL },

    /* 需要监听的IP地址或端口 */
    { ngx_string("listen"),
      NGX_RTMP_SRV_CONF|NGX_CONF_TAKE12,
      ngx_rtmp_core_listen,
      NGX_RTMP_SRV_CONF_OFFSET,
      0,
      NULL },

    /* 具体的应用 */
    { ngx_string("application"),
      NGX_RTMP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
      ngx_rtmp_core_application,
      NGX_RTMP_SRV_CONF_OFFSET,
      0,
      NULL },

    { ngx_string("so_keepalive"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, so_keepalive),
      &ngx_conf_deprecated_so_keepalive },

    { ngx_string("timeout"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, timeout),
      NULL },

    { ngx_string("ping"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, ping),
      NULL },

    { ngx_string("ping_timeout"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, ping_timeout),
      NULL },

    { ngx_string("max_streams"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, max_streams),
      NULL },

    { ngx_string("ack_window"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, ack_window),
      NULL },

    /* RTMP 传输数据块的最大值 */
    { ngx_string("chunk_size"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, chunk_size),
      NULL },

    { ngx_string("max_message"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, max_message),
      NULL },

    { ngx_string("out_queue"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, out_queue),
      NULL },

    { ngx_string("out_cork"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, out_cork),
      NULL },

    { ngx_string("busy"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_flag_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, busy),
      NULL },

    /* time fixes are needed for flash clients */
    { ngx_string("play_time_fix"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_flag_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, play_time_fix),
      NULL },

    { ngx_string("publish_time_fix"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_flag_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, publish_time_fix),
      NULL },

    { ngx_string("buflen"),
      NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      NGX_RTMP_SRV_CONF_OFFSET,
      offsetof(ngx_rtmp_core_srv_conf_t, buflen),
      NULL },

      ngx_null_command
};

2.1.2.2 上下文结构体

static ngx_rtmp_module_t  ngx_rtmp_core_module_ctx = {
    NULL,                                   /* preconfiguration */
    NULL,                                   /* postconfiguration */
    ngx_rtmp_core_create_main_conf,         /* create main configuration */
    NULL,                                   /* init main configuration */
    ngx_rtmp_core_create_srv_conf,          /* create server configuration */
    ngx_rtmp_core_merge_srv_conf,           /* merge server configuration */
    ngx_rtmp_core_create_app_conf,          /* create app configuration */
    ngx_rtmp_core_merge_app_conf            /* merge app configuration */
};

这几个 create 方法主要是为存储配置项分配对应的内存空间,并初始化其值。

2.1.2.3 模块的定义

ngx_module_t  ngx_rtmp_core_module = {
    NGX_MODULE_V1,
    &ngx_rtmp_core_module_ctx,             /* module context */
    ngx_rtmp_core_commands,                /* module directives */
    NGX_RTMP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

2.1.2.4 该模块声明的几个主要结构体

ngx_rtmp_core_main_conf_t:
typedef struct {
    /* 存储指针的动态数组,每个指针指向ngx_http_core_srv_conf_t结构体的地址,
     * 也就是其成员类型为ngx_rtmp_core_srv_conf_t** */
    ngx_array_t             servers;    /* ngx_rtmp_core_srv_conf_t */
    /* 每一个 listen 对应一个 ngx_rtmp_listen_t 结构体,
     * 该数组保存着 rtmp{} 下的所有 listen 配置项 */
    ngx_array_t             listen;     /* ngx_rtmp_listen_t */

    /* rtmp 的事件数组,每个模块针对某个事件实现各自的方法都
     * 放入到该数组中 */
    ngx_array_t             events[NGX_RTMP_MAX_EVENT];

    ngx_hash_t              amf_hash;
    ngx_array_t             amf_arrays;
    
    /* 保存各 RTMP 模块在各自实现的 postconfiguration 方法中添加的
     * ngx_rtmp_amf_handler_t 结构体指针,该结构体为各 RTMP 模块当
     * 检测到客户端发来的数据有自己想要处理的 amf 字段时调用的回调
     * 方法 */
    ngx_array_t             amf;
} ngx_rtmp_core_main_conf_t;
ngx_rtmp_core_srv_conf_t:
typedef struct ngx_rtmp_core_srv_conf_s {
    /* 将同一个 server 块内多个表达 application 块的 ngx_rtmp_core_app_conf_t 
     * 结构体指针保存到该数组中 */
    ngx_array_t             applications; /* ngx_rtmp_core_app_conf_t */

    ngx_msec_t              timeout;
    ngx_msec_t              ping;
    ngx_msec_t              ping_timeout;
    ngx_flag_t              so_keepalive;
    ngx_int_t               max_streams;

    ngx_uint_t              ack_window;

    /* rtmp 数据传输块大小 */
    ngx_int_t               chunk_size;
    ngx_pool_t             *pool;
    ngx_chain_t            *free;
    ngx_chain_t            *free_hs;
    size_t                  max_message;
    ngx_flag_t              play_time_fix;
    ngx_flag_t              publish_time_fix;
    ngx_flag_t              busy;
    size_t                  out_queue;
    size_t                  out_cork;
    ngx_msec_t              buflen;

    /* 指向当前server块所属的 ngx_rtmp_conf_ctx_t 结构体 */
    ngx_rtmp_conf_ctx_t    *ctx;
} ngx_rtmp_core_srv_conf_t;
ngx_rtmp_core_app_conf_t:
typedef struct {
    ngx_array_t             applications; /* ngx_rtmp_core_app_conf_t */
    /* 该应用的名称,即 nginx.conf 中 application 后的表达式 */
    ngx_str_t               name;
    /* 指向所属 application 块内 ngx_rtmp_conf_ctx_t 结构体中 app_conf 指针数组,
     * 它保存着当前 application 块内所有 RTMP 模块 create_app_conf 方法产生的结
     * 构体指针 */
    void                  **app_conf;
} ngx_rtmp_core_app_conf_t;

2.2 参与搭建配置项管理的几个函数

2.2.1 ngx_rtmp_block

static char *ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    char                        *rv;
    ngx_uint_t                   i, m, mi, s;
    ngx_conf_t                   pcf;
    ngx_array_t                  ports;
    ngx_module_t               **modules;
    ngx_rtmp_listen_t           *listen;
    ngx_rtmp_module_t           *module;
    ngx_rtmp_conf_ctx_t         *ctx;
    ngx_rtmp_core_srv_conf_t    *cscf, **cscfp;
    ngx_rtmp_core_main_conf_t   *cmcf;

    /* 为存放 rtmp{} 块内的配置项分配一个上下文结构体,该结构体中管理着所有
     * rtmp{} 块内的配置项 */
    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_conf_ctx_t));
    if (ctx == NULL) {
        return NGX_CONF_ERROR;
    }

    /* 将该结构体指针赋给核心结构体 ngx_cycle_t 的成员 conf_ctx 指针数组中的相应位置  */
    *(ngx_rtmp_conf_ctx_t **) conf = ctx;

    /* count the number of the rtmp modules and set up their indices */

#if (nginx_version >= 1009011)

    ngx_rtmp_max_module = ngx_count_modules(cf->cycle, NGX_RTMP_MODULE);

#else

    /* 遍历所有的 RTMP 模块,初始化每个 RTMP 的 ctx_index 索引  */
    ngx_rtmp_max_module = 0;
    for (m = 0; ngx_modules[m]; m++) {
        if (ngx_modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        ngx_modules[m]->ctx_index = ngx_rtmp_max_module++;
    }

#endif


    /* the rtmp main_conf context, it is the same in the all rtmp contexts */
    /* 为所有 RTMP 模块都创建一个保存 rtmp{} 块下的 main 级别的配置项的指针数组 */
    ctx->main_conf = ngx_pcalloc(cf->pool,
                                 sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->main_conf == NULL) {
        return NGX_CONF_ERROR;
    }


    /*
     * the rtmp null srv_conf context, it is used to merge
     * the server{}s' srv_conf's
     */
    /* 为所有 RTMP 模块都创建一个保存 rtmp{} 块下的 srv 级别的配置项的指针数组 */
    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->srv_conf == NULL) {
        return NGX_CONF_ERROR;
    }


    /*
     * the rtmp null app_conf context, it is used to merge
     * the server{}s' app_conf's
     */
    /* 为所有 RTMP 模块都创建一个保存 rtmp{} 块下的 app 级别的配置项的指针数组 */
    ctx->app_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->app_conf == NULL) {
        return NGX_CONF_ERROR;
    }


    /*
     * create the main_conf's, the null srv_conf's, and the null app_conf's
     * of the all rtmp modules
     */

#if (nginx_version >= 1009011)
    modules = cf->cycle->modules;
#else
    modules = ngx_modules;
#endif

    /* 遍历 Nginx 中所有的 RTMP 模块,调用各自 RTMP 模块实现的
     * create_main_conf、create_srv_conf、create_app_conf 方法 */
    for (m = 0; modules[m]; m++) {
        if (modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[m]->ctx;
        mi = modules[m]->ctx_index;

        if (module->create_main_conf) {
            ctx->main_conf[mi] = module->create_main_conf(cf);
            if (ctx->main_conf[mi] == NULL) {
                return NGX_CONF_ERROR;
            }
        }

        if (module->create_srv_conf) {
            ctx->srv_conf[mi] = module->create_srv_conf(cf);
            if (ctx->srv_conf[mi] == NULL) {
                return NGX_CONF_ERROR;
            }
        }

        if (module->create_app_conf) {
            ctx->app_conf[mi] = module->create_app_conf(cf);
            if (ctx->app_conf[mi] == NULL) {
                return NGX_CONF_ERROR;
            }
        }
    }

    pcf = *cf;
    cf->ctx = ctx;

    /* 在解析配置项前,调用所有 RTMP 模块的 preconfiguration 方法
     * 在 nginx-rmtp-module 中,基本不需要实现此方法 */
    for (m = 0; modules[m]; m++) {
        if (modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[m]->ctx;

        if (module->preconfiguration) {
            if (module->preconfiguration(cf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
        }
    }

    /* parse inside the rtmp{} block */
    /* 开始解析 rtmp{}  */
    cf->module_type = NGX_RTMP_MODULE;
    cf->cmd_type    = NGX_RTMP_MAIN_CONF;
    rv = ngx_conf_parse(cf, NULL);

    if (rv != NGX_CONF_OK) {
        *cf = pcf;
        return rv;
    }


    /* init rtmp{} main_conf's, merge the server{}s' srv_conf's */

    /* 获取所属 rtmp{} 块下的第一个 RTMP 模块创建的 ngx_rtmp_core_main_conf_t 结构体 */
    cmcf = ctx->main_conf[ngx_rtmp_core_module.ctx_index];
    /* cmcf->servers 数组中保存着 rtmp{} 块下所有 server{} 块的
     * ngx_rtmp_core_srv_conf_t 结构体 */
    cscfp = cmcf->servers.elts;

    /* 遍历所有的 RTMP 模块 */
    for (m = 0; modules[m]; m++) {
        if (modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[m]->ctx;
        mi = modules[m]->ctx_index;

        /* init rtmp{} main_conf's */

        cf->ctx = ctx;
        
        if (module->init_main_conf) {
            rv = module->init_main_conf(cf, ctx->main_conf[mi]);
            if (rv != NGX_CONF_OK) {
                *cf = pcf;
                return rv;
            }
        }

        for (s = 0; s < cmcf->servers.nelts; s++) {

            /* merge the server{}s' srv_conf's */

            cf->ctx = cscfp[s]->ctx;

            /* 合并 main 和 srv 这两个级别下出现同样配置项的值,合并规则为,
             * 1. 若 main 和 srv 级别下都无该配置项,则使用默认值;
             * 2. 若 srv 级别下有该配置项的值,而以 srv 级别下的为准;
             * 3. 若 srv 级别下没有而 mian 级别下有该配置项的值,则才使用 main 级别下的值 */
            if (module->merge_srv_conf) {
                rv = module->merge_srv_conf(cf,
                                            ctx->srv_conf[mi],
                                            cscfp[s]->ctx->srv_conf[mi]);
                if (rv != NGX_CONF_OK) {
                    *cf = pcf;
                    return rv;
                }
            }

            if (module->merge_app_conf) {

                /* merge the server{}'s app_conf */

                /*ctx->app_conf = cscfp[s]->ctx->loc_conf;*/

                rv = module->merge_app_conf(cf,
                                            ctx->app_conf[mi],
                                            cscfp[s]->ctx->app_conf[mi]);
                if (rv != NGX_CONF_OK) {
                    *cf = pcf;
                    return rv;
                }

                /* merge the applications{}' app_conf's */
                /* cscf 指向 server{} 级别下 ngx_rtmp_conf_ctx_t 结构体的
                 * srv_conf 指针数组的第 1 个元素(即 srv_conf[0]),也即
                 * 该 server{} 块对应的 ngx_rtmp_core_srv_conf_t 结构体 */
                cscf = cscfp[s]->ctx->srv_conf[ngx_rtmp_core_module.ctx_index];

                /* 合并 server 级别下同时出现在 srv 和 app 中的相同配置项.
                 * 
                 * 参数含义:
                 * @ cf:指向全局的 ngx_conf_t 结构体指针
                 * @ cscf->applications: 指向该 server{} 块下所有 application{} 的数组首地址,
                 *          该数组每一个元素对应一个 application{},即 ngx_rtmp_core_app_conf_t 
                 * @ cscfp[s]->ctx->app_conf: 指向所属 server{} 块下的 ngx_rtmp_conf_ctx_t 
                 *          结构体的 app_conf 指针数组
                 * @ module:rtmp 模块
                 * @ mi: 该 rtmp 模块在所有 RTMP 模块中的序号 */
                rv = ngx_rtmp_merge_applications(cf, &cscf->applications,
                                            cscfp[s]->ctx->app_conf,
                                            module, mi);
                if (rv != NGX_CONF_OK) {
                    *cf = pcf;
                    return rv;
                }
            }

        }
    }

    /* 初始化 ngx_rtmp_core_main_conf_t 结构体下的 
     * events 数组和 amf 数组 */
    if (ngx_rtmp_init_events(cf, cmcf) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    /* 调用所有模块的 postconfiguration 方法 */
    for (m = 0; modules[m]; m++) {
        if (modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[m]->ctx;

        /* 每个 rtmp 模块想要执行的动作都是通过该方法插入的 */
        if (module->postconfiguration) {
            if (module->postconfiguration(cf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
        }
    }

    *cf = pcf;

    /* 初始化接收到相应的rtmp消息时执行的回调函数 */
    if (ngx_rtmp_init_event_handlers(cf, cmcf) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_rtmp_conf_port_t))
        != NGX_OK)
    {
        return NGX_CONF_ERROR;
    }

    /* cmcf->listen 数组保存着 rtmp{} 下所有需要监听的端口 */
    listen = cmcf->listen.elts;

    for (i = 0; i < cmcf->listen.nelts; i++) {
        if (ngx_rtmp_add_ports(cf, &ports, &listen[i]) != NGX_OK) {
            return NGX_CONF_ERROR;
        }
    }

    /* 主要生成 ngx_listening_t 结构体 */
    return ngx_rtmp_optimize_servers(cf, &ports);
}

Nginx 提供了两个接口可以在 ngx_cycle_t 核心结构体中找到对应模块的 main 级别下的配置结构体:

/* s 为 ngx_rtmp_session_t 类型的指针 */
#define ngx_rtmp_get_module_main_conf(s, module)                             \
    (s)->main_conf[module.ctx_index]

/* cf 是 ngx_conf_t 类型的指针 */
#define ngx_rtmp_conf_get_module_main_conf(cf, module)                       \
    ((ngx_rtmp_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
main 级别下的配置项存储示意图:

image

2.2.2 ngx_rtmp_core_server

static char *ngx_rtmp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    char                       *rv;
    void                       *mconf;
    ngx_uint_t                  m;
    ngx_conf_t                  pcf;
    ngx_module_t              **modules;
    ngx_rtmp_module_t          *module;
    ngx_rtmp_conf_ctx_t        *ctx, *rtmp_ctx;
    ngx_rtmp_core_srv_conf_t   *cscf, **cscfp;
    ngx_rtmp_core_main_conf_t  *cmcf;

    /* 建立属于这个 server 块的 ngx_rtmp_conf_ctx_t 结构体 */
    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_conf_ctx_t));
    if (ctx == NULL) {
        return NGX_CONF_ERROR;
    }

    rtmp_ctx = cf->ctx;
    /* ctx->main_conf 指向所属的 rtmp 块下的 ngx_rtmp_conf_ctx_t 结构体的 main_conf 指针数组 */
    ctx->main_conf = rtmp_ctx->main_conf;

    /* ctx->srv_conf 和 ctx->app_conf 要重新分配指针数组 */
    
    /* the server{}'s srv_conf */
    
    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->srv_conf == NULL) {
        return NGX_CONF_ERROR;
    }

    ctx->app_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->app_conf == NULL) {
        return NGX_CONF_ERROR;
    }

#if (nginx_version >= 1009011)
    modules = cf->cycle->modules;
#else
    modules = ngx_modules;
#endif

    /* 循环调用所有 RTMP 模块的 create_srv_conf 方法和 create_app_conf 方法 */
    for (m = 0; modules[m]; m++) {
        if (modules[m]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[m]->ctx;

        if (module->create_srv_conf) {
            mconf = module->create_srv_conf(cf);
            if (mconf == NULL) {
                return NGX_CONF_ERROR;
            }

            ctx->srv_conf[modules[m]->ctx_index] = mconf;
        }

        if (module->create_app_conf) {
            mconf = module->create_app_conf(cf);
            if (mconf == NULL) {
                return NGX_CONF_ERROR;
            }

            ctx->app_conf[modules[m]->ctx_index] = mconf;
        }
    }

    /* the server configuration context */
    
    /* 第一个 RTMP 模块就是 ngx_rtmp_core_module 模块,它在 create_srv_conf 方法将生成
     * ngx_rtmp_core_srv_conf_t 配置结构体,这个结构体对应着当前正在解析的 server 块,这
     * 时,将 ngx_rtmp_core_srv_conf_t 添加到全局的 ngx_rtmp_core_main_conf_t 结构体
     * 的 servers 动态数组中 */
    cscf = ctx->srv_conf[ngx_rtmp_core_module.ctx_index];
    cscf->ctx = ctx;

    cmcf = ctx->main_conf[ngx_rtmp_core_module.ctx_index];

    cscfp = ngx_array_push(&cmcf->servers);
    if (cscfp == NULL) {
        return NGX_CONF_ERROR;
    }

    *cscfp = cscf;


    /* parse inside server{} */

    pcf = *cf;
    /* 将cf->ctx 临时指向 server 块下的 ngx_rtmp_conf_ctx_t */
    cf->ctx = ctx;
    cf->cmd_type = NGX_RTMP_SRV_CONF;

    rv = ngx_conf_parse(cf, NULL);

    *cf = pcf;

    return rv;
}

Nginx 提供了两个接口可以在 ngx_cycle_t 核心结构体中找到对应模块的 srv 级别下的配置结构体:

/* s 为 ngx_rtmp_session_t 类型的指针 */
#define ngx_rtmp_get_module_srv_conf(s, module)  (s)->srv_conf[module.ctx_index]

/* cf 是 ngx_conf_t 类型的指针 */
#define ngx_rtmp_conf_get_module_srv_conf(cf, module)                        \
    ((ngx_rtmp_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
server 级别下的配置项存储示意图:

image

2.2.3 ngx_rtmp_core_application

static char *ngx_rtmp_core_application(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    char                       *rv;
    ngx_int_t                   i;
    ngx_str_t                  *value;
    ngx_conf_t                  save;
    ngx_module_t              **modules;
    ngx_rtmp_module_t          *module;
    ngx_rtmp_conf_ctx_t        *ctx, *pctx;
    ngx_rtmp_core_srv_conf_t   *cscf;
    ngx_rtmp_core_app_conf_t   *cacf, **cacfp;

    /* 建立所属 application 块下的 ngx_rtmp_conf_ctx_t 结构体 */
    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_conf_ctx_t));
    if (ctx == NULL) {
        return NGX_CONF_ERROR;
    }

    pctx = cf->ctx;
    /* 将 ctx->main_conf 指向所属的 server 块下 ngx_rtmp_conf_ctx_t 
     * 结构体的 main_conf 指针数组  */
    ctx->main_conf = pctx->main_conf;
    /* 将 ctx->srv_conf 指向所属的 server 块下的 ngx_rtmp_conf_ctx_t 
     * 结构体的 srv_conf 指针数组 */
    ctx->srv_conf = pctx->srv_conf;

    /* 而 ctx->app_conf 则需要重新分配内存空间 */
    ctx->app_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
    if (ctx->app_conf == NULL) {
        return NGX_CONF_ERROR;
    }

#if (nginx_version >= 1009011)
    modules = cf->cycle->modules;
#else
    modules = ngx_modules;
#endif

    /* 调用所有 RTMP 模块的 create_app_conf 方法 */
    for (i = 0; modules[i]; i++) {
        if (modules[i]->type != NGX_RTMP_MODULE) {
            continue;
        }

        module = modules[i]->ctx;

        if (module->create_app_conf) {
            ctx->app_conf[modules[i]->ctx_index] = module->create_app_conf(cf);
            if (ctx->app_conf[modules[i]->ctx_index] == NULL) {
                return NGX_CONF_ERROR;
            }
        }
    }

    /* 第一个 RTMP 模块就是 ngx_rtmp_core_module 模块,它在 create_srv_conf 方法将生成
     * ngx_rtmp_core_srv_conf_t 配置结构体,这个结构体对应着当前正在解析的 server 块,这
     * 时,将 ngx_rtmp_core_srv_conf_t 添加到全局的 ngx_rtmp_core_main_conf_t 结构体
     * 的 servers 动态数组中 */

    /* 第一个 RTMP 模块就是 ngx_rtmp_core_module 模块,它在 create_app_conf 方法将生成
     * ngx_rtmp_core_app_conf_t 配置结构体,这个结构体对应着当前正在解析的 application
     * 块,这时,将 ngx_rtmp_core_app_conf_t 添加到该 application 所属的 server 块下的
     * ngx_rtmp_conf_ctx_t 结构体的 srv_conf 指针数组的第1个位置(即 srv_conf[0])
     * 指向的 ngx_rtmp_core_srv_conf_t 结构体的 application 数组中 */
    cacf = ctx->app_conf[ngx_rtmp_core_module.ctx_index];
    cacf->app_conf = ctx->app_conf;

    value = cf->args->elts;

    /* 将应用的名字赋给 cacf->name */
    cacf->name = value[1];
    cscf = pctx->srv_conf[ngx_rtmp_core_module.ctx_index];

    cacfp = ngx_array_push(&cscf->applications);
    if (cacfp == NULL) {
        return NGX_CONF_ERROR;
    }

    *cacfp = cacf;

    save = *cf;
    cf->ctx = ctx;
    cf->cmd_type = NGX_RTMP_APP_CONF;

    rv = ngx_conf_parse(cf, NULL);

    *cf= save;

    return rv;
}

Nginx 提供了两个接口可以在 ngx_cycle_t 核心结构体中找到对应模块的 app 级别下的配置结构体:

/* s 为 ngx_rtmp_session_t 类型的指针 */
#define ngx_rtmp_get_module_app_conf(s, module)  ((s)->app_conf ? \
    (s)->app_conf[module.ctx_index] : NULL)

/* cf 是 ngx_conf_t 类型的指针 */
#define ngx_rtmp_conf_get_module_app_conf(cf, module)                       \
    ((ngx_rtmp_conf_ctx_t *) cf->ctx)->app_conf[module.ctx_index]
application 级别下的配置项存储示意图:

image

2.2.4 ngx_rtmp_merge_applications

/* 合并 server 级别下同时出现在 srv 和 app 中的相同配置项.
 *
 * 参数含义:
 * @ cf:指向全局的 ngx_conf_t 结构体指针
 * @ applications: 指向该 server{} 块下所有 application{} 的数组首地址,
 *                 该数组每一个元素对应一个 application{},即 ngx_rtmp_core_app_conf_t 
 * @ app_conf: 指向所属 server{} 块下的 ngx_rtmp_conf_ctx_t 结构体
 *             的 app_conf 指针数组
 * @ module:rtmp 模块
 * @ mi: 该 rtmp 模块在所有 RTMP 模块中的序号 */
static char *ngx_rtmp_merge_applications(ngx_conf_t *cf, ngx_array_t *applications,
            void **app_conf, ngx_rtmp_module_t *module, ngx_uint_t ctx_index)
{
    char                           *rv;
    ngx_rtmp_conf_ctx_t            *ctx, saved;
    ngx_rtmp_core_app_conf_t      **cacfp;
    ngx_uint_t                      n;
    ngx_rtmp_core_app_conf_t       *cacf;

    /* 若该 server{} 块下没有 application{},则直接返回 */
    if (applications == NULL) {
        return NGX_CONF_OK;
    }

    /* ctx 指向 rtmp{} 块下的 ngx_rtmp_conf_ctx_t 结构体 */
    ctx = (ngx_rtmp_conf_ctx_t *) cf->ctx;
    saved = *ctx;

    /* 遍历 server{} 块下 所有的 application{} 块 */
    cacfp = applications->elts;
    for (n = 0; n < applications->nelts; ++n, ++cacfp) {

        /* 先将该 application{} 块所属的 ngx_rtmp_conf_ctx_t 结构体的
         * app_conf 指针数组赋给 ctx->app_conf */
        ctx->app_conf = (*cacfp)->app_conf;

        /* 合并同时出现在 server{} 块下和 application{} 块下配置项 */
        rv = module->merge_app_conf(cf, app_conf[ctx_index],
                (*cacfp)->app_conf[ctx_index]);
        if (rv != NGX_CONF_OK) {
            return rv;
        }

        /* 这里是合并嵌套 application 的情况 */
        cacf = (*cacfp)->app_conf[ngx_rtmp_core_module.ctx_index];
        rv = ngx_rtmp_merge_applications(cf, &cacf->applications,
                                         (*cacfp)->app_conf,
                                         module, ctx_index);
        if (rv != NGX_CONF_OK) {
            return rv;
        }
    }

    *ctx = saved;

    return NGX_CONF_OK;
}

2.2.5 ngx_rtmp_init_event_handlers

static ngx_int_t ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
{
    ngx_hash_init_t             calls_hash;
    ngx_rtmp_handler_pt        *eh;
    ngx_rtmp_amf_handler_t     *h;
    ngx_hash_key_t             *ha;
    size_t                      n, m;

    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
    };

    /* 初始化当接收到 pm_events 数组中包含的 RTMP 消息时调用的回调函数
     * 支持的 RTMP 消息有 chunk_size、abort、ack、ack_size、bandwidth */
    /* 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;
    }

    /* 初始化当接收到 amf_events 数组中包含的 amf 类型事件时调用的回调函数 */
    /* 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;

    /* init amf callbacks */
    ngx_array_init(&cmcf->amf_arrays, cf->pool, 1, sizeof(ngx_hash_key_t));

    /* cmcf->amf 数组是调用所有的 RTMP 模块的 postconfiguration 方法
     * 由各 RTMP 模块向该数组中添加自己想要处理 amf 回调方法,主要有
     * 三个 RTMP 模块会向该数组中添加,分别为 ngx_rtmp_cmd_module、
     * ngx_rtmp_codec_module、ngx_rtmp_relay_module */
    h = cmcf->amf.elts;
    for(n = 0; n < cmcf->amf.nelts; ++n, ++h) {
        ha = cmcf->amf_arrays.elts;
        for(m = 0; m < cmcf->amf_arrays.nelts; ++m, ++ha) {
            if (h->name.len == ha->key.len
                    && !ngx_strncmp(h->name.data, ha->key.data, ha->key.len))
            {
                break;
            }
        }
        /* 若 amf 数组中没有存在与 amf_arrays 中相同的 amf 字符串,
         * 则需要重新为该 h->name 进行散列 */
        if (m == cmcf->amf_arrays.nelts) {
            ha = ngx_array_push(&cmcf->amf_arrays);
            ha->key = h->name;
            /* 根据关键字散列出映射的槽 */
            ha->key_hash = ngx_hash_key_lc(ha->key.data, ha->key.len);
            ha->value = ngx_array_create(cf->pool, 1,
                    sizeof(ngx_rtmp_handler_pt));
            if (ha->value == NULL) {
                return NGX_ERROR;
            }
        }

        eh = ngx_array_push((ngx_array_t*)ha->value);
        *eh = h->handler;
    }

    /* 指向 amf_hash 散列表 */
    calls_hash.hash = &cmcf->amf_hash;
    calls_hash.key = ngx_hash_key_lc;
    calls_hash.max_size = 512;
    calls_hash.bucket_size = ngx_cacheline_size;
    calls_hash.name = "amf_hash";
    calls_hash.pool = cf->pool;
    calls_hash.temp_pool = NULL;

    /* 初始化该散列表 */
    if (ngx_hash_init(&calls_hash, cmcf->amf_arrays.elts, cmcf->amf_arrays.nelts)
            != NGX_OK)
    {
        return NGX_ERROR;
    }

    return NGX_OK;
}

猜你喜欢

转载自www.cnblogs.com/jimodetiantang/p/8967676.html