Changes between Initial Version and Version 1 of Python_SIP/Hello_World


Ignore:
Timestamp:
Jul 23, 2008 12:11:21 PM (16 years ago)
Author:
bennylp
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Python_SIP/Hello_World

    v1 v1  
     1= Your Python SIP "Hello World!" Application = 
     2 
     3[[TracNav(Python_SIP/TOC)]] 
     4 
     5 
     6I'm not sure how one is supposed to make a SIP "Hello World!" application, but anyway here it is, a simple application to make call to a destination. 
     7 
     8 
     9== Simple Caller Application == #makecall 
     10 
     11This application will make call to a destination URI specified in the command line argument, and establish audio communication with remote party once the call is connected. 
     12 
     13 {{{ 
     14#!python 
     15import sys 
     16import pjsua as pj 
     17 
     18# Logging callback 
     19def log_cb(level, str, len): 
     20    print str, 
     21 
     22# Callback to receive events from Call 
     23class MyCallCallback(pj.CallCallback): 
     24    def __init__(self, call=None): 
     25        pj.CallCallback.__init__(self, call) 
     26 
     27    # Notification when call state has changed 
     28    def on_state(self): 
     29        print "Call is ", self.call.info().state_text, 
     30        print "last code =", self.call.info().last_code,  
     31        print "(" + self.call.info().last_reason + ")" 
     32         
     33    # Notification when call's media state has changed. 
     34    def on_media_state(self): 
     35        if self.call.info().media_state == pj.MediaState.ACTIVE: 
     36            # Connect the call to sound device 
     37            call_slot = self.call.info().conf_slot 
     38            pj.Lib.instance().conf_connect(call_slot, 0) 
     39            pj.Lib.instance().conf_connect(0, call_slot) 
     40            print "Media is now active" 
     41        else: 
     42            print "Media is inactive" 
     43 
     44 
     45# Check command line argument 
     46if len(sys.argv) != 2: 
     47    print "Usage: simplecall.py <dst-URI>" 
     48    sys.exit(1) 
     49 
     50try: 
     51    # Create library instance 
     52    lib = pj.Lib() 
     53 
     54    # Init library with default config 
     55    lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb)) 
     56 
     57    # Create UDP transport which listens to any available port 
     58    transport = lib.create_transport(pj.TransportType.UDP) 
     59     
     60    # Start the library 
     61    lib.start() 
     62 
     63    # Create local/user-less account 
     64    acc = lib.create_account_for_transport(transport) 
     65 
     66    # Make call 
     67    call = acc.make_call(sys.argv[1], MyCallCallback()) 
     68 
     69    # Wait for ENTER before quitting 
     70    print "Press <ENTER> to quit" 
     71    input = sys.stdin.readline().rstrip("\r\n") 
     72 
     73    # We're done, shutdown the library 
     74    lib.destroy() 
     75    lib = None 
     76 
     77except pj.Error, e: 
     78    print "Exception: " + str(e) 
     79    lib.destroy() 
     80    lib = None 
     81    sys.exit(1) 
     82 }}} 
     83 
     84Save the snippet above as '''simplecall.py'''. 
     85 
     86Now you can call another softphone (such as [http://www.pjsip.org/pjsua.htm pjsua]) by specifying the SIP URI as command line argument. For example, if the other softphone is on "sip:192.168.0.15:5080", run the program as follows: 
     87 
     88{{{ 
     89 python simplecall.py sip:192.168.0.15:5080 
     90}}} 
     91 
     92Here's the output of the program when I run it on Windows XP: 
     93 
     94{{{ 
     95 13:05:25.718 os_core_win32. pjlib 0.9.0-trunk for win32 initialized 
     96 13:05:25.718 sip_endpoint.c Creating endpoint instance... 
     97 13:05:25.718          pjlib WinNT IOCP I/O Queue created (00D230A0) 
     98 13:05:25.734 sip_endpoint.c Module "mod-msg-print" registered 
     99 13:05:25.734 sip_transport. Transport manager created. 
     100 13:05:25.968   pjsua_core.c pjsua version 0.9.0-trunk for win32 initialized 
     101Call is  CALLING last code = 0 () 
     102Press <ENTER> to quit 
     103Call is  EARLY last code = 180 (Ringing) 
     104Call is  CONNECTING last code = 200 (OK) 
     105Media is now active 
     106Call is  CONFIRMED last code = 200 (OK) 
     107Call is  DISCONNCTD last code = 200 (Normal call clearing) 
     108}}} 
     109