Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMSampleBuffer to byte-array in Swift

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

like image 653
JeWol Avatar asked Oct 18 '22 15:10

JeWol


1 Answers

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)
}
like image 112
Arek Avatar answered Oct 21 '22 05:10

Arek