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 3, 2023
1 parent dd2622d commit ccd770a
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 @@ -66,7 +66,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 @@ -187,12 +198,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 ccd770a

Please sign in to comment.