这几天比较轻松,所以打算好好来看看linphone的代码,源码版本为3.5.2。从linphone初始化的过程开始,首先来看linphone_core_new函数。
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中实现
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");
ortp_init();
lc->dyn_pt=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);
_linphone_core_configure_resolver();
{
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);
}
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);
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();
lc->msevq=ms_event_queue_new();
ms_set_global_event_queue(lc->msevq);
lc->config=lp_config_new(config_path);
if (factory_config_path)
lp_config_read_file(lc->config,factory_config_path);
lc->sal=sal_init();
sal_set_user_pointer(lc->sal,lc);
sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
sip_setup_register_all();
sound_config_read(lc);
net_config_read(lc);
rtp_config_read(lc);
codecs_config_read(lc);
sip_config_read(lc);
video_config_read(lc);
lc->presence_mode=LinphoneStatusOnline;
misc_config_read(lc);
ui_config_read(lc);
lc->tunnel=linphone_core_tunnel_new(lc);
if (lc->tunnel) linphone_tunnel_configure(lc->tunnel);
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的实现
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); //将支持的负载类型添加到链表中
}