-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenSharingController.py
353 lines (313 loc) · 16.1 KB
/
ScreenSharingController.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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from Foundation import (NSBundle,
NSImage,
NSLocalizedString,
NSStatusBar,
NSMakeSize,
NSMenu,
NSObject,
NSRunLoop,
NSRunLoopCommonModes,
NSTimer,
NSURL,
NSWorkspace)
from AppKit import NSEventTrackingRunLoopMode, NSRunAlertPanel
import objc
from application.notification import IObserver, NotificationCenter
from application.python import Null
from zope.interface import implements
from sipsimple.streams.msrp import ScreenSharingStream, ExternalVNCServerHandler, ExternalVNCViewerHandler, 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 allocate_autorelease_pool, run_in_gui_thread
class StatusItem(NSObject):
items = []
menu = None
statusItem = None
def show(self, item):
if not self.items:
self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(30)
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.getTitle(), "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
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
def update(self, item, state):
if self.menu:
mitem = self.menu.itemWithTag_(item.sessionController.identifier)
if mitem:
name = item.sessionController.getTitleShort()
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)
class ScreenSharingController(MediaStream):
# TODO video: stop stream if video is added
type = "screen-sharing"
implements(IObserver)
viewer = None
vncServerPort = 5900
exhanged_bytes = 0
must_reset_trace_msrp = False
#statusItem = StatusItem.alloc().init()
statusWindow = objc.IBOutlet()
statusLabel = objc.IBOutlet()
statusProgress = objc.IBOutlet()
stopButton = objc.IBOutlet()
def initWithOwner_stream_(self, scontroller, stream):
self = super(ScreenSharingController, self).initWithOwner_stream_(scontroller, stream)
BlinkLogger().log_debug(u"Creating %s" % self)
self.stream = stream
self.direction = stream.handler.type
self.vncViewerTask = None
self.close_timer = None
return self
def startIncoming(self, is_update):
if self.direction == "active": # viewer
# open viewer
self.sessionController.log_info("Preparing to view remote screen")
self.stream.handler = ExternalVNCViewerHandler()
else:
self.sessionController.log_info("Sharing local screen...")
self.stream.handler = ExternalVNCServerHandler(("localhost", self.vncServerPort))
NSBundle.loadNibNamed_owner_("ScreenServerWindow", self)
self.statusProgress.startAnimation_(None)
self.statusWindow.setTitle_(NSLocalizedString("Screen Sharing with %s", "Window title") % self.sessionController.getTitleShort())
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)
def startOutgoing(self, is_update):
if self.direction == "active": # viewer
# open viewer
self.sessionController.log_info("Requesting access to remote screen")
else:
self.sessionController.log_info("Sharing local screen...")
NSBundle.loadNibNamed_owner_("ScreenServerWindow", self)
self.statusProgress.startAnimation_(None)
self.statusWindow.setTitle_(NSLocalizedString("Screen Sharing with %s", "Window title") % self.sessionController.getTitleShort())
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()
def end(self):
self.stopButton.setHidden_(True)
if self.status in (STREAM_DISCONNECTING, STREAM_CANCELLING, STREAM_IDLE, STREAM_FAILED):
if self.statusWindow:
self.statusWindow.close()
self.statusWindow = None
elif self.status == STREAM_PROPOSING:
self.sessionController.log_info("Cancelling screen sharing session as per user request")
self.sessionController.cancelProposal(self.stream)
self.changeStatus(STREAM_CANCELLING)
elif self.status in (STREAM_CONNECTED, STREAM_INCOMING):
self.sessionController.log_info("Removing Screen Stream from Session")
self.sessionController.endStream(self)
self.changeStatus(STREAM_DISCONNECTING)
else:
self.sessionController.log_info("Cancelling screen sharing session as per user request")
self.sessionController.end()
self.changeStatus(STREAM_DISCONNECTING)
def updateStatusIcon(self):
pass
def sessionStateChanged(self, newstate, detail):
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)
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 viewer to vnc://127.0.0.1:%s" % port)
url = NSURL.URLWithString_("vnc://localhost:%i" % (port))
NSWorkspace.sharedWorkspace().openURL_(url)
else:
self.statusWindow.makeKeyAndOrderFront_(None)
#self.statusItem.update(self, newstate)
if self.statusLabel and self.statusWindow:
if newstate == STREAM_CONNECTED:
_t = self.sessionController.getTitleShort()
label = NSLocalizedString("%s requests your screen. Please confirm when asked.", "Label") % _t
self.statusProgress.setHidden_(False)
self.statusProgress.startAnimation_(None)
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"))
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)
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()
self.status = newstate
MediaStream.changeStatus(self, newstate, fail_reason)
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_(5, self, "closeWindows:", None, False)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.close_timer, NSRunLoopCommonModes)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.close_timer, NSEventTrackingRunLoopMode)
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
@allocate_autorelease_pool
@run_in_gui_thread
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification.sender, notification.data)
def _NH_MediaStreamDidStart(self, sender, data):
self.sessionController.log_info("Screen sharing started")
self.changeStatus(STREAM_CONNECTED)
def _NH_MediaStreamDidFail(self, sender, data):
self.sessionController.log_info("Screen sharing failed")
self.changeStatus(STREAM_IDLE)
self.resetTrace()
if self.statusWindow:
self.stopButton.setHidden_(True)
def _NH_MediaStreamDidEnd(self, sender, data):
self.sessionController.log_info("Screen sharing ended")
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()
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.getTitleShort()
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()
def _NH_ScreenSharingHandlerDidFail(self, sender, data):
if data.failure.type == VNCConnectionError:
self.sessionController.log_info("%s" % data.reason.title())
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")
def dealloc(self):
BlinkLogger().log_debug(u"Dealloc %s" % self)
self.resetTrace()
self.stream = None
self.sessionController = None
super(ScreenSharingController, self).dealloc()
class ScreenSharingViewerController(ScreenSharingController):
@classmethod
def createStream(cls):
return ScreenSharingStream(ExternalVNCViewerHandler())
class ScreenSharingServerController(ScreenSharingController):
@classmethod
def createStream(cls):
return ScreenSharingStream(ExternalVNCServerHandler(("localhost", cls.vncServerPort)))