Skip to content

Commit 0ae8e2d

Browse files
committed
Fix iOS 16 crash
Incorporate changes from PR tanhakabir#175 (Kuama-IT:fix-ios-16-crash) tanhakabir@97a9bf6 Only bring over code changes, not whitespace Unsure if max() -> min() change in totalPredictedPacketCount is necessary, but kept it just in case
1 parent 2fdfc62 commit 0ae8e2d

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

Source/Engine/AudioStreamEngine.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ class AudioStreamEngine: AudioEngine {
203203
}
204204

205205
private func pollForNextBufferRecursive() {
206+
if(!converter.initialized) {
207+
return
208+
}
206209
do {
207210
var nextScheduledBuffer: AVAudioPCMBuffer! = try converter.pullBuffer()
208211
numberOfBuffersScheduledFromPoll += 1

Source/Engine/Converter/AudioConverter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import AudioToolbox
3535

3636
protocol AudioConvertable {
3737
var engineAudioFormat: AVAudioFormat {get}
38+
var initialized:Bool {get}
3839

3940
init(withRemoteUrl url: AudioURL, toEngineAudioFormat: AVAudioFormat, withPCMBufferSize size: AVAudioFrameCount) throws
4041
func pullBuffer() throws -> AVAudioPCMBuffer
@@ -74,6 +75,11 @@ class AudioConverter: AudioConvertable {
7475

7576
//Field
7677
var converter: AudioConverterRef? //set by AudioConverterNew
78+
79+
public var initialized:Bool {
80+
converter != nil
81+
}
82+
7783
var currentAudioPacketIndex: AVAudioPacketCount = 0
7884

7985
// use to store reference to the allocated buffers from the converter to properly deallocate them before the next packet is being converted

Source/Engine/Converter/AudioConverterListener.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ func ConverterListener(_ converter: AudioConverterRef, _ packetCount: UnsafeMuta
8080
ioData.pointee.mBuffers.mDataByteSize = UInt32(packetByteCount)
8181

8282
selfAudioConverter.converterBuffer = ioData.pointee.mBuffers.mData
83-
84-
if let lastDescription = selfAudioConverter.converterDescriptions {
85-
lastDescription.deallocate()
86-
}
87-
83+
8884
// Handle packet descriptions for compressed formats (MP3, AAC, etc)
8985
let fileFormatDescription = fileAudioFormat.streamDescription.pointee
9086
if fileFormatDescription.mFormatID != kAudioFormatLinearPCM {
9187
if outPacketDescriptions?.pointee == nil {
88+
if let lastDescription = selfAudioConverter.converterDescriptions {
89+
lastDescription.deallocate()
90+
}
9291
outPacketDescriptions?.pointee = UnsafeMutablePointer<AudioStreamPacketDescription>.allocate(capacity: 1)
9392
}
9493
outPacketDescriptions?.pointee?.pointee.mDataByteSize = UInt32(packetByteCount)

Source/Engine/Parser/AudioParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class AudioParser: AudioParsable {
9797
let predictedCount = AVAudioPacketCount(Double(sizeOfFileInBytes) / bytesPerPacket)
9898

9999
guard networkProgress != 1.0 else {
100-
return max(AVAudioPacketCount(audioPackets.count), predictedCount)
100+
return min(AVAudioPacketCount(audioPackets.count), predictedCount)
101101
}
102102

103103
return predictedCount

0 commit comments

Comments
 (0)