Skip to content

Instantly share code, notes, and snippets.

@jmbldwn
Last active June 18, 2018 07:30
Show Gist options
  • Save jmbldwn/0ca994f69ed9f7d0e5adcef2bb4c1e4f to your computer and use it in GitHub Desktop.
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
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);
@lukaskollmer
Copy link

fyi, the objc.js call in line 21 is unnecessary. you can use a for ... of loop to iterate over NSArray, NSDictionary and NSSet instances:

for (const device of AVCaptureDevice.devices()) {
    console.log(`${device.localizedName()}`);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment