-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathViewController.swift
More file actions
510 lines (455 loc) · 22.1 KB
/
ViewController.swift
File metadata and controls
510 lines (455 loc) · 22.1 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
//
// ViewController.swift
// samplecamera
//
// Created by laurent denoue on 7/1/22.
//
import AVFoundation
import Cocoa
import CoreMediaIO
import SystemExtensions
class ViewController: NSViewController {
private var debugCaption: NSTextField!
private var needToStreamCaption: NSTextField!
private var needToStream: Bool = false
private var mirrorCamera: Bool = false
private var image = NSImage(named: "cham-index")
private var activating: Bool = false
private var readyToEnqueue = false
private var enqueued = false
private var _videoDescription: CMFormatDescription!
private var _bufferPool: CVPixelBufferPool!
private var _bufferAuxAttributes: NSDictionary!
private var _whiteStripeStartRow: UInt32 = 0
private var _whiteStripeIsAscending: Bool = false
private var overlayMessage: Bool = false
private var sequenceNumber = 0
private var timer: Timer?
private var propTimer: Timer?
func activateCamera() {
guard let extensionIdentifier = ViewController._extensionBundle().bundleIdentifier else {
return
}
self.activating = true
let activationRequest = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: extensionIdentifier, queue: .main)
activationRequest.delegate = self
OSSystemExtensionManager.shared.submitRequest(activationRequest)
}
func deactivateCamera() {
guard let extensionIdentifier = ViewController._extensionBundle().bundleIdentifier else {
return
}
self.activating = false
let deactivationRequest = OSSystemExtensionRequest.deactivationRequest(forExtensionWithIdentifier: extensionIdentifier, queue: .main)
deactivationRequest.delegate = self
OSSystemExtensionManager.shared.submitRequest(deactivationRequest)
}
private class func _extensionBundle() -> Bundle {
let extensionsDirectoryURL = URL(fileURLWithPath: "Contents/Library/SystemExtensions", relativeTo: Bundle.main.bundleURL)
let extensionURLs: [URL]
do {
extensionURLs = try FileManager.default.contentsOfDirectory(at: extensionsDirectoryURL,
includingPropertiesForKeys: nil,
options: .skipsHiddenFiles)
} catch let error {
fatalError("Failed to get the contents of \(extensionsDirectoryURL.absoluteString): \(error.localizedDescription)")
}
guard let extensionURL = extensionURLs.first else {
fatalError("Failed to find any system extensions")
}
guard let extensionBundle = Bundle(url: extensionURL) else {
fatalError("Failed to find any system extensions")
}
return extensionBundle
}
func getJustProperty(streamId: CMIOStreamID) -> String? {
let selector = FourCharCode("just")
var address = CMIOObjectPropertyAddress(selector, .global, .main)
let exists = CMIOObjectHasProperty(streamId, &address)
if exists {
var dataSize: UInt32 = 0
var dataUsed: UInt32 = 0
CMIOObjectGetPropertyDataSize(streamId, &address, 0, nil, &dataSize)
var name: CFString = "" as NSString
CMIOObjectGetPropertyData(streamId, &address, 0, nil, dataSize, &dataUsed, &name);
return name as String
} else {
return nil
}
}
func setJustProperty(streamId: CMIOStreamID, newValue: String) {
let selector = FourCharCode("just")
var address = CMIOObjectPropertyAddress(selector, .global, .main)
let exists = CMIOObjectHasProperty(streamId, &address)
if exists {
var settable: DarwinBoolean = false
CMIOObjectIsPropertySettable(streamId,&address,&settable)
if settable == false {
return
}
var dataSize: UInt32 = 0
CMIOObjectGetPropertyDataSize(streamId, &address, 0, nil, &dataSize)
var newName: CFString = newValue as NSString
CMIOObjectSetPropertyData(streamId, &address, 0, nil, dataSize, &newName)
}
}
func makeDevicesVisible(){
var prop = CMIOObjectPropertyAddress(
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices),
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMain))
var allow : UInt32 = 1
let dataSize : UInt32 = 4
let zero : UInt32 = 0
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop, zero, nil, dataSize, &allow)
}
var sourceStream: CMIOStreamID?
var sinkStream: CMIOStreamID?
var sinkQueue: CMSimpleQueue?
func initSink(deviceId: CMIODeviceID, sinkStream: CMIOStreamID) {
let dims = CMVideoDimensions(width: fixedCamWidth, height: fixedCamHeight)
CMVideoFormatDescriptionCreate(
allocator: kCFAllocatorDefault,
codecType: kCVPixelFormatType_32BGRA,
width: dims.width, height: dims.height, extensions: nil, formatDescriptionOut: &_videoDescription)
var pixelBufferAttributes: NSDictionary!
pixelBufferAttributes = [
kCVPixelBufferWidthKey: dims.width,
kCVPixelBufferHeightKey: dims.height,
kCVPixelBufferPixelFormatTypeKey: _videoDescription.mediaSubType,
kCVPixelBufferIOSurfacePropertiesKey: [:]
]
CVPixelBufferPoolCreate(kCFAllocatorDefault, nil, pixelBufferAttributes, &_bufferPool)
let pointerQueue = UnsafeMutablePointer<Unmanaged<CMSimpleQueue>?>.allocate(capacity: 1)
// see https://stackoverflow.com/questions/53065186/crash-when-accessing-refconunsafemutablerawpointer-inside-cgeventtap-callback
//let pointerRef = UnsafeMutableRawPointer(Unmanaged.passRetained(self).toOpaque())
let pointerRef = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
let result = CMIOStreamCopyBufferQueue(sinkStream, {
(sinkStream: CMIOStreamID, buf: UnsafeMutableRawPointer?, refcon: UnsafeMutableRawPointer?) in
let sender = Unmanaged<ViewController>.fromOpaque(refcon!).takeUnretainedValue()
sender.readyToEnqueue = true
},pointerRef,pointerQueue)
if result != 0 {
showMessage("error starting sink")
} else {
if let queue = pointerQueue.pointee {
self.sinkQueue = queue.takeUnretainedValue()
}
let resultStart = CMIODeviceStartStream(deviceId, sinkStream) == 0
if resultStart {
showMessage("initSink started")
} else {
showMessage("initSink error startstream")
}
}
}
func getDevice(name: String) -> AVCaptureDevice? {
print("getDevice name=",name)
var devices: [AVCaptureDevice]?
if #available(macOS 10.15, *) {
let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.externalUnknown],
mediaType: .video,
position: .unspecified)
devices = discoverySession.devices
} else {
// Fallback on earlier versions
devices = AVCaptureDevice.devices(for: .video)
}
guard let devices = devices else { return nil }
return devices.first { $0.localizedName == name}
}
func getCMIODevice(uid: String) -> CMIOObjectID? {
var dataSize: UInt32 = 0
var devices = [CMIOObjectID]()
var dataUsed: UInt32 = 0
var opa = CMIOObjectPropertyAddress(CMIOObjectPropertySelector(kCMIOHardwarePropertyDevices), .global, .main)
CMIOObjectGetPropertyDataSize(CMIOObjectPropertySelector(kCMIOObjectSystemObject), &opa, 0, nil, &dataSize);
let nDevices = Int(dataSize) / MemoryLayout<CMIOObjectID>.size
devices = [CMIOObjectID](repeating: 0, count: Int(nDevices))
CMIOObjectGetPropertyData(CMIOObjectPropertySelector(kCMIOObjectSystemObject), &opa, 0, nil, dataSize, &dataUsed, &devices);
for deviceObjectID in devices {
opa.mSelector = CMIOObjectPropertySelector(kCMIODevicePropertyDeviceUID)
CMIOObjectGetPropertyDataSize(deviceObjectID, &opa, 0, nil, &dataSize)
var name: CFString = "" as NSString
//CMIOObjectGetPropertyData(deviceObjectID, &opa, 0, nil, UInt32(MemoryLayout<CFString>.size), &dataSize, &name);
CMIOObjectGetPropertyData(deviceObjectID, &opa, 0, nil, dataSize, &dataUsed, &name);
if String(name) == uid {
return deviceObjectID
}
}
return nil
}
func getInputStreams(deviceId: CMIODeviceID) -> [CMIOStreamID]
{
var dataSize: UInt32 = 0
var dataUsed: UInt32 = 0
var opa = CMIOObjectPropertyAddress(CMIOObjectPropertySelector(kCMIODevicePropertyStreams), .global, .main)
CMIOObjectGetPropertyDataSize(deviceId, &opa, 0, nil, &dataSize);
let numberStreams = Int(dataSize) / MemoryLayout<CMIOStreamID>.size
var streamIds = [CMIOStreamID](repeating: 0, count: numberStreams)
CMIOObjectGetPropertyData(deviceId, &opa, 0, nil, dataSize, &dataUsed, &streamIds)
return streamIds
}
func connectToCamera() {
if let device = getDevice(name: cameraName), let deviceObjectId = getCMIODevice(uid: device.uniqueID) {
let streamIds = getInputStreams(deviceId: deviceObjectId)
if streamIds.count == 2 {
sinkStream = streamIds[1]
showMessage("found sink stream")
initSink(deviceId: deviceObjectId, sinkStream: streamIds[1])
}
if let firstStream = streamIds.first {
showMessage("found source stream")
sourceStream = firstStream
}
}
}
@objc func activate(_ sender: Any? = nil) {
activateCamera()
}
@objc func deactivate(_ sender: Any? = nil) {
deactivateCamera()
}
func registerForDeviceNotifications() {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVCaptureDeviceWasConnected, object: nil, queue: nil) { (notif) -> Void in
// when the user click "activate", we will receive a notification
// we can then try to connect to our "Sample Camera" (if not already connected to)
if self.sourceStream == nil {
self.connectToCamera()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
registerForDeviceNotifications()
let button = NSButton(title: "activate", target: self, action: #selector(activate(_:)))
self.view.addSubview(button)
let button2 = NSButton(title: "deactivate", target: self, action: #selector(deactivate(_:)))
self.view.addSubview(button2)
button2.frame = CGRect(x: 120, y: 0, width: button2.frame.width, height: button.frame.height)
debugCaption = fakeLabel("")
debugCaption.isEditable = false
let frame = self.view.frame
debugCaption.frame = frame.insetBy(dx: 0, dy: 32)
self.view.addSubview(debugCaption)
needToStreamCaption = fakeLabel("need to stream = ???")
needToStreamCaption.frame = needToStreamCaption.frame.offsetBy(dx: button2.frame.maxX + 16, dy: 4)
self.view.addSubview(needToStreamCaption)
self.makeDevicesVisible()
connectToCamera()
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 1/30.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
propTimer?.invalidate()
propTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(propertyTimer), userInfo: nil, repeats: true)
}
func showMessage(_ text: String) {
print("showMessage",text)
debugCaption.stringValue += "\(text)\n"
}
func fakeLabel(_ text: String) -> NSTextField {
let label = NSTextField()
label.frame = CGRect(origin: .zero, size: CGSize(width: 200, height: 24))
label.stringValue = text
label.backgroundColor = .clear
//label.isBezeled = false
label.isEditable = false
//label.sizeToFit()
return label
}
func enqueue(_ queue: CMSimpleQueue, _ image: CGImage) {
guard CMSimpleQueueGetCount(queue) < CMSimpleQueueGetCapacity(queue) else {
print("error enqueuing")
return
}
var err: OSStatus = 0
var pixelBuffer: CVPixelBuffer?
err = CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(kCFAllocatorDefault, self._bufferPool, self._bufferAuxAttributes, &pixelBuffer)
if let pixelBuffer = pixelBuffer {
CVPixelBufferLockBaseAddress(pixelBuffer, [])
/*var bufferPtr = CVPixelBufferGetBaseAddress(pixelBuffer)!
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let rowBytes = CVPixelBufferGetBytesPerRow(pixelBuffer)
memset(bufferPtr, 0, rowBytes * height)
let whiteStripeStartRow = self._whiteStripeStartRow
if self._whiteStripeIsAscending {
self._whiteStripeStartRow = whiteStripeStartRow - 1
self._whiteStripeIsAscending = self._whiteStripeStartRow > 0
}
else {
self._whiteStripeStartRow = whiteStripeStartRow + 1
self._whiteStripeIsAscending = self._whiteStripeStartRow >= (height - kWhiteStripeHeight)
}
bufferPtr += rowBytes * Int(whiteStripeStartRow)
for _ in 0..<kWhiteStripeHeight {
for _ in 0..<width {
var white: UInt32 = 0xFFFFFFFF
memcpy(bufferPtr, &white, MemoryLayout.size(ofValue: white))
bufferPtr += MemoryLayout.size(ofValue: white)
}
}*/
let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
// optimizing context: interpolationQuality and bitmapInfo
// see https://stackoverflow.com/questions/7560979/cgcontextdrawimage-is-extremely-slow-after-large-uiimage-drawn-into-it
if let context = CGContext(data: pixelData,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: rgbColorSpace,
//bitmapInfo: UInt32(CGImageAlphaInfo.noneSkipFirst.rawValue) | UInt32(CGImageByteOrderInfo.order32Little.rawValue))
bitmapInfo: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue)
{
context.interpolationQuality = .low
if mirrorCamera {
context.translateBy(x: CGFloat(width), y: 0.0)
context.scaleBy(x: -1.0, y: 1.0)
}
context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
var sbuf: CMSampleBuffer!
var timingInfo = CMSampleTimingInfo()
timingInfo.presentationTimeStamp = CMClockGetTime(CMClockGetHostTimeClock())
err = CMSampleBufferCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer, dataReady: true, makeDataReadyCallback: nil, refcon: nil, formatDescription: self._videoDescription, sampleTiming: &timingInfo, sampleBufferOut: &sbuf)
if err == 0 {
if let sbuf = sbuf {
let pointerRef = UnsafeMutableRawPointer(Unmanaged.passRetained(sbuf).toOpaque())
CMSimpleQueueEnqueue(queue, element: pointerRef)
}
}
} else {
print("error getting pixel buffer")
}
}
@objc func propertyTimer() {
if let sourceStream = sourceStream {
self.setJustProperty(streamId: sourceStream, newValue: "random")
let just = self.getJustProperty(streamId: sourceStream)
if let just = just {
if just == "sc=1" {
needToStream = true
} else {
needToStream = false
}
}
needToStreamCaption.stringValue = "need to stream = \(needToStream)"
}
}
@objc func fireTimer() {
if needToStream {
if (enqueued == false || readyToEnqueue == true), let queue = self.sinkQueue {
enqueued = true
readyToEnqueue = false
if let image = image, let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) {
self.enqueue(queue, cgImage)
}
}
}
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
extension ViewController:OSSystemExtensionRequestDelegate
{
func request(_ request: OSSystemExtensionRequest, actionForReplacingExtension existing: OSSystemExtensionProperties,
withExtension ext: OSSystemExtensionProperties) -> OSSystemExtensionRequest.ReplacementAction {
showMessage("Replacing extension version \(existing.bundleShortVersion) with \(ext.bundleShortVersion)")
return .replace
}
func requestNeedsUserApproval(_ request: OSSystemExtensionRequest) {
showMessage("Extension needs user approval")
}
func request(_ request: OSSystemExtensionRequest, didFinishWithResult result: OSSystemExtensionRequest.Result) {
showMessage("Request finished with result: \(result.rawValue)")
if result == .completed {
if self.activating {
showMessage("The camera is activated")
} else {
showMessage("The camera is deactivated")
}
} else {
if self.activating {
showMessage("Please reboot to finish activating the Scregle camera")
} else {
showMessage("Please Reboot to finish deactivating the Scregle camera")
}
}
}
func request(_ request: OSSystemExtensionRequest, didFailWithError error: Error) {
if self.activating {
showMessage("Failed to activate the camera, run samplecamera from inside /Applications :)")
} else {
showMessage("Failed to deactivate the camera")
}
}
}
extension FourCharCode: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
var code: FourCharCode = 0
// Value has to consist of 4 printable ASCII characters, e.g. '420v'.
// Note: This implementation does not enforce printable range (32-126)
if value.count == 4 && value.utf8.count == 4 {
for byte in value.utf8 {
code = code << 8 + FourCharCode(byte)
}
}
else {
print("FourCharCode: Can't initialize with '\(value)', only printable ASCII allowed. Setting to '????'.")
code = 0x3F3F3F3F // = '????'
}
self = code
}
public init(extendedGraphemeClusterLiteral value: String) {
self = FourCharCode(stringLiteral: value)
}
public init(unicodeScalarLiteral value: String) {
self = FourCharCode(stringLiteral: value)
}
public init(_ value: String) {
self = FourCharCode(stringLiteral: value)
}
public var string: String? {
let cString: [CChar] = [
CChar(self >> 24 & 0xFF),
CChar(self >> 16 & 0xFF),
CChar(self >> 8 & 0xFF),
CChar(self & 0xFF),
0
]
return String(cString: cString)
}
}
public extension CMIOObjectPropertyAddress {
init(_ selector: CMIOObjectPropertySelector,
_ scope: CMIOObjectPropertyScope = .anyScope,
_ element: CMIOObjectPropertyElement = .anyElement) {
self.init(mSelector: selector, mScope: scope, mElement: element)
}
}
public extension CMIOObjectPropertyScope {
/// The CMIOObjectPropertyScope for properties that apply to the object as a whole.
/// All CMIOObjects have a global scope and for some it is their only scope.
static let global = CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal)
/// The wildcard value for CMIOObjectPropertyScopes.
static let anyScope = CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard)
/// The CMIOObjectPropertyScope for properties that apply to the input signal paths of the CMIODevice.
static let deviceInput = CMIOObjectPropertyScope(kCMIODevicePropertyScopeInput)
/// The CMIOObjectPropertyScope for properties that apply to the output signal paths of the CMIODevice.
static let deviceOutput = CMIOObjectPropertyScope(kCMIODevicePropertyScopeOutput)
/// The CMIOObjectPropertyScope for properties that apply to the play through signal paths of the CMIODevice.
static let devicePlayThrough = CMIOObjectPropertyScope(kCMIODevicePropertyScopePlayThrough)
}
public extension CMIOObjectPropertyElement {
/// The CMIOObjectPropertyElement value for properties that apply to the master element or to the entire scope.
//static let master = CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster)
static let main = CMIOObjectPropertyElement(kCMIOObjectPropertyElementMain)
/// The wildcard value for CMIOObjectPropertyElements.
static let anyElement = CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard)
}