Skip to content

Commit

Permalink
Considering cases where PTS may be rolled back.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Oct 7, 2023
1 parent 85906b9 commit 180eb99
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Sources/Media/IOAudioRingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

0 comments on commit 180eb99

Please sign in to comment.