-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
86 lines (64 loc) · 2.56 KB
/
ViewController.swift
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
//
// ViewController.swift
// Capture device
//
// Created by Morten Just on 5/15/21.
//
import Cocoa
import AVFoundation
import CoreMediaIO
import SceneKit
class ViewController: NSViewController {
@IBOutlet weak var sceneView: SCNView!
var session = AVCaptureSession()
let output = AVCaptureMovieFileOutput()
var input : AVCaptureDeviceInput?
var scene : SCNScene!
var boxNode : SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
// add a scene and a blue box
scene = SCNScene()
sceneView.scene = scene
sceneView.allowsCameraControl = true
sceneView.autoenablesDefaultLighting = true
let box = SCNBox()
boxNode = SCNNode(geometry: box)
box.firstMaterial?.diffuse.contents = NSColor.blue
scene.rootNode.addChildNode(boxNode)
}
@IBAction func start(_ sender: Any) {
session = AVCaptureSession()
session.beginConfiguration()
session.addOutput(output)
session.commitConfiguration()
AVCaptureDevice.requestAccess(for: .video) { granted in
print("granted")
}
// Deprecated, but seems it's the only way. (Works with an NSView layer.)
let device = AVCaptureDevice.devices(for: .muxed).first!
input = try! AVCaptureDeviceInput(device: device)
session.addInput(input!)
let layer = AVCaptureVideoPreviewLayer(session: session)
// the layer doesn't have a size immediately, and we need one to avoid a Metal crash
layer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
// set the layer as the contents
boxNode.geometry?.firstMaterial?.diffuse.contents = layer
// start session
session.startRunning()
}
@IBAction func stop(_ sender: Any) {
session.stopRunning()
}
// this just makes the connected iOS device screen available as a camera
static func prepareForDeviceMonitoring(){
var prop = CMIOObjectPropertyAddress(
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices),
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster))
var allow: UInt32 = 1;
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop,
0, nil,
UInt32(MemoryLayout.size(ofValue: allow)), &allow)
}
}