原文:http://blog.5ibc.net/p/33817.html
编码、解码失效都可以restart一下。
IOS硬件解码VideoToolbox在应用中进入后台VTDecompressionSession失效解决办法
*前段时间在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]; }