RustAudio / coreaudio-rs

A friendly rust interface to Apple's Core Audio API.
Apache License 2.0
203 stars 38 forks source link

set_input_callback does not resize the AudioBufferList buffer correctly #90

Closed RLMorley closed 1 year ago

RLMorley commented 1 year ago

Hi!

I'm experiencing an issue that occurs when the number of samples provided by the callback set using "kAudioOutputUnitProperty_SetInputCallback" in AudioUnit.set_input_callback increases.

On my device, this happens whenever a bluetooth headset is selected as the current audio device.

When switching from the speaker or earpeice (which provide 1024 samples), to a bluetooth headset, the change in the number of samples is detected and the buffer is resized using "vec.reserve_exact(data_byte_size - len);"

However, the mDataByteSize property of the buffer is not updated to reflect this change. This results in the framework printing the error: 3072 frames, 2 bytes/frame, expected 6144-byte buffer; ioData.mBuffers[0].mDataByteSize=2048; kAudio_ParamError

I tried updating this value and API worked for a few seconds before crashing with memory corruption. e.g. malloc: Incorrect checksum for freed object 0x10700b800: probably modified after being freed.

I'm unsure how the corruption is occurring, and for now I have worked around this in our project by allocating a new buffer when the number of samples changes.

Any thoughts?

Thanks

yamadapc commented 1 year ago

I had the same issue & believe I have fixed it in https://github.com/RustAudio/coreaudio-rs/pull/91

MichaelHills commented 1 year ago

Marking as closed (fixed in https://github.com/RustAudio/coreaudio-rs/pull/91)