Created
July 12, 2016 20:52
-
-
Save bennyty/bcd298b5cb9c4d5d3b8a344e4d282b0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let duration = CMTime( value:10, timescale:600, flags: CMTimeFlags(rawValue: 0), epoch:0) | |
let presentationTimeStamp = CMTime( value:0, timescale:600, flags:CMTimeFlags(rawValue: 0), epoch:0) | |
var timingInfo = CMSampleTimingInfo( duration:duration, presentationTimeStamp:presentationTimeStamp, decodeTimeStamp:kCMTimeInvalid ) | |
// let potentialBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer) | |
// var imageBufferCopy = UnsafeMutablePointer<CVImageBuffer>.alloc(1) | |
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { | |
fatalError("Sample Buffer did not contain a pixel buffer") | |
} | |
// Get pixel buffer info | |
CVPixelBufferLockBaseAddress(pixelBuffer, 0); | |
let bufferWidth = CVPixelBufferGetWidth(pixelBuffer); | |
let bufferHeight = CVPixelBufferGetHeight(pixelBuffer); | |
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); | |
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer); | |
// Copy the pixel buffer | |
var pixelBufferCopy: CVPixelBuffer? | |
let status = CVPixelBufferCreate(kCFAllocatorDefault, bufferWidth, bufferHeight, CVPixelBufferGetPixelFormatType(pixelBuffer), nil, &pixelBufferCopy); | |
guard status == 0 else { | |
fatalError("Failed to create CVPixelBuffer for cloning. Error code: \(status)") | |
} | |
CVPixelBufferLockBaseAddress(pixelBufferCopy!, 0); | |
let copyBaseAddress = CVPixelBufferGetBaseAddress(pixelBufferCopy!); | |
memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow); | |
// let copy = NSData(bytes: baseAddress, length: bufferHeight * bytesPerRow) | |
// memcpy(copyBaseAddress, copy.bytes, bufferHeight * bytesPerRow); | |
print(pixelBuffer) | |
print("hi") | |
print(pixelBufferCopy) | |
let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) | |
// let formatDescription = CMFormatDescription() | |
var output: CMSampleBuffer? | |
// let outputPointer = UnsafeMutablePointer<CMSampleBuffer?>.alloc(1) | |
let err: OSStatus | |
// var sampleSize: Int = CMBlockBufferGetDataLength(blockBuffer) | |
err = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufferCopy!, true, nil, nil, formatDescription!, &timingInfo, &output) | |
// err = CMSampleBufferCreateima( kCFAllocatorDefault, blockBuffer, true, nil, nil, formatDescription, 1, 1, &timingInfo, 1, &sampleSize, &output ) | |
guard err == 0 else { | |
fatalError("Error creating sample buffer for copying data from camera. Error code: \(err)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I noted that
Copy the pixel buffer
will make slower, just make a create and do not copy any would be better