这几天比较轻松,所以打算好好来看看linphone的代码,源码版本为3.5.2。从linphone初始化的过程开始,首先来看linphone_core_new函数。
[cpp] view plain copy print ?
- LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
- const char *config_path, const char *factory_config_path, void * userdata)
- {
- LinphoneCore *core=ms_new(LinphoneCore,1);
- linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
- return core;
- }
LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
const char *config_path, const char *factory_config_path, void * userdata)
{
LinphoneCore *core=ms_new(LinphoneCore,1);
linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
return core;
}
主要工作基本上在函数linphone_core_init中实现
[plain] view plain copy print ?
- static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
- const char *factory_config_path, void * userdata)
- {
- memset (lc, 0, sizeof (LinphoneCore));
- lc->data=userdata;
- lc->ringstream_autorelease=TRUE;
-
-
- memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable)); //保存了回调函数表
-
-
- linphone_core_set_state(lc,LinphoneGlobalStartup,"Starting up"); //设置linphone内部状态
- ortp_init(); //ortp模块初始化
- lc->dyn_pt=96; //负载类型从96开始都是动态负载类型
- linphone_core_assign_payload_type(lc,&payload_type_pcmu8000,0,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_gsm,3,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_pcma8000,8,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_speex_nb,110,"vbr=on");
- linphone_core_assign_payload_type(lc,&payload_type_speex_wb,111,"vbr=on");
- linphone_core_assign_payload_type(lc,&payload_type_speex_uwb,112,"vbr=on");
- linphone_core_assign_payload_type(lc,&payload_type_telephone_event,101,"0-11");
- linphone_core_assign_payload_type(lc,&payload_type_g722,9,NULL);
-
-
- #if defined(ANDROID) || defined (__IPHONE_OS_VERSION_MIN_REQUIRED)
- /*shorten the DNS lookup time and send more retransmissions on mobiles:
- - to workaround potential packet losses
- - to avoid hanging for 30 seconds when the network doesn't work despite the phone thinks it does.
- */
- _linphone_core_configure_resolver();
- #endif
-
-
- #ifdef ENABLE_NONSTANDARD_GSM
- {
- PayloadType *pt;
- pt=payload_type_clone(&payload_type_gsm);
- pt->clock_rate=11025;
- linphone_core_assign_payload_type(lc,pt,-1,NULL);
- pt->clock_rate=22050;
- linphone_core_assign_payload_type(lc,pt,-1,NULL);
- payload_type_destroy(pt);
- }
- #endif
-
-
- #ifdef VIDEO_ENABLED
- linphone_core_assign_payload_type(lc,&payload_type_h263,34,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_theora,97,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_h263_1998,98,"CIF=1;QCIF=1");
- linphone_core_assign_payload_type(lc,&payload_type_mp4v,99,"profile-level-id=3");
- linphone_core_assign_payload_type(lc,&payload_type_h264,102,"profile-level-id=428014");
- linphone_core_assign_payload_type(lc,&payload_type_vp8,103,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_x_snow,-1,NULL);
- /* due to limited space in SDP, we have to disable this h264 line which is normally no more necessary */
- /* linphone_core_assign_payload_type(&payload_type_h264,-1,"packetization-mode=1;profile-level-id=428014");*/
- #endif
-
-
- //下面的这些负载类型,没有指定类型号
- /*add all payload type for which we don't care about the number */
- linphone_core_assign_payload_type(lc,&payload_type_ilbc,-1,"mode=30");
- linphone_core_assign_payload_type(lc,&payload_type_amr,-1,"octet-align=1");
- linphone_core_assign_payload_type(lc,&payload_type_amrwb,-1,"octet-align=1");
- linphone_core_assign_payload_type(lc,&payload_type_lpc1015,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_g726_16,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_g726_24,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_g726_32,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_g726_40,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_16,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_24,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_32,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_40,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_silk_nb,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_silk_mb,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_silk_wb,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_silk_swb,-1,NULL);
- linphone_core_assign_payload_type(lc,&payload_type_g729,18,"annexb=no");
-
- ms_init(); //初始化mediastreamer模块
- /* create a mediastreamer2 event queue and set it as global */
- /* This allows to run event's callback in linphone_core_iterate() */
- lc->msevq=ms_event_queue_new(); //事件队列,linphone_core_iterate函数中取出事件并调用相应的回调函数
- ms_set_global_event_queue(lc->msevq); //队列指针保存到一个全局的变量ms_global_event_queue里
-
-
- lc->config=lp_config_new(config_path); //读取配置文件,config保存的是一个ini文档,类似xml文档
- if (factory_config_path) //出厂配置文件是只读的,用于恢复出厂配置
- lp_config_read_file(lc->config,factory_config_path);
-
-
- lc->sal=sal_init(); //初始化exosip模块,sal是什么的缩写呢?
- sal_set_user_pointer(lc->sal,lc);
- sal_set_callbacks(lc->sal,&linphone_sal_callbacks); //设置sal回调函数
-
-
- sip_setup_register_all(); //这里将加载所有插件
- sound_config_read(lc); //读取音频相关配置
- net_config_read(lc); //读取网络相关配置
- rtp_config_read(lc); //读取rtp相关配置
- codecs_config_read(lc); //读取编码器相关配置
- sip_config_read(lc); //读取sip协议相关配置,/* this will start eXosip*/
- video_config_read(lc); //视频相关配置
- //autoreplier_config_init(&lc->autoreplier_conf);
- lc->presence_mode=LinphoneStatusOnline; //在线、离线,还是其它
- misc_config_read(lc); //配置最大历史记录条数等
- ui_config_read(lc); //读取联系人(朋友),读取通话记录
- #ifdef TUNNEL_ENABLED
- lc->tunnel=linphone_core_tunnel_new(lc);
- if (lc->tunnel) linphone_tunnel_configure(lc->tunnel);
- #endif
- if (lc->vtable.display_status)
- lc->vtable.display_status(lc,_("Ready")); //更新状态
- lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
- linphone_core_set_state(lc,LinphoneGlobalOn,"Ready"); //这里将会调用状态改变的回调函数
- }
static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
const char *factory_config_path, void * userdata)
{
memset (lc, 0, sizeof (LinphoneCore));
lc->data=userdata;
lc->ringstream_autorelease=TRUE;
memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable)); //保存了回调函数表
linphone_core_set_state(lc,LinphoneGlobalStartup,"Starting up"); //设置linphone内部状态
ortp_init(); //ortp模块初始化
lc->dyn_pt=96; //负载类型从96开始都是动态负载类型
linphone_core_assign_payload_type(lc,&payload_type_pcmu8000,0,NULL);
linphone_core_assign_payload_type(lc,&payload_type_gsm,3,NULL);
linphone_core_assign_payload_type(lc,&payload_type_pcma8000,8,NULL);
linphone_core_assign_payload_type(lc,&payload_type_speex_nb,110,"vbr=on");
linphone_core_assign_payload_type(lc,&payload_type_speex_wb,111,"vbr=on");
linphone_core_assign_payload_type(lc,&payload_type_speex_uwb,112,"vbr=on");
linphone_core_assign_payload_type(lc,&payload_type_telephone_event,101,"0-11");
linphone_core_assign_payload_type(lc,&payload_type_g722,9,NULL);
#if defined(ANDROID) || defined (__IPHONE_OS_VERSION_MIN_REQUIRED)
/*shorten the DNS lookup time and send more retransmissions on mobiles:
- to workaround potential packet losses
- to avoid hanging for 30 seconds when the network doesn't work despite the phone thinks it does.
*/
_linphone_core_configure_resolver();
#endif
#ifdef ENABLE_NONSTANDARD_GSM
{
PayloadType *pt;
pt=payload_type_clone(&payload_type_gsm);
pt->clock_rate=11025;
linphone_core_assign_payload_type(lc,pt,-1,NULL);
pt->clock_rate=22050;
linphone_core_assign_payload_type(lc,pt,-1,NULL);
payload_type_destroy(pt);
}
#endif
#ifdef VIDEO_ENABLED
linphone_core_assign_payload_type(lc,&payload_type_h263,34,NULL);
linphone_core_assign_payload_type(lc,&payload_type_theora,97,NULL);
linphone_core_assign_payload_type(lc,&payload_type_h263_1998,98,"CIF=1;QCIF=1");
linphone_core_assign_payload_type(lc,&payload_type_mp4v,99,"profile-level-id=3");
linphone_core_assign_payload_type(lc,&payload_type_h264,102,"profile-level-id=428014");
linphone_core_assign_payload_type(lc,&payload_type_vp8,103,NULL);
linphone_core_assign_payload_type(lc,&payload_type_x_snow,-1,NULL);
/* due to limited space in SDP, we have to disable this h264 line which is normally no more necessary */
/* linphone_core_assign_payload_type(&payload_type_h264,-1,"packetization-mode=1;profile-level-id=428014");*/
#endif
//下面的这些负载类型,没有指定类型号
/*add all payload type for which we don't care about the number */
linphone_core_assign_payload_type(lc,&payload_type_ilbc,-1,"mode=30");
linphone_core_assign_payload_type(lc,&payload_type_amr,-1,"octet-align=1");
linphone_core_assign_payload_type(lc,&payload_type_amrwb,-1,"octet-align=1");
linphone_core_assign_payload_type(lc,&payload_type_lpc1015,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_g726_16,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_g726_24,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_g726_32,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_g726_40,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_16,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_24,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_32,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_aal2_g726_40,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_silk_nb,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_silk_mb,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_silk_wb,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_silk_swb,-1,NULL);
linphone_core_assign_payload_type(lc,&payload_type_g729,18,"annexb=no");
ms_init(); //初始化mediastreamer模块
/* create a mediastreamer2 event queue and set it as global */
/* This allows to run event's callback in linphone_core_iterate() */
lc->msevq=ms_event_queue_new(); //事件队列,linphone_core_iterate函数中取出事件并调用相应的回调函数
ms_set_global_event_queue(lc->msevq); //队列指针保存到一个全局的变量ms_global_event_queue里
lc->config=lp_config_new(config_path); //读取配置文件,config保存的是一个ini文档,类似xml文档
if (factory_config_path) //出厂配置文件是只读的,用于恢复出厂配置
lp_config_read_file(lc->config,factory_config_path);
lc->sal=sal_init(); //初始化exosip模块,sal是什么的缩写呢?
sal_set_user_pointer(lc->sal,lc);
sal_set_callbacks(lc->sal,&linphone_sal_callbacks); //设置sal回调函数
sip_setup_register_all(); //这里将加载所有插件
sound_config_read(lc); //读取音频相关配置
net_config_read(lc); //读取网络相关配置
rtp_config_read(lc); //读取rtp相关配置
codecs_config_read(lc); //读取编码器相关配置
sip_config_read(lc); //读取sip协议相关配置,/* this will start eXosip*/
video_config_read(lc); //视频相关配置
//autoreplier_config_init(&lc->autoreplier_conf);
lc->presence_mode=LinphoneStatusOnline; //在线、离线,还是其它
misc_config_read(lc); //配置最大历史记录条数等
ui_config_read(lc); //读取联系人(朋友),读取通话记录
#ifdef TUNNEL_ENABLED
lc->tunnel=linphone_core_tunnel_new(lc);
if (lc->tunnel) linphone_tunnel_configure(lc->tunnel);
#endif
if (lc->vtable.display_status)
lc->vtable.display_status(lc,_("Ready")); //更新状态
lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
linphone_core_set_state(lc,LinphoneGlobalOn,"Ready"); //这里将会调用状态改变的回调函数
}
上面的代码做了大量的工作,分配负载类型,初始化了各子模块,如ortp、sal(对exosip的封装吧)、mediastream2,加载了所有的插件,最后读取配置文件。
再来看看分配负载类型的函数linphone_core_assign_payload_type的实现
[plain] view plain copy print ?
- static void linphone_core_assign_payload_type(LinphoneCore *lc, PayloadType *const_pt, int number, const char *recv_fmtp){
- PayloadType *pt;
- pt=payload_type_clone(const_pt);
-
-
- //找一个未分配的负载类型码,分配给本负载类型
- if (number==-1){
- /*look for a free number */
- MSList *elem;
- int i;
- for(i=lc->dyn_pt;i<=127;++i){
- bool_t already_assigned=FALSE;
- for(elem=lc->payload_types;elem!=NULL;elem=elem->next){
- PayloadType *it=(PayloadType*)elem->data;
- if (payload_type_get_number(it)==i){
- already_assigned=TRUE;
- break;
- }
- }
- if (!already_assigned){
- number=i;
- lc->dyn_pt=i+1;
- break;
- }
- }
- if (number==-1){
- ms_fatal("FIXME: too many codecs, no more free numbers.");
- }
- }
- ms_message("assigning %s/%i payload type number %i",pt->mime_type,pt->clock_rate,number);
- payload_type_set_number(pt,number);
- if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
- rtp_profile_set_payload(&av_profile,number,pt); //将负载类型的对应关系保存到全局的RtpProfile结构中
- lc->payload_types=ms_list_append(lc->payload_types,pt); //将支持的负载类型添加到链表中
- }