summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pygui/chat.py
blob: 760af1c7944e4f5ba0dd217f5c4a0819ddcd66b2 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# $Id$
#
# pjsua Python GUI Demo
#
# Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
#
import sys
if sys.version_info[0] >= 3: # Python 3
	import tkinter as tk
	from tkinter import ttk
else:
	import Tkinter as tk
	import ttk

import buddy
import call
import chatgui as gui
import endpoint as ep
import pjsua2 as pj
import re

SipUriRegex = re.compile('(sip|sips):([^:;>\@]*)@?([^:;>]*):?([^:;>]*)')
ConfIdx = 1

# Simple SIP uri parser, input URI must have been validated
def ParseSipUri(sip_uri_str):
	m = SipUriRegex.search(sip_uri_str)
	if not m:
		assert(0)
		return None
	
	scheme = m.group(1)
	user = m.group(2)
	host = m.group(3)
	port = m.group(4)
	if host == '':
		host = user
		user = ''
		
	return SipUri(scheme.lower(), user, host.lower(), port)
	
class SipUri:
	def __init__(self, scheme, user, host, port):
		self.scheme = scheme
		self.user = user
		self.host = host
		self.port = port
		
	def __cmp__(self, sip_uri):
		if self.scheme == sip_uri.scheme and self.user == sip_uri.user and self.host == sip_uri.host:
			# don't check port, at least for now
			return 0
		return -1
	
	def __str__(self):
		s = self.scheme + ':'
		if self.user: s += self.user + '@'
		s += self.host
		if self.port: s+= ':' + self.port
		return s
	
class Chat(gui.ChatObserver):
	def __init__(self, app, acc, uri, call_inst=None):
		self._app = app
		self._acc = acc
		self.title = ''
		
		global ConfIdx
		self.confIdx = ConfIdx
		ConfIdx += 1
		
		# each participant call/buddy instances are stored in call list
		# and buddy list with same index as in particpant list
		self._participantList = []	# list of SipUri
		self._callList = []		# list of Call
		self._buddyList = []		# list of Buddy
		
		self._gui = gui.ChatFrame(self)
		self.addParticipant(uri, call_inst)
	
	def _updateGui(self):
		if self.isPrivate():
			self.title = str(self._participantList[0])
		else:
			self.title = 'Conference #%d (%d participants)' % (self.confIdx, len(self._participantList))
		self._gui.title(self.title)
		self._app.updateWindowMenu()
		
	def _getCallFromUriStr(self, uri_str, op = ''):
		uri = ParseSipUri(uri_str)
		if uri not in self._participantList:
			print "=== %s cannot find participant with URI '%s'" % (op, uri_str)
			return None
		idx = self._participantList.index(uri)
		if idx < len(self._callList):
			return self._callList[idx]
		return None
	
	def _getActiveMediaIdx(self, thecall):
		ci = thecall.getInfo()
		for mi in ci.media:
			if mi.type == pj.PJMEDIA_TYPE_AUDIO and \
			  (mi.status != pj.PJSUA_CALL_MEDIA_NONE and \
			   mi.status != pj.PJSUA_CALL_MEDIA_ERROR):
				return mi.index
		return -1
		
	def _getAudioMediaFromUriStr(self, uri_str):
		c = self._getCallFromUriStr(uri_str)
		if not c: return None

		idx = self._getActiveMediaIdx(c)
		if idx < 0: return None

		m = c.getMedia(idx)
		am = pj.AudioMedia.typecastFromMedia(m)
		return am
		
	def _sendTypingIndication(self, is_typing, sender_uri_str=''):
		sender_uri = ParseSipUri(sender_uri_str) if sender_uri_str else None
		type_ind_param = pj.SendTypingIndicationParam()
		type_ind_param.isTyping = is_typing
		for idx, p in enumerate(self._participantList):
			# don't echo back to the original sender
			if sender_uri and p == sender_uri:
				continue
				
			# send via call, if any, or buddy
			sender = None
			if self._callList[idx] and self._callList[idx].connected:
				sender = self._callList[idx]
			else:
				sender = self._buddyList[idx]
			assert(sender)
				
			try:
				sender.sendTypingIndication(type_ind_param)
			except:
				pass

	def _sendInstantMessage(self, msg, sender_uri_str=''):
		sender_uri = ParseSipUri(sender_uri_str) if sender_uri_str else None
		send_im_param = pj.SendInstantMessageParam()
		send_im_param.content = str(msg)
		for idx, p in enumerate(self._participantList):
			# don't echo back to the original sender
			if sender_uri and p == sender_uri:
				continue
				
			# send via call, if any, or buddy
			sender = None
			if self._callList[idx] and self._callList[idx].connected:
				sender = self._callList[idx]
			else:
				sender = self._buddyList[idx]
			assert(sender)
			
			try:
				sender.sendInstantMessage(send_im_param)
			except:
				# error will be handled via Account::onInstantMessageStatus()
				pass

	def isPrivate(self):
		return len(self._participantList) <= 1
		
	def isUriParticipant(self, uri):
		return uri in self._participantList
		
	def registerCall(self, uri_str, call_inst):
		uri = ParseSipUri(uri_str)
		try:
			idx = self._participantList.index(uri)
			bud = self._buddyList[idx]
			self._callList[idx] = call_inst
			call_inst.chat = self
			call_inst.peerUri = bud.cfg.uri
		except:
			assert(0) # idx must be found!
		
	def showWindow(self, show_text_chat = False):
		self._gui.bringToFront()
		if show_text_chat:
			self._gui.textShowHide(True)
		
	def addParticipant(self, uri, call_inst=None):
		# avoid duplication
		if self.isUriParticipant(uri): return
		
		uri_str = str(uri)
		
		# find buddy, create one if not found (e.g: for IM/typing ind),
		# it is a temporary one and not really registered to acc
		bud = None
		try:
			bud = self._acc.findBuddy(uri_str)
		except:
			bud = buddy.Buddy(None)
			bud_cfg = pj.BuddyConfig()
			bud_cfg.uri = uri_str
			bud_cfg.subscribe = False
			bud.create(self._acc, bud_cfg)
			bud.cfg = bud_cfg
			bud.account = self._acc
			
		# update URI from buddy URI
		uri = ParseSipUri(bud.cfg.uri)
		
		# add it
		self._participantList.append(uri)
		self._callList.append(call_inst)
		self._buddyList.append(bud)
		self._gui.addParticipant(str(uri))
		self._updateGui()
	
	def kickParticipant(self, uri):
		if (not uri) or (uri not in self._participantList):
			assert(0)
			return
		
		idx = self._participantList.index(uri)
		del self._participantList[idx]
		del self._callList[idx]
		del self._buddyList[idx]
		self._gui.delParticipant(str(uri))
		
		if self._participantList:
			self._updateGui()
		else:
			self.onCloseWindow()
			
	def addMessage(self, from_uri_str, msg):
		if from_uri_str:
			# print message on GUI
			msg = from_uri_str + ': ' + msg
			self._gui.textAddMessage(msg)
			# now relay to all participants
			self._sendInstantMessage(msg, from_uri_str)
		else:
			self._gui.textAddMessage(msg, False)
			
	def setTypingIndication(self, from_uri_str, is_typing):
		# notify GUI
		self._gui.textSetTypingIndication(from_uri_str, is_typing)
		# now relay to all participants
		self._sendTypingIndication(is_typing, from_uri_str)
		
	def startCall(self):
		self._gui.enableAudio()
		call_param = pj.CallOpParam()
		call_param.opt.audioCount = 1
		call_param.opt.videoCount = 0
		fails = []
		for idx, p in enumerate(self._participantList):
			# just skip if call is instantiated
			if self._callList[idx]:
				continue
			
			uri_str = str(p)
			c = call.Call(self._acc, uri_str, self)
			self._callList[idx] = c
			self._gui.audioUpdateState(uri_str, gui.AudioState.INITIALIZING)
			
			try:
				c.makeCall(uri_str, call_param)
			except:
				self._callList[idx] = None
				self._gui.audioUpdateState(uri_str, gui.AudioState.FAILED)
				fails.append(p)
				
		for p in fails:
			# kick participants with call failure, but spare the last (avoid zombie chat)
			if not self.isPrivate():
				self.kickParticipant(p)
			
	def stopCall(self):
		for idx, p in enumerate(self._participantList):
			self._gui.audioUpdateState(str(p), gui.AudioState.DISCONNECTED)
			c = self._callList[idx]
			if c:
				c.hangup(pj.CallOpParam())

	def updateCallState(self, thecall, info = None):
		# info is optional here, just to avoid calling getInfo() twice (in the caller and here)
		if not info: info = thecall.getInfo()
		
		if info.state < pj.PJSIP_INV_STATE_CONFIRMED:
			self._gui.audioUpdateState(thecall.peerUri, gui.AudioState.INITIALIZING)
		elif info.state == pj.PJSIP_INV_STATE_CONFIRMED:
			self._gui.audioUpdateState(thecall.peerUri, gui.AudioState.CONNECTED)
			med_idx = self._getActiveMediaIdx(thecall)
			si = thecall.getStreamInfo(med_idx)
			stats_str = "Audio codec: %s/%s\n..." % (si.codecName, si.codecClockRate)
			self._gui.audioSetStatsText(thecall.peerUri, stats_str)
		elif info.state == pj.PJSIP_INV_STATE_DISCONNECTED:
			if info.lastStatusCode/100 != 2:
				self._gui.audioUpdateState(thecall.peerUri, gui.AudioState.FAILED)
			else:
				self._gui.audioUpdateState(thecall.peerUri, gui.AudioState.DISCONNECTED)
			
			# reset entry in the callList
			try:
				idx = self._callList.index(thecall)
				if idx >= 0: self._callList[idx] = None
			except:
				pass
			
			self.addMessage(None, "Call to '%s' disconnected: %s" % (thecall.peerUri, info.lastReason))
			
			# kick the disconnected participant, but the last (avoid zombie chat)
			if not self.isPrivate():
				self.kickParticipant(ParseSipUri(thecall.peerUri))

			
	# ** callbacks from GUI (ChatObserver implementation) **
	
	# Text
	def onSendMessage(self, msg):
		self._sendInstantMessage(msg)

	def onStartTyping(self):
		self._sendTypingIndication(True)
		
	def onStopTyping(self):
		self._sendTypingIndication(False)
		
	# Audio
	def onHangup(self, peer_uri_str):
		c = self._getCallFromUriStr(peer_uri_str, "onHangup()")
		if not c: return
		call_param = pj.CallOpParam()
		c.hangup(call_param)

	def onHold(self, peer_uri_str):
		c = self._getCallFromUriStr(peer_uri_str, "onHold()")
		if not c: return
		call_param = pj.CallOpParam()
		c.setHold(call_param)

	def onUnhold(self, peer_uri_str):
		c = self._getCallFromUriStr(peer_uri_str, "onUnhold()")
		if not c: return
		
		call_param = pj.CallOpParam()
		call_param.opt.audioCount = 1
		call_param.opt.videoCount = 0
		call_param.opt.flag |= pj.PJSUA_CALL_UNHOLD
		c.reinvite(call_param)
		
	def onRxMute(self, peer_uri_str, mute):
		am = self._getAudioMediaFromUriStr(peer_uri_str)
		if not am: return
		if mute:
			am.stopTransmit(ep.Endpoint.instance.audDevManager().getPlaybackDevMedia())
			self.addMessage(None, "Muted audio from '%s'" % (peer_uri_str))
		else:
			am.startTransmit(ep.Endpoint.instance.audDevManager().getPlaybackDevMedia())
			self.addMessage(None, "Unmuted audio from '%s'" % (peer_uri_str))
		
	def onRxVol(self, peer_uri_str, vol_pct):
		am = self._getAudioMediaFromUriStr(peer_uri_str)
		if not am: return
		# pjsua volume range = 0:mute, 1:no adjustment, 2:100% louder
		am.adjustRxLevel(vol_pct/50.0)
		self.addMessage(None, "Adjusted volume level audio from '%s'" % (peer_uri_str))
			
	def onTxMute(self, peer_uri_str, mute):
		am = self._getAudioMediaFromUriStr(peer_uri_str)
		if not am: return
		if mute:
			ep.Endpoint.instance.audDevManager().getCaptureDevMedia().stopTransmit(am)
			self.addMessage(None, "Muted audio to '%s'" % (peer_uri_str))
		else:
			ep.Endpoint.instance.audDevManager().getCaptureDevMedia().startTransmit(am)
			self.addMessage(None, "Unmuted audio to '%s'" % (peer_uri_str))

	# Chat room
	def onAddParticipant(self):
		buds = []
		dlg = AddParticipantDlg(None, self._app, buds)
		if dlg.doModal():
			for bud in buds:
				uri = ParseSipUri(bud.cfg.uri)
				self.addParticipant(uri)
			if not self.isPrivate():
				self.startCall()
				
	def onStartAudio(self):
		self.startCall()

	def onStopAudio(self):
		self.stopCall()
		
	def onCloseWindow(self):
		self.stopCall()
		# will remove entry from list eventually destroy this chat?
		if self in self._acc.chatList: self._acc.chatList.remove(self)
		self._app.updateWindowMenu()
		# destroy GUI
		self._gui.destroy()


