From 3bc10cb0c3fd7d0d422d3e370da5d7cd7d6cf240 Mon Sep 17 00:00:00 2001 From: Dmitry Starkov <21260939+starkdmi@users.noreply.github.com> Date: Mon, 8 Jan 2024 21:25:27 +0300 Subject: [PATCH] Release the buffer --- .../CMSampleBuffer+Extensions.swift | 84 ++++++++++--------- Sources/Video.swift | 10 ++- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/Sources/Extensions/CMSampleBuffer+Extensions.swift b/Sources/Extensions/CMSampleBuffer+Extensions.swift index 456af375..886666e7 100644 --- a/Sources/Extensions/CMSampleBuffer+Extensions.swift +++ b/Sources/Extensions/CMSampleBuffer+Extensions.swift @@ -4,47 +4,49 @@ import CoreMedia internal extension CMSampleBuffer { /// Modify `CMSampleBuffer` by executing `PixelBufferProcessor` handler on`CVPixelBuffer` func editingPixelBuffer(_ handler: PixelBufferProcessor) -> CMSampleBuffer { - // Convert `CMSampleBuffer` to `CVPixelBuffer` - guard let pixelBuffer = CMSampleBufferGetImageBuffer(self) else { - return self + autoreleasepool { + // Convert `CMSampleBuffer` to `CVPixelBuffer` + guard let pixelBuffer = CMSampleBufferGetImageBuffer(self) else { + return self + } + + CVPixelBufferLockBaseAddress( + pixelBuffer, + CVPixelBufferLockFlags.readOnly) + + // Run custom pixel buffer modifier + let processedPixelBuffer = handler(pixelBuffer) + + CVPixelBufferUnlockBaseAddress( + pixelBuffer, + CVPixelBufferLockFlags.readOnly) + + // Convert `CVPixelBuffer` to `CMSampleBuffer` + var formatDescription: CMFormatDescription! + let createFormatDescriptionStatus = CMVideoFormatDescriptionCreateForImageBuffer( + allocator: kCFAllocatorDefault, + imageBuffer: processedPixelBuffer, + formatDescriptionOut: &formatDescription + ) + guard createFormatDescriptionStatus == noErr else { return self } + + // Recreate timing info + var timingInfo = CMSampleTimingInfo() + timingInfo.duration = CMSampleBufferGetDuration(self) + timingInfo.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(self) + timingInfo.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(self) + + // Create `CMSampleBuffer` + var sampleBuffer: CMSampleBuffer! + let createSampleBufferStatus = CMSampleBufferCreateReadyWithImageBuffer( + allocator: kCFAllocatorDefault, + imageBuffer: processedPixelBuffer, + formatDescription: formatDescription, + sampleTiming: &timingInfo, + sampleBufferOut: &sampleBuffer + ) + + return createSampleBufferStatus == noErr ? sampleBuffer : self } - - CVPixelBufferLockBaseAddress( - pixelBuffer, - CVPixelBufferLockFlags.readOnly) - - // Run custom pixel buffer modifier - let processedPixelBuffer = handler(pixelBuffer) - - CVPixelBufferUnlockBaseAddress( - pixelBuffer, - CVPixelBufferLockFlags.readOnly) - - // Convert `CVPixelBuffer` to `CMSampleBuffer` - var formatDescription: CMFormatDescription! - let createFormatDescriptionStatus = CMVideoFormatDescriptionCreateForImageBuffer( - allocator: kCFAllocatorDefault, - imageBuffer: processedPixelBuffer, - formatDescriptionOut: &formatDescription - ) - guard createFormatDescriptionStatus == noErr else { return self } - - // Recreate timing info - var timingInfo = CMSampleTimingInfo() - timingInfo.duration = CMSampleBufferGetDuration(self) - timingInfo.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(self) - timingInfo.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(self) - - // Create `CMSampleBuffer` - var sampleBuffer: CMSampleBuffer! - let createSampleBufferStatus = CMSampleBufferCreateReadyWithImageBuffer( - allocator: kCFAllocatorDefault, - imageBuffer: processedPixelBuffer, - formatDescription: formatDescription, - sampleTiming: &timingInfo, - sampleBufferOut: &sampleBuffer - ) - - return createSampleBufferStatus == noErr ? sampleBuffer : self } } diff --git a/Sources/Video.swift b/Sources/Video.swift index 5f5bccac..9067f066 100644 --- a/Sources/Video.swift +++ b/Sources/Video.swift @@ -892,11 +892,13 @@ public struct VideoTool { // Set custom processor if no frame rate adjustment required if let sampleBufferModifier = sampleBufferModifier { return { sample in - // Apply custom sample processor - let buffer = sampleBufferModifier(sample) + autoreleasepool { + // Apply custom sample processor + let buffer = sampleBufferModifier(sample) - // Append the new sample buffer to the input - variables.videoInput.append(buffer) + // Append the new sample buffer to the input + variables.videoInput.append(buffer) + } } } return nil