Skip to content

Instantly share code, notes, and snippets.

@samjoch
Created November 4, 2016 12:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samjoch/d06f7fb39b2cbbca087ddcb1af59b28e to your computer and use it in GitHub Desktop.
Save samjoch/d06f7fb39b2cbbca087ddcb1af59b28e to your computer and use it in GitHub Desktop.
Playground to capture ios video input stream into a window
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