summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/py_pjsua/pjsua_app.py
blob: ce94d6a25fb3da96af0e986168dc195868469cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# $Id$
#
# Sample and simple Python script to make and receive calls, and do
# presence and instant messaging/IM using PJSUA-API binding for Python.
#
# Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
#
import py_pjsua
import sys
import thread

#
# Configurations
#
THIS_FILE = "pjsua_app.py"
C_QUIT = 0
C_LOG_LEVEL = 4

# STUN config.
# Set C_STUN_SRV to the address of the STUN server to enable STUN
#
C_STUN_SRV = ""
C_SIP_PORT = 5060
C_STUN_PORT = 3478


# Globals
#
g_acc_id = py_pjsua.PJSUA_INVALID_ID
g_current_call = py_pjsua.PJSUA_INVALID_ID


# Utility to get call info
#
def call_name(call_id):
	ci = py_pjsua.call_get_info(call_id)
	return "[Call " + `call_id` + " " + ci.remote_info + "]"

# Callback when call state has changed.
#
def on_call_state(call_id, e):	
	global g_current_call
	ci = py_pjsua.call_get_info(call_id)
	write_log(3, call_name(call_id) + " state = " + `ci.state_text`)
	if ci.state == py_pjsua.PJSIP_INV_STATE_DISCONNECTED:
		g_current_call = py_pjsua.PJSUA_INVALID_ID

# Callback for incoming call
#
def on_incoming_call(acc_id, call_id, rdata):
	global g_current_call
	if g_current_call != py_pjsua.PJSUA_INVALID_ID:
		# There's call in progress - answer Busy
		py_pjsua.call_answer(call_id, 486, None, None)
		return

	g_current_call = call_id
	ci = py_pjsua.call_get_info(call_id)
	write_log(3, "*** Incoming call: " + call_name(call_id) + "***")
	write_log(3, "*** Press a to answer or h to hangup  ***");

	
# Callback when media state has changed (e.g. established or terminated)
#
def on_call_media_state(call_id):
	ci = py_pjsua.call_get_info(call_id)
	if ci.media_status == py_pjsua.PJSUA_CALL_MEDIA_ACTIVE:
		py_pjsua.conf_connect(ci.conf_slot, 0)
		py_pjsua.conf_connect(0, ci.conf_slot)
		write_log(3, call_name(call_id) + ": media is active")
	else:
		write_log(3, call_name(call_id) + ": media is inactive")


# Callback when account registration state has changed
#
def on_reg_state(acc_id):
	acc_info = py_pjsua.acc_get_info(acc_id)
	if acc_info.status != 0 and acc_info.status != 200:
		write_log(3, "Account (un)registration failed: rc=" + `acc_info.status` + " " + acc_info.status_text)
	else:
		write_log(3, "Account successfully (un)registered")


# Callback when buddy's presence state has changed
#
def on_buddy_state(buddy_id):
	write_log(3, "On Buddy state called")
	buddy_info = py_pjsua.buddy_get_info(buddy_id)
	if buddy_info.status != 0 and buddy_info.status != 200:
		write_log(3, "Status of " + `buddy_info.uri` + " is " + `buddy_info.status_text`)
	else:
		write_log(3, "Status : " + `buddy_info.status`)

# Callback on incoming pager (MESSAGE)
#		
def on_pager(call_id, strfrom, strto, contact, mime_type, text):
	write_log(3, "MESSAGE from " + `strfrom` + " : " + `text`)


# Callback on the delivery status of outgoing pager (MESSAGE)
#	
def on_pager_status(call_id, strto, body, user_data, status, reason):
	write_log(3, "MESSAGE to " + `strto` + " status " + `status` + " reason " + `reason`)


# Utility: display PJ error and exit
#
def err_exit(title, rc):
    py_pjsua.perror(THIS_FILE, title, rc)
    exit(1)


# Logging function (also callback, called by pjsua-lib)
#
def log_cb(level, str, len):
    if level <= C_LOG_LEVEL:
        print str,

def write_log(level, str):
    log_cb(level, str + "\n", 0)


#
# Initialize pjsua.
#
def app_init():
	global g_acc_id

	# Create pjsua before anything else
	status = py_pjsua.create()
	if status != 0:
		err_exit("pjsua create() error", status)

	# Create and initialize logging config
	log_cfg = py_pjsua.logging_config_default()
	log_cfg.level = C_LOG_LEVEL
	log_cfg.cb = log_cb

	# Create and initialize pjsua config
	# Note: for this Python module, thread_cnt must be 0 since Python
	#       doesn't like to be called from alien thread (pjsua's thread
	#       in this case)	    
	ua_cfg = py_pjsua.config_default()
	ua_cfg.thread_cnt = 0
	ua_cfg.user_agent = "PJSUA/Python 0.1"
	ua_cfg.cb.on_incoming_call = on_incoming_call
	ua_cfg.cb.on_call_media_state = on_call_media_state
	ua_cfg.cb.on_reg_state = on_reg_state
	ua_cfg.cb.on_call_state = on_call_state
	ua_cfg.cb.on_buddy_state = on_buddy_state
	ua_cfg.cb.on_pager = on_pager
	ua_cfg.cb.on_pager_status = on_pager_status
	

	# Create and initialize media config
	med_cfg = py_pjsua.media_config_default()
	med_cfg.ec_tail_len = 0

	#
	# Initialize pjsua!!
	#
	status = py_pjsua.init(ua_cfg, log_cfg, med_cfg)
	if status != 0:
		err_exit("pjsua init() error", status)

	# Configure STUN config
	stun_cfg = py_pjsua.stun_config_default()
	stun_cfg.stun_srv1 = C_STUN_SRV
	stun_cfg.stun_srv2 = C_STUN_SRV
	stun_cfg.stun_port1 = C_STUN_PORT
	stun_cfg.stun_port2 = C_STUN_PORT

	# Configure UDP transport config
	transport_cfg = py_pjsua.transport_config_default()
	transport_cfg.port = C_SIP_PORT
	transport_cfg.stun_config = stun_cfg
	if C_STUN_SRV != "":
		transport_cfg.use_stun = 1

	# Create UDP transport
	status, transport_id = \
	    py_pjsua.transport_create(py_pjsua.PJSIP_TRANSPORT_UDP, transport_cfg)
	if status != 0:
		py_pjsua.destroy()
		err_exit("Error creating UDP transport", status)

	# Create initial default account
	status, acc_id = py_pjsua.acc_add_local(transport_id, 1)
	if status != 0:
		py_pjsua.destroy()
		err_exit("Error creating account", status)

	g_acc_id = acc_id

# Add SIP account interractively
#
def add_account():
	global g_acc_id

	acc_domain = ""
	acc_username = ""
	acc_passwd =""
	confirm = ""
	
	# Input account configs
	print "Your SIP domain (e.g. myprovider.com): ",
	acc_domain = sys.stdin.readline()
	if acc_domain == "\n": 
		return
	acc_domain = acc_domain.replace("\n", "")

	print "Your username (e.g. alice): ",
	acc_username = sys.stdin.readline()
	if acc_username == "\n":
		return
	acc_username = acc_username.replace("\n", "")

	print "Your password (e.g. secret): ",
	acc_passwd = sys.stdin.readline()
	if acc_passwd == "\n":
		return
	acc_passwd = acc_passwd.replace("\n", "")

	# Configure account configuration
	acc_cfg = py_pjsua.acc_config_default()
	acc_cfg.id = "sip:" + acc_username + "@" + acc_domain
	acc_cfg.reg_uri = "sip:" + acc_domain
	acc_cfg.cred_count = 1
	acc_cfg.cred_info[0].realm = acc_domain
	acc_cfg.cred_info[0].scheme = "digest"
	acc_cfg.cred_info[0].username = acc_username
	acc_cfg.cred_info[0].data_type = 0
	acc_cfg.cred_info[0].data = acc_passwd

	# Add new SIP account
	status, acc_id = py_pjsua.acc_add(acc_cfg, 1)
	if status != 0:
		py_pjsua.perror(THIS_FILE, "Error adding SIP account", status)
	else:
		g_acc_id = acc_id
		write_log(3, "Account " + acc_cfg.id + " added")


