1# $Id$
2#
3# SIP account and registration sample. In this sample, the program
4# will block to wait until registration is complete
5#
6# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21#
22import sys
23import pjsua as pj
24
25# Logging callback
26def log_cb(level, str, len):
27    print str,
28
29# Callback to receive events from Call
30class MyCallCallback(pj.CallCallback):
31    def __init__(self, call=None):
32        pj.CallCallback.__init__(self, call)
33
34    # Notification when call state has changed
35    def on_state(self):
36        print "Call is ", self.call.info().state_text,
37        print "last code =", self.call.info().last_code,
38        print "(" + self.call.info().last_reason + ")"
39
40    # Notification when call's media state has changed.
41    def on_media_state(self):
42        global lib
43        if self.call.info().media_state == pj.MediaState.ACTIVE:
44            # Connect the call to sound device
45            call_slot = self.call.info().conf_slot
46            lib.conf_connect(call_slot, 0)
47            lib.conf_connect(0, call_slot)
48            print "Hello world, I can talk!"
49
50
51# Check command line argument
52if len(sys.argv) != 2:
53    print "Usage: simplecall.py <dst-URI>"
54    sys.exit(1)
55
56try:
57    # Create library instance
58    lib = pj.Lib()
59
60    # Init library with default config
61    lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb))
62
63    # Create UDP transport which listens to any available port
64    transport = lib.create_transport(pj.TransportType.UDP)
65
66    # Start the library
67    lib.start()
68
69    # Create local/user-less account
70    acc = lib.create_account_for_transport(transport)
71
72    # Make call
73    call = acc.make_call(sys.argv[1], MyCallCallback())
74
75    # Wait for ENTER before quitting
76    print "Press <ENTER> to quit"
77    input = sys.stdin.readline().rstrip("\r\n")
78
79    # We're done, shutdown the library
80    lib.destroy()
81    lib = None
82
83except pj.Error, e:
84    print "Exception: " + str(e)
85    lib.destroy()
86    lib = None
87    sys.exit(1)
88
89