This repository has been archived by the owner on Jun 27, 2021. It is now read-only.
/
VideoMirrorWindowController.py
113 lines (85 loc) · 3.73 KB
/
VideoMirrorWindowController.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
# Copyright (C) 2011 AG Projects. See LICENSE for details.
#
from Foundation import (NSBundle,
NSMakeRect,
NSObject,
NSScreen,
NSUserDefaults,
NSView)
import objc
import QTKit
class VideoMirrorWindowController(NSObject):
window = objc.IBOutlet()
view = objc.IBOutlet()
visible = False
mirrorSession = None
def __new__(cls, *args, **kwargs):
return cls.alloc().init()
def init(self):
self = super(VideoMirrorWindowController, self).init()
if self:
NSBundle.loadNibNamed_owner_("VideoMirrowWindow", 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_(0.9)
return self
def windowDidMove_(self, notification):
if self.window.frameAutosaveName():
self.window.saveFrameUsingName_(self.window.frameAutosaveName())
def windowShouldClose_(self, sender):
self.hide()
def initCapture(self):
if self.mirrorSession is None:
self.mirrorSession = QTKit.QTCaptureSession.alloc().init()
# Find a video device
device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
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.view.setCaptureSession_(self.mirrorSession)
def show(self):
self.initCapture()
self.mirrorSession.startRunning()
self.window.orderFront_(None)
self.visible = True
def hide(self):
if self.mirrorSession is not None:
self.mirrorSession.stopRunning()
self.window.orderOut_(self)
self.visible = False
class VideoMirrowView(NSView):
initialLocation = None
def keyDown_(self, event):
s = event.characters()
key = s[0].upper()
if key == chr(27):
self.delegate.hide()
else:
NSView.keyDown_(self, event)
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);