-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoNativeLocalWindowController.py
350 lines (282 loc) · 12.9 KB
/
VideoNativeLocalWindowController.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
# Copyright (C) 2014 AG Projects. See LICENSE for details.
#
from AppKit import (NSSize,
NSBorderlessWindowMask,
NSResizableWindowMask,
NSWindowController,
NSPanel,
NSWindow,
NSOnState,
NSOffState,
NSView,
NSFloatingWindowLevel,
NSTrackingMouseEnteredAndExited,
NSTrackingActiveAlways,
NSRightMouseUp
)
from Foundation import (NSBundle,
NSColor,
NSDate,
NSEvent,
NSLocalizedString,
NSMakeRect,
NSMenu,
NSUserDefaults,
NSTimer,
NSMenu,
NSMenuItem,
NSScreen,
NSTrackingArea,
NSZeroRect
)
import QTKit
from QTKit import QTFormatDescriptionVideoEncodedPixelsSizeAttribute
import objc
from BlinkLogger import BlinkLogger
from util import run_in_gui_thread
from sipsimple.core import Engine
from sipsimple.application import SIPApplication
from sipsimple.configuration.settings import SIPSimpleSettings
ALPHA = 1.0
class VideoNativeLocalWindowController(NSWindowController):
localVideoView = objc.IBOutlet()
visible = False
close_timer = None
tracking_area = None
def __new__(cls, *args, **kwargs):
return cls.alloc().init()
def init(self):
self = super(VideoNativeLocalWindowController, self).init()
if self:
NSBundle.loadNibNamed_owner_("VideoNativeLocalWindow", self)
userdef = NSUserDefaults.standardUserDefaults()
savedFrame = userdef.stringForKey_("NSWindow Frame MirrorWindow")
if savedFrame:
x, y, w, h = str(savedFrame).split()[:4]
frame = NSMakeRect(int(x), int(y), int(w), int(h))
self.window().setFrame_display_(frame, True)
self.window().setAlphaValue_(ALPHA)
self.window().setLevel_(NSFloatingWindowLevel)
self.window().closeButton.setHidden_(True)
self.updateTrackingAreas()
return self
def updateTrackingAreas(self):
if self.tracking_area is not None:
self.window().contentView().removeTrackingArea_(self.tracking_area)
self.tracking_area = None
rect = NSZeroRect
rect.size = self.window().contentView().frame().size
self.tracking_area = NSTrackingArea.alloc().initWithRect_options_owner_userInfo_(rect,
NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways, self, None)
self.window().contentView().addTrackingArea_(self.tracking_area)
def mouseEntered_(self, event):
self.window().closeButton.setHidden_(False)
def mouseExited_(self, event):
self.window().closeButton.setHidden_(True)
def windowShouldClose_(self, sender):
self.visible = False
if self.close_timer is None:
self.close_timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(0.05, self, "fade:", None, True)
return False
def windowWillResize_toSize_(self, window, frameSize):
if self.localVideoView.aspect_ratio is None:
return frameSize
currentSize = self.window().frame().size
scaledSize = frameSize
scaledSize.width = frameSize.width
scaledSize.height = scaledSize.width / self.localVideoView.aspect_ratio
return scaledSize
def windowDidResize_(self, notification):
self.updateTrackingAreas()
def windowDidMove_(self, notification):
if self.window().frameAutosaveName():
self.window().saveFrameUsingName_(self.window().frameAutosaveName())
@run_in_gui_thread
def close(self):
BlinkLogger().log_debug('Close %s' % self)
self.localVideoView.close()
self.localVideoView = None
self.window().close()
@run_in_gui_thread
def refreshAfterCameraChanged(self):
self.localVideoView.refreshAfterCameraChanged()
def show(self):
BlinkLogger().log_debug('Show %s' % self)
self.visible = True
self.localVideoView.show()
if self.close_timer is not None and self.close_timer.isValid():
self.close_timer.invalidate()
self.close_timer = None
if self.localVideoView.aspect_ratio is not None:
self._show()
@run_in_gui_thread
def _show(self):
self.window().setAlphaValue_(ALPHA)
frame = self.window().frame()
currentSize = frame.size
scaledSize = currentSize
scaledSize.height = scaledSize.width / self.localVideoView.aspect_ratio
frame.size = scaledSize
self.window().setFrame_display_animate_(frame, True, False)
self.window().orderFront_(None)
def dealloc(self):
self.window().contentView().removeTrackingArea_(self.tracking_area)
self.tracking_area = None
BlinkLogger().log_debug('Dealloc %s' % self)
super(VideoNativeLocalWindowController, self).dealloc()
@run_in_gui_thread
def hide(self):
if not self.visible:
return
self.visible = False
self.window().performClose_(None)
def fade_(self, timer):
if self.window().alphaValue() > 0.0:
self.window().setAlphaValue_(self.window().alphaValue() - 0.05)
else:
self.close_timer.invalidate()
self.close_timer = None
self.localVideoView.hide()
self.window().close()
self.window().setAlphaValue_(ALPHA) # make the window fully opaque again for next time
class LocalNativeVideoView(NSView):
initialLocation = None
deviceView = objc.IBOutlet()
parentWindow = objc.IBOutlet()
mirrorSession = None
aspect_ratio = None
def close(self):
BlinkLogger().log_debug('Close %s' % self)
if self.mirrorSession is not None:
if self.mirrorSession.isRunning():
self.mirrorSession.stopRunning()
self.mirrorSession = None
self.removeFromSuperview()
def dealloc(self):
self.mirrorSession = None
BlinkLogger().log_debug('Dealloc %s' % self)
super(LocalNativeVideoView, self).dealloc()
def keyDown_(self, event):
s = event.characters()
key = s[0].upper()
if key == chr(27):
self.parentWindow.delegate().hide()
else:
NSView.keyDown_(self, event)
def rightMouseDown_(self, event):
point = self.parentWindow.convertScreenToBase_(NSEvent.mouseLocation())
event = NSEvent.mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure_(
NSRightMouseUp, point, 0, NSDate.timeIntervalSinceReferenceDate(), self.parentWindow.windowNumber(),
self.parentWindow.graphicsContext(), 0, 1, 0)
videoDevicesMenu = NSMenu.alloc().init()
lastItem = videoDevicesMenu.addItemWithTitle_action_keyEquivalent_(NSLocalizedString("Select Video Device", "Menu item"), "", "")
lastItem.setEnabled_(False)
videoDevicesMenu.addItem_(NSMenuItem.separatorItem())
for item in Engine().video_devices:
if str(item) == "Colorbar generator":
continue
lastItem = videoDevicesMenu.addItemWithTitle_action_keyEquivalent_(item, "changeVideoDevice:", "")
lastItem.setRepresentedObject_(item)
if SIPApplication.video_device.real_name == item:
lastItem.setState_(NSOnState)
NSMenu.popUpContextMenu_withEvent_forView_(videoDevicesMenu, event, self)
def changeVideoDevice_(self, sender):
settings = SIPSimpleSettings()
settings.video.device = sender.representedObject()
settings.save()
def mouseDown_(self, event):
self.initialLocation = event.locationInWindow()
def mouseDragged_(self, event):
screenVisibleFrame = NSScreen.mainScreen().visibleFrame()
windowFrame = self.window().frame();
newOrigin = windowFrame.origin;
currentLocation = event.locationInWindow()
newOrigin.x += (currentLocation.x - self.initialLocation.x);
newOrigin.y += (currentLocation.y - self.initialLocation.y);
if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)):
newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
self.window().setFrameOrigin_(newOrigin);
def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
if not self.aspect_ratio:
self.getAspectRatio()
def getDevice(self):
# Find a video device
try:
device = (device for device in QTKit.QTCaptureDevice.inputDevices() if device.localizedDisplayName() == SIPApplication.video_device.real_name).next()
except StopIteration:
BlinkLogger().log_info('No camera found')
return None
else:
return device
def getAspectRatio(self):
# this can be optained only after capturing data from device
if self.aspect_ratio:
return
device = self.getDevice()
if device:
for desc in device.formatDescriptions():
value = desc.attributeForKey_(QTFormatDescriptionVideoEncodedPixelsSizeAttribute)
if value:
size = value.sizeValue()
self.aspect_ratio = size.width/float(size.height) if size.width > size.height else size.height/float(size.width)
BlinkLogger().log_info('Opened %s camera at %0.fx%0.f resolution' % (SIPApplication.video_device.real_name, size.width, size.height))
else:
BlinkLogger().log_debug('Error getting camera properties')
self.aspect_ratio = 1.77
self.parentWindow.delegate()._show()
break
def refreshAfterCameraChanged(self):
if not self.mirrorSession:
return
if self.mirrorSession.isRunning():
self.hide()
self.mirrorSession = None
self.aspect_ratio = None
self.show()
def show(self):
BlinkLogger().log_debug('Show %s' % self)
if self.mirrorSession is None:
self.mirrorSession = QTKit.QTCaptureSession.alloc().init()
# Find a video device
device = self.getDevice()
if not device:
return
success, error = device.open_(None)
if not success:
return
# Add a device input for that device to the capture session
captureDeviceInput = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
success, error = self.mirrorSession.addInput_error_(captureDeviceInput, None)
if not success:
return
# Add a decompressed video output that returns raw frames to the session
captureDecompressedVideoOutput = QTKit.QTCaptureVideoPreviewOutput.alloc().init()
captureDecompressedVideoOutput.setDelegate_(self)
success, error = self.mirrorSession.addOutput_error_(captureDecompressedVideoOutput, None)
if not success:
return
self.deviceView.setCaptureSession_(self.mirrorSession)
BlinkLogger().log_debug('Start aquire video %s' % self)
self.mirrorSession.startRunning()
def hide(self):
BlinkLogger().log_debug('Hide %s' % self)
if self.mirrorSession is not None:
BlinkLogger().log_debug('Stop aquire video %s' % self)
self.mirrorSession.stopRunning()
class RoundWindow(NSPanel):
closeButton = objc.IBOutlet()
def initWithContentRect_styleMask_backing_defer_(self, contentRect, aStyle, bufferingType, flag):
self = super(RoundWindow, self).initWithContentRect_styleMask_backing_defer_(contentRect, NSBorderlessWindowMask, bufferingType, flag)
if self:
self.setStyleMask_(NSBorderlessWindowMask|NSResizableWindowMask)
self.setOpaque_(False)
self.setBackgroundColor_(NSColor.clearColor())
return self
def setContentView_(self, view):
view.setWantsLayer_(True)
view.layer().setFrame_(view.frame())
view.layer().setCornerRadius_(12.0)
view.layer().setMasksToBounds_(True)
super(RoundWindow, self).setContentView_(view)
def performClose_(self, sender):
self.delegate().windowShouldClose_(self)