1
# $Id: call.py 2171 2008-07-24 09:01:33Z bennylp $
5
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
def log_cb(level, str, len):
32
# Callback to receive events from account
33
class MyAccountCallback(pj.AccountCallback):
35
def __init__(self, account=None):
36
pj.AccountCallback.__init__(self, account)
38
# Notification on incoming call
39
def on_incoming_call(self, call):
42
call.answer(486, "Busy")
45
print "Incoming call from ", call.info().remote_uri
46
print "Press 'a' to answer"
50
call_cb = MyCallCallback(current_call)
51
current_call.set_callback(call_cb)
53
current_call.answer(180)
56
# Callback to receive events from Call
57
class MyCallCallback(pj.CallCallback):
59
def __init__(self, call=None):
60
pj.CallCallback.__init__(self, call)
62
# Notification when call state has changed
65
print "Call with", self.call.info().remote_uri,
66
print "is", self.call.info().state_text,
67
print "last code =", self.call.info().last_code,
68
print "(" + self.call.info().last_reason + ")"
70
if self.call.info().state == pj.CallState.DISCONNECTED:
72
print 'Current call is', current_call
74
# Notification when call's media state has changed.
75
def on_media_state(self):
76
if self.call.info().media_state == pj.MediaState.ACTIVE:
77
# Connect the call to sound device
78
call_slot = self.call.info().conf_slot
79
pj.Lib.instance().conf_connect(call_slot, 0)
80
pj.Lib.instance().conf_connect(0, call_slot)
81
print "Media is now active"
83
print "Media is inactive"
85
# Function to make call
88
print "Making call to", uri
89
return acc.make_call(uri, cb=MyCallCallback())
91
print "Exception: " + str(e)
95
# Create library instance
99
# Init library with default config and some customized
101
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
103
# Create UDP transport which listens to any available port
104
transport = lib.create_transport(pj.TransportType.UDP,
105
pj.TransportConfig(0))
106
print "\nListening on", transport.info().host,
107
print "port", transport.info().port, "\n"
112
# Create local account
113
acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
115
# If argument is specified then make call to the URI
116
if len(sys.argv) > 1:
117
lck = lib.auto_lock()
118
current_call = make_call(sys.argv[1])
119
print 'Current call is', current_call
122
my_sip_uri = "sip:" + transport.info().host + \
123
":" + str(transport.info().port)
127
print "My SIP URI is", my_sip_uri
128
print "Menu: m=make call, h=hangup call, a=answer call, q=quit"
130
input = sys.stdin.readline().rstrip("\r\n")
133
print "Already have another call"
135
print "Enter destination URI to call: ",
136
input = sys.stdin.readline().rstrip("\r\n")
139
lck = lib.auto_lock()
140
current_call = make_call(input)
145
print "There is no call"
147
current_call.hangup()
151
print "There is no call"
153
current_call.answer(200)
158
# Shutdown the library
166
print "Exception: " + str(e)