Multiple AVCaptureVideoDataOutput per a single AVCaptureDevice at the same time

552 Views Asked by At

Scenario

I am working on an application that does video processing and streaming. I already have video capture from the back camera streaming perfectly. The problem is I have to do my processing to the video data also, but only locally. As it turns out the API I am using to do the local video processing requires a different pixel format than the APIs I am using to stream the data to my server. It seems I need to have two separate sessions capturing video from the back camera simultaneously. That would allow one session to do the processing and one for streaming.

Problem

Every time I attempt to create a new session to use the same AVCaptureDevice (back), my streaming immediately stops. Code below:

captureSession = [[AVCaptureSession alloc] init];

AVCaptureDeviceInput *videoIn = [[AVCaptureDeviceInput alloc]
                                 initWithDevice:[self videoDeviceWithPosition:AVCaptureDevicePositionBack]
                                 error:nil];

if ([captureSession canAddInput:videoIn])
{
    [captureSession addInput:videoIn];
}

AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];

[videoOut setAlwaysDiscardsLateVideoFrames:YES];
[videoOut setVideoSettings:
 @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)}];
dispatch_queue_t videoCaptureQueue =
dispatch_queue_create("Video Process Queue", DISPATCH_QUEUE_SERIAL);
[videoOut setSampleBufferDelegate:self queue:videoCaptureQueue];
if ([captureSession canAddOutput:videoOut]) {
    [captureSession addOutput:videoOut];
}

I receive an interruption reason videoDeviceInUseByAnotherClient.

videoDeviceInUseByAnotherClient: An interruption caused by the video device temporarily being made unavailable (for example, when used by another capture session).

I have also tried adding the output of the original capture session to the new session but every time the canAddOutput: method returns NO. My guess is because there is already a session associated with that output.

Question

How do I use the same AVCaptureDevice to output to two separate AVCaptureVideoDataOutputs at the same time? Or how can I achieve the same thing as the diagram below?

enter image description here

0

There are 0 best solutions below