CVMetalTextureCacheCreateTextureFromImage在macOS 10.13上返回-6660 [英] CVMetalTextureCacheCreateTextureFromImage returns -6660 on macOS 10.13
问题描述
我正在将屏幕从iPhone设备记录到Mac.作为预览层,我直接从AVCaptureVideoDataOutput
收集样本缓冲区,从中创建纹理并使用Metal
渲染它们.我遇到的问题是在更新到10.13
之后在10.13
之前在macOS中工作的代码停止了工作.即
I'm recording the screen from my iPhone device to my Mac. As a preview layer I am collecting sample buffers directly from a AVCaptureVideoDataOutput
, from which I'm creating textures and rendering them with Metal
. The problem I'm having is that code that worked in macOS prior to 10.13
stopped working after updating to 10.13
. Namely,
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_currentSampleBuffer);
if (!imageBuffer) return;
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVMetalTextureRef metalTexture = NULL;
CVReturn result = CVMetalTextureCacheCreateTextureFromImage(nil,
self.textureCache,
imageBuffer,
nil,
self.pixelFormat,
width,
height,
0,
&metalTexture);
if (result == kCVReturnSuccess) {
self.texture = CVMetalTextureGetTexture(metalTexture);
}
返回result = -6660
,它转换为通用的kCVReturnError
,如
Returns result = -6660
, which translates to a generic kCVReturnError
, as can be seen on the official Apple docs, and the metalTexture = NULL
.
我正在使用的像素格式为MTLPixelFormatBGRG422
,因为来自相机的样本为2vuy
.
The pixel format I'm using is MTLPixelFormatBGRG422
, since the samples coming from camera are 2vuy
.
作为从sampleBuffer
创建metalTexture
的解决方法,我现在
像这样创建一个中间NSImage
:
As a workaround to creating metalTexture
from sampleBuffer
, I am now
creating an intermediate NSImage
like so:
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(_currentSampleBuffer);
NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:[CIImage imageWithCVImageBuffer:imageBuffer]];
NSImage *image = [[NSImage alloc] initWithSize:[imageRep size]];
[image addRepresentation:imageRep];
并由此创建一个MTLTexture
.显然,这是直接使用CVMetalTextureCacheCreateTextureFromImage
的较差解决方案.
and creating a MTLTexture
from that. That is obviously a subpar solution to using CVMetalTextureCacheCreateTextureFromImage
directly.
再次,有问题的代码在macOS < 10.13
中可以正常工作,我想知道是否有人遇到类似问题,如果有,您是否有解决此问题的想法?
Once again, the code in question works perfectly fine in macOS < 10.13
, I'd like to know if anyone has similar issues, and if so, do you have any ideas how to overcome this?
推荐答案
我遇到了相同的问题,该问题不是在配置AVCaptureVideoDataOutput
时要求Metal兼容性.我想系统开始在macOS 10.13中进行检查,可能会在不要求的情况下进行一些优化.
I've come across the same issue, the problem was not asking for Metal compatibility when configuring the AVCaptureVideoDataOutput
. I guess the system started to check this in macOS 10.13, possibly to apply some optimization when not requested.
解决方案是将kCVPixelBufferMetalCompatibilityKey
添加到AVCaptureVideoDataOutput
的videoSettings
属性.
The solution was to add the kCVPixelBufferMetalCompatibilityKey
to the videoSettings
property of AVCaptureVideoDataOutput
.
在Objective-C中:
In Objective-C:
outputCapture.videoSettings = @{
/* ... */
(NSString *)kCVPixelBufferMetalCompatibilityKey: @YES
};
在Swift中:
outputCapture.videoSettings = [
/* ... */
kCVPixelBufferMetalCompatibilityKey as String: true
]
我认为这很有必要,要求Apple至少在发生这种情况时打印警告消息.我会更新的.
I think this warrants a radar, to ask Apple to at least print a warning message when this occurs. I'll update this if I get to it.
这篇关于CVMetalTextureCacheCreateTextureFromImage在macOS 10.13上返回-6660的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!