Asterisk内核框架(转帖)
Asterisk是一个开源的pbx系统,在公开的资料中,很难找到asterisk内核系统的详细描述。因此,很有必要写一篇内核框架的描述文档
,作为内部培训文档,相互学习提高。
本文主要从三个层面来描述asterisk内核,即asterisk内核模块、内核启动过程、基本呼叫流程。
一、asterisk内核模块
Asterisk由内部核心和外围动态可加载模块组成。内部核心由以下六个部分组成:PBX交换核心模块(PBX Switching Core)、调度和I/O管
理模块(Scheduler and I/O Manager)、应用调用模块(Application Launcher)、编解码转换模块(Codec Translator)、动态模块加载器模
块(Dynamic Module Loader)和CDR生成模块(CDR Core)。
外围动态可加载模块包括以App_开始的Applications、以Func_开始的Functions、以Res_开始的Resources、以Chan_开始的channels、
以Codec_开始的codec编解码模块等。
1、内核模块
1) PBX交换核心模块(PBX Switching Core):
pbx.c
pbx.c是asterisk的核心模块,每路呼叫都需要经过它调度。pbx实现了builtin applications,也就是内置的应用,比如最常见的
Answer,Hangup, Background,Wait等等。
struct ast_app是一个关键数据结构,它定义了注册builtin applications的结构。
load_pbx函数用来注册builtin applications和一些命令行CLI命令(每个模块都有些CLI命令)。该函数在系统启动时被调用。
pbx_exec是Answer/BackGround/Busy/Goto/GotoIf/Hangup/Set等builtin applications的执行入口函数,它被pbx_extension_helper调用
。
ast_pbx_start函数是每路呼叫的起点。
2) 调度和I/O管理模块(Scheduler and I/O Manager):
Channel.c:
Channel.c/channel.h定义了channel操作的结构体和接口函数。
struct ast_channel_tech结构体是所有channel都要用到的关键结构体,它定义channel操作的一系列回调函数指针,如call、hangup、
answer等。每个channel模块都会定义ast_channel_tech的实体,并将各自的回调函数赋值给它。例如chan_sip.c中定义如下:
/*! \brief Definition of this channel for PBX channel registration */
static const struct ast_channel_tech sip_tech = {
.type = "SIP",
.description = "Session Initiation Protocol (SIP)",
.capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
.requester = sip_request_call,
.devicestate = sip_devicestate,
.call = sip_call,
.hangup = sip_hangup,
.answer = sip_answer,
.read = sip_read,
.write = sip_write,
.write_video = sip_write,
.indicate = sip_indicate,
.transfer = sip_transfer,
.fixup = sip_fixup,
.send_digit_begin = sip_senddigit_begin,
.send_digit_end = sip_senddigit_end,
.bridge = ast_rtp_bridge,
.send_text = sip_sendtext,
.func_channel_read = acf_channel_read,
};
ast_call、ast_hangup、ast_answer等函数分别实现ast_channel_tech中的call、hangup、answer等回调函数的调用。
struct ast_channel结构体定义了channel的上下文参数,它是每个参与呼叫的channel必不可少的,都会调用ast_channel_alloc来申请
ast_channel。
io.c
io.c实现了asterisk跟外部交互时的I/O管理,如chan_sip为了从外部接收SIP信令,调用ast_io_add添加IO接口,并调用ast_io_wait实现外
部消息接收。
3) 应用调用模块(Application Launcher):
在pbx.c中定义了一系列的应用调用接口。
applications模块定义了application回调函数并注册后,在pbx.c中通过应用调用接口回调执行。
应用调用接口的关键函数是pbx_extension_helper,它执行dialplan,在cli上打印“Executing ……”,并抛出ami event事件,同时调用
pbx_exec执行application回调函数。
4) 编解码转换模块(Codec Translator):
Translate.c:
struct ast_translator:编码转换描述结构体,它定义了编码转换的名称、回调函数、运行时选项。
struct ast_trans_pvt:编码转换上下文描述结构体。
ast_register_translator:编码转换注册接口函数,供各编码模块调用,注册struct ast_translator类型的结构体变量。
ast_unregister_translator:编码转换注销函数
ast_translate:编码转换的执行函数。
codec_gsm.c/codec_...:对应各种编码的编解码执行模块,如g.711alaw/g.711ulaw/gsm等。
5) 动态模块加载器模块(Dynamic Module Loader):
该模块主要是Module.h。
Module.h中定义了struct ast_module_info结构,用来保存各模块的注册、注销回调函数,以及模块描述信息。
load_module、unload_module,每个应用模块的注册、注销函数,由各个模块自行定义为static函数。
AST_MODULE_INFO_STANDARD:注册接口、注销接口、模块描述信息等模块信息的登记接口。它是一个宏定义,动态模块调用它时,首先定义类
型为ast_module_info的__mod_info静态结构变量,保存模块信息,并定义__attribute__ ((constructor)) __reg_module和
__attribute__ ((destructor)) __unreg_module,在程序启动和退出时调用。
6) CDR生成模块(CDR Core):
Cdr.c:
ast_cdr_register:cdr driver注册,供cdr_mysql等调用,注册话单保存的回调函数。
ast_cdr_engine_init:CDR模块初始化,注册cdr status、加载cdr.conf、启动CDR线程。
ast_cdr_detach:产生话单的接口函数,呼叫结束时被调用。
2、外围可加载模块:
1) Applications
以app_开始的模块,如app_dial.c、app_db.c、app_queue.c、app_record.c、app_meetme.c等,代码保存在apps目录中。每个application
模块都定义了load_module函数和unload_module函数,分别用来注册和注销application。
load_module函数调用ast_register_application函数,注册application命令,例如app_dial模块注册Dial:res =
ast_register_application(app, dial_exec, synopsis, descrip)。
unload_module函数调用ast_unregister_application函数,注销application命令。
每个application模块都会使用AST_MODULE_INFO_STANDARD宏来登记模块信息__mod_info。AST_MODULE_INFO_STANDARD将load_module和
unload_module注册为回调函数,供module load/unload/reload调用。
2) Channel
以chan_开始的模块,如chan_sip.c、chan_h323.c、chan_mgcp.c 、chan_iax2.c、 chan_zap.c等,对应代码保存在channels目录中。
channel注册、注销过程和application基本类似。由于每个channel需要和外部交互,都会在load_module中启用do_monitor线程来侦听外部
tcp/udp端口,接收外部消息。
每个channel也定义了各自的cli命令和Function命令,例如chan_sip定义了sip debug/history/no/notify/prune/ reload/set/show等cli
命令和SIP_HEADER、CHECKSIPDOMAIN、SIPPEER、SIPCHANINFO等Function命令。
3) Functions
以Fun_开始的模块,例如Fun_db.c、func_moh.c、func_cdr.c等,对应代码保存在funcs目录中。
Function注册、注销过程也和application类似。
每个Function模块也定义了各自的Function命令,例如Fun_db.c就定义了DB、DB_EXISTS、DB_DELETE等Function命令。