From 253e72315122aad0ead54e6083c57c25b519edcf Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Fri, 11 Oct 2024 01:39:39 +0900 Subject: [PATCH] Encoding is now processed when PTS increases in monotonic time. --- Sources/Codec/VideoCodec.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Sources/Codec/VideoCodec.swift b/Sources/Codec/VideoCodec.swift index efe2e4f68..05dc7abe0 100644 --- a/Sources/Codec/VideoCodec.swift +++ b/Sources/Codec/VideoCodec.swift @@ -70,14 +70,14 @@ final class VideoCodec { } } private var invalidateSession = true - private var presentationTimeStamp: CMTime = .invalid + private var presentationTimeStamp: CMTime = .zero init(lockQueue: DispatchQueue) { self.lockQueue = lockQueue } func append(_ imageBuffer: CVImageBuffer, presentationTimeStamp: CMTime, duration: CMTime) { - guard isRunning.value, !willDropFrame(presentationTimeStamp) else { + guard isRunning.value, useFrame(presentationTimeStamp) else { return } if invalidateSession { @@ -168,14 +168,17 @@ final class VideoCodec { } } - private func willDropFrame(_ presentationTimeStamp: CMTime) -> Bool { + private func useFrame(_ presentationTimeStamp: CMTime) -> Bool { guard startedAt <= presentationTimeStamp else { - return true + return false } - guard kVideoCodec_defaultFrameInterval < frameInterval else { + guard self.presentationTimeStamp < presentationTimeStamp else { return false } - return presentationTimeStamp.seconds - self.presentationTimeStamp.seconds <= frameInterval + guard kVideoCodec_defaultFrameInterval < frameInterval else { + return true + } + return frameInterval < presentationTimeStamp.seconds - self.presentationTimeStamp.seconds } #if os(iOS) || os(tvOS) || os(visionOS) @@ -233,7 +236,7 @@ extension VideoCodec: Running { self.needsSync.mutate { $0 = true } self.inputFormat = nil self.outputFormat = nil - self.presentationTimeStamp = .invalid + self.presentationTimeStamp = .zero self.startedAt = .zero #if os(iOS) || os(tvOS) || os(visionOS) NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)