-
-
Save jmbldwn/0ca994f69ed9f7d0e5adcef2bb4c1e4f to your computer and use it in GitHub Desktop.
Attempt to capture frames of video from the camera using the 'objc' bridge module
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
const objc = require('objc'); | |
const ffi = require('ffi'); | |
objc.import('AVFoundation'); | |
const { | |
AVCaptureDevice, | |
AVCaptureDeviceInput, | |
AVCaptureSession, | |
AVCaptureSessionPreset640x480, | |
AVCaptureVideoDataOutput, | |
AVMediaTypeVideo, | |
kCVPixelBufferPixelFormatTypeKey | |
} = objc; | |
const c = new ffi.Library(null, { | |
dispatch_queue_create: ['pointer', ['string', 'pointer']] | |
}); | |
// list attached devices | |
let devices = objc.js(AVCaptureDevice.devices()); | |
devices.forEach(device => { | |
let name = objc.js(device.localizedName()); | |
console.log(name); | |
}); | |
// capture some frames | |
let captureSession = AVCaptureSession.alloc().init(); | |
captureSession.sessionPreset = AVCaptureSessionPreset640x480; | |
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType_( | |
AVMediaTypeVideo | |
); | |
const error = objc.allocRef(); | |
let videoInput = AVCaptureDeviceInput.deviceInputWithDevice_error_( | |
captureDevice, | |
error | |
); | |
captureSession.addInput_(videoInput); | |
let videoDataOutput = AVCaptureVideoDataOutput.alloc().init(); | |
captureSession.addOutput_(videoDataOutput); | |
const AVCaptureVideoDataOutputSampleBufferDelegate = objc.createClass( | |
'AVCaptureVideoDataOutputSampleBufferDelegate', | |
'NSObject', | |
{ | |
'captureOutput:didOutputSampleBuffer:fromConnection:': ( | |
self, | |
cmd, | |
captureOutput, | |
sampleBuffer, | |
connection | |
) => { | |
console.log( | |
'-[AVCaptureVideoDataOutputSampleBufferDelegate captureOutput:didOutputSampleBuffer:fromConnection:]' | |
); | |
return 1; | |
}, | |
_encodings: { | |
'captureOutput:didOutputSampleBuffer:fromConnection:': [ | |
'v40', | |
['@0', ':8', '@16', '@24', '@32'] | |
] | |
} | |
} | |
); | |
const delegate = AVCaptureVideoDataOutputSampleBufferDelegate.new(); | |
//dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL); | |
let queue = c.dispatch_queue_create('myQueue', null); | |
// videoDataOutput.videoSettings = objc.ns({ | |
// kCVPixelBufferPixelFormatTypeKey: 'BGRA' | |
// }, [hint = '@']) | |
videoDataOutput.setSampleBufferDelegate_queue_(delegate, queue); | |
captureSession.startRunning(); | |
console.log(error); | |
console.log('hey'); | |
setTimeout(() => { | |
console.log('end'); | |
}, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fyi, the
objc.js
call in line 21 is unnecessary. you can use afor ... of
loop to iterate overNSArray
,NSDictionary
andNSSet
instances: