From 180eb9907e4cae06150aa3e4dcb432fb03010a22 Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Tue, 3 Oct 2023 20:43:09 +0900 Subject: [PATCH] Considering cases where PTS may be rolled back. --- Sources/Media/IOAudioRingBuffer.swift | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Sources/Media/IOAudioRingBuffer.swift b/Sources/Media/IOAudioRingBuffer.swift index bc1aab1d1..f600c4e48 100644 --- a/Sources/Media/IOAudioRingBuffer.swift +++ b/Sources/Media/IOAudioRingBuffer.swift @@ -69,7 +69,18 @@ final class IOAudioRingBuffer { } } } - skip = numSamples(sampleBuffer) + let distance = distance(sampleBuffer) + if 0 <= distance { + skip = distance + } else { + // #1289. Considering cases where PTS may be rolled back. + let newHead = head + distance + if 0 <= newHead { + head = newHead + } else { + head = Int(buffer.frameLength) + newHead + } + } appendAudioPCMBuffer(workingBuffer) } @@ -190,12 +201,12 @@ final class IOAudioRingBuffer { return noErr } - private func numSamples(_ sampleBuffer: CMSampleBuffer) -> Int { + private func distance(_ sampleBuffer: CMSampleBuffer) -> Int { // Device audioMic or ReplayKit audioMic. let sampleRate = Int32(format.sampleRate) if presentationTimeStamp.timescale == sampleRate { let presentationTimeStamp = CMTimeAdd(presentationTimeStamp, CMTime(value: CMTimeValue(counts), timescale: presentationTimeStamp.timescale)) - return max(Int(sampleBuffer.presentationTimeStamp.value - presentationTimeStamp.value), 0) + return Int(sampleBuffer.presentationTimeStamp.value - presentationTimeStamp.value) } return 0 }