Changes between Initial Version and Version 1 of Python_SIP/Basic_Concept

Jul 22, 2008 9:50:28 PM (14 years ago)



  • Python_SIP/Basic_Concept

    v1 v1  
     1= PJSUA Python Module = 
     5== Developing Python SIP Application == #develop 
     7=== Concepts === #concepts 
     9==== Asynchronous Operations ==== 
     11If 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. 
     13In 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. 
     16==== Where to Start ==== 
     18The [ 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.  
     20 '''[ Lib] class:''' :: 
     21   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. 
     23 '''[ Transport] class:''' :: 
     24   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. 
     26  '''[ Account] class:''' :: 
     27   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. 
     29  '''[ Call] class:''' :: 
     30   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. 
     32  '''[ Buddy] class:''' :: 
     33   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. 
     36==== Basic Usage Pattern ==== 
     38With 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? 
     40It's with the callback classes. 
     42Callback 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. 
     44It's probably much easier to explain with an example. 
     46First derive a class from the appropriate callback class, say [ CallCallback] class: 
     48  {{{ 
     51class MyCallCallback(pjsua.CallCallback): 
     52    # You can put glue attributes here. 
     53    # e.g. we'll put an imaginary text control here 
     54    text_ctrl = None 
     56    def __init__(self, call=None): 
     57        pjsua.CallCallback.__init__(self, call) 
     59    def on_state(self): 
     60        text_ctrl.set_text("Call state is " + 
     63def make_call(dst_uri): 
     64    my_cb = MyCallCallback(none) 
     65    try: 
     66        call = acc.make_call(dst_uri, cb=my_cb) 
     67    except pjsua.Error, err: 
     68        print 'Error making call', err 
     69        return None 
     70    return call 
     71  }}} 
     73I 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. 
     76==== Error Handling ==== 
     78By 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: 
     80 {{{ 
     82import pjsua 
     85    call = acc.make_call('') 
     86except pjsua.Error, err: 
     87    print 'Exception has occured:', err 
     89    print 'Ouch..' 
     90 }}} 
     92The 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. 
     95==== Threading ==== 
     97For 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. 
     99The pjsua module should be thread safe.