double output of a camera video stream

Hello, everybody,


I would like to display the camera image of the back camera twice on the screen (as with the cardboard). I created two views and they each take 50% of the screen, I can also output the live image of the camera on one view.


// views left and right
    var viewRect:CGRect!
    var leftView:UIView!
    var rightView:UIView!
//...

//left
        viewRect = CGRect(x: 0, y: 0, width: 0.5 * self.view.frame.width, height: self.view.frame.height)
        leftView = UIView(frame: viewRect)
        leftView.backgroundColor = .green
        view.addSubview(leftView)

        //rgiht
        viewRect = CGRect(x: 0.5 *  self.view.frame.width, y: 0, width: 0.5 * self.view.frame.width, height: self.view.frame.height)
        rightView = UIView(frame: viewRect)
        rightView.backgroundColor = .red
        view.addSubview(rightView)
//...

    func beginSession(){
        captureSession.sessionPreset = AVCaptureSession.Preset.photo
        let devices = AVCaptureDevice.devices()
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaType.video)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevice.Position.back) {
                    captureDevice = device as? AVCaptureDevice
                }
            }
        }
        
        do {
            try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice!))
            captureSession.sessionPreset = AVCaptureSession.Preset.photo
        } catch _ {
        }

        let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeLeft
        previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        self.leftPreviewLayer = previewLayer
        self.leftPreviewLayer.frame = self.view.layer.frame
        self.leftPreviewLayer.frame = self.leftView.bounds
        self.leftView.layer.addSublayer(previewLayer)

        self.leftView.layer.addSublayer(previewLayer)
        captureSession.startRunning()
    }


I already found something like "let replicatorLayer = CAReplicatorLayer()" online, but can't get it to work 😟

it would be great if someone could give me a hint 😉

Thx and cya Tom

Replies

What about adding a

AVCaptureVideoDataOutput
with an attached
AVCaptureVideoDataOutputSampleBufferDelegate
to the
AVCaptureSession
(instead of the
AVCaptureVideoPreviewLayer
), and using
AVSampleBufferDisplayLayers
in each of the left/right views? Then, in the didOutputSample method of the delegate, simply enqueue the
CMSampleBuffer
to the SampleBufferDisplayLayers...


\nhb

  • Any updates on this? Did the answer above make the code work? Facing this dilemma myself with AVFoundation

Add a Comment