您的位置:首页 > 移动开发 > IOS开发

IOS硬件解码VTDecompressionSession失效

2016-01-18 02:06 489 查看

IOS硬件解码VideoToolbox在应用中进入后台VTDecompressionSession失效解决办法

Author:baohonglai

Email:baohonglai@gmail.com

版权所有。转载本BLOG内任何文章,请以超链接形式注明出处。

*前段时间在IOS上用VideoToolbox进行视频播放器硬件解码时遇到一个问题,就是播放器进入进入到后台后再切换回来会导致VTDecompressionSession直接失效,这个问题纠结了很久,后来终于找到解决办法了,直接重新初始化session,下面展示一段示例代码。

参考链接

http://www.zhihu.com/question/20692215

引用了stevenyao/iOSHardwareDecoder · GitHub的代码,这段代码里面没有对session失效了进行处理,我在里面加了下面这段处理,就可以了。

- (void)resetH264Decoder
{
if(_deocderSession) {
VTDecompressionSessionInvalidate(_deocderSession);
CFRelease(_deocderSession);
_deocderSession = NULL;
}
CFDictionaryRef attrs = NULL;
const void *keys[] = { kCVPixelBufferPixelFormatTypeKey };
//      kCVPixelFormatType_420YpCbCr8Planar is YUV420
//      kCVPixelFormatType_420YpCbCr8BiPlanarFullRange is NV12
uint32_t v = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
const void *values[] = { CFNumberCreate(NULL, kCFNumberSInt32Type, &v) };
attrs = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

VTDecompressionOutputCallbackRecord callBackRecord;
callBackRecord.decompressionOutputCallback = didDecompress;
callBackRecord.decompressionOutputRefCon = NULL;
if(VTDecompressionSessionCanAcceptFormatDescription(_deocderSession, _decoderFormatDescription))
{
NSLog(@"yes");
}

OSStatus status = VTDecompressionSessionCreate(kCFAllocatorSystemDefault,
_decoderFormatDescription,
NULL, attrs,
&callBackRecord,
&_deocderSession);
CFRelease(attrs);
}


然后在遇到session失效时调用

if(decodeStatus == kVTInvalidSessionErr)
{
NSLog(@"IOS8VT: Invalid session, reset decoder session");
[self resetH264Decoder];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: