oboe icon indicating copy to clipboard operation
oboe copied to clipboard

Oboe interferes with normal device audio when DeviceId is specified

Open danielandujar opened this issue 4 years ago • 10 comments

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;
}

danielandujar avatar Jan 04 '22 20:01 danielandujar

I was able to reproduce this on a Samsung Note10.

REPRO STEPS

  1. Plug in USB adapter with headphones.
  2. Launch OboeTester.
  3. Check "Enable background operation" on Main screen.
  4. Press "TEST OUTPUT"
  5. Select "built-in speaker" from "Device" menu.
  6. Press OPEN and START
  7. You should hear sound from the speakers.
  8. Uncheck channel 1 to make it less annoying.
  9. Press HOME button.
  10. Launch YouTube
  11. 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.

philburk avatar Jan 05 '22 23:01 philburk

Tracked internally at: b/213381702 | Audio misrouted to speaker if OboeTester has selected speakers on Note 10

philburk avatar Jan 05 '22 23:01 philburk

I wonder if the issue is specific to Oboe on Samsung or if using AAudio and setDeviceId would produce the same result.

danielandujar avatar Jan 06 '22 19:01 danielandujar

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.

philburk avatar Mar 30 '22 22:03 philburk

I did test an AAudio implementation for this issue, however, a coworker was able to reproduce the issue on a Pixel4 XL.

danielandujar avatar Mar 30 '22 22:03 danielandujar

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.

philburk avatar Mar 30 '22 23:03 philburk

So, it is possible through the SDK to get two Audio Devices for different MediaPlayer objects but not from AAudio/Oboe?

danielandujar avatar Mar 30 '22 23:03 danielandujar

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:

  1. Force close the app
  2. Turn ON and OFF the speaker
  3. Use the phone normally.

danielandujar avatar Mar 30 '22 23:03 danielandujar

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.

philburk avatar Mar 31 '22 00:03 philburk

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

philburk avatar Mar 31 '22 00:03 philburk