FreeSwitch Sofia模块加载过程

            模块加载入口函数mod_sofia_load(),首先一系列switch_event_reserve_subclass()调用,注册事件类型。然后调用switch_queue_create()建立三个消息队列。

    switch_queue_create(&mod_sofia_globals.presence_queue, SOFIA_QUEUE_SIZE, mod_sofia_globals.pool);
	switch_queue_create(&mod_sofia_globals.general_event_queue, SOFIA_QUEUE_SIZE, mod_sofia_globals.pool);

	mod_sofia_globals.cpu_count = switch_core_cpu_count();
	mod_sofia_globals.max_msg_queues = (mod_sofia_globals.cpu_count / 2) + 1;
	if (mod_sofia_globals.max_msg_queues < 2) {
		mod_sofia_globals.max_msg_queues = 2;
	}

	if (mod_sofia_globals.max_msg_queues > SOFIA_MAX_MSG_QUEUE) {
		mod_sofia_globals.max_msg_queues = SOFIA_MAX_MSG_QUEUE;
	}

	switch_queue_create(&mod_sofia_globals.msg_queue, SOFIA_MSG_QUEUE_SIZE * mod_sofia_globals.max_msg_queues, mod_sofia_globals.pool);

           接下来调用sofia_init()函数初始化协议栈,并把协议栈的日志重定向到FS的日志系统。初始化完毕之后,调用config_sofia()对模块进行配置。读取配置文件进行设置,这里关键是对每个profile初始化监听线程,调用的是launch_sofia_profile_thread(),每个profile,初始化一个sofia库事件处理线程,一个FS应用层消息队列处理线程。在launch_sofia_profile_thread()中,初始化协议栈事件线程,线程入口函数sofia_profile_thread_run(),在这个线程里,调用nua_create接口实例化NUA对象,同时绑定事件回调函数sofia_event_callback();调用switch_queue_create初始化应用层的消息队列,然后调用launch_sofia_worker_thread()派生出应用层消息队列处理线程。

					if (profile->sipip) {
						switch_event_t *s_event;
						if (!profile->extsipport) profile->extsipport = profile->sip_port;

						launch_sofia_profile_thread(profile);
						if (profile->odbc_dsn) {
							switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Connecting ODBC Profile %s [%s]\n", profile->name, url);
							switch_yield(1000000);
						} else {
							switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Started Profile %s [%s]\n", profile->name, url);
						}
						if ((switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_PROFILE_START) == SWITCH_STATUS_SUCCESS)) {
							switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "module_name", "mod_sofia");
							switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "profile_name", profile->name);
							if (profile) {
								switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "profile_uri", profile->url);
							}
							switch_event_fire(&s_event);
						}
					}

 

         接下来,调用sofia_msg_thread_start(0);启动消息处理线程,这是用于处理FS消息队列事件的。

         接下来,调用switch_event_bind()绑定一系列的事件回调函数。其中,有一些事件的回调函数是sofia_presence_event_handler(),这个回调函数会创建一个线程来处理presence事件。

         最后,注册模块的APP和API实现。

上时序图:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章