gpt4 book ai didi

objective-c - 运行多个 AVCaptureSession 或添加多个输入

转载 作者:太空狗 更新时间:2023-10-30 03:18:08 24 4
gpt4 key购买 nike

我想在两个相邻的 UIView 中显示 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];

这对任一相机都适用。为了并行显示流,我尝试创建另一个 session ,但是一旦建立第二个 session ,第一个 session 就会卡住。

然后我尝试向 session 添加两个 AVCaptureDeviceInput,但目前似乎最多支持一个输入。

有什么有用的想法可以从两个摄像头进行流式传输吗?

最佳答案

可以从 MacOS X 上的多个视频设备获取 CMSampleBufferRef。您必须设置 AVCaptureConnection手动对象。例如,假设您有这些对象:

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

不要像这样添加输出:

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

相反,添加它们并告诉 session 不要建立任何连接:

[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;
}
}

最后,确保为两个输出设置示例缓冲区委托(delegate):

[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有关组合来自多个视频设备的实时预览的示例。

关于objective-c - 运行多个 AVCaptureSession 或添加多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com