AVSampleBufferDisplayLayer如何显示H.264

时间:2014-09-25 09:44:20

标签: ios video-streaming h.264

我想分享我在某些日子里制定的知识。关于它没有太多可以找到。

我仍然对声音感到震惊。欢迎提出意见和建议。 ; - )

2 个答案:

答案 0 :(得分:14)

这里是我的代码片段。声明它

@property (nonatomic, retain) AVSampleBufferDisplayLayer *videoLayer;

首先设置视频图层

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];

将视频数据提供给图层

__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];
        }
    }];
}

有关详细信息:2014年WWDC会议513非常有用。

答案 1 :(得分:2)

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

我从原始字节流创建NALUnits,并使用以下命令传递IDR和非IDR切片:

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

EnqueueSampleBuffer没有返回任何错误状态,因此很难找出它出错的地方。