linphone源码分析----初始化部分

简介: <span style="color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif; font-size:16px; line-height:28.799999237060547px">这几天比较轻松,所以打算好好来看看
这几天比较轻松,所以打算好好来看看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");  //设置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的实现 
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);  //将支持的负载类型添加到链表中
}
目录
相关文章
|
6月前
如果我想在`__init__`方法中添加一些初始化逻辑,应该如何实现?
如果我想在`__init__`方法中添加一些初始化逻辑,应该如何实现?
42 0
|
缓存 JavaScript 算法
v-if和v-show的区别及源码分析
v-if和v-show的区别及源码分析
135 1
|
Java 编译器
java----类的加载与其初始化
java----类的加载与其初始化
|
Java 编译器
04-面试:类的初始化做了什么?初始化的时机是?
类的初始化是指在首次使用类时,JVM对类进行的初始化操作。在类初始化阶段,JVM会执行一系列的步骤。
80 0
04-面试:类的初始化做了什么?初始化的时机是?
|
小程序 JavaScript 程序员
小程序----生命周期
小程序----生命周期
|
小程序
小程序----全局配置(二)
小程序----全局配置(二)
|
存储 前端开发 Java
二十三.SpringCloudConfig源码-初始化配置
今天这篇文章我们来分析一下Spring Cloud Config 配置中心的源码,这应该是Spring Cloud Netflix的源码分析的最后一篇。下一个系列我将会继续分析Spring Cloud Alibaba相关组件的源码。Spring Cloud Config 基础使用请移步 《[配置中心Spring Cloud Config](https://blog.csdn.net/u014494148/article/details/117253831)》
|
安全 NoSQL Java
28个案例问题分析---15---登陆之后我加入的课程调用接口报错--ArrayList线程不安全。占用内存情况
28个案例问题分析---15---登陆之后我加入的课程调用接口报错--ArrayList线程不安全。占用内存情况
84 0
class_create函数源码分析
class_create函数源码分析