AVCaptureVideoPreviewLayer & AVCaptureSession

Hi all,

I’m trying to get video capture working (from an iSight/FaceTime camera) but am stuck on setting up the AVCaptureVideoPreviewLayer. Seems like it should be pretty straight-forward… but obviously I am missing something, as I am getting an unrecognized selector error.


         set AVCaptureSession to current application's AVCaptureSession's alloc()'s init()

        AVCaptureSession's startRunning()
        AVCaptureSession's beginConfiguration()
        
        AVCaptureSession's setSessionPreset_((current application's AVCaptureSessionPresetHigh))
        
        set videoCaptureDevice to current application's AVCaptureDevice's defaultDeviceWithMediaType_((current application's AVMediaTypeVideo))
        set videoInput to current application's AVCaptureDeviceInput's deviceInputWithDevice_error_(videoCaptureDevice, missing value)
        set videoOutput to current application's AVCaptureMovieFileOutput's alloc()'s init()
        
        set audioCaptureDevice to current application's AVCaptureDevice's defaultDeviceWithMediaType_((current application's AVMediaTypeAudio))
        set audioInput to current application's AVCaptureDeviceInput's deviceInputWithDevice_error_(audioCaptureDevice, missing value)
        set audioOutput to current application's AVCaptureAudioPreviewOutput's alloc()'s init()
        audioOutput's setVolume_(0)
        
        AVCaptureSession's addInput_(videoInput)
        AVCaptureSession's addInput_(audioInput)
        AVCaptureSession's addOutput_(videoOutput)
        AVCaptureSession's addOutput_(audioOutput)

        AVCaptureSession's commitConfiguration()

        set AVCaptureVideoPreviewLayer to current application's AVCaptureVideoPreviewLayer's alloc()'s initWithSession_(AVCaptureSession) -- ERRORS WITH UNRECOGNIZED SELECTOR

After successfully setting up AVCaptureVideoPreviewLayer, I think I will need to then set up a CALayer (which I am a bit confused on how to accomplish, as well), and then ‘attach’ the AVCaptureVideoPreviewLayer to the CALayer. Can a CALayer be added to an NSView object by doing something like:


--aView is a NSView linked in IB
aView's setWantsLayer_(true)
aView's layer()'s addSublayer_(AVCaptureVideoPreviewLayer)

Hi,

a sublayer can only be added to a layer, for example


aView's layer()'s addSublayer_(AVCaptureVideoPreviewLayer)


aView's addSublayer_(AVCaptureVideoPreviewLayer)

^ Complete mis-type on my part. Thanks for the correction, Stefan. I hastily typed that last bit of my previous post. Unfortunately I’m still stuck on:


set AVCaptureVideoPreviewLayer to current application's AVCaptureVideoPreviewLayer's alloc()'s initWithSession_(AVCaptureSession) -- ERRORS WITH UNRECOGNIZED SELECTOR

For what it’s worth, when starting the AVCaptureSession – AVCaptureSession’s startRunning() – the little green light on my FaceTime/iSight camera comes on, which makes me think I’m close to getting this working. Also, I have added the QuartzCore framework to my project (which I think is required for the AVCaptureVideoPreviewLayer).

If I log my AVCaptureSession, here is what I get:

I don’t really understand why there’s a doubling of the objects when I log AVCaptureSession… when I log captureSession’s inputs() I get:

Finally got this (seemingly) working by doing the following (in addition to most of the code in the first post above):


-- INITIALIZE THE PREVIEW LAYER OBJECT
set AVCaptureVideoPreviewLayer to current application's AVCaptureVideoPreviewLayer's layerWithSessionWithNoConnection_(AVCaptureSession)

-- GET PORTS OF VIDEO INPUT DEVICE
set videoPorts to videoInput's  ports()
set videoPort to item 1 of videoPorts

-- CREATE A CONNECTION AND ADD IT TO THE SESSION
set captureConnection to current application's AVCaptureConnection's connectionWithInputPort_videoPreviewLayer_(videoPort, AVCaptureVideoPreviewLayer)
AVCaptureSession's addConnection_(captureConnection)

-- SIZE THE PREVIEW LAYERS TO THE VIEW'S SIZE
AVCaptureVideoPreviewLayer's setFrame_(aView's |bounds|())
        
-- CONNECT THE PREVIEW LAYER TO THE IBOUTLET'S LAYER
aView's setWantsLayer_(true)
aView's layer()'s addSublayer_(AVCaptureVideoPreviewLayer)
    
captureSession's startRunning() -- START THE SESSION RUNNING

I’m still not entirely sure what was going wrong with the AVCaptureVideoPreviewLayer’s alloc()'s initWithSession_(AVCaptureSession) method I was trying to use, but if I had to guess, I think it was that my videoInput object had an array of ports. Trying to pass this array of ports to a AVCaptureConnection failed, so that’s why I tried the set videoPort to item 1 of videoPorts line.