I spent a couple of days trying to figure this out but no luck.
I'm trying to render camera feed to OpenGL texture. I managed to get it working without using CVOpenGLESTextureCacheCreateTextureFromImage, simply by creating CGImage out of sampleBuffer. I got some pretty bad performances and I need to use the cache.
As reference I used RosyWriter example. I changed stuff around dozen of times but this is what I got right now which to me looks like just the example but more simplified.
I won't paste AV setup code (if need be I can). Here's the delegate method implementation:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
size_t frameWidth = CVPixelBufferGetWidth(pixelBuffer);
size_t frameHeight = CVPixelBufferGetHeight(pixelBuffer);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
videoTextureCache,
pixelBuffer,
NULL,
GL_TEXTURE_2D,
GL_RGBA,
frameWidth,
frameHeight,
GL_BGRA,
GL_UNSIGNED_BYTE,
0,
&texture);
if (!texture || err) {
NSLog(@"CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
return;
}
}
As you can see I dropped the preview queue (tried using it, didn't work out) and anything that I thought was unnecessary.
Properties passed to the function look ok (pixelBuffer has iosurface, frameWidth/Height are correct).
CVOpenGLESTextureCacheCreateTextureFromImage logs
"Failed to create IOSurface image (texture)"
and returns an error
CVOpenGLESTextureCacheCreateTextureFromImage failed (error: -6683)
As I said I tried alot of things, had different errors and sometimes just had a black texture.
I'm just starting in OpenGL so it might be something obvious, in any case it would be great if someone took a look at the issue. I pasted the code I thought I was relevant, if there's need for anything else I'll update.
UPDATE
After adding this line to the AV output setup, I stopped receiving errors but instead got a blank black texture. I suspected it could have something with color range/format, any ideas?
[output setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];