Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Oct 7, 2023
1 parent 180eb99 commit 6e7a72d
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Sources/Media/IOAudioRingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,8 @@ final class IOAudioRingBuffer {
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)
appendAudioPCMBuffer(workingBuffer, offset: offsetCount(sampleBuffer) / 8)
}

func appendAudioPCMBuffer(_ audioPCMBuffer: AVAudioPCMBuffer, offset: Int = 0) {
Expand Down Expand Up @@ -200,6 +192,17 @@ final class IOAudioRingBuffer {
}
return noErr
}

private func offsetCount(_ sampleBuffer: CMSampleBuffer) -> Int {
let data = sampleBuffer.dataBuffer?.data?.bytes ?? []
let count = 0
for i in 0..<data.count {
if (data[i * 2 * 4] != 0) {
return i * 2 * 4
}
}
return count
}

private func distance(_ sampleBuffer: CMSampleBuffer) -> Int {
// Device audioMic or ReplayKit audioMic.
Expand Down

0 comments on commit 6e7a72d

Please sign in to comment.