Cocoa touch 来自CALayer(AVPlayerLayer)的OpenGL纹理

Cocoa touch 来自CALayer(AVPlayerLayer)的OpenGL纹理,cocoa-touch,ios,opengl-es,calayer,avfoundation,Cocoa Touch,Ios,Opengl Es,Calayer,Avfoundation,我有一个AVPlayerLayer,我想用它创建一个OpenGL纹理。我对opengl纹理很满意,甚至对将CGImageRef转换为opengl纹理也很满意。在我看来,下面的代码应该可以工作,但我得到的只是纯黑色。我做错了什么?我需要先在CALayer/AVPlayerLayer上设置任何属性吗 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); int width = (int)[layer bounds].size.wid

我有一个AVPlayerLayer,我想用它创建一个OpenGL纹理。我对opengl纹理很满意,甚至对将CGImageRef转换为opengl纹理也很满意。在我看来,下面的代码应该可以工作,但我得到的只是纯黑色。我做错了什么?我需要先在CALayer/AVPlayerLayer上设置任何属性吗

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

int width = (int)[layer bounds].size.width;
int height = (int)[layer bounds].size.height;

CGContextRef context = CGBitmapContextCreate(NULL,
                                 width,
                                 height,
                                 8,
                                 width * 4,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedLast);

CGColorSpaceRelease(colorSpace);

if (context== NULL) {
    ofLog(OF_LOG_ERROR, "getTextureFromLayer: failed to create context 1");
    return;
}

[[layer presentationLayer] renderInContext:context];

CGImageRef cgImage = CGBitmapContextCreateImage(context);

int bytesPerPixel   = CGImageGetBitsPerPixel(cgImage)/8;
if(bytesPerPixel == 3) bytesPerPixel = 4;

GLubyte *pixels     = (GLubyte *) malloc(width * height * bytesPerPixel);

CGContextRelease(context);
context = CGBitmapContextCreate(pixels,
                                width,
                                height,
                                CGImageGetBitsPerComponent(cgImage),
                                width * bytesPerPixel,
                                CGImageGetColorSpace(cgImage),
                                kCGImageAlphaPremultipliedLast);

if(context == NULL) {
    ofLog(OF_LOG_ERROR, "getTextureFromLayer: failed to create context 2");
    free(pixels);
    return;
}

CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), cgImage);

int glMode;
switch(bytesPerPixel) {
    case 1:
        glMode = GL_LUMINANCE;
        break;
    case 3: 
        glMode = GL_RGB;
        break;
    case 4: 
    default:
        glMode = GL_RGBA; break;
}

if(texture.bAllocated() == false || texture.getWidth() != width || texture.getHeight() != height) {
    NSLog(@"getTextureFromLayer: allocating texture %i, %i\n", width, height);
    texture.allocate(width, height, glMode, true);
}

// test texture
//        for(int i=0; i<width*height*4; i++) pixels[i] = ofRandomuf() * 255;

texture.loadData(pixels, width, height, glMode);

CGContextRelease(context);
CFRelease(cgImage);
free(pixels);
初始化 拉环
我想这会对你的问题有所帮助。

谢谢,这是一种不同的方法,但我现在也尝试过。我总是从copyNextSampleBuffer返回NULL,我在问题中添加了新代码。我遗漏了什么吗?如果你的视频是HTTP直播格式,那么就没有简单的方法从中获取单个帧。你是在流FairPlay保护的HLS媒体吗?如果是这样,我认为试图获取对解密数据的引用在设计上是行不通的:
AVPlayer                *videoPlayer;
AVPlayerLayer           *videoLayer;
AVAssetReader           *videoReader;
AVAssetReaderTrackOutput*videoOutput;
    videoPlayer = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:videoPath.c_str()]]];

    if(videoPlayer == nil) {
        NSLog(@"videoPlayer == nil ERROR LOADING %s\n", videoPath.c_str());
    } else {
        NSLog(@"videoPlayer: %@", videoPlayer);
        videoLayer = [[AVPlayerLayer playerLayerWithPlayer:videoPlayer] retain];
        videoLayer.frame = [ThreeDView instance].bounds;
        // [[ThreeDView instance].layer addSublayer:videoLayer]; // test to see if it's loading and running

        AVAsset *asset = videoPlayer.currentItem.asset;
        NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
        NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], (NSString*)kCVPixelBufferPixelFormatTypeKey, nil];

        videoReader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
        videoOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:[tracks objectAtIndex:0] outputSettings:settings];
        [videoReader addOutput:videoOutput];
        [videoReader startReading];
    }
    if(videoPlayer == 0) {
        ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoPlayer == 0");
        return;
    }


    if(videoOutput == 0) {
        ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoOutput == 0");
        return;
    }

    CMSampleBufferRef sampleBuffer = [videoOutput copyNextSampleBuffer];

    if(sampleBuffer == 0) {
        ofLog(OF_LOG_ERROR, "Shot::drawVideo: sampleBuffer == 0, status: %i", videoReader.status);
        return;
    }


    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CFRelease(sampleBuffer);

    CVPixelBufferLockBaseAddress(imageBuffer,0); 

    unsigned char *pixels = ( unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer); 

    int width = CVPixelBufferGetWidth(imageBuffer); 
    int height = CVPixelBufferGetHeight(imageBuffer);

    if(videoTexture.bAllocated() == false || videoTexture.getWidth() != width || videoTexture.getHeight() != height) {
        NSLog(@"Shot::drawVideo() allocating texture %i, %i\n", width, height);
        videoTexture.allocate(width, height, GL_RGBA, true);
    }

    videoTexture.loadData(pixels, width, height, GL_BGRA);

    CVPixelBufferUnlockBaseAddress(imageBuffer,0);