freeswitch源码阅读 之 sofia模块

31 篇文章 4 订阅

  sofia模块在freeswitch中的位置非常重要, 所有的sip通话都和它有关, 那么我们就看一下该模块的执行流程。

一、 实现的功能:

1. sip注册;

2. 呼叫;

3. Presence;

4. SLA, 等。


二、 主要的方法, 有三个, 分别为:

#define SWITCH_MODULE_LOAD_FUNCTION(name) switch_status_t name SWITCH_MODULE_LOAD_ARGS
#define SWITCH_MODULE_RUNTIME_FUNCTION(name) switch_status_t name SWITCH_MODULE_RUNTIME_ARGS
#define SWITCH_MODULE_SHUTDOWN_FUNCTION(name) switch_status_t name SWITCH_MODULE_SHUTDOWN_ARGS
</pre><pre name="code" class="cpp">SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_sofia_shutdown);
SWITCH_MODULE_DEFINITION(mod_sofia, mod_sofia_load, mod_sofia_shutdown, NULL);

这三个方法依次分别为:模块加载, 卸载 ,运行 。


三、 分析 mod_sofia_load方法:

1. sofia模块分别包含 api命令行可执行命令, application应用app, 即供拨号计划等其它模块调用的,提供完整功能的应用, 即时聊天的应用, management管理应用等实例, 并对其进行了定义, 并定义了mod_sofia_globals sofia全局数据中心实例, 并一一对依变量进行初始化, 结构如下:

struct mod_sofia_globals {
switch_memory_pool_t *pool;  //内存池管理器
switch_hash_t *profile_hash;   //sip_profile的hash表
switch_hash_t *gateway_hash;  //internal或external的网关列表
switch_mutex_t *hash_mutex;   //本结构hash表互斥信号量
uint32_t callid;  //呼叫ID
int32_t running;  //是否已运行的标志
int32_t threads;   //线程号
int cpu_count;   //本机CPU个数
int max_msg_queues;  //最大的消息队列数
switch_mutex_t *mutex;  本结构互斥信号量
char guess_ip[80];  // nat环境下, 智能分析出的可访问地址
char hostname[512];  //分析出的本机名称
switch_queue_t *presence_queue; //状态队列
switch_queue_t *msg_queue; //sip消息队列
switch_thread_t *msg_queue_thread[SOFIA_MAX_MSG_QUEUE];  //不同队列所对应的分析线程句柄数组
int msg_queue_len;   //消息队列的长度
struct sofia_private destroy_private;   
struct sofia_private keep_private;
int guess_mask;
char guess_mask_str[16]; 
int debug_presence; //调试的状态标志, 从sofia.conf.xml文件中读取
int debug_sla;  //调试的路由域标志
int auto_restart;  //是否自动重启的标志,  从sofia.conf.xml文件中读取
int reg_deny_binding_fetch_and_no_lookup; /* backwards compatibility */
int auto_nat; //是否自动nat环境分析, 从sofia.conf.xml文件中读取
int tracelevel; //日志等级, 从sofia.conf.xml文件中读取
char *capture_server; 
int rewrite_multicasted_fs_path;  
int presence_flush;
switch_thread_t *presence_thread;  //状态处理线程句柄
uint32_t max_reg_threads;  //最大注册线程数
time_t presence_epoch; 
};  

mod_sofia_globals这个全局数据集使用了系统中的内存池, 分别创建了状态队列,  消息队列.


2.  开启对消息队列的监听及处理, 事件的分发:

先进入sofia_msg_thread_start(int idx)方法, 循环创建 msg_queue_len 大小个 任务处理实体, 并分别分配了线程上下文数据, 交由 任务方法 sofia_msg_thread_run 去一直死循环POP队列数据, , 取出 sofia_dispatch_event_t 事件并交由sofia_process_dispatch_event方法进行事件分发处理。


3. 回调注册的事件处理句柄,针对具体事件进行处理。

事件接收线程将事件的必要参数取出来后, 直接调用

static void our_sofia_event_callback(nua_event_t event,
 int status,
 char const *phrase,
 nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip,
sofia_dispatch_event_t *de, tagi_t tags[]) 

方法进行具体的事件处理。


