-->

运行多个AVCaptureSessions或添加多个输入(Run multiple AVCaptur

2019-06-25 13:12发布

我想显示的前部的流和彼此相邻面对一个iPad2的相机背面两个UIViews。 到流式传输一个设备的图像I使用下面的代码

AVCaptureDeviceInput *captureInputFront = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:nil];

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session addInput:captureInputFront];
session setSessionPreset:AVCaptureSessionPresetMedium];
session startRunning];

AVCaptureVideoPreviewLayer *prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
prevLayer.frame = self.view.frame;
[self.view.layer addSublayer:prevLayer];

这对于无论是相机工作正常。 要显示并行我试图创建另一个会话流,但只要第二届第一次建立冻结。

然后我试图两AVCaptureDeviceInput添加到会话,但好像最多一个输入的时刻支持。

任何有益的想法如何从摄像头流?

Answer 1:

可能获得CMSampleBufferRef从MacOS X上你不得不安装多个视频设备的S AVCaptureConnection手动对象。 例如,假设你有这些对象:

AVCaptureSession *session;
AVCaptureInput *videoInput1;
AVCaptureInput *videoInput2;
AVCaptureVideoDataOutput *videoOutput1;
AVCaptureVideoDataOutput *videoOutput2;

不要加上这样的输出:

[session addOutput:videoOutput1];
[session addOutput:videoOutput2];

相反,加入他们,告诉会话不作任何连接:

[session addOutputWithNoConnections:videoOutput1];
[session addOutputWithNoConnections:videoOutput2];

然后,对于每个输入/输出对使从所述输入的视频端口手动输出连接:

for (AVCaptureInputPort *port in [videoInput1 ports]) {
    if ([[port mediaType] isEqualToString:AVMediaTypeVideo]) {
        AVCaptureConnection* cxn = [AVCaptureConnection
            connectionWithInputPorts:[NSArray arrayWithObject:port]
            output:videoOutput1
        ];
        if ([session canAddConnection:cxn]) {
            [session addConnection:cxn];
        }
        break;
    }
}

最后,一定要设置采样缓冲器代表两个输出:

[videoOutput1 setSampleBufferDelegate:self queue:someDispatchQueue];
[videoOutput2 setSampleBufferDelegate:self queue:someDispatchQueue];

现在你应该能够从两个设备处理框架:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    if (captureOutput == videoOutput1)
    {
        // handle frames from first device
    }
    else if (captureOutput == videoOutput2)
    {
        // handle frames from second device
    }
}

又见AVVideoWall样本项目进行实时预览来自多个视频设备相结合的一个例子。



文章来源: Run multiple AVCaptureSessions or add multiple inputs