Question

Hello I need to send a CMSampleBufferRef over a network. The client then plays the CMSampleBufferRef via the Audio Queue Services. I have seen some examples on stack overflow, but most of them just send the buffer. But then some informations are lost. I have found at that [AVAssetReaderOutput copyNextSampleBuffer] returns a reference to a opaqueCMSampleBuffer struct. I know how to get the memory address of the opaqueCMSampleBuffer, but how do I copy the content of the address to a array so I can send it over the network? Or are there any more elegant approaches to send the CMSampleBuffer over the network. Or can I even somehow access the opaqueCMSampleBuffer?

thanks for your time and help

No correct solution

OTHER TIPS

Here's how you create an NSData object from a CMSampleBufferRef:

  1. In the interface (.h) file, add the sample buffer reference as a property, casting to an object at the same time:

    @property (nonatomic, strong) __attribute__((NSObject)) CMSampleBufferRef sampleBuffer;

  2. In the implementation file (.m):

    CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)[(AVAssetReaderTrackOutput *)[assetReader outputs][0] copyNextSampleBuffer];

    NSPurgeableData *sampleBufferData = (NSPurgeableData *)[self imageToBuffer:sampleBuffer];

To access the sample buffer from the NSData object, simply use a cast:

(CMSampleBufferRef)sampleBufferData;

Here's another way:

- (NSData *) imageToBuffer:(CMSampleBufferRef)source {
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(source);
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    void *src_buff = CVPixelBufferGetBaseAddress(imageBuffer);

    NSData *data = [NSData dataWithBytes:src_buff length:bytesPerRow * height];

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    return data;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top