FreeSWITCH Call Flow Analysis

Source: Internet
Author: User
Tags lua freeswitch

Today, the document found in the previous collation of the FreeSWITCH call related content, written in the blog to share it is also convenient for me to check later.

Overall structure diagram

Freeswitchcore Module Loading process

FreeSWITCH The main program is initialized, the configuration is read from the Modules.conf.xml file, if the configuration is in effect:

<module= "Mod_sofia"/>

The Load Sofia module operation is performed.

The process is as follows:

MAINSWITCH_CORE_INIT_AND_MODLOADSWITCH_LOADABLE_MODULE_INITSWITCH_LOADABLE_MODULE_LOAD_MODULE_EX: Read the XML file and load the module
State machine-related state machine initialization
switch.c  : mainswitch_core_init_and_modload    = switch_core_init        = switch _core_session_init            = Switch_core_session_thread_pool_manager                = check_queue                     = switch_core_session_thread_pool_worker                        = switch_core_session_thread                             = Switch_core_session_run   
Change state

The state machine state change is implemented by calling Switch_channel_set_state.

Handling state changes

When the state changes, by switch_channel_set_running_state function to change the running_state,
and executes the associated callback to notify it that the state has changed:
Endpoint_interface->io_routines->state_run

Caller status Change
    • Cs_new

Switch_core_session_run Initial state is cs_new

  • Cs_init

    Sofia_handle_sip_i_state
    Case Nua_callstate_received (Received invite request) modifies the state of the state machine: cs_new ==> cs_init switch_channel_set_state (channel, Cs_init);

    Switch_core_session_run
    State machine processing State Change State_macro (init, "Init"); On_init namely: Sofia_on_init
    Switch_core_standard_on_init

  • Cs_routing

    Sofia_on_init

    修改状态机的状态 : CS_INIT == > CS_ROUTINGswitch_channel_set_state(channel, CS_ROUTING);

    Switch_core_session_run
    State machine processing State change State_macro (routing, "Routing"); On_routing namely: sofia_on_routing
    Switch_core_standard_on_routing

  • Cs_execute

    Switch_core_standard_on_routing Modify state machine Status: cs_routing = = > Cs_execute switch_channel_set_state (session->channel , Cs_execute);

    Switch_core_session_run
    State machine processing State Change State_macro (Execute, "execute"); On_execute namely: Sofia_on_execute
    Switch_core_standard_on_execute

  • Cs_hangup

    Sofia_handle_sip_i_bye Switch_channel_hangup: That is Switch_channel_perform_hangup modify state machine state channel->state = CS_HANGUP;

        状态机处理状态变化    switch_core_session_hangup_state                    STATE_MACRO(hangup, "HANGUP");
Called State change
    • Cs_new

Switch_core_session_run Initial state is cs_new

    • Cs_init

      Sofia_outgoing_channel Modify state machine state: Cs_new ==> cs_init switch_channel_set_state (Nchannel, cs_init);

The state machine handles the logical reference caller.

    • Cs_routing

      Sofia_on_init
      Modify state machine Status: Cs_init = = > cs_routing switch_channel_set_state (Channel, cs_routing);

The state machine handles the logical reference caller.

    • Cs_consume_media

      Originate_on_routing Modifying state machine Status: Cs_routing, Cs_consume_media switch_channel_set_state (channel, Cs_consume_media) ;

      Switch_core_session_run
      State machine processing State Change State_macro (Consume_media, "Consume_media"); On_ Consume_media is: NULL switch_core_standard_on_consume_media

    • Cs_exchange_media

      Switch_core_session_run
      State machine processing State Change State_macro (Exchange_media, "Exchange_media"); On_exchange_media namely: Sofia_on_exchange_media
      Switch_core_standard_on_exchange_media

    • Cs_hangup

      Audio_bridge_on_exchange_media
      Switch_channel_hangup: That is, Switch_channel_perform_hangup modify state machine state channel->state = Cs_hangup;

          状态机处理状态变化    switch_core_session_hangup_state        STATE_MACRO(hangup, "HANGUP");
Sofia Application Layer Module loading process

To start the event processing thread pool:

switch_module_load_function (mod_sofia_load)     = Sofia_msg_thread_start (0);         = Sofia_msg_thread_run            = sofia_process_dispatch_event                = Our_sofia_event_ Callback: Handling Messages

Start Server monitoring:

switch_module_load_function (mod_sofia_load)= Config_sofia (0, NULL)=Launch_sofia_profile_thread=Sofia_profile_thread_run=nua_create=su_home_new=Nua_stack_init=nta_agent_create=Nta_agent_add_tport=Tport_tbind=Tport_bind_server= = Tport_listen: Listens for data sent from the client
Data Sheet

Database default path:/usr/local/freeswitch/db

FreeSWITCH Core Related:

    • Aliases
    • Calls
    • Channels
    • Complete
    • Interfaces
    • Nat
    • Recovery
    • Registrations
    • Tasks

