How AVSampleBufferDisplayLayer displays H.264

不羁的心 提交于 2019-12-18 10:26:27

问题


I want to share my knowledge which I worked out in some days about it. There isnt a lot to find about it.

I am still fizzeling about the sound. Comments and tips are welcomed. ;-)


回答1:


here my code snippets. Declare it

@property (nonatomic, retain) AVSampleBufferDisplayLayer *videoLayer;

at first setup the video layer

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

providing the video data to the layer

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

For further details: the Session 513 in WWDC 2014 is very informative.




回答2:


I am attempting this but finding that there is no image on the AVSampleBufferDisplay Layer.

I create the NALUnits from a raw byte stream and pass the IDR and Non-IDR slices using:

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

There is no error status returned from EnqueueSampleBuffer so can be difficult to find out where it is going wrong.



来源:https://stackoverflow.com/questions/26035380/how-avsamplebufferdisplaylayer-displays-h-264

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!