我试图了解为什么我的AVCaptureOutput丢帧。在captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)委托方法中,我得到一个CMSampleBuffer,该代码应包含解释帧被丢弃的原因的附件(doc)

原因可能是那些CFString之一:

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

从文档中,实际上还不清楚如何获得此值。我尝试使用CMGetAttachment,但这会返回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
}

而且我真的不知道如何将该UInt32与CFString常量匹配

最佳答案

我愚蠢地没有看正确的输出:

var mode: CMAttachmentMode = 0
let reason = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_DroppedFrameReason, &mode)
print("reason \(String(describing: reason))") // Optional(OutOfBuffers)

10-08 03:51