Created
November 4, 2016 12:00
-
-
Save samjoch/d06f7fb39b2cbbca087ddcb1af59b28e to your computer and use it in GitHub Desktop.
Playground to capture ios video input stream into a window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import AVFoundation | |
import CoreMediaIO | |
import XCPlayground | |
var currentDevice: AVCaptureDevice! | |
var currentName: String! | |
var currentUUID: String! | |
var window = NSWindow(contentRect: NSRect(x: 30, y: 30, width: 375, height: 667), | |
styleMask: NSTitledWindowMask, backing: .buffered, defer: false) | |
let frame = NSRect(x: 0, y: 0, width: 375, height: 667) | |
let preview = NSView(frame: frame) | |
class IOSDeviceHelper: NSObject { | |
override init () { | |
super.init() | |
visible() | |
} | |
func visible () { | |
var prop = CMIOObjectPropertyAddress( | |
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices), | |
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal), | |
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster)) | |
var allow : UInt32 = 1 | |
let dataSize : UInt32 = 4 | |
let zero : UInt32 = 0 | |
CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop, zero, nil, dataSize, &allow) | |
} | |
func start () { | |
print("\(AVCaptureDevice.devices().count)") | |
NotificationCenter.default | |
.addObserver(forName: NSNotification.Name.AVCaptureDeviceWasDisconnected, object: nil, queue: nil) | |
{ (notif) -> Void in | |
self.iosDeviceDetached(device: notif.object! as! AVCaptureDevice) | |
} | |
NotificationCenter.default | |
.addObserver(forName: NSNotification.Name.AVCaptureDeviceWasConnected, object: nil, queue: nil) | |
{ (notif) -> Void in | |
self.iosDeviceAttached(device: notif.object! as! AVCaptureDevice) | |
} | |
} | |
func iosDeviceAttached(device:AVCaptureDevice) { | |
print("ios device attached \(device)") | |
print("Found device \(device.localizedName)") | |
if device.modelID! == "iOS Device" { | |
currentDevice = device | |
currentName = currentDevice.localizedName | |
currentUUID = currentDevice.uniqueID | |
print("hello \(currentName) aka \(currentUUID)") | |
let session = AVCaptureSession() | |
let input = (try! AVCaptureDeviceInput(device: currentDevice)) | |
let output = AVCaptureVideoDataOutput() | |
session.addOutput(output) | |
session.addInput(input) | |
let layerPreview = AVCaptureVideoPreviewLayer(session: session) | |
layerPreview?.videoGravity = AVLayerVideoGravityResizeAspectFill | |
layerPreview?.frame = preview.bounds | |
preview.layer = layerPreview | |
preview.wantsLayer = true | |
window.contentView!.addSubview(preview) | |
window.makeKeyAndOrderFront(nil) | |
session.startRunning() | |
} | |
} | |
func iosDeviceDetached(device:AVCaptureDevice) { | |
print("ios device detached") | |
print("stop running") | |
// session.stopRunning() | |
} | |
} | |
let iosHelper = IOSDeviceHelper(); | |
iosHelper.start(); | |
// NSRunLoop.mainRunLoop().run() | |
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment