Scale CMSampleBuffer Size Without Exceeding the Memory Limit Of Broadcast Extension

I am developing an app that sends pixel buffers from the Broadcast Upload Extension to OpenTok. When I run my broadcast extension it hits its memory limit in seconds. I have been looking for ways to reduce the size and scale of CMSampleBuffers and ended the process by first converting them to CIImage, then scaling them, and then converting them to CVPixelBuffers for sending OpenTok Servers. Unfortunately, the extension still crashes, even though I tried to reduce the pixel buffer. My code follows:

First I convert the CMSampleBuffer to CVPixelBuffer in processSampleBuffer function from Sample Handler then pass CVPixelBuffer to my function along with timestamps. Here I convert the CVPixelBuffer to cIImage and scale it using cIFilter(CILanczosScaleTransform). After that, I generate Pixel Buffer from CIImage using PixelBufferPool and cIContext and then send the new buffer to OpenTok Servers using videoCaptureConsumer.

    func processPixelBuffer(pixelBuffer:CVPixelBuffer, timeStamp ts:CMTime) {
    
    guard  let ciImage = self.scaleFilterImage(inputImage: pixelBuffer.cmIImage, withAspectRatio: 1.0, scale: CGFloat(kVideoFrameScaleFactor)) else {return}
    

    if self.pixelBufferPool == nil ||
        self.pixelBuffer?.size != pixelBuffer.size{
        
        self.destroyPixelBuffers()
        self.updateBufferPool(newWidth: Int(ciImage.extent.size.width), newHeight: Int(ciImage.extent.size.height))
        
        guard CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, self.pixelBufferPool, &self.pixelBuffer) == kCVReturnSuccess
        else {return}
        
    }
    
    context?.render(ciImage, to:pixelBuffer)
    
    self.videoCaptureConsumer?.consumeImageBuffer(pixelBuffer,
                                                  orientation:.up,
                                                  timestamp:ts,
                                                  metadata:nil)
    
}

If the pixelBufferPool is nil or there is a change in the size of the pixelBuffer I update the pool.

 private func updateBufferPool(newWidth: Int, newHeight: Int) {
    
    let pixelBufferAttributes: [String: Any] = [
        kCVPixelBufferPixelFormatTypeKey as String: UInt(self.videoFormat),
            kCVPixelBufferWidthKey as String: newWidth,
            kCVPixelBufferHeightKey as String: newHeight,
            kCVPixelBufferIOSurfacePropertiesKey as String: [:]
        ]

    
    CVPixelBufferPoolCreate(nil,nil, pixelBufferAttributes as NSDictionary?, &pixelBufferPool)
}

This is the function I use to scale the cIImage:

func scaleFilterImage(inputImage:CIImage, withAspectRatio aspectRatio:CGFloat, scale:CGFloat) -> CIImage? {
    scaleFilter?.setValue(inputImage, forKey:kCIInputImageKey)
    scaleFilter?.setValue(scale, forKey:kCIInputScaleKey)
    scaleFilter?.setValue(aspectRatio, forKey:kCIInputAspectRatioKey)
    return scaleFilter?.outputImage
}
  • My question is why it still keeps crashing and is there another way to reduce the CVPixelBuffer size without causing a memory limit crash?

I would appreciate any help on this. Swift or Objective - C, I am open to all suggestions.

Hi, did you figure out a solution? If so, would you be able to share it?

Scale CMSampleBuffer Size Without Exceeding the Memory Limit Of Broadcast Extension
 
 
Q