SDL: [Android] audio output breaks when removing / inserting earphones on S9 android 10
This was tested on a Samsung Galaxy S9, running android 10.
However, this does not happen on nvidia shield running android 5.1 and mi9 running lineageos 11.0.
starting my app without or without earphones works normally. However, if i insert / remove them, then audio playback stops. Also, the sdl_audio_callback is called more frequently (haven’t measured but it seems to be like max speed my phone can handle).
log from plugging in earphones
D/InputTransport: Input channel destroyed: '5af41f5', fd=69
W/AudioTrack: restoreTrack_l(988): dead IAudioTrack, PCM, creating a new one from obtainBuffer()
W/AudioSystem: ioConfigChanged() modifying unknown output! 13
D/AAudio: AAudioStream_requestPause(s#1) called
D/AudioStreamLegacy: onAudioDeviceUpdate() devId 3 => 990
onAudioDeviceUpdate() DISCONNECT the stream now
D/: PlayerBase::pause() from IPlayer
D/AAudio: AAudioStream_requestStart(s#1) called --------------
E/AAudioStream: setState(1) tried to set to 3 but already DISCONNECTED
D/: PlayerBase::start() from IPlayer
D/AAudio: AAudioStream_requestStart(s#1) returned 0 ---------
log pulling out earphones (after restarting the app with headphones already plugged in
D/InputTransport: Input channel destroyed: '32fe96 ', fd=69
W/AudioTrack: restoreTrack_l(1007): dead IAudioTrack, PCM, creating a new one from obtainBuffer()
W/AudioSystem: ioConfigChanged() modifying unknown output! 13
D/AAudio: AAudioStream_requestPause(s#1) called
D/: PlayerBase::pause() from IPlayer
D/AAudio: AAudioStream_requestStart(s#1) called --------------
D/: PlayerBase::start() from IPlayer
D/AAudio: AAudioStream_requestStart(s#1) returned 0 ---------
D/AudioStreamLegacy: onAudioDeviceUpdate() devId 1000 => 3
as it seems to only happen on my s9, and theres not a way to simulate plugging in headphones using AS emulators, this it probably going to be a pain to debug haha. Let me know if theres anything you want me to test. I can fork sdl and try the changes locally.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (4 by maintainers)
Commits related to this issue
- WIP android / switch port, fix many audio issues, add rewind android port is not yet finished. there's a few bugs with sdl upstream, for example https://github.com/libsdl-org/SDL/issues/4985. ... — committed to ITotalJustice/TotalGB by ITotalJustice 2 years ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. — committed to icculus/SDL by icculus 5 months ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. — committed to icculus/SDL by icculus 5 months ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. — committed to icculus/SDL by icculus 5 months ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. — committed to icculus/SDL by icculus 5 months ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. — committed to libsdl-org/SDL by icculus 5 months ago
- aaudio: Backport headphone hotplugging support from SDL3. Fixes #4985. (cherry picked from commit ec25d6b1e860e1689044c1d145cbbcbe1aa5011f) — committed to libsdl-org/SDL by icculus 5 months ago
Okay, I’ll see how hard that is to backport to SDL2.
I did a quick test on a handful of devices and it seems to work well now. Thank you!
So your crash is probably because there’s a time when the audio thread might set the stream to NULL, while the main thread is pumping the event loop and assumes it isn’t NULL. I’ve added a fix for this.
I’ve tested on a real phone now; the headphone hotplugging appears to work.
If anyone wants to take one more run at it, that’s cool, otherwise I’m going to close this bug under the assumption we’re good.
Tested it with DevilutionX, on previous versions the game would lock up when i disconnected the headphones. With 2.29.3 the game correctly switches to the build in speakers and the game continues to run without issues. Nice work!
(This is in revision control now, but please do speak up if this is still busted for you and I’ll reopen!)
I’ve taken a shot at this in #8937, but I need someone to test it on real hardware. Can anyone do this really quickly, so we can maybe tuck this into the 2.30.0 release that is about to ship? I’d really appreciate it.
this looks like it’s fixed in https://github.com/libsdl-org/SDL/commit/482c2389535a8366187e5b859624bb66b3565f71 2.28 (the version that was tested) still had the bug https://github.com/libsdl-org/SDL/blob/05eb08053d48fea9052ad02b3d619244aeb868d3/src/audio/aaudio/SDL_aaudio.c#L219 and so does the latest sdl2 commit
I just disconnected my bluetooth headphones while testing and noticed this issue in my build. I proceeded to test my whole pile of Android devices. I’m testing on 2.28.x - not on SDL3.
The issue occurs on every device that I tested that uses AAudio. Here is the list: Moto G5 Plus, Moto G7, Fire HD 10 (13th Generation), Fire 7 (12th Generation), Huawei P40 lite, Xiaomi Mi 9 SE, Pixel 6. Not only does the sound stop working, the game becomes mostly unplayable - every sound that is played causes a half second or more delay.
The only devices that I have that it worked on are the Fire HD 8 (7th gen) - API Level 22 and BLU Advance 5.0 HD - API Level 23. The API level on these are so old that AAudio would not be available and they are falling back to OpenSL ES. The original poster @ITotalJustice had two devices that they said worked - I believe these were also old enough to be falling back to OpenSL ES.
Based on my testing, I believe this is actually a pretty severe bug that affects almost every Android device newer than 6 years old.
My current solution is to use OpenSL ES on all Android devices. I don’t know if there are downsides to this. I did a quick test on all my devices and it worked on all them.
Sorry I didn’t test 3.2.0 - but it appears to me that you at least don’t need to test on any particular Android device to see if it is fixed. Anything using AAudio should suffice.