class AddParticipantDlg(tk.Toplevel):
	"""
	List of buddies
	"""
	def __init__(self, parent, app, bud_list):
		tk.Toplevel.__init__(self, parent)
		self.title('Add participants..')
		self.transient(parent)
		self.parent = parent
		self._app = app
		self.buddyList = bud_list
		
		self.isOk = False
		
		self.createWidgets()
	
	def doModal(self):
		if self.parent:
			self.parent.wait_window(self)
		else:
			self.wait_window(self)
		return self.isOk
		
	def createWidgets(self):
		# buddy list
		list_frame = ttk.Frame(self)
		list_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=1, padx=20, pady=20)
		#scrl = ttk.Scrollbar(self, orient=tk.VERTICAL, command=list_frame.yview)
		#list_frame.config(yscrollcommand=scrl.set)
		#scrl.pack(side=tk.RIGHT, fill=tk.Y)
		
		# draw buddy list
		self.buddies = []
		for acc in self._app.accList:
			self.buddies.append((0, acc.cfg.idUri))
			for bud in acc.buddyList:
				self.buddies.append((1, bud))
		
		self.bud_var = []
		for idx,(flag,bud) in enumerate(self.buddies):
			self.bud_var.append(tk.IntVar())
			if flag==0:
				s = ttk.Separator(list_frame, orient=tk.HORIZONTAL)
				s.pack(fill=tk.X)
				l = tk.Label(list_frame, anchor=tk.W, text="Account '%s':" % (bud))
				l.pack(fill=tk.X)
			else:
				c = tk.Checkbutton(list_frame, anchor=tk.W, text=bud.cfg.uri, variable=self.bud_var[idx])
				c.pack(fill=tk.X)
		s = ttk.Separator(list_frame, orient=tk.HORIZONTAL)
		s.pack(fill=tk.X)

		# Ok/cancel buttons
		tail_frame = ttk.Frame(self)
		tail_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
		
		btnOk = ttk.Button(tail_frame, text='Ok', default=tk.ACTIVE, command=self.onOk)
		btnOk.pack(side=tk.LEFT, padx=20, pady=10)
		btnCancel = ttk.Button(tail_frame, text='Cancel', command=self.onCancel)
		btnCancel.pack(side=tk.RIGHT, padx=20, pady=10)
		
	def onOk(self):
		self.buddyList[:] = []
		for idx,(flag,bud) in enumerate(self.buddies):
			if not flag: continue
			if self.bud_var[idx].get() and not (bud in self.buddyList):
				self.buddyList.append(bud)
			
		self.isOk = True
		self.destroy()
		
	def onCancel(self):
		self.destroy()