I am using AVCaptureDataOutputSynchronizerDelegate
to handle capturing data for video, depth and metadata
private let videoDataOutput = AVCaptureVideoDataOutput()
private let depthDataOutput = AVCaptureDepthDataOutput()
private let metadataOutput = AVCaptureMetadataOutput()
So using the below code, I am able to get specifically video data within the delegate method used from AVCaptureDataOutputSynchronizerDelegate
.
func dataOutputSynchronizer(_ synchronizer: AVCaptureDataOutputSynchronizer, didOutput synchronizedDataCollection: AVCaptureSynchronizedDataCollection) {
guard let syncedVideoData = synchronizedDataCollection.synchronizedData(for: self.videoDataOutput) as? AVCaptureSynchronizedSampleBufferData else { return }
The problem is, when I try to save the videoData into an array as below, I get a OutOfBuffers error. This problem persists if I try to save the videoData/the imagery associated/anything related to this data.
let array:[CMSampleBuffer] = []
...
array.append(syncedVideoData)
//Gets to about 5-6 sets of data, then it runs out of buffers.
//I think the buffer is being retained permanently since I am saving to a global variable here.
//Leading to out of buffer error
So, what I am thinking is happening is that since I am saving any related data into an array, it is keeping the data in the buffer in memory whereas it is normally released.
The webpage linked earlier for OutOfBuffers indicates that I can
If you need to perform extended processing of captured data, copy that data into buffers whose lifetimes you manage instead of relying on buffers vended by the capture output.
I attempted to create a new CMSampleBuffer
extension VideoCapture: AVCaptureDataOutputSynchronizerDelegate {
func dataOutputSynchronizer(_ synchronizer: AVCaptureDataOutputSynchronizer, didOutput synchronizedDataCollection: AVCaptureSynchronizedDataCollection) {
var newData:CMSampleBuffer?
guard let syncedVideoData = synchronizedDataCollection.synchronizedData(for: self.videoDataOutput) as? AVCaptureSynchronizedSampleBufferData else { return }
guard !syncedVideoData.sampleBufferWasDropped else {
print(syncedVideoData.droppedReason.rawValue)
return
}
let videoSampleBuffer = syncedVideoData.sampleBuffer
CMSampleBufferCreateCopy(allocator: kCFAllocatorDefault, sampleBuffer: videoSampleBuffer, sampleBufferOut: &newData)
if(newData != nil) {
self.buffer.append(newData!)
}
}
but this causes the same issues -- the videoData is still staying in the buffer. I get to about 5-6 sets of videoData and then I get no more datums.
Any guidance on how to "copy that data into buffers whose lifetimes you manage instead of relying on buffers vended by the capture output." as indicated on the outOfBuffers website?