Sofia Related:

    • Sip_authentication
    • Sip_dialogs
    • Sip_presence
    • Sip_registrations
    • Sip_shared_appearance_dialogs
    • Sip_shared_appearance_subscriptions
    • Sip_subscriptions

Limit Related:

    • Db_data
    • Group_data
    • Limit_data

FIFO Correlation:

    • Fifo_bridge
    • Fifo_callers
    • Fifo_outbound

Voice Mail Related:

    • Voicemail_msgs
    • Voicemail_prefs
Call flow involves content

1, received a nua_i_invite, return 407, specifically as follows:

Sofia_handle_sip_i_invite = Sofia_reg_handle_register = Sofia_reg_auth_challenge = 407

2, received a nua_i_invite, return 180, specifically as follows:

Sofia_handle_sip_i_invite = Sofia_reg_handle_register = Sofia_reg_parse_auth

Handling Nua_i_state Messages
Sofia.c:sofia_handle_sip_i_state
...
Mod_dialplan_xml.c:dialplan_hunt (Ring_ready)
Resolve the dialing scheme, execute the LUA script, and set the channel variables.
...
Send 180 events

Data Sheet involved:
Sip_registrations, Ip_dialogs

3, send invite to B extension, specific as follows:
Handling Nua_r_invite Messages
Sofia_handle_sip_r_invite = originate_on_routing
Data Sheet involved:
Sip_dialogs

4, B answer, the two sides of the call, specifically as follows:

Sofia_receive_message:switch_message_indicate_answer                    = Sofia_answer_channel        = sofia_glue_tech_choose_port        = sofia_glue_set_ LOCAL_SDP        = SOFIA_GLUE_ACTIVATE_RTP
Encoding negotiation process

This describes the post-negotiation process

    • Match FreeSWITCH Default codec to find the specific bitrate information for the supported type from the loaded module

Sofia_handle_sip_i_invite
= Sofia_glue_tech_prepare_codecs
= switch_loadable_module_get_codecs_sorted

    • Negotiate to the called SDP

Sofia_glue_do_invite
= Sofia_glue_tech_prepare_codecs
= switch_loadable_module_get_codecs_sorted

    ocodec + codec_string     
    • Generates m headers and sends invite containing the SDP

Sofia_glue_do_invite
= SOFIA_GLUE_SET_LOCAL_SDP
= Generate_m
= Nua_invite

    • Negotiate the SDP with OK

Sofia_answer_channel
= Sofia_glue_tech_prepare_codecs
= switch_loadable_module_get_codecs_sorted

Media interaction
    • RTP Data interaction

Audio_bridge_on_exchange_media
= Audio_bridge_thread

Send and receive audio data
= Switch_core_session_read_frame
= = Session->endpoint_interface->io_routines->read_frame: sofia_read_frame

= Switch_core_session_write_frame
= Perform_write
= = Session->endpoint_interface->io_routines->write_frame: sofia_write_frame

Send and receive video data
1. Start thread
= Launch_video
= Video_bridge_thread

2. Send and receive data
= Switch_core_session_read_video_frame
=>session->endpoint_interface->io_routines->read_video_frame
namely: Sofia_read_video_frame

= Switch_core_session_write_video_frame
= Session->endpoint_interface->io_routines->write_video_frame
namely: Sofia_write_video_frame

    • Terminating RTP Interactions

Audio_bridge_thread
= Switch_core_session_kill_channel (Session_b, switch_sig_break);
namely: Switch_core_session_perform_kill_channel
= Session->endpoint_interface->io_routines->kill_channel
namely: Sofia_kill_channel
= Switch_rtp_break

Sofia Protocol Stack protocol stack structure diagram

NUA: Features of the basic SIP User agent, including call management, message and event retrieval.
The Nta:sofia SIP Transaction API (NTA) provides a simple interface for SIP transactions, transport, and information processing.
Tport: This module contains a generic transport interface used by the SIP,RTSP and HTTP protocols, which is an abstraction layer between the protocol stack and the Transport Protocol implementation.

SIP signaling Specific process

Scene Description: A call B, b after the call after a period of time, hang up the phone.

    • A Send INVITE Request

Tport.c:tport_recv_data
Nta.c:agent_recv_request

    • FS Response 100 to a extension

Nua_server.c:nua_stack_process_request
Nua_server.c:sr_status1 (SR, sip_100_trying)
Nta.c:nta_incoming_treply
Nta.c:incoming_reply
Tport.c:tport_tsend

    • FS sends authentication request to a extension

Nua_application_event

Handling Nua_i_invite Messages

Sofia.c:sofia_process_dispatch_event
Sofia.c:our_sofia_event_callback
Sofia.c:sofia_handle_sip_i_invite
Sofia.c:sofia_reg_handle_register
Sofia_reg.c:sofia_reg_auth_challenge

Reply 407

Nua.c:nua_respond
Nua_stack.c:nua_signal
Nua_stack.c:nua_stack_signal
Nua_server.c:nua_stack_respond
Nua_server.c:nua_server_respond

Send 407 to a extension

Nua_invite_server_respond
Nua_base_server_respond
Nta_incoming_mreply
Incoming_reply
Tport_tsend
Tport_resolve
Tport_by_addrinfo
Tport_prepare_and_send
Tport_send_msg
Tport_vsend
Sent 407 Proxy authentication Required for INVITE (1)

Call State (A): init--Received
Call State (A): Received-terminated

    • A extension sends an ACK
      Nta.c:agent_recv_message
      Nta.c:agent_recv_request

    • A extension resend INVITE request, additional authentication information
      Tport.c:tport_recv_data
      Nta.c:agent_recv_request

    • FS Response 100 to a extension

Nua_server.c:nua_stack_process_request
Nua_server.c:sr_status1 (SR, sip_100_trying)
Nta.c:nta_incoming_treply
Nta.c:incoming_reply
Tport.c:tport_tsend

    • FS sends INVITE to B extension

Switch_ivr_originate.c:switch_ivr_originate

Sofia_on_init
Sofia_glue_do_invite
Nua_invite:nua_r_invite message
Nua_signal

Sofia.c:sofia_process_dispatch_event
Sofia.c:our_sofia_event_callback
Sofia.c:sofia_handle_sip_r_invite
...
Sofia_on_init
Sofia_on_routing
Switch_ivr_originate.c:originate_on_routing
...
Nua_stack_signal
Nua_stack_invite
...
Nta_leg_tcreate
...
Performing a Send Invite request
Nta.c:outgoing_send
Tport_tsend

Call State (B): init--calling

    • B extension response 180 to FS
      Tport_recv_event
      Agent_recv_response

Handling Nua_r_invite Messages

Sofia.c:sofia_process_dispatch_event
Sofia.c:our_sofia_event_callback
Sofia.c:sofia_handle_sip_r_invite

Call State (B): calling-proceeding

    • FS sends 180 to a extension

Nua_session.c:signal_call_state_change
Nua_stack_tevent:nua_i_state
Nua_application_event

Handling Nua_i_invite Messages

Sofia.c:sofia_process_dispatch_event
Sofia.c:our_sofia_event_callback
Sofia.c:sofia_handle_sip_i_invite
Sofia_reg.c:sofia_reg_handle_register
Sofia_reg.c:sofia_reg_parse_auth

Handling Nua_i_state Messages
Sofia.c:sofia_handle_sip_i_state
...
Mod_dialplan_xml.c:dialplan_hunt (Ring_ready)
Resolve the dialing scheme, execute the LUA script, and set the channel variables.
...

Send 180
Switch_channel_mark_ring_ready

Nua_invite_server_respond
Nua_base_server_respond
Nta_incoming_mreply
Incoming_reply
Tport_tsend
Tport_resolve
Tport_by_addrinfo
Tport_prepare_and_send
Tport_send_msg
Tport_vsend

Call State (A): init--Received

? b Send the OK
Tport_recv_event
Agent_recv_response

Handling Nua_r_invite Messages

Sofia.c:sofia_process_dispatch_event
Sofia.c:our_sofia_event_callback
Sofia.c:sofia_handle_sip_r_invite

Call State (B): proceeding-completing

    • FS Sends Ack to B
      Nua_ack
      Nua_signal:nua_r_ack
      Outgoing_send
      Tport_tsend

Call State (A): Received-early
Call State (B): Completing, ready

    • FS Sends a
      Sofia_glue_tech_set_codec
      Switch_rtp_create
      Sofia_glue_negotiate_sdp
      Sofia_glue_activate_rtp
      ...
      Switch_channel_perform_answer:switch_message_indicate_answer
      Switch_channel_perform_mark_answered
      ...
      Auto_record.lua: Perform recording operation B
      ...
      Auto_record.lua: Perform recording operation a
      ...
      Sofia_receive_message (Switch_message_indicate_answer)
      ...
      Send a $ OK
      nua_respond:200
      Nua_signal:nua_r_respond
      Nua_stack_signal
      Nua_stack_respond
      Nua_server_respond
      Nta_incoming_treply
      Nta_incoming_mreply
      Incoming_reply
      Tport_tsend

Call State (A): early-completed

    • A response Ack
      Nta.c:agent_recv_message
      Nta.c:agent_recv_request

Call State (A): Completed, ready

    • After a period of talk, a actively send bye
      Tport_recv_event
      Agent_recv_message
      Agent_recv_request
      ...
      Sofia_on_hangup

    • FS Sends bye to B
      Nua_bye:nua_r_bye
      Nua_stack_signal
      Outgoing_send
      Tport_tsend

    • FS Sends a
      Nta:sent OK for BYE
      Tport_tsend

Call State (A): Ready, terminating
Call State (A): terminated

    • B sends a $ OK to FS, responding to a pending request
      Nta:received OK for BYE

Status change:
Call State (B): Ready, terminating
Call State (B): terminating-terminated

This article GitHub address:

Https://github.com/mike-zhang/mikeBlogEssays/blob/master/2016/20160907_freeswitch Call Flow Analysis . MD

Welcome to Supplement

FreeSWITCH Call Flow analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.