Search code examples
iosobjective-cavfoundationavplayercmsamplebufferref

How can we use AVSampleBufferDisplayLayer to render CMSampleBufferRef?


I have this delegate method

-(void)airPlayServer:(id)server sampleBufferReceived:(CMSampleBufferRef)sampleBuffer
{
}

which gives me sampleBuffer.

Now I need to know how can I use AVSampleBufferDisplayLayer to render my sampleBuffer. I know we have to use - enqueueSampleBuffer - but I am new to iOS so how can we do it?

I don't want to convert sampleBuffer to CGImage and then draw it.

Code example is highly appreciated :)


Solution

  • Like so:

       CMSampleBufferRef sampleBufferRef = ...;
    
        // Force display as soon as possible
    
        CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBufferRef, YES);
        CFMutableDictionaryRef dict = (CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0);
        CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue);
    
        [sampleBufferLayer enqueueSampleBuffer:sampleBufferRef];            
        [sampleBufferLayer setNeedsDisplay];