I'm trying to implement a vidoestream for a multipeer connectivity app. The captured frame will be compressed by VTCompressionSession and my callback is being called.
Now my CMSamplebuffer contains a CMBlockBuffer and i could extract the NALUs etc. like mentioned in this answer How to use VideoToolbox to decompress H.264 video stream but i'm searching for a different way to do it.
Is it possible to write the whole bytes of a CMSampleBuffer to an UInt8 Array? I'm able to get the Pointer of the CMSampleBuffer but how can i figure out the length?
Sorry for my bad english and let me know if anythings wrong, it´s my first post. Thank You
Here is the code I used
private func bufferToUInt(sampleBuffer: CMSampleBuffer) -> [UInt8] {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0))
let byterPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
let height = CVPixelBufferGetHeight(imageBuffer)
let srcBuff = CVPixelBufferGetBaseAddress(imageBuffer)
let data = NSData(bytes: srcBuff, length: byterPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0))
return [UInt8].init(repeating: 0, count: data.length / MemoryLayout<UInt8>.size)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With