Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - get kCMSampleBufferAttachmentKey_DroppedFrameReason from CMSampleBuffer

I'm trying to understand why my AVCaptureOutput is dropping frames. In the captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) delegate method, I get a CMSampleBuffer that should contains an attachement explaining the reason the frame was dropped (doc)

The reason is expected to be one of those CFString:

kCMSampleBufferDroppedFrameReason_FrameWasLate // "FrameWasLate"
kCMSampleBufferDroppedFrameReason_OutOfBuffers // "OutOfBuffers"
kCMSampleBufferDroppedFrameReason_Discontinuity // "Discontinuity"

From the docs it's really not clear how to get this value. I've tried using CMGetAttachment but this returns a CMAttachmentMode aka UInt32:

func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
  var reason: CMAttachmentMode = 0
  CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_DroppedFrameReason, &reason)
  print("reason \(reason)") // 1
}

and I don't really know how to match this UInt32 to the CFString constant

like image 230
Guig Avatar asked Jul 26 '17 20:07

Guig


1 Answers

I was stupidly not looking at the right output:

var mode: CMAttachmentMode = 0
let reason = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_DroppedFrameReason, &mode)
print("reason \(String(describing: reason))") // Optional(OutOfBuffers)
like image 104
Guig Avatar answered Nov 09 '22 23:11

Guig