4. 这个回调方法主要处理了:

  通过事件消息获取 session等相关数据, 并设置到channel中去;

  判断该命令发起的用户是否已通过了鉴权认证, 如果认证未通过, 则直接返回401或407要求重新鉴权,  如果认证通过了, 则直接修改本channel的 sip_authorized标志, 设置为true, 表示通过认证, 下次事件消息过来后, 就直接放行。 如果事件状态码为401或407, 表明本服务需要向对方进行认证, 便通过sofia_reg_handle_sip_r_challenge方法进行验证。

  接下来就是switch各种sip信令进行不同的处理分支, 实际上有很多种,其中以nua_r打头的消息都是收到了一条响应消息, 以nua_i打头的消息是收到了一条请求消息, 我们只关心nua_i_invite(呼叫相关), nua_i_option(心跳保持), nua_i_register(注册)。


 5.  其中sofia_handle_sip_i_invite方法中进行了一次呼叫发起的执行, 先进行call数据是否达上限的一个保护逻辑, 如果超过则直接返回503, 再判断是不是无效的sip包是则返回400,  如果是SDP消息, 则调用core中的 switch_core_media_set_sdp_codec_string 方法进行处理其结果存于session中, 再判断nat类型是"via received" or “via host" or "via port" 分别记录到is_nat中去, 再判断是否有权限, 如果无, 则直接返回403, 否则继续, 再断续填充channel相关参数, 再设置IVR用户相关数据,  再校验被叫号码是否合法,前面一堆都是针对channel的各种状态设置及各种状态交换的保存, 以前主叫方的数据获取, 并设置桥接profile, 通过switch_channel_set_originatee_caller_profile方法进行。

   其中有针对几张数据表的操作:

</pre><pre name="code" class="cpp">switch_mprintf("select 'appearance-index=1' from sip_subscriptions where expires > -1 and hostname='%q' and event='call-info' and "
 "sub_to_user='%q' and sub_to_host='%q'", mod_sofia_globals.hostname, sip->sip_to->a_url->url_user,
 sip->sip_from->a_url->url_host);
</pre>对sip_subscriptions的操作, 查询用户是否在线。</p><p>  </p><p>如果在线, 则直接更新sip_dialogs的呼叫信息及吃叫信息状态字段, 通过session的uuid.</p><p><pre name="code" class="cpp">sql = switch_mprintf("update sip_dialogs set call_info='%q',call_info_state='%q' "
 "where uuid='%q'", buf, state, switch_core_session_get_uuid(session));

从sip_dialogs表中查询callid信息
sql =
switch_mprintf
("select call_id from sip_dialogs where call_info='%q' and ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') "
 "and call_id is not null",
 switch_str_nil(p), user, host, user, host);

通过call_id更新表sip_dialogs的call_info_state字段为idle状态

char *sql = switch_mprintf("update sip_dialogs set call_info_state='idle' where call_id='%q'", b_call_id);

向sip_dialogs表中插入呼叫相关信息

sql = switch_mprintf("insert into sip_dialogs "
 "(call_id,uuid,sip_to_user,sip_to_host,sip_to_tag,sip_from_user,sip_from_host,sip_from_tag,contact_user,"
 "contact_host,state,direction,user_agent,profile_name,hostname,contact,presence_id,presence_data,"
 "call_info,rcd,call_info_state) "
 "values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld,'')",
 call_id,
tech_pvt->sofia_private->uuid,
to_user, to_host, to_tag, dialog_from_user, dialog_from_host, from_tag,
contact_user, contact_host, "confirmed", "inbound", user_agent,
profile->name, mod_sofia_globals.hostname, switch_str_nil(full_contact),
switch_str_nil(presence_id), switch_str_nil(presence_data), switch_str_nil(p), now);

6.  SIP状态机的分析, nua_i_state, 状态机也是和invate等其它事件一样的通过our_sofia_event_callback方法进入到事件选择处理方法中。

case nua_i_state:
sofia_handle_sip_i_state(session, status, phrase, nua, profile, nh, sofia_private, sip, de, tags);

if (r_sdp && !sofia_test_flag(tech_pvt, TFLAG_SDP)) {
				if (switch_channel_test_flag(channel, CF_PROXY_MODE)) {
					switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "RECEIVED_NOMEDIA");
					sofia_set_flag_locked(tech_pvt, TFLAG_READY);
					if (switch_channel_get_state(channel) == CS_NEW) {
						switch_channel_set_state(channel, CS_INIT);
					}
					sofia_set_flag(tech_pvt, TFLAG_SDP);
					goto done;
				} else if (switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MEDIA)) {
					switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "PROXY MEDIA");
					sofia_set_flag_locked(tech_pvt, TFLAG_READY);
					if (switch_channel_get_state(channel) == CS_NEW) {
						switch_channel_set_state(channel, CS_INIT);
					}
				} else if (sofia_test_flag(tech_pvt, TFLAG_LATE_NEGOTIATION)) {
					switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "DELAYED NEGOTIATION");
					sofia_set_flag_locked(tech_pvt, TFLAG_READY);
					if (switch_channel_get_state(channel) == CS_NEW) {
						switch_channel_set_state(channel, CS_INIT);
					}
				}

