/
SessionRinger.py
399 lines (341 loc) · 18.8 KB
/
SessionRinger.py
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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
import datetime
import time
from dateutil.tz import tzlocal
from itertools import chain
from zope.interface import implements
from application.notification import NotificationCenter, IObserver
from sipsimple.configuration.settings import SIPSimpleSettings
from sipsimple.account import AccountManager
from sipsimple.audio import WavePlayer
from sipsimple.application import SIPApplication
from resources import Resources
from util import allocate_autorelease_pool
HANGUP_TONE_THROTLE_DELAY = 2.0
CHAT_TONE_THROTLE_DELAY = 3.0
class Ringer(object):
implements(IObserver)
"""
Manages ringtone playing for incoming sessions.
- updates ringtone when settings or default account change
- plays ringtone when an incoming session arrives
- plays ringtone when an outgoing audio session gets SIPSessionGotRingIndication
- selects between user ringtone and "discrete", tone based ringtone according to active sessions
- stops playing ringtones when appropriate
- "beeps" when call ends
"""
owner = None
incoming_audio_sessions = {}
ringing_sessions = set()
chat_sessions = {}
ds_sessions = {}
filesend_sessions = {}
filerecv_sessions = {}
active_sessions = set()
on_hold_session_count = 0
audio_primary_ringtone = None
audio_secondary_ringtone = None
initial_hold_tone = None
secondary_hold_tone = None
chat_primary_ringtone = None
chat_secondary_ringtone = None
chat_message_outgoing_sound = None
chat_message_incoming_sound = None
file_transfer_outgoing_sound = None
file_transfer_incoming_sound = None
last_hangup_tone_time = 0
chat_beep_time = 0
def __init__(self, owner):
self.owner = owner
self.started = False
def start(self):
notification_center = NotificationCenter()
notification_center.add_observer(self, name="BlinkFileTransferDidEnd")
notification_center.add_observer(self, name="AudioStreamDidChangeHoldState")
notification_center.add_observer(self, name="CFGSettingsObjectDidChange")
notification_center.add_observer(self, name="ChatViewControllerDidDisplayMessage")
notification_center.add_observer(self, name="ConferenceHasAddedAudio")
notification_center.add_observer(self, name="BlinkWillCancelProposal")
self.started = True
def stop(self):
if not self.started:
return
notification_center = NotificationCenter()
notification_center.remove_observer(self, name="BlinkFileTransferDidEnd")
notification_center.remove_observer(self, name="AudioStreamDidChangeHoldState")
notification_center.remove_observer(self, name="CFGSettingsObjectDidChange")
notification_center.remove_observer(self, name="ChatViewControllerDidDisplayMessage")
notification_center.remove_observer(self, name="ConferenceHasAddedAudio")
notification_center.remove_observer(self, name="BlinkWillCancelProposal")
def update_playing_ringtones(self, account=None):
settings = SIPSimpleSettings()
should_play_audio_primary_ringtone = False
should_play_audio_primary_ringtone_tone = False
should_play_chat_primary_ringtone = False
should_play_chat_secondary_ringtone = False
if 'audio' in (stream.type for stream in chain(*(session.streams for session in self.active_sessions if session.streams))):
# play only secondary ringtones when there's active audio sessions
if self.incoming_audio_sessions:
should_play_audio_primary_ringtone_tone = True
if self.chat_sessions or self.filerecv_sessions or self.ds_sessions:
should_play_chat_secondary_ringtone = True
else:
# play ringtone
if self.incoming_audio_sessions:
should_play_audio_primary_ringtone = True
if self.chat_sessions or self.filerecv_sessions or self.ds_sessions:
should_play_chat_primary_ringtone = True
if self.ringing_sessions:
# proposal for adding new streams to an ongoing session
proposed_stream_types = [stream.type for stream in chain(*(session.proposed_streams or [] for session in self.ringing_sessions))]
has_chat_proposed = 'chat' in proposed_stream_types
has_ds_proposed = 'desktop-sharing' in proposed_stream_types
has_file_proposed = 'file-transfer' in proposed_stream_types
has_audio_ongoing = 'audio' in (stream.type for stream in chain(*(session.streams for session in self.ringing_sessions if session.streams)))
should_play_chat_secondary_ringtone = (has_chat_proposed or has_file_proposed or has_ds_proposed) and not has_audio_ongoing
if self.audio_primary_ringtone:
if should_play_audio_primary_ringtone and not self.audio_primary_ringtone.is_active:
self.update_ringtones(account)
this_hour = int(datetime.datetime.now(tzlocal()).strftime("%H"))
volume = None
if settings.sounds.night_volume.start_hour < settings.sounds.night_volume.end_hour:
if this_hour < settings.sounds.night_volume.end_hour and this_hour >= settings.sounds.night_volume.start_hour:
volume = settings.sounds.night_volume.volume
elif settings.sounds.night_volume.start_hour > settings.sounds.night_volume.end_hour:
if this_hour < settings.sounds.night_volume.end_hour:
volume = settings.sounds.night_volume.volume
elif this_hour >= settings.sounds.night_volume.start_hour:
volume = settings.sounds.night_volume.volume
if volume is None:
if account is not None:
if hasattr(account.sounds.audio_inbound.sound_file, 'volume'):
volume = account.sounds.audio_inbound.sound_file.volume
else:
volume = settings.sounds.audio_inbound.volume
else:
volume = settings.sounds.audio_inbound.volume
self.audio_primary_ringtone.volume = volume
self.audio_primary_ringtone.start()
elif not should_play_audio_primary_ringtone and self.audio_primary_ringtone.is_active:
self.audio_primary_ringtone.stop()
if self.audio_secondary_ringtone:
if should_play_audio_primary_ringtone_tone and not self.audio_secondary_ringtone.is_active:
self.audio_secondary_ringtone.start()
elif not should_play_audio_primary_ringtone_tone and self.audio_secondary_ringtone.is_active:
self.audio_secondary_ringtone.stop()
if self.chat_primary_ringtone:
if should_play_chat_primary_ringtone and not self.chat_primary_ringtone.is_active:
self.chat_primary_ringtone.start()
elif not should_play_chat_primary_ringtone and self.chat_primary_ringtone.is_active:
self.chat_primary_ringtone.stop()
if self.chat_secondary_ringtone:
if should_play_chat_secondary_ringtone and not self.chat_secondary_ringtone.is_active:
self.chat_secondary_ringtone.start()
elif not should_play_chat_secondary_ringtone and self.chat_secondary_ringtone.is_active:
self.chat_secondary_ringtone.stop()
def update_ringtones(self, account=None):
settings = SIPSimpleSettings()
if account is None:
account = AccountManager().default_account
app = SIPApplication()
def change_tone(name, new_tone):
current = getattr(self, name)
if current and current.is_active:
current.stop()
if new_tone:
new_tone.start()
setattr(self, name, new_tone)
change_tone("initial_hold_tone", WavePlayer(app.voice_audio_mixer, Resources.get('hold_tone.wav'), volume=10))
app.voice_audio_bridge.add(self.initial_hold_tone)
change_tone("secondary_hold_tone", WavePlayer(app.voice_audio_mixer, Resources.get('hold_tone.wav'), loop_count=0, pause_time=45, volume=10, initial_play=False))
app.voice_audio_bridge.add(self.secondary_hold_tone)
if account:
audio_primary_ringtone = account.sounds.audio_inbound.sound_file if account.sounds.audio_inbound is not None else None
else:
audio_primary_ringtone = None
if audio_primary_ringtone and not settings.audio.silent:
# Workaround not to use same device from two bridges. -Saul
if settings.audio.alert_device is not None and app.alert_audio_mixer.real_output_device == app.voice_audio_mixer.real_output_device:
new_tone = WavePlayer(app.voice_audio_mixer, audio_primary_ringtone.path, loop_count=0, pause_time=6)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = WavePlayer(app.alert_audio_mixer, audio_primary_ringtone.path, loop_count=0, pause_time=6)
app.alert_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("audio_primary_ringtone", new_tone)
if audio_primary_ringtone and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, Resources.get('ring_tone.wav'), loop_count=0, pause_time=6)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("audio_secondary_ringtone", new_tone)
if audio_primary_ringtone and not settings.audio.silent:
# Workaround not to use same device from two bridges. -Saul
if settings.audio.alert_device is not None and app.alert_audio_mixer.real_output_device == app.voice_audio_mixer.real_output_device:
new_tone = WavePlayer(app.voice_audio_mixer, Resources.get('ring_tone.wav'), loop_count=0, pause_time=6)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = WavePlayer(app.alert_audio_mixer, Resources.get('ring_tone.wav'), loop_count=0, pause_time=6)
app.alert_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("chat_primary_ringtone", new_tone)
if audio_primary_ringtone and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, Resources.get('ring_tone.wav'), loop_count=0, pause_time=6)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("chat_secondary_ringtone", new_tone)
chat_message_outgoing_sound = settings.sounds.message_sent
if chat_message_outgoing_sound and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, chat_message_outgoing_sound.path, volume=chat_message_outgoing_sound.volume)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("chat_message_outgoing_sound", new_tone)
chat_message_incoming_sound = settings.sounds.message_received
if chat_message_incoming_sound and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, chat_message_incoming_sound.path, volume=chat_message_incoming_sound.volume)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("chat_message_incoming_sound", new_tone)
file_transfer_outgoing_sound = settings.sounds.file_sent
if file_transfer_outgoing_sound and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, file_transfer_outgoing_sound.path, volume=file_transfer_outgoing_sound.volume)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("file_transfer_outgoing_sound", new_tone)
file_transfer_incoming_sound = settings.sounds.file_received
if file_transfer_incoming_sound and not settings.audio.silent:
new_tone = WavePlayer(app.voice_audio_mixer, file_transfer_incoming_sound.path, volume=file_transfer_incoming_sound.volume)
app.voice_audio_bridge.add(new_tone)
else:
new_tone = None
change_tone("file_transfer_incoming_sound", new_tone)
def add_incoming(self, session, streams):
settings = SIPSimpleSettings()
stream_types = [s.type for s in streams]
if 'audio' in stream_types and not settings.audio.silent:
self.incoming_audio_sessions[session] = streams
else:
if 'chat' in stream_types:
self.chat_sessions[session] = streams
if 'file-transfer' in stream_types:
self.filerecv_sessions[session] = streams
NotificationCenter().add_observer(self, sender=session)
self.update_playing_ringtones(session.account)
def add_outgoing(self, session, streams):
NotificationCenter().add_observer(self, sender=session)
def stop_ringing(self, session):
self.ringing_sessions.discard(session)
self.incoming_audio_sessions.pop(session, None)
self.chat_sessions.pop(session, None)
self.ds_sessions.pop(session, None)
self.filerecv_sessions.pop(session, None)
self.update_playing_ringtones()
def play_hangup(self):
settings = SIPSimpleSettings()
if settings.audio.silent:
return
if time.time() - self.last_hangup_tone_time > HANGUP_TONE_THROTLE_DELAY:
hangup_tone = WavePlayer(SIPApplication.voice_audio_mixer, Resources.get('hangup_tone.wav'), volume=30)
NotificationCenter().add_observer(self, sender=hangup_tone, name="WavePlayerDidEnd")
SIPApplication.voice_audio_bridge.add(hangup_tone)
hangup_tone.start()
self.last_hangup_tone_time = time.time()
@allocate_autorelease_pool
def handle_notification(self, notification):
session = notification.sender
name = notification.name
data = notification.data
settings = SIPSimpleSettings()
if name == "SIPSessionWillStart":
self.active_sessions.add(session)
self.stop_ringing(session)
elif name in ("SIPSessionWillEnd", "SIPSessionDidEnd", "SIPSessionDidFail"):
streams = session.streams or session.proposed_streams
if not streams:
# not connected
has_audio = session in self.incoming_audio_sessions
else:
stream_types = [s.type for s in streams]
has_audio = 'audio' in stream_types
# play hangup tone
if has_audio:
self.play_hangup()
NotificationCenter().remove_observer(self, sender=session)
self.active_sessions.discard(session)
self.stop_ringing(session)
elif name in ("SIPSessionGotAcceptProposal", "SIPSessionGotRejectProposal", "SIPSessionHadProposalFailure", "BlinkWillCancelProposal"):
self.stop_ringing(session)
elif name == "SIPSessionGotProposal":
stream_types = [stream.type for stream in data.streams]
if 'audio' in stream_types:
if data.originator == "local":
self.ringing_sessions.add(session)
else:
self.incoming_audio_sessions[session] = data.streams
elif 'chat' in stream_types:
self.chat_sessions[session] = data.streams
elif 'desktop-sharing' in stream_types:
self.ds_sessions[session] = data.streams
elif 'file-transfer' in stream_types:
self.filerecv_sessions[session] = data.streams
self.update_playing_ringtones(session.account)
elif name == "SIPSessionDidRenegotiateStreams":
if data.action == "remove":
for stream in (s for s in data.streams if s.type=='audio'):
self.play_hangup()
elif name == "SIPSessionGotRingIndication":
self.ringing_sessions.add(session)
self.update_playing_ringtones()
elif name == "AudioStreamDidChangeHoldState":
if not settings.audio.silent:
if data.on_hold:
self.on_hold_session_count += 1
else:
self.on_hold_session_count -= 1
if self.secondary_hold_tone:
if self.on_hold_session_count == 1:
self.secondary_hold_tone.start()
elif self.on_hold_session_count == 0:
self.secondary_hold_tone.stop()
if data.on_hold and data.originator == 'remote' and self.initial_hold_tone and not self.initial_hold_tone.is_active:
self.initial_hold_tone.start()
elif name == "ConferenceHasAddedAudio":
# TODO: play-resume iTunes after adding audio to conference tone has been played -adi
self.initial_hold_tone.start()
elif name == "ChatViewControllerDidDisplayMessage":
if not settings.audio.silent:
now = time.time()
if self.chat_message_outgoing_sound and now - self.chat_beep_time > CHAT_TONE_THROTLE_DELAY and not data.history_entry:
if data.direction == 'outgoing':
self.chat_message_outgoing_sound.stop()
self.chat_message_outgoing_sound.start()
else:
self.chat_message_incoming_sound.stop()
self.chat_message_incoming_sound.start()
self.chat_beep_time = now
elif name == "BlinkFileTransferDidEnd":
if not settings.audio.silent:
if data == "send":
if self.file_transfer_outgoing_sound:
self.file_transfer_outgoing_sound.start()
else:
if self.file_transfer_incoming_sound:
self.file_transfer_incoming_sound.start()
elif name == "CFGSettingsObjectDidChange":
sound_attributes = ['audio.silent', 'audio.alert_device', 'audio.output_device', 'sounds.audio_inbound', 'sounds.message_sent', 'sounds.message_received', 'sounds.file_sent', 'sounds.file_received']
if set(sound_attributes).intersection(data.modified):
self.update_ringtones()
if 'audio.silent' in data.modified:
self.update_playing_ringtones()
elif name == "WavePlayerDidEnd":
NotificationCenter().remove_observer(self, sender=notification.sender, name="WavePlayerDidEnd")
on_hold_streams = [stream for stream in chain(*(session.streams for session in self.active_sessions if session.streams)) if stream.on_hold]
if not on_hold_streams and self.secondary_hold_tone is not None and self.secondary_hold_tone.is_active:
self.secondary_hold_tone.stop()