Internet Telephony and SIP based VOIP application development on android


1)Internet telephony and SIP

Internet telephony is one of the latest emerging technology which provides the transmission of human voice over Internet channel.It provides call services at cheaper rate than normal PSTN based call. The technology establishes call and sends/receives data over existing internet network using IP protocol.SIP is the application layer protocol, a signaling protocol which is used for establishing multimedia session over IP network in the internet telephony process.The standard port for SIP is 5060.The protocol can use both UDP or TCP as underlying transport protocol. SIP is used for multimedia session establishment ,session termination like call set up and call release.The application has to create TCP/UDP socket with the SIP server for sending SIP signaling.SIP packet contains header and payload. SIP headers are formed as per RFC specification.

2)Device Network transaction

2.1)Device Registration

To use SIP services, application has to perform registration with its identity to SIP server, an application server for SIP signaling and routing packet to the external IP network. User(application) sends SIP REGISTER packet to perform Registration with the SIP server. The server may ask for user authentication through SIP unauthorized 401 and user again needs to register with proper credential response information in an encrypted form. If User credential is validated by Server, then Register acceptance request in form of 200 OK will be sent to user.The SIP server contacts HSS to get user information, user subscription and for user credential verification.

The below figure depicts the SIP transaction process that happens when User performs registration to SIP server

1

2.2)Call Establishment

Once device is registered with SIP server, it is allowed to use SIP service request like call,sms over IP channel. To make audio calls over internet ,a VOIP application has to be installed on mobile device which would use SIP protocol . Once the call session is established using SIP signaling, real time data like audio/video will be passed through medial protocol like RTP(Real time protocol).

The below figure depicts the SIP transaction process that happens between calling party, A and called party B on establishing audio call.

2

The above call transaction is defined as let’s calling party A wants to make call to called party B assuming both the parties are registered to SIP server.The calling party A sends call set up message using SIP packet INVITE to its registered SIP server.The SIP server of A sends to SIP server of B over IP network.SIP server of B delivers call set up request INVITE to called party B.The B acknowledges the request with SIP 100 trying. When called party device B starts ringing, it sends SIP 180 Ringing to calling party,A. When called party picks up and receives the call, SIP 200 OK is sent from B to A. The calling party, A sends acknowledgement for 200 Ok to called party as SIP ACK. The signaling is completed and call session is established between A and B.Now the actual audio conversion is transmitted as data using RTP .When called party B ends the call SIP BYE packet is sent to calling party. The calling part A, acknowledges the call termination with SIP 200 OK. SIP REGISTER SIP BYE,SIP 200,180 are the different kinds of SIP packet.

3)SIP based VOIP (voice over IP) client development on android.

Android 2.3 version, Gingerbread has come up with VOIP framework stack which ease developer to make VOIP application and use VOIP framework API’s.The VOIP application connects with Android SIP service which runs as a separate process.Like TelephonyManager is an interface to use phone services, similarly android provides SIPManager for an application to start SIP services and use exposed apis of SIP service like makeAudoCall(). When SIP service receives call request from application, it uses SIP signaling method first to establish call session and then actual audio data will be sent over RTP.The SIP service uses javax.sip package to create SIP headers, to send and receive SIP packets.

The steps for developing VOIP includes following points:

a)Setting up User profile and Registering Intent.

SIP manager is instantiated first as

mSIPManager = new SIManager(context)

This installation connects application with SIP services. The application first registers his identity for using sip services like audio call to sip service. A sip profile object, sp is created which includes SIP account user id,password,SIP server domain name. The user must hold account with VOIP provider. Secondly, application has to register an intent to the sip service for receiving incoming call. This intent is passed to the SIP service. When SIP service receives incoming VOIP call ,it broadcast the registered intent and register receiver in application context receives the broadcast intent.

PendingIntent pi = PendingIntent.getBroadcast(this,0,new Intent(“android.SipDemo.INCOMING_CAL”),Intent.FILL_IN_DATA);

The application uses SIPManager’s open(sp,pi) call to register sip profile and incoming call intent to SIP service. The application must define a broadcast receiver to receive the broadcast from SIP service with intent action android.SipDemo.INCOMING_CAL.This intent is broadcasted when sip service receives incoming SIP call from network.

