AVCaptureSession视频稳定滞后

时间:2019-03-15 21:21:45

标签: ios swift avcapturesession

以前,我是在AVCaptureMovieFileOutput上添加AVCaptureSession并启用电影级preferredVideoStabilizationMode,收到了不错的预期效果。

我刚切换到AVCaptureVideoDataOutput,并且电影的稳定导致了非常明显的延迟-在图像预览更新之前大约整整一秒的延迟。

设置:

//  Video data output
let outputSettings: [String: Any] = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]
videoDataOutput = AVCaptureVideoDataOutput()
videoDataOutput?.videoSettings = outputSettings
let videoSessionQueue = DispatchQueue(label: "video_capture_session_queue")
videoDataOutput?.setSampleBufferDelegate(self, queue: videoSessionQueue)
videoDataOutput?.alwaysDiscardsLateVideoFrames = true

//  Audio data output
let audioDataOutput = AVCaptureAudioDataOutput()
let audioSessionQueue = DispatchQueue(label: "audio_capture_session_queue")
audioDataOutput.setSampleBufferDelegate(self, queue: audioSessionQueue)

//  Configure session
captureSession.beginConfiguration()

let videoInput = try AVCaptureDeviceInput(device: currentCameraDevice)
captureSession.addInput(videoInput)
captureSession.addOutput(videoDataOutput!)

if let videoDataOutputConnection = videoDataOutput?.connection(with: .video), videoDataOutputConnection.isVideoStabilizationSupported {
    videoDataOutputConnection.preferredVideoStabilizationMode = .cinematic
}

if let audioInput = AVCaptureDevice.default(for: .audio) {
    try captureSession.addInput(AVCaptureDeviceInput(device: audioInput))
    captureSession.addOutput(audioDataOutput)
}

captureSession.commitConfiguration()

captureSession.startRunning()

有人有什么见解或解决方案吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

对感兴趣的人来说,这是我经过几天的挖掘而学到的东西:

渲染到非AVCaptureVideoPreviewLayer时无法避免延迟-实际上,这是您每次运行AVCaptureVideoDataOutput处理单个帧的时间(对于CI和Metal设置均是如此)。

稳定模式.cinematic导致最明显的延迟-〜1秒。切换到.standard可以将时间缩短到约1/10秒,比完全没有稳定效果要好。