Debian Codegraph @

Xpjproject/pjsip-apps/src/python/samples/call.py

raw symbols SCIP
1 #
2 # SIP call sample.
3 #
4 # Copyright (C) 2003-2008 Benny Prijono <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 import sys
21 import pjsua as pj
22
23 LOG_LEVEL=3
24 current_call = None
25
26 # Logging callback
27 def log_cb(level, str, len):
28 print str,
29
30
31 # Callback to receive events from account
32 class MyAccountCallback(pj.AccountCallback):
33
34 def __init__(self, account=None):
35 pj.AccountCallback.__init__(self, account)
36
37 # Notification on incoming call
38 def on_incoming_call(self, call):
39 global current_call
40 if current_call:
41 call.answer(486, "Busy")
42 return
43
44 print "Incoming call from ", call.info().remote_uri
45 print "Press 'a' to answer"
46
47 current_call = call
48
49 call_cb = MyCallCallback(current_call)
50 current_call.set_callback(call_cb)
51
52 current_call.answer(180)
53
54
55 # Callback to receive events from Call
56 class MyCallCallback(pj.CallCallback):
57
58 def __init__(self, call=None):
59 pj.CallCallback.__init__(self, call)
60
61 # Notification when call state has changed
62 def on_state(self):
63 global current_call
64 print "Call with", self.call.info().remote_uri,
65 print "is", self.call.info().state_text,
66 print "last code =", self.call.info().last_code,
67 print "(" + self.call.info().last_reason + ")"
68
69 if self.call.info().state == pj.CallState.DISCONNECTED:
70 current_call = None
71 print 'Current call is', current_call
72
73 # Notification when call's media state has changed.
74 def on_media_state(self):
75 if self.call.info().media_state == pj.MediaState.ACTIVE:
76 # Connect the call to sound device
77 call_slot = self.call.info().conf_slot
78 pj.Lib.instance().conf_connect(call_slot, 0)
79 pj.Lib.instance().conf_connect(0, call_slot)
80 print "Media is now active"
81 else:
82 print "Media is inactive"
83
84 # Function to make call
85 def make_call(uri):
86 try:
87 print "Making call to", uri
88 return acc.make_call(uri, cb=MyCallCallback())
89 except pj.Error, e:
90 print "Exception: " + str(e)
91 return None
92
93
94 # Create library instance
95 lib = pj.Lib()
96
97 try:
98 # Init library with default config and some customized
99 # logging config.
100 lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
101
102 # Create UDP transport which listens to any available port
103 transport = lib.create_transport(pj.TransportType.UDP,
104 pj.TransportConfig(0))
105 print "\nListening on", transport.info().host,
106 print "port", transport.info().port, "\n"
107
108 # Start the library
109 lib.start()
110
111 # Create local account
112 acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
113
114 # If argument is specified then make call to the URI
115 if len(sys.argv) > 1:
116 lck = lib.auto_lock()
117 current_call = make_call(sys.argv[1])
118 print 'Current call is', current_call
119 del lck
120
121 my_sip_uri = "sip:" + transport.info().host + \
122 ":" + str(transport.info().port)
123
124 # Menu loop
125 while True:
126 print "My SIP URI is", my_sip_uri
127 print "Menu: m=make call, h=hangup call, a=answer call, q=quit"
128
129 input = sys.stdin.readline().rstrip("\r\n")
130 if input == "m":
131 if current_call:
132 print "Already have another call"
133 continue
134 print "Enter destination URI to call: ",
135 input = sys.stdin.readline().rstrip("\r\n")
136 if input == "":
137 continue
138 lck = lib.auto_lock()
139 current_call = make_call(input)
140 del lck
141
142 elif input == "h":
143 if not current_call:
144 print "There is no call"
145 continue
146 current_call.hangup()
147
148 elif input == "a":
149 if not current_call:
150 print "There is no call"
151 continue
152 current_call.answer(200)
153
154 elif input == "q":
155 break
156
157 # Shutdown the library
158 transport = None
159 acc.delete()
160 acc = None
161 lib.destroy()
162 lib = None
163
164 except pj.Error, e:
165 print "Exception: " + str(e)
166 lib.destroy()
167 lib = None
168