Reputation: 1699
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
Upvotes: 5
Views: 3381
Reputation: 1527
Here's how you create an NSData object from a CMSampleBufferRef:
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;
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;
}
Upvotes: 0