AVSampleBufferDisplayLayer如何显示H.264

我想分享一下我在一些日子里所学到的知识。 没有很多发现。

我仍然对这个声音感到兴奋。 欢迎提出意见和build议。 😉

在这里我的代码片段。 申报

@property (nonatomic, retain) AVSampleBufferDisplayLayer *videoLayer; 

首先设置video层

 self.videoLayer = [[AVSampleBufferDisplayLayer alloc] init]; self.videoLayer.bounds = self.bounds; self.videoLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); self.videoLayer.videoGravity = AVLayerVideoGravityResizeAspect; self.videoLayer.backgroundColor = [[UIColor greenColor] CGColor]; //set Timebase CMTimebaseRef controlTimebase; CMTimebaseCreateWithMasterClock( CFAllocatorGetDefault(), CMClockGetHostTimeClock(), &controlTimebase ); self.videoLayer.controlTimebase = controlTimebase; CMTimebaseSetTime(self.videoLayer.controlTimebase, CMTimeMake(5, 1)); CMTimebaseSetRate(self.videoLayer.controlTimebase, 1.0); // connecting the videolayer with the view [[self layer] addSublayer:_videoLayer]; 

将video数据提供给图层

 __block AVAssetReaderTrackOutput *outVideo = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:video outputSettings:dic]; if( [assetReaderVideo startReading] ) { [_videoLayer requestMediaDataWhenReadyOnQueue: assetQueue usingBlock: ^{ while( [_videoLayer isReadyForMoreMediaData] ) { CMSampleBufferRef *sampleVideo = [outVideo copyNextSampleBuffer]; [_videoLayer enqueueSampleBuffer:sampleVideo.data]; } }]; } 

更多详情:WWDC 2014会议513是非常丰富的。

我正在尝试,但发现AVSampleBufferDisplay图层上没有图像。

我从原始字节stream创buildNALUnits,并使用以下命令传递IDR和Non-IDR切片:

 if ([avLayer isReadyForMoreMediaData]) { [avLayer enqueueSampleBuffer:sampleBuffer]; } 

EnqueueSampleBuffer没有返回错误状态,所以很难找出错误的地方。