Skip to content

Commit

Permalink
Encoding is now processed when PTS increases in monotonic time.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Oct 12, 2024
1 parent dbeec84 commit 253e723
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Sources/Codec/VideoCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ final class VideoCodec<T: VideoCodecDelegate> {
}
}
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 {
Expand Down Expand Up @@ -168,14 +168,17 @@ final class VideoCodec<T: VideoCodecDelegate> {
}
}

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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 253e723

Please sign in to comment.