freeswitch 内核研究笔记1

freeswitch 高性能技术特性:memory pool、task queue、event driven、multithread、hash、state Machine(内存池、多线程,任务队列,事件驱动,哈希,状态机


内核启动流程:

两个函数
switch_core_init 负责核心的初始化
apr_initialize(),
switch_core_session_init,
switch_core_hash_init,
switch_console_init,
switch_event_init,
switch_xml_init,
switch_log_init,
switch_core_state_machine_init
switch_scheduler_task_thread_start
switch_nat_late_init,

switch_rtp_init,

switch_loadable_module_init 各个模块的初始化。


呼叫流程:呼入
sip协议栈从传输层收到sip消息,最终转到SIP UA层,进入 sofia_event_callback->sofia_queue_message

信令处理线程sofia_msg_thread_start->sofia_msg_thread_run -->
sofia_process_dispatch_event  --> our_sofia_event_callback --> sofia_handle_sip_i_invite --> switch_core_session_request

状态机处理线程switch_core_session_thread_launch --> switch_core_session_thread_launch --> audio_bridge_funcation --> switch_ivr_originate



外呼: fifo、conferrance、switch_ivr_originate
enterprise_originate_thread-->switch_ivr_originate-->switch_core_session_outgoing_channel-->sofia_outgoing_channel->switch_core_session_request


freeswitch 内核几个概念:session、channle、tec_private、event
核心通过一个有限状态机来管理呼叫过程中每个状态对应的回调。

一个呼叫进入系统后建立一条channel,此channle属于一个 server 维护的session,核心通过状态机来调度session以实现事件的流转。
一个典型的呼叫流程 uac1->uas, uas->uac2, uac1<->bridge<->uac2,呼叫进入系统,系统建立session后启动switch_core_session_thread_launch 线程不断监听session状态,根据状态调用相应的回调,当然,这里的状态机是一个状态有限的,freeswitch 状态机分以下状态:
on_init,
on_routing,
on_execute,
on_hangup,
on_exchange_media
on_soft_execute,
on_consume_media,
on_hibernate,
on_reset,
on_park,
on_reporting,
on_destroy。
实际呼叫过程为这些状态的流转。

uac1->uas: 呼入系统,系统建立session:
sofia_handle_sip_i_invite->switch_core_session_request->switch_core_session_thread_launch,
解析请求后从CS_NEW -> CS_INIT, 初始化后进入dialplan : CS_INIT -> CS_ROUTING, 状态机回调 switch_core_standard_on_routing 查找dialplan。 系统根据主叫、被叫的sip设置查找其对应的dialplan,然后加载到内存。 呼叫状态转换:CS_ROUTING -> CS_EXECUT,状态机调用 switch_core_standard_on_execute,然后按照规则一条一条执行刚加载的dialplan,这里即是可编程软交换的体现,根据需求灵活控制提供给一个呼叫的服务。当执行到bridge action时,是让系统呼叫uac2, 调用switch_ivr_originate外乎uac2,创建uac2在服务器端的session、channel,
最后启动状态机线程switch_core_session_thread_launch进入状态机模式,CS_NEW -> CS_INIT-> CS_ROUTING-> CS_CONSUME_MEDIA,

usc2最终应答 200 OK,服务器根据200 ok sdp消息体与uac1提供的sdp协商,成功后发200 OK给uac1,呼叫桥接成功。uac2状态机转换CS_CONSUME_MEDIA -> CS_EXCHANGE_MEDIA,媒体流桥接开始。



状态机流转节点:

/*!
  \enum switch_channel_state_t
  \brief Channel States (these are the defaults, CS_SOFT_EXECUTE, CS_EXCHANGE_MEDIA, and CS_CONSUME_MEDIA 
            are often overridden by specific apps)
CS_NEW       - Channel is newly created.
CS_INIT      - Channel has been initilized.
CS_ROUTING   - Channel is looking for an extension to execute.
CS_SOFT_EXECUTE  - Channel is ready to execute from 3rd party control.
CS_EXECUTE   - Channel is executing it's dialplan.
CS_EXCHANGE_MEDIA  - Channel is exchanging media with another channel.
CS_PARK      - Channel is accepting media awaiting commands.
CS_CONSUME_MEDIA         - Channel is consuming all media and dropping it.
CS_HIBERNATE - Channel is in a sleep state.
CS_RESET      - Channel is in a reset state.
CS_HANGUP    - Channel is flagged for hangup and ready to end.
CS_REPORTING - Channel is ready to collect call detail.
CS_DESTROY      - Channel is ready to be destroyed and out of the state machine
*/

你可能感兴趣的:(sip协议)