-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDL audio callback uses wrong buffer size after WASAPI device change #11122
Comments
Otherwise, it would fill the previous size's worth of data into the current size's buffer. Fixes #11122.
(This was an excellent bug report...thank you for being so thorough!) This is SDL2-specifc, even though it landed in the 3.2.0 milestone, fwiw. All this code was almost completely replaced in SDL3. I believe this should be fixed by 5b0e838, but I don't have a way to test this here to verify. If this turned out to not fix the problem, please report back and I'll reopen this issue! |
Our user updated to SDL 2.30.9 and reported his game was still crashing when plugging/unplugging his headphones. We ended up sending him a debug build to get a stack trace from ASAN. From what I can tell, the trace indicates that this issue is not fully resolved. Here is what I consider to be the relevant parts from the log. The full log is attached at the bottom.
From the stack trace, these are the relevant lines from SDL_audiolib.
Plus, to show how this ties into the SDL audio system, here is the line where SDL_audiolib assigns the A basic review of the callback function in const int out_len_samples = outLen / (SDL_AUDIO_BITSIZE(fAudioSpec.format) / 8);
const int out_len_frames = out_len_samples / fAudioSpec.channels;
if (fStrmBuf.size() != out_len_samples) {
fFinalMixBuf.reset(out_len_samples);
fStrmBuf.reset(out_len_samples);
fProcessorBuf.reset(out_len_samples);
}
Frankly, I'm a bit stumped. The fix from icculus looks to me like it would have resolved this completely, so I'm hoping someone else notices something that I don't. Full log: stderr.log |
It's maybe worth noting that the fix that was applied did resolve the audio corruption he experienced. |
Actually, I managed to find an audio device with Lines 701 to 707 in c98c4fb
You can see here that we grab You can see that EDIT: Also, I moved that line |
Should be fixed now that #11496 is in |
We recently received a report from a user who has encountered audio distortion and app crashes as a result of plugging/unplugging his headphones. He appears to have a somewhat rare audio setup where his speakers and headphones are connected as separate playback devices with different default device periods. As a result, the WASAPI driver assigns
this->spec.samples = 236
for the speakers, andthis->spec.samples = 221
for the headphones. This can be observed in the logs from our game client that he provided to us in the issue report.diasurgical/devilutionX#7452 (comment)
Speakers:
VERBOSE: Aulib sampleRate=22050 channels=2 frameSize=236 format=0x8120
Headphones:
VERBOSE: Aulib sampleRate=22050 channels=2 frameSize=221 format=0x8120
This version of our application uses SDL release version 2.30.5.
In
SDL_RunAudio()
, the callback is invoked withdata
anddata_len
parameters.udata
is irrelevant in our case.SDL/src/audio/SDL_audio.c
Line 736 in 2eef7ca
data
is the return value ofWASAPI_GetDeviceBuf()
.SDL/src/audio/SDL_audio.c
Line 707 in 2eef7ca
data_len
is assigned the value fromdevice->callbackspec.size
.SDL/src/audio/SDL_audio.c
Line 703 in 2eef7ca
When the WASAPI driver updates the number of samples in the audio spec, it calls
SDL_CalculateAudioSpec(&this->spec)
to updatedevice->spec.size
. But that does nothing todevice->callbackspec
.SDL/src/audio/wasapi/SDL_wasapi.c
Lines 486 to 500 in 2eef7ca
WASAPI_GetDeviceBuf()
usesthis->spec.samples
to allocate the buffer for audio samples.SDL/src/audio/wasapi/SDL_wasapi.c
Line 183 in 2eef7ca
I believe that if the
channels
,format
, orfreq
are also updated, then SDL will create an audio stream to resample internally. But if only the number of samples changes, then it will "use the existing audio stream" which means that nothing actually changes. Even if the stream was created, I have doubts about whether the callback would be providing the right number of samples to the resampler.SDL/src/audio/wasapi/SDL_wasapi.c
Lines 82 to 93 in 2eef7ca
Therefore, unless I've missed something, after the device change the callback is made to fill a 221-sample buffer with 236 samples or vice versa.
The text was updated successfully, but these errors were encountered: