Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How AVSampleBufferDisplayLayer displays H.264

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. ;-)

like image 980
Karsten Avatar asked Sep 25 '14 09:09

Karsten


2 Answers

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.

like image 82
Karsten Avatar answered Nov 19 '22 07:11

Karsten


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.

like image 28
Md1079 Avatar answered Nov 19 '22 06:11

Md1079