我可以使用AVFoundation将下载的视频帧流式传输到OpenGL ES纹理中吗?

Mat*_*ing 20 streaming opengl-es objective-c avfoundation ios

我已经能够使用AVFoundation的AVAssetReader类将视频帧上传到OpenGL ES纹理中.但是,它有一个警告,因为当它与AVURLAsset指向远程媒体一起使用时会失败.这个失败没有很好的记录,我想知道是否有任何解决方法的缺点.

Mat*_*ing 31

我已经能够使用iOS 6发布的一些API来简化这个过程.它根本不使用AVAssetReader,而是依赖于一个叫做的类AVPlayerItemVideoOutput.可以AVPlayerItem通过新-addOutput:方法将此类的实例添加到任何实例.

AVAssetReader此类不同,此类对于AVPlayerItem由遥控器支持的s 可以正常工作AVURLAsset,并且还具有允许更复杂的回放接口(支持非线性回放)的优点-copyPixelBufferForItemTime:itemTimeForDisplay:(而不是AVAssetReader严格限制的-copyNextSampleBuffer方法.


示例代码

// Initialize the AVFoundation state
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:someUrl options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{

    NSError* error = nil;
    AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:&error];
    if (status == AVKeyValueStatusLoaded)
    {
        NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] };
        AVPlayerItemVideoOutput* output = [[[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings] autorelease];
        AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
        [playerItem addOutput:[self playerItemOutput]];
        AVPlayer* player = [AVPlayer playerWithPlayerItem:playerItem];

        // Assume some instance variable exist here. You'll need them to control the
        // playback of the video (via the AVPlayer), and to copy sample buffers (via the AVPlayerItemVideoOutput).
        [self setPlayer:player];
        [self setPlayerItem:playerItem];
        [self setOutput:output];
    }
    else
    {
        NSLog(@"%@ Failed to load the tracks.", self);
    }
}];

// Now at any later point in time, you can get a pixel buffer
// that corresponds to the current AVPlayer state like this:
CVPixelBufferRef buffer = [[self output] copyPixelBufferForItemTime:[[self playerItem] currentTime] itemTimeForDisplay:nil];
Run Code Online (Sandbox Code Playgroud)

获得缓冲区后,可以根据需要将其上传到OpenGL.我推荐的可怕记录的CVOpenGLESTextureCacheCreateTextureFromImage()功能,因为你会在所有的新设备,这是得到硬件加速的的速度比glTexSubImage2D().有关示例,请参阅Apple的GLCameraRippleRosyWriter演示.