| 1 | = Your Python SIP "Hello World!" Application = |
| 2 | |
| 3 | [[TracNav(Python_SIP/TOC)]] |
| 4 | |
| 5 | |
| 6 | 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. |
| 7 | |
| 8 | |
| 9 | == Simple Caller Application == #makecall |
| 10 | |
| 11 | 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. |
| 12 | |
| 13 | {{{ |
| 14 | #!python |
| 15 | import sys |
| 16 | import pjsua as pj |
| 17 | |
| 18 | # Logging callback |
| 19 | def log_cb(level, str, len): |
| 20 | print str, |
| 21 | |
| 22 | # Callback to receive events from Call |
| 23 | class 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 |
| 46 | if len(sys.argv) != 2: |
| 47 | print "Usage: simplecall.py <dst-URI>" |
| 48 | sys.exit(1) |
| 49 | |
| 50 | try: |
| 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 | |
| 77 | except pj.Error, e: |
| 78 | print "Exception: " + str(e) |
| 79 | lib.destroy() |
| 80 | lib = None |
| 81 | sys.exit(1) |
| 82 | }}} |
| 83 | |
| 84 | Save the snippet above as '''simplecall.py'''. |
| 85 | |
| 86 | Now 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 | |
| 92 | Here'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 |
| 101 | Call is CALLING last code = 0 () |
| 102 | Press <ENTER> to quit |
| 103 | Call is EARLY last code = 180 (Ringing) |
| 104 | Call is CONNECTING last code = 200 (OK) |
| 105 | Media is now active |
| 106 | Call is CONFIRMED last code = 200 (OK) |
| 107 | Call is DISCONNCTD last code = 200 (Normal call clearing) |
| 108 | }}} |
| 109 | |