VideoToolBox 编码图像显示在 Yosemite OSX 中闪烁
VideoToolBox Decoding image display is flickring in Yosemite OSX
我使用了videoToolBox来编码和解码数据,
所以一旦我们直接获得解码数据,我们就会传递给 AVSampleBufferDisplayLayer
这是代码片段
-(CVPixelBufferRef)decode:(AVPacket*)vp {
CVPixelBufferRef outputPixelBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
(void*)vp->data, vp->size,
kCFAllocatorNull,
NULL, 0, vp->size,
0, &blockBuffer);
//NSLog(@"status is for CMBlockBufferCreateWithMemoryBlock is %d",(int)status);
if(status == kCMBlockBufferNoErr) {
CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {vp->size};
status = CMSampleBufferCreateReady(kCFAllocatorDefault,
blockBuffer,
formatDesc ,
1, 0, NULL, 1, sampleSizeArray,
&sampleBuffer);
// NSLog(@"status is for formatdesc is %d",(int)status);
if (status == kCMBlockBufferNoErr && sampleBuffer) {
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, YES);
CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
[displayLayer flush];
if([displayLayer isReadyForMoreMediaData])
{
[displayLayer enqueueSampleBuffer:sampleBuffer];
}
[displayLayer setNeedsDisplay];
[self performSelectorOnMainThread:@selector(setView:) withObject:[[RPCViewerAppInitializer sharedRPCViewerAppInitializer] rpcView] waitUntilDone:NO];
CFRelease(blockBuffer);
CFRelease(sampleBuffer);
}
}
return outputPixelBuffer;
}
以上代码在 10.10 中运行良好 osx 但在 10.10 中我的屏幕出现闪烁。
任何帮助都将不胜感激。
这个问题已经解决:
我将 AVSampleBufferDisplayLayer 的 controlTimebase 属性 设置为 nil,这是原因,现在我根本不设置这个 属性
我使用了videoToolBox来编码和解码数据,
所以一旦我们直接获得解码数据,我们就会传递给 AVSampleBufferDisplayLayer
这是代码片段
-(CVPixelBufferRef)decode:(AVPacket*)vp {
CVPixelBufferRef outputPixelBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
(void*)vp->data, vp->size,
kCFAllocatorNull,
NULL, 0, vp->size,
0, &blockBuffer);
//NSLog(@"status is for CMBlockBufferCreateWithMemoryBlock is %d",(int)status);
if(status == kCMBlockBufferNoErr) {
CMSampleBufferRef sampleBuffer = NULL;
const size_t sampleSizeArray[] = {vp->size};
status = CMSampleBufferCreateReady(kCFAllocatorDefault,
blockBuffer,
formatDesc ,
1, 0, NULL, 1, sampleSizeArray,
&sampleBuffer);
// NSLog(@"status is for formatdesc is %d",(int)status);
if (status == kCMBlockBufferNoErr && sampleBuffer) {
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, YES);
CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
[displayLayer flush];
if([displayLayer isReadyForMoreMediaData])
{
[displayLayer enqueueSampleBuffer:sampleBuffer];
}
[displayLayer setNeedsDisplay];
[self performSelectorOnMainThread:@selector(setView:) withObject:[[RPCViewerAppInitializer sharedRPCViewerAppInitializer] rpcView] waitUntilDone:NO];
CFRelease(blockBuffer);
CFRelease(sampleBuffer);
}
}
return outputPixelBuffer;
}
以上代码在 10.10 中运行良好 osx 但在 10.10 中我的屏幕出现闪烁。
任何帮助都将不胜感激。
这个问题已经解决:
我将 AVSampleBufferDisplayLayer 的 controlTimebase 属性 设置为 nil,这是原因,现在我根本不设置这个 属性