Oboe interferes with normal device audio when DeviceId is specified
Tested on Android version(s): 10 & 12 Android device(s): Samsung Galaxy S10 & Samsung Galaxy S21 Ultra Oboe version: latest
Short description I'm using oboe to play sounds (like notifications) with the least possible latency, we were able to reduce around 300ms playback latency using oboe. For our use case, we need to play notifications through the phone's speakers. BUT we are facing a weird issue, after setting the DeviceId and starting the stream, the devices forces all audio through that preferred device instead of just the audio we play on Oboe. If the user for example receives a phone call, the device routes the call audio through the speakers as well instead of the default audio device (headset, bluetooth etc) even after the stream was stopped.
I'm not an expert using the NDK or c++, I used the DrumThumper for guidance in how to implement oboe for our use case (I have mono wav files, and different wav files that the app would choose from).
Some code snippets: getting the playback device.
playbackDevice = am?.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
?.firstOrNull { x -> x.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER }
nativeAudioManager.init(playbackDevice)
Initializing the engine
void AudioEngine::setPreferredDeviceId(int32_t deviceId) {
preferredDeviceId = deviceId;
}
bool AudioEngine::init(){
channelCount = 1;
AudioStreamBuilder builder;
builder.setChannelCount(channelCount);
// we will resample source data to device rate, so take default sample rate
builder.setCallback(this);
//Here we set the deviceId we want the stream to play to.
//for our use case, we'll send it the id of the speakerphone
if (preferredDeviceId != -1 )
builder.setDeviceId(preferredDeviceId);
builder.setPerformanceMode(PerformanceMode::LowLatency);
builder.setSharingMode(SharingMode::Exclusive);
builder.setSampleRateConversionQuality(SampleRateConversionQuality::Medium);
Result result = builder.openStream(audioStream);
if (result != Result::OK){
LOGE("openStream failed. Error: %s", convertToText(result));
return false;
}
// Reduce stream latency by setting the buffer size to a multiple of the burst size
// Note: this will fail with ErrorUnimplemented if we are using a callback with OpenSL ES
// See oboe::AudioStreamBuffered::setBufferSizeInFrames
result = audioStream->setBufferSizeInFrames(
audioStream->getFramesPerBurst() * 2);
if (result != Result::OK) {
LOGW("setBufferSizeInFrames failed. Error: %s", convertToText(result));
}
sampleRate = audioStream->getSampleRate();
return true;
}
I was able to reproduce this on a Samsung Note10.
REPRO STEPS
- Plug in USB adapter with headphones.
- Launch OboeTester.
- Check "Enable background operation" on Main screen.
- Press "TEST OUTPUT"
- Select "built-in speaker" from "Device" menu.
- Press OPEN and START
- You should hear sound from the speakers.
- Uncheck channel 1 to make it less annoying.
- Press HOME button.
- Launch YouTube
- Play a video.
EXPECT Hear sound from the headphones.
ACTUAL Hear sound from the speakers.
ADDITIONAL With a Pixel 6 running S I hear sound from the headphones.
Tracked internally at: b/213381702 | Audio misrouted to speaker if OboeTester has selected speakers on Note 10
I wonder if the issue is specific to Oboe on Samsung or if using AAudio and setDeviceId would produce the same result.
I wonder if the issue is specific to Oboe on Samsung
It is not specific to Oboe. Oboe just passes device request through to AAudio.
I believe this is a Samsung specific behavior. We have asked Samsung to comment. We will update this bug when they do.
I did test an AAudio implementation for this issue, however, a coworker was able to reproduce the issue on a Pixel4 XL.
We have determined that the AAudio API contract for setDeviceId() does not guarantee that an app will get the requested device. DSP limitations may force device sharing.
We are updating the docs for AAudioStreamBuilder_setDeviceId() to say:
Request an audio device identified by an ID.
The ID could be obtained from the Java AudioManager.
AudioManager.getDevices() returns an array of {@link AudioDeviceInfo},
which contains a getId() method. That ID can be passed to this function.
It is possible that you may not get the device that you requested.
So if it is important to you, you should call
AAudioStream_getDeviceId() after the stream is opened to
verify the actual ID.
So, it is possible through the SDK to get two Audio Devices for different MediaPlayer objects but not from AAudio/Oboe?
Also, the original issue was NOT that we did not get the requested Audio Device, We did get the Audio Device (a.k.a the Speakerphone) but, when we create a stream (not even needing to open it). the Phone's CALL audio gets routed to the Speakerphone and locking (does not let to switch off the speaker).
After the issue is reproduced, to fix the phone's speaker we had to:
- Force close the app
- Turn ON and OFF the speaker
- Use the phone normally.
The limitation is in the AudioPolicy and DSP paths. It is not in AAudio/Oboe. So all APIs would probably be the same in this regard.
the Phone's CALL audio gets routed to the Speakerphone and locking (does not let to switch off the speaker).
Yes, opening a specific audio path can affect other apps that try to do audio at the same time.
This is not an Oboe issue. If you want to request a new behavior for Android then you can file a Feature Request through Buganizer. Then we can get it reviewed by Product Management and maybe approved as a work item. We consider developer Feature Requests to be very important.
https://source.android.com/setup/contribute/report-bugs