wiki:Python_SIP/Basic_Concept

Version 1 (modified by bennylp, 11 years ago) (diff)

--

PJSUA Python Module

Developing Python SIP Application

Concepts

Asynchronous Operations

If you have developed applications with PJSIP you'll know about this already, but this concept probably needs to be explained a little bit here to new PJSIP users.

In PJSIP, all operations that involve sending and receiving SIP messages are asynchronous, meaning that the function that invokes the operation will complete immediately, and you will be given the completion status as callbacks. Take a look for example the make_call() method of the Account class. This function is used to initiate outgoing call to a destination. When this function returns successfully, it does not mean that the call has been established, but rather that the call has been initiated successfully. You will be given the report of the call completion (such as Ringing or Connected/Confirmed events) in the on_state() callback of CallCallback class.

Where to Start

The PJSUA Python Module Reference Manual shows quite few classes there, and it doesn't show clearly where to start. But actually the concept is quite simple, with the main classes described below.

Lib class:
This is the main class of the library. You need to instantiate one and exactly one of this class, and from the instance you initialize and start the library, and create other objects such as transports and accounts.
Transport class:
A transport more or less specifies a listening socket. You need to create one or more SIP transports before the application can send or receive SIP messages, and the transport creation will return a Transport object. There is not much use of the Transport object, except to add a local account (which is optional if you have a real account) and to display list of listening socket addresses to users if you want to.
Account class:
An account specifies the identity of the person (or endpoint) on one side of SIP conversation. At least one Account instance needs to be created before anything else, and from the account instance you can start making/receiving calls as well as adding buddies.
Call class:
This class is used to manipulate calls, such as to answer the call, hangup the call, put the call on hold, transfer the call, etc.
Buddy class:
This class represents a remote buddy (a person, or a SIP endpoint). You can subscribe to presence status of a buddy to know whether the buddy is online/offline/etc., and you can send and receive instant messages to/from the buddy.

Basic Usage Pattern

With the methods of the main classes above, you will be able to invoke various operations quite easily. But how can we get events/notifications from these classes?

It's with the callback classes.

Callback classes also provide the mean to connect pjsua objects with your application's objects. The idea is like this. All the pjsua main classes above (Lib, Account, Call, and Buddy) are final classes, meaning they are not intended to be subclasses or derived. Each of these classes (except Lib) have their corresponding callback class (AccountCallback, [CallCallback, and [BuddyCallback), and events emitted by these classes will be sent to the callback class. These callback classes are subclass-able, and in fact you must derive a class from these callback class in order to customize the processing of the events.

It's probably much easier to explain with an example.

First derive a class from the appropriate callback class, say CallCallback class:

class MyCallCallback(pjsua.CallCallback):
    # You can put glue attributes here.
    # e.g. we'll put an imaginary text control here
    text_ctrl = None

    def __init__(self, call=None):
        pjsua.CallCallback.__init__(self, call)

    def on_state(self):
        text_ctrl.set_text("Call state is " + self.call.info().state_text)


def make_call(dst_uri):
    my_cb = MyCallCallback(none)
    try:
        call = acc.make_call(dst_uri, cb=my_cb)
    except pjsua.Error, err:
        print 'Error making call', err
        return None
    return call

I hope the snippet above will give you a bit of info on the basic pattern used by the library. More will be explained in later sections.

Error Handling

By convention, we use exceptions as means to report error, as this would make the program flows more naturally. Operations which yield error will raise pjsua.Error exception. Here is a sample:

import pjsua

try:
    call = acc.make_call('sip:buddy@example.org')
except pjsua.Error, err:
    print 'Exception has occured:', err
except:
    print 'Ouch..'

The sample above will print the full error information to stdout. If you prefer to display the error in more structured manner, the pjsua.Error class has several members to explain the error, such as the object name that raised the error, the operation name, and the error message itself.

Threading

For platforms that require polling, the pjsua module provides it's own worker thread to poll pjsip, so it is not necessary to instantiate own your polling thread. Having said that the application should be prepared to have the callbacks called by different thread than the main thread.

The pjsua module should be thread safe.