Your Python SIP "Hello World!" Application
TracNav
I'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 in less than 100 lines.
Simple Caller Application
This 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.
import sys import pjsua as pj # Logging callback def log_cb(level, str, len): print str, # Callback to receive events from Call class MyCallCallback(pj.CallCallback): def __init__(self, call=None): pj.CallCallback.__init__(self, call) # Notification when call state has changed def on_state(self): print "Call is ", self.call.info().state_text, print "last code =", self.call.info().last_code, print "(" + self.call.info().last_reason + ")" # Notification when call's media state has changed. def on_media_state(self): global lib if self.call.info().media_state == pj.MediaState.ACTIVE: # Connect the call to sound device call_slot = self.call.info().conf_slot lib.conf_connect(call_slot, 0) lib.conf_connect(0, call_slot) print "Hello world, I can talk!" # Check command line argument if len(sys.argv) != 2: print "Usage: simplecall.py <dst-URI>" sys.exit(1) try: # Create library instance lib = pj.Lib() # Init library with default config lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb)) # Create UDP transport which listens to any available port transport = lib.create_transport(pj.TransportType.UDP) # Start the library lib.start() # Create local/user-less account acc = lib.create_account_for_transport(transport) # Make call call = acc.make_call(sys.argv[1], MyCallCallback()) # Wait for ENTER before quitting print "Press <ENTER> to quit" input = sys.stdin.readline().rstrip("\r\n") # We're done, shutdown the library lib.destroy() lib = None except pj.Error, e: print "Exception: " + str(e) lib.destroy() lib = None sys.exit(1)
Save the snippet above as simplecall.py.
Now you can call another softphone (such as 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:
python simplecall.py sip:192.168.0.15:5080
Here's the output of the program when I run it on Windows XP:
13:05:25.718 os_core_win32. pjlib 0.9.0-trunk for win32 initialized 13:05:25.718 sip_endpoint.c Creating endpoint instance... 13:05:25.718 pjlib WinNT IOCP I/O Queue created (00D230A0) 13:05:25.734 sip_endpoint.c Module "mod-msg-print" registered 13:05:25.734 sip_transport. Transport manager created. 13:05:25.968 pjsua_core.c pjsua version 0.9.0-trunk for win32 initialized Call is CALLING last code = 0 () Press <ENTER> to quit Call is EARLY last code = 180 (Ringing) Call is CONNECTING last code = 200 (OK) Hello world, I can talk! Call is CONFIRMED last code = 200 (OK) Call is DISCONNCTD last code = 200 (Normal call clearing)
More Call Samples
Issues
- Authentication: the call samples above are not equipped with any SIP authentication credentials, thus you cannot call URI which requires SIP authentication. Please follow the rest of this tutorial (especially the Account page) to see how to put authentication credentials.