@@ -65,6 +65,12 @@ final class AudioPlayerRenderProcessor: NSObject {
6565 let frameSizeInBytes = bufferContext. sizeInBytes
6666 let used = bufferContext. frameUsedCount
6767 let start = bufferContext. frameStartIndex
68+ #if DEBUG
69+ assert ( start < bufferContext. totalFrameCount,
70+ " frameStartIndex ( \( start) ) >= totalFrameCount ( \( bufferContext. totalFrameCount) ) - logic error! " )
71+ assert ( used <= bufferContext. totalFrameCount,
72+ " frameUsedCount ( \( used) ) > totalFrameCount ( \( bufferContext. totalFrameCount) ) - logic error! " )
73+ #endif
6874 let end = ( bufferContext. frameStartIndex + bufferContext. frameUsedCount) % bufferContext. totalFrameCount
6975 let signal = rendererContext. waiting. value && used < bufferContext. totalFrameCount / 2
7076
@@ -122,12 +128,33 @@ final class AudioPlayerRenderProcessor: NSObject {
122128 totalFramesCopied = framesToCopy
123129
124130 rendererContext. lock. lock ( )
131+ #if DEBUG
132+ if totalFramesCopied > bufferContext. frameUsedCount {
133+ Logger . debug ( " Buffer race: tried to consume %d frames but only %d available (reset likely occurred) " ,
134+ category: . audioRendering,
135+ args: totalFramesCopied, bufferContext. frameUsedCount)
136+ }
137+ #endif
125138 bufferContext. frameStartIndex = ( bufferContext. frameStartIndex + totalFramesCopied) % bufferContext. totalFrameCount
126- bufferContext. frameUsedCount -= totalFramesCopied
139+ if totalFramesCopied <= bufferContext. frameUsedCount {
140+ bufferContext. frameUsedCount -= totalFramesCopied
141+ } else {
142+ bufferContext. frameUsedCount = 0
143+ }
127144 rendererContext. lock. unlock ( )
128145
129146 } else {
130- let frameToCopy = min ( inNumberFrames, bufferContext. totalFrameCount - start)
147+ let frameToCopy : UInt32
148+ if start < bufferContext. totalFrameCount {
149+ frameToCopy = min ( inNumberFrames, bufferContext. totalFrameCount - start)
150+ } else {
151+ #if DEBUG
152+ Logger . debug ( " Buffer race: start index %d >= totalFrameCount %d (reset likely occurred) " ,
153+ category: . audioRendering,
154+ args: start, bufferContext. totalFrameCount)
155+ #endif
156+ frameToCopy = 0
157+ }
131158 bufferList. mBuffers. mNumberChannels = 2
132159 bufferList. mBuffers. mDataByteSize = frameSizeInBytes * frameToCopy
133160
@@ -160,8 +187,19 @@ final class AudioPlayerRenderProcessor: NSObject {
160187 totalFramesCopied = frameToCopy + moreFramesToCopy
161188
162189 rendererContext. lock. lock ( )
190+ #if DEBUG
191+ if totalFramesCopied > bufferContext. frameUsedCount {
192+ Logger . debug ( " Buffer race: tried to consume %d frames but only %d available (reset likely occurred) " ,
193+ category: . audioRendering,
194+ args: totalFramesCopied, bufferContext. frameUsedCount)
195+ }
196+ #endif
163197 bufferContext. frameStartIndex = ( bufferContext. frameStartIndex + totalFramesCopied) % bufferContext. totalFrameCount
164- bufferContext. frameUsedCount -= totalFramesCopied
198+ if totalFramesCopied <= bufferContext. frameUsedCount {
199+ bufferContext. frameUsedCount -= totalFramesCopied
200+ } else {
201+ bufferContext. frameUsedCount = 0
202+ }
165203 rendererContext. lock. unlock ( )
166204 }
167205
0 commit comments