Changes between Initial Version and Version 1 of Python_SIP/Basic_Concept


Ignore:
Timestamp:
Jul 22, 2008 9:50:28 PM (16 years ago)
Author:
bennylp
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Python_SIP/Basic_Concept

    v1 v1  
     1= PJSUA Python Module = 
     2 
     3[[TracNav(Python_SIP/TOC)]] 
     4 
     5== Developing Python SIP Application == #develop 
     6 
     7=== Concepts === #concepts 
     8 
     9==== Asynchronous Operations ==== 
     10 
     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. 
     12 
     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 [http://www.pjsip.org/python/pjsua.htm#Account 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 [http://www.pjsip.org/python/pjsua.htm#CallCallback CallCallback] class. 
     14 
     15 
     16==== Where to Start ==== 
     17 
     18The [http://www.pjsip.org/python/pjsua.htm 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.  
     19 
     20 '''[http://www.pjsip.org/python/pjsua.htm#Lib 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. 
     22 
     23 '''[http://www.pjsip.org/python/pjsua.htm#Transport 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. 
     25 
     26  '''[http://www.pjsip.org/python/pjsua.htm#Account 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. 
     28 
     29  '''[http://www.pjsip.org/python/pjsua.htm#Call 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. 
     31 
     32  '''[http://www.pjsip.org/python/pjsua.htm#Buddy 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. 
     34 
     35 
     36==== Basic Usage Pattern ==== 
     37 
     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? 
     39 
     40It's with the callback classes. 
     41 
     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 ([http://www.pjsip.org/python/pjsua.htm#AccountCallback AccountCallback], [[http://www.pjsip.org/python/pjsua.htm#CallCallback CallCallback], and [[http://www.pjsip.org/python/pjsua.htm#BuddyCallback 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. 
     43 
     44It's probably much easier to explain with an example. 
     45 
     46First derive a class from the appropriate callback class, say [http://www.pjsip.org/python/pjsua.htm#CallCallback CallCallback] class: 
     47 
     48  {{{ 
     49#!python 
     50 
     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 
     55 
     56    def __init__(self, call=None): 
     57        pjsua.CallCallback.__init__(self, call) 
     58 
     59    def on_state(self): 
     60        text_ctrl.set_text("Call state is " + self.call.info().state_text) 
     61 
     62 
     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  }}} 
     72 
     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. 
     74 
     75 
     76==== Error Handling ==== 
     77 
     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 [http://www.pjsip.org/python/pjsua.htm#Error pjsua.Error] exception. Here is a sample: 
     79 
     80 {{{ 
     81#!python 
     82import pjsua 
     83 
     84try: 
     85    call = acc.make_call('sip:buddy@example.org') 
     86except pjsua.Error, err: 
     87    print 'Exception has occured:', err 
     88except: 
     89    print 'Ouch..' 
     90 }}} 
     91 
     92The sample above will print the full error information to stdout. If you prefer to display the error in more structured manner, the [http://www.pjsip.org/python/pjsua.htm#Error 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. 
     93 
     94 
     95==== Threading ==== 
     96 
     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. 
     98 
     99The pjsua module should be thread safe.