类似这样, 有相应的状态事件过来后, 找到对应的channel, 跟据当前的状态值, 直接推断后续的状态, 并更新状态机。


7. CHANNEL状态机分析

  只要channel的状态一变成CS_INIT, freeswitch核心的状态机就会负责处理各种状态变化了, 因而各Endpoint模块就不需要再自已维护状态机了。也就是说在Endpoint模块中跟踪channel状态机的变化, 这就需要靠在核心状态机上注册相应的回调函数实现。 回调方法如下:

switch_state_handler_table_t sofia_event_handlers = {
	/*.on_init */ sofia_on_init,
	/*.on_routing */ sofia_on_routing,
	/*.on_execute */ sofia_on_execute,
	/*.on_hangup */ sofia_on_hangup,
	/*.on_exchange_media */ sofia_on_exchange_media,
	/*.on_soft_execute */ sofia_on_soft_execute,
	/*.on_consume_media */ NULL,
	/*.on_hibernate */ sofia_on_hibernate,
	/*.on_reset */ sofia_on_reset,
	/*.on_park */ NULL,
	/*.on_reporting */ NULL,
	/*.on_destroy */ sofia_on_destroy
};

四、 分析 mod_sofia_shutdown方法:

  这里是针对sofia模块申请资源的一种释放, 主要包括线程, 内存, 及状态值, 方法如下:

SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_sofia_shutdown)
{
	int sanity = 0;
	int i;
	switch_status_t st;

	switch_console_del_complete_func("::sofia::list_profiles");
	switch_console_set_complete("del sofia");

	switch_mutex_lock(mod_sofia_globals.mutex);
	if (mod_sofia_globals.running == 1) {
		mod_sofia_globals.running = 0; //改成非运行状态
	}
	switch_mutex_unlock(mod_sofia_globals.mutex);
        
	switch_event_unbind_callback(sofia_presence_event_handler);//卸载状态事件回调方法

	switch_event_unbind_callback(general_event_handler); //<span style="font-family: Arial, Helvetica, sans-serif;">卸载全局事件回调方法</span>

	switch_event_unbind_callback(event_handler); 

	switch_queue_push(mod_sofia_globals.presence_queue, NULL);
	switch_queue_interrupt_all(mod_sofia_globals.presence_queue); //终断所有执行POPO列队的任务线程 

	while (mod_sofia_globals.threads) {   
		switch_cond_next();
		if (++sanity >= 60000) {
			break;
		}
	}


	for (i = 0; mod_sofia_globals.msg_queue_thread[i]; i++) {
		switch_queue_push(mod_sofia_globals.msg_queue, NULL);
		switch_queue_interrupt_all(mod_sofia_globals.msg_queue);
	}


	for (i = 0; mod_sofia_globals.msg_queue_thread[i]; i++) {
		switch_thread_join(&st, mod_sofia_globals.msg_queue_thread[i]);
	}

	if (mod_sofia_globals.presence_thread) {
		switch_thread_join(&st, mod_sofia_globals.presence_thread);
	}

	//switch_yield(1000000);
	su_deinit();

	switch_mutex_lock(mod_sofia_globals.hash_mutex);
	switch_core_hash_destroy(&mod_sofia_globals.profile_hash);
	switch_core_hash_destroy(&mod_sofia_globals.gateway_hash);
	switch_mutex_unlock(mod_sofia_globals.hash_mutex);

	return SWITCH_STATUS_SUCCESS;
}


五、 分析 模块方法的定义声明, SWITCH_MODULE_DEFINITION, :

#define SWITCH_MODULE_DEFINITION_EX(name, load, shutdown, runtime, flags)					\
static const char modname[] =  #name ;														\
SWITCH_MOD_DECLARE_DATA switch_loadable_module_function_table_t name##_module_interface = {	\
	SWITCH_API_VERSION,																		\
	load,																					\
	shutdown,																				\
	runtime,																				\
	flags																					\
}
将三个方法被始化到 switch_loadable_module_function_table_t  mod_sofia_module_interface 变量中去。


完结!








 



  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值