#
# Worker thread function.
# Python doesn't like it when it's called from an alien thread
# (pjsua's worker thread, in this case), so for Python we must
# disable worker thread in pjsua and poll pjsua from Python instead.
#
def worker_thread_main(arg):
	global C_QUIT
	thread_desc = 0;
	status = py_pjsua.thread_register("python worker", thread_desc)
	if status != 0:
		py_pjsua.perror(THIS_FILE, "Error registering thread", status)
	else:
		while C_QUIT == 0:
			py_pjsua.handle_events(50)
		print "Worker thread quitting.."
		C_QUIT = 2


# Start pjsua
#
def app_start():
	# Done with initialization, start pjsua!!
	#
	status = py_pjsua.start()
	if status != 0:
		py_pjsua.destroy()
		err_exit("Error starting pjsua!", status)

	# Start worker thread
	thr = thread.start_new(worker_thread_main, (0,))
    
	print "PJSUA Started!!"


# Print application menu
#
def print_menu():
	print """
Menu:
  q   Quit application
 +a   Add account
 +b   Add buddy
  m   Make call
  a   Answer current call (if any)
  h   Hangup current call (if any)
  i   Send instant message
	"""
	print "Choice: ", 

# Menu
#
def app_menu():
	global g_acc_id
	global g_current_call

	quit = 0
	while quit == 0:
		print_menu()
		choice = sys.stdin.readline()

		if choice[0] == "q":
			quit = 1

		elif choice[0] == "i":
			# Sending IM	
			print "Send IM to SIP URL: ",
			url = sys.stdin.readline()
			if url == "\n":
				continue

			# Send typing indication
			py_pjsua.im_typing(g_acc_id, url, 1, None) 

			print "The content: ",
			message = sys.stdin.readline()
			if message == "\n":
				py_pjsua.im_typing(g_acc_id, url, 0, None) 		
				continue

			# Send the IM!
			py_pjsua.im_send(g_acc_id, url, None, message, None, 0)

		elif choice[0] == "m":
			# Make call 
			print "Using account ", g_acc_id
			print "Make call to SIP URL: ",
			url = sys.stdin.readline()
			url = url.replace("\n", "")
			if url == "":
				continue

			# Initiate the call!
			status, call_id = py_pjsua.call_make_call(g_acc_id, url, 0, 0, None)
            
			if status != 0:
				py_pjsua.perror(THIS_FILE, "Error making call", status)
			else:
				g_current_call = call_id

		elif choice[0] == "+" and choice[1] == "b":
			# Add new buddy
			bc = py_pjsua.Buddy_Config()
			print "Buddy URL: ",
			bc.uri = sys.stdin.readline()
			if bc.uri == "\n":
				continue
            
			bc.subscribe = 1
			status, buddy_id = py_pjsua.buddy_add(bc)
			if status != 0:
				py_pjsua.perror(THIS_FILE, "Error adding buddy", status)

		elif choice[0] == "+" and choice[1] == "a":
			# Add account
			add_account()

		elif choice[0] == "h":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:
				py_pjsua.call_hangup(g_current_call, 603, None, None)
			else:
				print "No current call"

		elif choice[0] == "a":
			if g_current_call != py_pjsua.PJSUA_INVALID_ID:
				py_pjsua.call_answer(g_current_call, 200, None, None)
			else:
				print "No current call"


#
# main
#
app_init()
app_start()
app_menu()

#
# Done, quitting..
#
print "PJSUA shutting down.."
C_QUIT = 1
# Give the worker thread chance to quit itself
while C_QUIT != 2:
    py_pjsua.handle_events(50)

print "PJSUA destroying.."
py_pjsua.destroy()