-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScreenSharingController.py
More file actions
577 lines (506 loc) · 24.9 KB
/
ScreenSharingController.py
File metadata and controls
577 lines (506 loc) · 24.9 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from AppKit import NSStatusBar
from Foundation import (NSBundle,
NSImage,
NSLocalizedString,
NSMakeSize,
NSMenu,
NSObject,
NSRunLoop,
NSRunLoopCommonModes,
NSTimer,
NSURL,
NSWorkspace)
from AppKit import NSEventTrackingRunLoopMode, NSRunAlertPanel
import objc
import socket
from application.notification import IObserver, NotificationCenter, NotificationData
from application.python import Null
from eventlib.coros import event
from eventlib.greenio import GreenSocket
from eventlib.proc import spawn
from eventlib.util import tcp_socket, set_reuse_addr
from zope.interface import implementer
from sipsimple.streams.msrp.screensharing import ScreenSharingStream, ScreenSharingServerHandler, ScreenSharingViewerHandler, VNCConnectionError
from sipsimple.configuration.settings import SIPSimpleSettings
from BlinkLogger import BlinkLogger
from MediaStream import (MediaStream,
STREAM_CONNECTED,
STREAM_CONNECTING,
STREAM_INCOMING,
STREAM_PROPOSING,
STREAM_DISCONNECTING,
STREAM_CANCELLING,
STREAM_IDLE,
STREAM_FAILED,
STATE_DNS_FAILED,
STATE_FAILED,
STATE_CONNECTED)
from util import run_in_gui_thread
class Interrupt(Exception):
pass
class ExternalVNCViewerHandler(ScreenSharingViewerHandler):
address = ('localhost', 0)
connect_timeout = 5
def __init__(self):
super(ExternalVNCViewerHandler, self).__init__()
self.vnc_starter_thread = None
self.vnc_socket = None
self.vnc_server_socket = GreenSocket(tcp_socket())
set_reuse_addr(self.vnc_server_socket)
self.vnc_server_socket.settimeout(self.connect_timeout)
self.vnc_server_socket.bind(self.address)
self.vnc_server_socket.listen(1)
self.address = self.vnc_server_socket.getsockname()
self._reconnect_event = event()
@objc.python_method
def _msrp_reader(self):
while True:
try:
data = self.incoming_msrp_queue.wait()
self.vnc_socket.sendall(data)
except Interrupt:
# Wait until the viewer reconnects and self.vnc_socket is replaced
self._reconnect_event.wait()
self._reconnect_event.reset()
except Exception as e:
self.msrp_reader_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='sending', reason=str(e)))
break
@objc.python_method
def _msrp_writer(self):
while True:
try:
data = self.vnc_socket.recv(2048)
if not data:
# Pause MSRP reader
self.msrp_reader_thread.kill(Interrupt)
self.vnc_socket.close()
self.vnc_socket = None
try:
sock, addr = self.vnc_server_socket.accept()
except socket.timeout:
raise VNCConnectionError("connection with the VNC viewer was closed")
self.vnc_socket = sock
# Signal the VNC server needs to be reconnected
self.outgoing_msrp_queue.send(b'BLINK_VNC_RECONNECT')
# Resume the MSRP reader
self._reconnect_event.send()
continue
self.outgoing_msrp_queue.send(data)
except Exception as e:
self.msrp_writer_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='reading', reason=str(e)))
break
@objc.python_method
def _start_vnc_connection(self):
try:
sock, addr = self.vnc_server_socket.accept()
self.vnc_socket = sock
except Exception as e:
self.vnc_starter_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='connecting', reason=str(e)))
else:
self.msrp_reader_thread = spawn(self._msrp_reader)
self.msrp_writer_thread = spawn(self._msrp_writer)
finally:
self.vnc_starter_thread = None
@objc.python_method
def _NH_MediaStreamDidStart(self, notification):
self.vnc_starter_thread = spawn(self._start_vnc_connection)
@objc.python_method
def _NH_MediaStreamWillEnd(self, notification):
if self.vnc_starter_thread is not None:
self.vnc_starter_thread.kill()
self.vnc_starter_thread = None
super(ExternalVNCViewerHandler, self)._NH_MediaStreamWillEnd(notification)
self.vnc_server_socket.close()
if self.vnc_socket is not None:
self.vnc_socket.close()
class ExternalVNCServerHandler(ScreenSharingServerHandler):
address = ('localhost', 5900)
connect_timeout = 5
def __init__(self):
super(ExternalVNCServerHandler, self).__init__()
self.vnc_starter_thread = None
self.vnc_socket = None
self._reconnect_event = event()
@objc.python_method
def _msrp_reader(self):
while True:
try:
data = self.incoming_msrp_queue.wait()
if data == b'BLINK_VNC_RECONNECT':
# Interrupt MSRP writer
self.msrp_writer_thread.kill(Interrupt)
self.vnc_socket.close()
self.vnc_socket = None
self.vnc_socket = GreenSocket(tcp_socket())
self.vnc_socket.settimeout(self.connect_timeout)
self.vnc_socket.connect(self.address)
self.vnc_socket.settimeout(None)
# Resume MSRP writer
self._reconnect_event.send()
continue
self.vnc_socket.sendall(data)
except Exception as e:
self.msrp_reader_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='sending', reason=str(e)))
break
@objc.python_method
def _msrp_writer(self):
while True:
try:
data = self.vnc_socket.recv(2048)
if not data:
raise VNCConnectionError("connection to the VNC server was closed")
self.outgoing_msrp_queue.send(data)
except Interrupt:
# Wait until we reconnect to the VNC server and self.vnc_socket is replaced
self._reconnect_event.wait()
self._reconnect_event.reset()
except Exception as e:
self.msrp_writer_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='reading', reason=str(e)))
break
@objc.python_method
def _start_vnc_connection(self):
try:
self.vnc_socket = GreenSocket(tcp_socket())
self.vnc_socket.settimeout(self.connect_timeout)
self.vnc_socket.connect(self.address)
self.vnc_socket.settimeout(None)
except Exception as e:
self.vnc_starter_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification
NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='connecting', reason=str(e)))
else:
self.msrp_reader_thread = spawn(self._msrp_reader)
self.msrp_writer_thread = spawn(self._msrp_writer)
finally:
self.vnc_starter_thread = None
@objc.python_method
def _NH_MediaStreamDidStart(self, notification):
self.vnc_starter_thread = spawn(self._start_vnc_connection)
@objc.python_method
def _NH_MediaStreamWillEnd(self, notification):
if self.vnc_starter_thread is not None:
self.vnc_starter_thread.kill()
self.vnc_starter_thread = None
super(ExternalVNCServerHandler, self)._NH_MediaStreamWillEnd(notification)
if self.vnc_socket is not None:
self.vnc_socket.close()
ScreenSharingStream.ServerHandler = ExternalVNCServerHandler
ScreenSharingStream.ViewerHandler = ExternalVNCViewerHandler
class StatusItem(NSObject):
items = []
menu = None
statusItem = None
@objc.python_method
def show(self, item):
if not self.items:
#self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(30)
self.statusItem = Null
self.menu = NSMenu.alloc().init()
image = NSImage.imageNamed_("display").copy()
image.setSize_(NSMakeSize(24, 24))
self.statusItem.setImage_(image)
self.statusItem.setMenu_(self.menu)
self.items.append(item)
mitem = self.menu.addItemWithTitle_action_keyEquivalent_(NSLocalizedString("%s - Waiting", "Menu item") % item.sessionController.titleLong, "activateItem:", "")
mitem.setTag_(item.sessionController.identifier)
mitem.setTarget_(self)
def activateItem_(self, sender):
for item in self.items:
if item.sessionController.identifier == sender.tag():
item.end()
break
@objc.python_method
def remove(self, item):
if self.menu:
mitem = self.menu.itemWithTag_(item.sessionController.identifier)
if mitem:
self.menu.removeItem_(mitem)
self.items.remove(item)
if not self.items:
#NSStatusBar.systemStatusBar().removeStatusItem_(self.statusItem)
self.statusItem = None
self.menu = None
@objc.python_method
def update(self, item, state):
if self.menu:
mitem = self.menu.itemWithTag_(item.sessionController.identifier)
if mitem:
name = item.sessionController.titleShort
if state == STREAM_CONNECTED:
mitem.setTitle_(NSLocalizedString("Disconnect %s", "Menu item") % name)
mitem.setEnabled_(True)
elif state in (STREAM_INCOMING, STREAM_PROPOSING, STREAM_CONNECTING):
mitem.setTitle_(NSLocalizedString("%s - Waiting", "Menu item") % name)
mitem.setEnabled_(True)
elif state in (STREAM_DISCONNECTING, STREAM_CANCELLING):
mitem.setTitle_("%s %s" % (state.title(), name))
mitem.setEnabled_(False)
else:
mitem.setTitle_(name)
mitem.setEnabled_(False)
@implementer(IObserver)
class ScreenSharingController(MediaStream):
# TODO video: stop stream if video is added
type = "screen-sharing"
viewer = None
exhanged_bytes = 0
must_reset_trace_msrp = False
loaded = False
#statusItem = StatusItem.alloc().init()
statusWindow = objc.IBOutlet()
statusLabel = objc.IBOutlet()
statusProgress = objc.IBOutlet()
stopButton = objc.IBOutlet()
def initWithOwner_stream_(self, scontroller, stream):
self = objc.super(ScreenSharingController, self).initWithOwner_stream_(scontroller, stream)
BlinkLogger().log_debug("Creating %s" % self)
self.stream = stream
self.direction = stream.handler.type
self.vncViewerTask = None
self.close_timer = None
return self
@objc.python_method
def startIncoming(self, is_update):
if self.direction == "active":
self.sessionController.log_info("Requesting remote screen...")
else:
self.sessionController.log_info("Offering local screen...")
NSBundle.loadNibNamed_owner_("ScreenServerWindow", self)
self.statusProgress.startAnimation_(None)
self.statusWindow.setTitle_(NSLocalizedString("Screen Sharing with %s", "Window title") % self.sessionController.titleShort)
settings = SIPSimpleSettings()
if not settings.logs.trace_msrp:
settings.logs.trace_msrp = True
settings.save()
self.must_reset_trace_msrp = True
NotificationCenter().add_observer(self, name="MSRPTransportTrace")
#self.statusItem.show(self)
NotificationCenter().add_observer(self, sender=self.stream.handler)
NotificationCenter().add_observer(self, sender=self.stream)
self.changeStatus(STREAM_INCOMING)
@objc.python_method
def startOutgoing(self, is_update):
if self.loaded:
return
if self.direction == "active":
self.sessionController.log_info("Requesting remote screen...")
else:
self.sessionController.log_info("Offering local screen...")
NSBundle.loadNibNamed_owner_("ScreenServerWindow", self)
self.loaded = True
self.statusProgress.startAnimation_(None)
self.statusWindow.setTitle_(NSLocalizedString("Screen Sharing with %s", "Window title") % self.sessionController.titleShort)
settings = SIPSimpleSettings()
if not settings.logs.trace_msrp:
settings.logs.trace_msrp = True
settings.save()
self.must_reset_trace_msrp = True
NotificationCenter().add_observer(self, name="MSRPTransportTrace")
#self.statusItem.show(self)
NotificationCenter().add_observer(self, sender=self.stream.handler)
NotificationCenter().add_observer(self, sender=self.stream)
self.changeStatus(STREAM_PROPOSING if is_update else STREAM_CONNECTING)
@objc.IBAction
def end_(self, sender):
self.end()
@objc.python_method
def end(self):
self.sessionController.log_info('Ending screen sharing in status %s' % self.status)
self.stopButton.setHidden_(True)
if self.status in (STREAM_CONNECTING, STREAM_DISCONNECTING, STREAM_CANCELLING, STREAM_IDLE, STREAM_FAILED):
if self.statusWindow:
self.statusWindow.close()
self.statusWindow = None
if self.status == STREAM_PROPOSING:
self.sessionController.log_info("Cancelling screen sharing proposal...")
self.sessionController.cancelProposal(self)
self.changeStatus(STREAM_CANCELLING)
elif self.status in (STREAM_CONNECTED, STREAM_INCOMING):
self.sessionController.log_info("Removing screen sharing...")
self.sessionController.endStream(self)
self.changeStatus(STREAM_DISCONNECTING)
else:
self.sessionController.log_info("Cancelling screen sharing...")
self.sessionController.end()
self.changeStatus(STREAM_DISCONNECTING)
@objc.python_method
def updateStatusIcon(self):
pass
@objc.python_method
def sessionStateChanged(self, newstate, detail):
if self.status == newstate:
return
if newstate == STATE_DNS_FAILED:
if self.statusWindow:
self.statusLabel.setStringValue_(NSLocalizedString("Error starting screen sharing session.", "Label"))
self.statusProgress.stopAnimation_(None)
else:
e = NSLocalizedString("Error starting screen sharing session.", "Label") + "\n%s" % detail
NSRunAlertPanel(NSLocalizedString("Error", "Window title"), e, NSLocalizedString("OK", "Button title"), None, None)
self.changeStatus(STREAM_FAILED, detail)
elif newstate == STATE_FAILED:
if self.statusWindow:
self.statusProgress.stopAnimation_(None)
else:
if detail and detail.lower() != "session cancelled" and not self.sessionController.hasStreamOfType("audio"):
e = NSLocalizedString("Error starting screen sharing session.", "Label") + "\n%s" % detail
NSRunAlertPanel(NSLocalizedString("Error", "Window title"), e, NSLocalizedString("OK", "Button title"), "", "")
self.changeStatus(STREAM_FAILED, detail)
elif newstate == STATE_CONNECTED:
# if the session is in connected state (ie, got SessionDidStart), we should have already
# received MediaStreamDidStart or DidEnd to indicate whether we got accepted
if self.status == STREAM_IDLE:
if self.direction == "passive":
# we got rejected
self.statusLabel.setStringValue_(NSLocalizedString("Error starting screen sharing session.", "Label"))
self.statusProgress.stopAnimation_(None)
@objc.python_method
def changeStatus(self, newstate, fail_reason=None):
if self.direction == "active":
if newstate == STREAM_CONNECTED:
ip, port = self.stream.handler.address
self.sessionController.log_info("Connecting screen sharing viewer to vnc://%s:%s" % (ip, port))
url = NSURL.URLWithString_("vnc://%s:%i" % (ip, port))
NSWorkspace.sharedWorkspace().openURL_(url)
else:
#self.statusItem.update(self, newstate)
if self.statusWindow:
if newstate == STREAM_CONNECTED:
_t = self.sessionController.titleShort
label = NSLocalizedString("%s requests your screen. Please confirm when asked.", "Label") % _t
self.statusProgress.setHidden_(True)
elif newstate == STREAM_DISCONNECTING:
self.statusLabel.setStringValue_(NSLocalizedString("Terminating Screen Sharing...", "Label"))
self.statusProgress.setHidden_(True)
self.start_auto_close_timer()
elif newstate == STREAM_CANCELLING:
self.statusLabel.setStringValue_(NSLocalizedString("Cancelling Screen Sharing...", "Label"))
self.statusProgress.setHidden_(True)
self.start_auto_close_timer()
elif newstate == STREAM_PROPOSING:
self.statusProgress.setHidden_(True)
self.stopButton.setHidden_(False)
self.stopButton.setTitle_(NSLocalizedString("Cancel Proposal", "Button title"))
elif newstate == STREAM_CONNECTING:
self.statusLabel.setStringValue_(NSLocalizedString("Offering Screen Sharing...", "Label"))
self.statusProgress.setHidden_(True)
self.stopButton.setHidden_(False)
self.statusWindow.makeKeyAndOrderFront_(None)
elif newstate == STREAM_FAILED:
_t = self.sessionController.failureReason or fail_reason
e = NSLocalizedString("Error starting screen sharing session.", "Label")
label = e + "\n%s" % _t if self.sessionController.failureReason or fail_reason else e
self.statusLabel.setStringValue_("Screen Sharing Failed")
self.statusProgress.setHidden_(True)
self.start_auto_close_timer()
elif newstate == STREAM_IDLE:
if self.status in (STREAM_CONNECTING, STREAM_PROPOSING):
self.statusLabel.setStringValue_(NSLocalizedString("Screen Sharing Failed", "Label"))
else:
self.statusLabel.setStringValue_(NSLocalizedString("Screen Sharing Ended", "Label"))
self.statusProgress.setHidden_(True)
if newstate == STREAM_IDLE:
#if self.direction == "passive":
#self.statusItem.remove(self)
self.removeFromSession()
self.start_auto_close_timer()
MediaStream.changeStatus(self, self.status, newstate, fail_reason)
self.status = newstate
@objc.python_method
def start_auto_close_timer(self):
if not self.close_timer:
# auto-close everything in 5s
self.close_timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(3, self, "closeWindows:", None, False)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.close_timer, NSRunLoopCommonModes)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.close_timer, NSEventTrackingRunLoopMode)
def windowShouldClose_(self, sender):
self.end()
return True
def closeWindows_(self, timer):
if self.statusWindow:
self.statusWindow.close()
self.statusWindow = None
if self.close_timer:
self.close_timer.invalidate()
self.close_timer = None
@objc.python_method
@run_in_gui_thread
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification.sender, notification.data)
@objc.python_method
def _NH_MediaStreamDidStart(self, sender, data):
self.sessionController.log_info("Screen sharing started")
self.changeStatus(STREAM_CONNECTED)
videoStream = self.sessionController.streamHandlerOfType("video")
if videoStream:
self.sessionController.removeVideoFromSession()
@objc.python_method
def _NH_MediaStreamDidNotInitialize(self, sender, data):
if data.reason == 'MSRPRelayAuthError':
reason = NSLocalizedString("MSRP relay authentication failed", "Label")
else:
reason = NSLocalizedString("MSRP connection failed", "Label")
self.sessionController.log_info(reason)
self.changeStatus(STREAM_FAILED, data.reason)
NotificationCenter().remove_observer(self, sender=self.stream.handler)
NotificationCenter().remove_observer(self, sender=self.stream)
@objc.python_method
def _NH_MediaStreamDidEnd(self, sender, data):
if data.error is None:
self.sessionController.log_info("Screen sharing ended")
self.changeStatus(STREAM_IDLE)
else:
self.sessionController.log_info("Screen sharing failed")
self.changeStatus(STREAM_IDLE)
if self.statusWindow:
self.stopButton.setHidden_(True)
self.statusProgress.setHidden_(True)
NotificationCenter().remove_observer(self, sender=self.stream.handler)
NotificationCenter().remove_observer(self, sender=self.stream)
self.resetTrace()
@objc.python_method
def _NH_MSRPTransportTrace(self, sender, data):
if sender is self.stream.msrp:
self.exhanged_bytes += len(data.data)
if self.exhanged_bytes > 10000:
if self.statusWindow:
_t = self.sessionController.titleShort
label = NSLocalizedString("%s is watching the screen", "Label") % _t
self.statusLabel.setStringValue_(label)
self.statusProgress.setHidden_(True)
self.stopButton.setHidden_(False)
self.stopButton.setTitle_(NSLocalizedString("Stop Screen Sharing", "Button title"))
self.resetTrace()
@objc.python_method
def _NH_ScreenSharingHandlerDidFail(self, sender, data):
self.sessionController.log_info("%s" % data.reason.title())
@objc.python_method
def resetTrace(self):
if self.must_reset_trace_msrp:
settings = SIPSimpleSettings()
settings.logs.trace_msrp = False
settings.save()
self.must_reset_trace_msrp = False
NotificationCenter().discard_observer(self, name="MSRPTransportTrace")
@objc.python_method
def dealloc(self):
BlinkLogger().log_debug("Dealloc %s" % self)
self.resetTrace()
self.stream = None
self.sessionController = None
objc.super(ScreenSharingController, self).dealloc()
class ScreenSharingViewerController(ScreenSharingController):
@classmethod
def createStream(cls):
return ScreenSharingStream(mode="viewer")
class ScreenSharingServerController(ScreenSharingController):
@classmethod
def createStream(cls):
return ScreenSharingStream(mode="server")