Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ final class AudioPlayerRenderProcessor: NSObject {
let frameSizeInBytes = bufferContext.sizeInBytes
let used = bufferContext.frameUsedCount
let start = bufferContext.frameStartIndex
#if DEBUG
assert(start < bufferContext.totalFrameCount,
"frameStartIndex (\(start)) >= totalFrameCount (\(bufferContext.totalFrameCount)) - logic error!")
assert(used <= bufferContext.totalFrameCount,
"frameUsedCount (\(used)) > totalFrameCount (\(bufferContext.totalFrameCount)) - logic error!")
#endif
let end = (bufferContext.frameStartIndex + bufferContext.frameUsedCount) % bufferContext.totalFrameCount
let signal = rendererContext.waiting.value && used < bufferContext.totalFrameCount / 2

Expand Down Expand Up @@ -122,12 +128,33 @@ final class AudioPlayerRenderProcessor: NSObject {
totalFramesCopied = framesToCopy

rendererContext.lock.lock()
#if DEBUG
if totalFramesCopied > bufferContext.frameUsedCount {
Logger.debug("Buffer race: tried to consume %d frames but only %d available (reset likely occurred)",
category: .audioRendering,
args: totalFramesCopied, bufferContext.frameUsedCount)
}
#endif
bufferContext.frameStartIndex = (bufferContext.frameStartIndex + totalFramesCopied) % bufferContext.totalFrameCount
bufferContext.frameUsedCount -= totalFramesCopied
if totalFramesCopied <= bufferContext.frameUsedCount {
bufferContext.frameUsedCount -= totalFramesCopied
} else {
bufferContext.frameUsedCount = 0
}
rendererContext.lock.unlock()

} else {
let frameToCopy = min(inNumberFrames, bufferContext.totalFrameCount - start)
let frameToCopy: UInt32
if start < bufferContext.totalFrameCount {
frameToCopy = min(inNumberFrames, bufferContext.totalFrameCount - start)
} else {
#if DEBUG
Logger.debug("Buffer race: start index %d >= totalFrameCount %d (reset likely occurred)",
category: .audioRendering,
args: start, bufferContext.totalFrameCount)
#endif
frameToCopy = 0
}
bufferList.mBuffers.mNumberChannels = 2
bufferList.mBuffers.mDataByteSize = frameSizeInBytes * frameToCopy

Expand Down Expand Up @@ -160,8 +187,19 @@ final class AudioPlayerRenderProcessor: NSObject {
totalFramesCopied = frameToCopy + moreFramesToCopy

rendererContext.lock.lock()
#if DEBUG
if totalFramesCopied > bufferContext.frameUsedCount {
Logger.debug("Buffer race: tried to consume %d frames but only %d available (reset likely occurred)",
category: .audioRendering,
args: totalFramesCopied, bufferContext.frameUsedCount)
}
#endif
bufferContext.frameStartIndex = (bufferContext.frameStartIndex + totalFramesCopied) % bufferContext.totalFrameCount
bufferContext.frameUsedCount -= totalFramesCopied
if totalFramesCopied <= bufferContext.frameUsedCount {
bufferContext.frameUsedCount -= totalFramesCopied
} else {
bufferContext.frameUsedCount = 0
}
rendererContext.lock.unlock()
}

Expand Down
8 changes: 7 additions & 1 deletion AudioStreaming/Streaming/Helpers/BufferContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ final class BufferContext {
var frameUsedCount: UInt32 = 0

var end: UInt32 {
(frameStartIndex + frameUsedCount) % totalFrameCount
#if DEBUG
assert(frameStartIndex < totalFrameCount,
"frameStartIndex (\(frameStartIndex)) exceeds totalFrameCount (\(totalFrameCount)) - logic error!")
assert(frameUsedCount <= totalFrameCount,
"frameUsedCount (\(frameUsedCount)) exceeds totalFrameCount (\(totalFrameCount)) - logic error!")
#endif
return (frameStartIndex + frameUsedCount) % totalFrameCount
}

init(sizeInBytes: UInt32, totalFrameCount: UInt32) {
Expand Down