Found an answer - CVPixelBufferCreate isn't that efficient so it runs on a few frames and then crashes.
You can include attributes using a CVPixelBufferPool, which apparently is designed for streaming frames and managing the buffers in memory
Also realized the format for the attributes dictionary was wrong
private var colorPixelBufferPool: CVPixelBufferPool!
private var pixelBufferPool_isPrepared = false
...
// within delegate
if !pixelBufferPool_isPrepared {
var videoDescription: CMFormatDescription
do {
videoDescription = try CMVideoFormatDescription(imageBuffer: videoBuffer)
} //videoBuffer is the original CVPixelBuffer
catch {
print("Failed video description")
return
}
let inputDimensions = CMVideoFormatDescriptionGetDimensions(videoDescription)
let outputPixelBufferAttributes: [String: Any] = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferWidthKey as String: Int(inputDimensions.height), // rotated 90 degrees
kCVPixelBufferHeightKey as String: Int(inputDimensions.width), // rotated 90 degrees
kCVPixelBufferIOSurfaceOpenGLESTextureCompatibilityKey as String: true,
kCVPixelBufferIOSurfacePropertiesKey as String: [:]
]
let poolAttributes = [kCVPixelBufferPoolMinimumBufferCountKey as String: 2]
var cvPixelBufferPool: CVPixelBufferPool?
// Create a pixel buffer pool with the same pixel attributes as the input format description
CVPixelBufferPoolCreate(kCFAllocatorDefault, poolAttributes as NSDictionary?, outputPixelBufferAttributes as NSDictionary?, &cvPixelBufferPool)
guard let pixelBufferPool = cvPixelBufferPool else {
assertionFailure("Allocation failure: Could not create pixel buffer pool")
return
}
colorPixelBufferPool = pixelBufferPool
pixelBufferPool_isPrepared = true
}
...
// let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(rotatedBuffer.width), Int(rotatedBuffer.height), kCVPixelFormatType_32BGRA, attributes, &colorBuffer) // throws error eventually, not efficient
let status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, colorPixelBufferPool!, &colorBuffer) // works!
Topic:
Media Technologies
SubTopic:
Video
Tags: