Skip to content

Commit

Permalink
Release the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
starkdmi committed Jan 8, 2024
1 parent b962626 commit 3bc10cb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
84 changes: 43 additions & 41 deletions Sources/Extensions/CMSampleBuffer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 6 additions & 4 deletions Sources/Video.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3bc10cb

Please sign in to comment.