How AVSampleBufferDisplayLayer displays H.264

2019-01-31 22:04发布

问题:

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.