b)Registration listener

To use SIP services, device has to register with SIP server as explained above. So application needs to set up registration listener to know its registration status with SIP server. If device is de-registered with server, it will not be able to make call. Registration is the signaling procedure that UE has to perform before making SIP audio call.

mSIPManager.setRegistrationListener(new SIPREgistrationListener(){

public void onRegistrationFailed(String localProfileUri, int errorCode,

String errorMessage) {

// TODO Auto-generated method stub

print(“sip registration is failed”);

} public void onRegistrationDone(String localProfileUri, long expiryTime) {

// TODO Auto-generated method stub

Log.d(LOG_TAG,”sip registration is done”);

} public void onRegistering(String localProfileUri) {

// TODO Auto-generated method stub

Log.d(LOG_TAG,”sip registration in progress”);

}}); });

c)Now the application is ready to make audio call using SIP manager api’s makeAdiocall passing audiolistner interface instance.

Let’s destAddr is the destination address String.

mSIPManager.makeAudioCall(mlocalProfile,(new SipProfile.Builder(destAddr)).build(), mSIPAdioListner , timeout_value);

where mSIPAudioListner is the audi listener interface which is the callback invoked when audio call is established, released.

SIPAudioCall.listner mSIPAdioListner = new SIPAudioCall.listner(){

@Override

public void onCallEstablished(SipAudioCall call) {

Log.d(LOG_TAG,”onCallEstablished”);

call.startAudio();

call.setSpeakerMode(true);

call.toggleMute();}

@Override

public void onCallEnded(SipAudioCall call) {

Log.d(LOG_TAG,”onCallEnded”); }}

Whenever SIP manager is instantiated by application, the application connects with SIP service. In makeAudioCall api call, the application creates SIP session with connected sip service and set up the audio listener.The makeAdiocall() api of SIP service is triggered with the arguments as destination and sender user profile. The SIP service process has SIPHelper class which has apis for sending SIP REGISTER,SIP INVITE,SIP 200 OK response, SIP ACK.This class uses javax.sip.header.HeaderFactory class to create SIP header from arguments user profile of sender and receiver. Once the SIP header is formed, SIPHelper sends SIP request to javax.sip.ClientTransaction class.Finally SIP packet delivered to network through established socket connection with SIP server.If you want to know the internally call flow in details, it is recommended to download VOIP framework source code from android git repo.

4)Source code :

1: SIP demo application example can be found at http://developer.android.com/resources/samples/SipDemo/index.html

2: The VOIP framework present in android source code directory is  /framework/base/voip

3: The third part application uses classes and interface present in package. /voip/java/android

4: The package used by SIP services process is  /voip/java/com/

5: Find the source code of android voip framework at  http://mirror.yongbok.net/linux/android/repository/frameworks/base/voip/

About Author

Jainendra Kumar is Android developer. He has rich experience in developing commercial Android application. He received B.E (Gold Medalist) from BIT Mesra and MS from BITS Pilani.

Advertisements

Andriod messaging: Radio Interface layer


Android SDK provides apis for sending sms and receiving SMS
The two primary classes as part of  android.telephony package used for developing SMS application are
1)SMSManager
2)SMSMessage

SMS Send :

SMSManager class has api sendTextMessage(String destination_Address,String service_center address,String message,
PendingIntent sentIntent,Pending deliveryIntent)

destination_Address
:  MDN no of sms receiver
Service_center_address :The service center address is retrieved from SIM.If null,sms packet will be routed to default MSC
Message :text message to send.

PendingIntent

Pending Intent (android.app.PendingIntent)
Android has provided PendingIntent class which defines an intent and the components which can handle the intent to perform specific task.
The sent pendingIntent,sendIntent which is called  from telephonic framework when sms is sent.
SMS is sent when sms packet is received by SMSC.

Delivery Intent :

