Skip to content

Commit

Permalink
Fix iOS 16 crash
Browse files Browse the repository at this point in the history
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
  • Loading branch information
micahjon committed Oct 11, 2022
1 parent 2fdfc62 commit 0ae8e2d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Source/Engine/AudioStreamEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class AudioStreamEngine: AudioEngine {
}

private func pollForNextBufferRecursive() {
if(!converter.initialized) {
return
}
do {
var nextScheduledBuffer: AVAudioPCMBuffer! = try converter.pullBuffer()
numberOfBuffersScheduledFromPoll += 1
Expand Down
6 changes: 6 additions & 0 deletions Source/Engine/Converter/AudioConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import AudioToolbox

protocol AudioConvertable {
var engineAudioFormat: AVAudioFormat {get}
var initialized:Bool {get}

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

//Field
var converter: AudioConverterRef? //set by AudioConverterNew

public var initialized:Bool {
converter != nil
}

var currentAudioPacketIndex: AVAudioPacketCount = 0

// use to store reference to the allocated buffers from the converter to properly deallocate them before the next packet is being converted
Expand Down
9 changes: 4 additions & 5 deletions Source/Engine/Converter/AudioConverterListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ func ConverterListener(_ converter: AudioConverterRef, _ packetCount: UnsafeMuta
ioData.pointee.mBuffers.mDataByteSize = UInt32(packetByteCount)

selfAudioConverter.converterBuffer = ioData.pointee.mBuffers.mData

if let lastDescription = selfAudioConverter.converterDescriptions {
lastDescription.deallocate()
}


// Handle packet descriptions for compressed formats (MP3, AAC, etc)
let fileFormatDescription = fileAudioFormat.streamDescription.pointee
if fileFormatDescription.mFormatID != kAudioFormatLinearPCM {
if outPacketDescriptions?.pointee == nil {
if let lastDescription = selfAudioConverter.converterDescriptions {
lastDescription.deallocate()
}
outPacketDescriptions?.pointee = UnsafeMutablePointer<AudioStreamPacketDescription>.allocate(capacity: 1)
}
outPacketDescriptions?.pointee?.pointee.mDataByteSize = UInt32(packetByteCount)
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Parser/AudioParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AudioParser: AudioParsable {
let predictedCount = AVAudioPacketCount(Double(sizeOfFileInBytes) / bytesPerPacket)

guard networkProgress != 1.0 else {
return max(AVAudioPacketCount(audioPackets.count), predictedCount)
return min(AVAudioPacketCount(audioPackets.count), predictedCount)
}

return predictedCount
Expand Down

0 comments on commit 0ae8e2d

Please sign in to comment.