我想在两个相互靠近的UIViews中显示iPad2的前置和后置摄像头的流。 要流式传输一个设备的图像,我使用以下代码
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,但目前最多支持一个输入。
如何从两台相机流式传输任何有用的想法?
答案 0 :(得分:14)
可以从MacOS X上的多个视频设备获取CMSampleBufferRef
。您必须手动设置AVCaptureConnection
个对象。例如,假设您有这些对象:
AVCaptureSession *session;
AVCaptureInput *videoInput1;
AVCaptureInput *videoInput2;
AVCaptureVideoDataOutput *videoOutput1;
AVCaptureVideoDataOutput *videoOutput2;
NOT 添加如下输出:
[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 sample project。