The pendingIntent deliveryIntent defines an the application component which is fired when telephoy framework broadcast the delivery status of message. The SMSC forwards the message to SMS receiver .When it receives ack from sms receiver,it sends special message called Status report. This is sent only when sender has requested for delivery report.The particular field has to be set in SMS pdu packet. When telephony framework receives the status report message,it broadcast the sms delivery status.
The delivery intent is null if user has not enabled message delivery option in Message setting.

For example
SMSManager sm=sms.getDefault();
sm.sendTextMessage(” 944437890″,NULL,”hello”,si,di);

The instance of sent penidingIntent,si is obtained by
PendingIntent si = PendingIntent.getBroadcast(context, 0, new Intent(“SMS_SENT_ACTION), 0);

The above argumented context must register broadcast receiver  sentIntent :
context.registerReceiver(sentIntent,new IntentFilter(“SMS_SENT_ACTION”);

By registering broadcast receiver,the sentIntent receiver is called when sms sent event is dispatched by SMSDispacther of Telephony Framework. through si.send() call.

BroadcastReceiver sentIntent =new BroadcastReceiver(
public void OnReceive(Intent intent)
{
switch(intet.resultCode)
{
case :A\ctivity.OK :
“sms sent successfully ,move to sent folder with sent status”
case : SMSManager.RESULT_ERROR_GENERIC_FAILURE :
“sms failed ”
case :SMSManager.RESULT_ERROR_NO_SERVICW
“push the message in the sms queue database,message will be sent when network service will be available,move to outbox”
}  } );

The Application sends next message when the sent intent of previous message is arrived.Each typed message is queued in sms database.The next message from the top of the queue is taken when sent event of previous message is processed.This way messages are sent sequentially.

The delivery Intent di is obtained by
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(“SMS_DELIVERED_ACTION), 0);
Like sentIntent,the context has to register broadcast receiver,which be would handled when sms application receives the delivery of the message from framework
through delivery intent
The telephony intent broadcast with the deliveryIntent’s intent and the receiver deliveryIntent is triggered.

context.registerReceiver(deliveryIntent,new IntentFilter(“SMS_DELIVERED_ACTION)
BroadcastReceiver deliveryIntent =new BroadcastReceiver(
{
public void onReceiver(Intent intent)
{
if(getResultCode==Activity.OK)
“sms delivered update the status of sms to delivered in the sent folder”
}});

SMS Framework :SMS Apis,sendtextMessage calls SMS framework classes.The main class is SMSDispatcher which handles incoming and outgoing sms transaction and interacts with application through intent.The sms framework can send sms over CS(circuit switched) or PS(packet switch) network

For CS,framework will make RIL request to RIL(radio interface layer) layer of android telephony,RIL layer sends request to MODEM.MODEM route sms packet in form of data burst message to Base station.

For PS,Framework dispatch pdu to SIP stack ,which forms SIP(session initiation protocol) packet  and send to registered SIP server. The SMS framework is part of phone process and it is initialized on bootup. SMS framework forms transport layer sms pdu from the input parameters ,destination_no,message,sent intent,delivery intent.

The pdu is formed as per 3gpp2 sms Specification for sms over CDMA channel and 3gpp spec for sms to be sent over GSM based channel.

SMS RECEIVE :

Whenever phone boots up,the smsDispatcher(part of phone process),registers itself as handler to RIL(radio interface layer)
Whenever RIL receives unsolicited sms receive command from MODEM,it calls the sms handler with NEW_SMS_RECEIVE event and
smsDispatcher post the sms receive event to SMS application. The SMS application receives the new sms intent from framework and intent data contains SMS pdu which is byte array. The application creates android.telephony.SMSMessage object from received pdu through SDK class

SMSMesssage msg=SMSMessage.creatFromPdu(pdu)
The message display to display on UI can be obtained from msg Object as

String text=msg.getDisplayMessageBody()
String origination_no = msg.getDisplayOriginatingAddress()

The application should include the required permissions android.permission.SEND_SMS and android.permission.RECEIVE_SMS in sms application’s android.manifest file.

About Author

Jainendra Kumar is Android developer. He has rich experience in developing commercial Android application. He received B.E (Gold Medalist) from BIT Mesra and M.S from BITS Pilani.