Source code explanation for Ozeki SIP/SDP Message Manipulator sample program
This page is entitled to be a source code explanation page that explains the main parts of the source code for Ozeki SIP/SDP Message Manipulator sample program in details.
Ozeki SIP/SDP Message Manipulator sample can be obtained by opening the download page: Download Ozeki SIP/SDP Message Manipulator sample! |
In this example, a mini-softphone is used to demonstrate the SIP/SDP message manipulation
in a simple but effective way. For this purpose, two examples have been included in
the sample program.
In the first example, you can specify an optional SIP/SDP message. In this case,
what you write in the textbox will be sent as a message, and it will be processed in case
of incoming messages.
In the other example, you can set the value of a header. This will be 'Organization' header in case of SIP,
and 'Connection' header in case of SDP messages.
Graphical User Interface
The basic softphone has a classic style (Figure 1). It has two status fields and a dial pad. Below the dial pad you can select which SIP/SDP Message Manipulator you wish to use. On the right side there is a TabControl with which you can define the content of the SIP/SDP message, and you can track incoming/outgoing messages.
Running the program
Once the main window is loaded, the system tries to register the SIP Account you set in the code of the program. If the registration process is successful, RegistrationSucceded notification appears in PhoneLine Status line. From this point you can make and receive calls. For making a call, you just need to dial the phone number and click on Pick up button.
Manipulation of the whole SIP message
First, the Custom SIP/SDP message radio box needs to be checked in the group box below the dial pad.
As a result, three tabs appear in TabControl: Custom SIP Message, Custom SDP Message and Message logs.
Under Custom SIP Message tab you can define the outgoing SIP messages, and also
the incoming SIP message what should be replaced for.
For saving settings, click on Apply button. These saved settings will affect the succeeding
SIP messages.
Modification of Organization and Connection headers
Check Only Organization/Connection radio box in the group box below the dial pad.
As a result, two tabs appear in TabControl: Organization/Connection and Message logs.
In Organization groupbox you can find Outgoing textbox. In this textbox you can define
the values of the Organization headers of outgoing SIP messages. While you
can define the values of the Organization header for incoming SIP messages in Incoming textbox .
In Outgoing textbox of Connection group box, you can specify the value of the Connection header for
outgoing SDP messages. While you can define the values of the Connection headers for incoming SDP messages
in Incoming textbox.
Click on Apply to save the settings (these settings will be valid for succeeding SDP messages).
Outgoing/Incoming messages can be found in the textbox of Message Logs tab.
Source code
For making the sample program simple, business logic has been implemented in the code-behind section of the main window. The project includes a PhoneMain class that is derived from Form class (as it is in case of WinForms windows). The basic data tags that are needed for the operation of the softphone are placed here beside the SIP Account data and manipulator classes. The methods required for call handling have been implemented in the events of the GUI.
Data tags of the class
ISoftPhone softPhone; // The SoftPhone object SIPAccount sipAccount; // The SIP Account IPhoneLine phoneLine; // Represents a phone line IPhoneCall call; // The current phone call object PhoneLineState phoneLineState; // Information about the state of the phone line bool inComingCall; // Indicates whether the current phone call is an incoming phone call SIPMessageLogger sipMessageLogger; // Logs the sent and received SIP messages // Replaces the entire SIP message to a custom message. SIP_CustomMessageManipulator sipMessageManipulator; // Replaces the entire SDP message to a custom message. SDP_CustomMessageManipulator sdpMessageManipulator; // Replaces only the 'Connection' header of the SDP message. SDP_ConnectionManipulator connectionManipulator; // Replaces only the 'Organization' header of the SIP message. SIP_OrganizationManipulator organizationManipulator;
Initialization of manipulator classes
When the GUI is loaded, the system registers the SIP Account by subscribing to Form Load event.
private void PhoneMain_Load(object sender, EventArgs e) { /* ... */ InitializeSoftPhone(); /* ... */ } private void InitializeSoftPhone() { try { /* Create Softphone */ InitManipulators(); /* Create and register PhoneLine */ /* Create SIP Logger */ } catch (Exception ex) { /* ... */ } } private void InitManipulators() { connectionManipulator = new SDP_ConnectionManipulator(); organizationManipulator = new SIP_OrganizationManipulator(); sdpMessageManipulator = new SDP_CustomMessageManipulator(); sipMessageManipulator = new SIP_CustomMessageManipulator(); }
Using manipulator classes for outgoing/incoming messages
You can provide the object that manipulates SIP messages with
the SetSIPMessageManipulator() method of ISoftPhone object.
You can provide the object that manipulates SDP messages with
the SetSDPMessageManipulator() method of ISoftPhone object.
You can provide the object that does the whole modification of SIP/SDP messages in SetCustomMessageManipulators() method of PhoneMain.
private void SetCustomMessageManipulators() { if (softPhone != null) { softPhone.SetSIPMessageManipulator(sipMessageManipulator); softPhone.SetSDPMessageManipulator(sdpMessageManipulator); } }
You can provide the objects that modify Connection and Organization headers in SetOrgConnManipulators() method of PhoneMain.
private void SetOrgConnManipulators() { if (softPhone != null) { softPhone.SetSIPMessageManipulator(organizationManipulator); softPhone.SetSDPMessageManipulator(connectionManipulator); } }
How to create your own SIP message manipulator
- Create a new class (e.g. MySIPManipulator).
- Implement ISIPMessageManipulator interface.
- Implement three methods:
a.) string PerformReceivedMessage(string incomingMessage)
It manipulates incoming messages. You receive the incoming SIP message in parameter that can be modified according to your needs. The method returns the manipulated incoming SIP message.
b.) string PerformSendMessage(string outGoingMessage)
It manipulates outgoing SIP messages. You receive SIP messages in parameter that can be modified according to your needs. The method returns the manipulated outgoing SIP message.
c.) IEnumerable<PreparedExtensionHeader> PrepareAdditionalHeaders(string outGoingMessage)
It adds further headers to the outgoing SIP messages. You receive outgoing SIP messages in parameter. The method returns a collection that includes the additional headers added to the outgoing messages.
More information
- Configuration steps for manipulating SIP/SDP messages using Ozeki VoIP SIP SDK
- Source code explanation for Ozeki SIP/SDP Message Manipulator sample program