oboe icon indicating copy to clipboard operation
oboe copied to clipboard

AAudio Stream disconnection

Open darshanbr opened this issue 5 years ago • 6 comments

Android version(s): R (AAOS)

Discription On Android R we are facing AAudio stream disconnection issue. Bluetooth will be playing initially(which uses AAudio). AAudio stream is being disconnected as soon as we switched from Bluetooth to FM. Therefore there is no audio available once after we switched back from FM to Bluetooth.

Steps to reproduce

  1. BT Stereo Audio is playing.
  2. Change the source to FM which uses HwAudioSource player
  3. Now, Change the source to BT SA - observed audio loss

Expected behavior Audio should be available

Actual behavior Audio not available after source switch

Below logs are from BT SA to FM switch, in which the AAudio stream goes to disconnect state

03-29 13:31:21.374 3555 6534 W APM_AudioPolicyManager: disconnectAudioSource source has neither SW nor HW output 03-29 13:31:21.374 3555 6534 E APM_AudioPolicyManager: releaseAudioPatchInternal: no patch found with handle=0 03-29 13:31:21.391 4651 8205 D AudioStreamLegacy: onAudioDeviceUpdate() devId 3 => 2 03-29 13:31:21.391 4651 8205 D AudioStreamLegacy: onAudioDeviceUpdate() DISCONNECT the stream now 03-29 13:31:21.391 4651 8205 D AAudioStream: setState(s#2) from 4 to 13 03-29 13:31:21.392 3555 3592 D AF::TrackHandle: OpPlayAudio: not muting track:68 usage:0 for service UID 1041 03-29 13:31:21.392 3555 3567 E AudioFlinger::PatchPanel: Software patch connections leaked 268 276 03-29 13:31:21.392 3555 3567 E AudioFlinger::PatchPanel: A non empty Patch Endpoint leaked, handle 268 03-29 13:31:21.392 3555 3567 E AudioFlinger::PatchPanel: A non empty Patch Endpoint leaked, handle 276 03-29 13:31:21.393 4651 4651 I bt_btif_a2dp_sink: btif_a2dp_sink_set_focus_state_req 03-29 13:31:21.393 4651 5580 I bt_btif_a2dp_sink: btif_a2dp_sink_set_focus_state_event: state=0

after this Tuner plays but when we change back to BT SA the AAudio stream (which was supposed to be paused due to FM playback) is unable to resume. i.e DISCONNECTED

our system property configurations are as below PRODUCT_PROPERTY_OVERRIDES += aaudio.mmap_policy=1 PRODUCT_PROPERTY_OVERRIDES += aaudio.mmap_exclusive_policy=1

it's an always issue and we can reproduce it anytime.

darshanbr avatar May 24 '21 04:05 darshanbr

An AAudio stream is generally disconnected when there is a new device added or removed, or something else causes a routing change. The app can reopen a new stream if it wants.

Some notes are here: https://github.com/google/oboe/blob/master/docs/notes/disconnect.md

Does your app try to reopen a stream? Does the open fail?

philburk avatar May 25 '21 17:05 philburk

@darshanbr - Do you still have questions about disconnect? If not then please close this.

philburk avatar Jun 02 '21 16:06 philburk

Hi @philburk

As you stated in your previous comment "An AAudio stream can be disconnected when there is a new device added or removed, or something else causes a routing change." In our case there is no new device added or removed. it would be helpful if you elaborate that "something else" which could cause routing change.

from the latest logs we observed that just before the stream disconnection, Device ID is been updated from 2=>3. how is it possible when there is actually no device is added or removed physically ? or does it mean something else ? what are these Device IDs ?

01-01 00:02:55.524 7932 9334 V AudioSystem: ioConfigChanged() new config for output 13 samplingRate 48000, format 0x5 channel mask 0x63f frameCount 1152 frameCountHAL 768 deviceId 3 01-01 00:02:55.524 7932 9334 V AudioSystem: ioConfigChanged() old config for output 13 samplingRate 48000, format 0x5 channel mask 0x63f frameCount 1152 frameCountHAL 768 deviceId 2 01-01 00:02:55.524 9472 9492 V AudioSystem: ioConfigChanged() event 3 01-01 00:02:55.524 9472 9492 W AudioSystem: AUDIO_OUTPUT_CONFIG_CHANGED 01-01 00:02:55.524 9472 9492 V AudioSystem: ioConfigChanged() new config for output 13 samplingRate 48000, format 0x5 channel mask 0x63f frameCount 1152 frameCountHAL 768 deviceId 3 01-01 00:02:55.524 9472 9492 V AudioSystem: ioConfigChanged() old config for output 13 samplingRate 48000, format 0x5 channel mask 0x63f frameCount 1152 frameCountHAL 768 deviceId 2 01-01 00:02:55.524 9472 9492 D AudioStreamLegacy: onAudioDeviceUpdate() devId 2 => 3 01-01 00:02:55.524 9472 9492 D AudioStreamLegacy: onAudioDeviceUpdate() DISCONNECT the stream now 01-01 00:02:55.524 9472 9492 D AAudioStream: setState(s#1) from 4 to 13

darshanbr avatar Jun 02 '21 16:06 darshanbr

@darshanbr - wrote

In our case there is no new device added or removed.

In your opening question you said you switched from BT to FM and then back to BT. How did you do that? Through Settings? An app? Is FM an FM radio? A BT device does not get "plugged in" but when Android connects that is equivalent to a plugin. Perhaps that device change caused the disconnection.

it would be helpful if you elaborate that "something else" which could cause routing change.

The decision on where to route audio is based on many factors:

  • hardware plugged in or out
  • playback capture (eg. Live Caption) enabled can cause a reroute one second after starting a stream
  • a phone call comes in
  • selecting speakerphone
  • another app tries to open an EXCLUSIVE MMAP stream and steals your EXCLUSIVE stream (>=R)

I'm sure there are others but I cannot think of any.

what are these Device IDs ?

The index of a physical or virtual device. To see a list, launch OboeTester and then select "Device Report". Scroll up to see devices. https://github.com/google/oboe/tree/master/apps/OboeTester/docs https://play.google.com/store/apps/details?id=com.mobileer.oboetester

QUESTION: PLEASE ANSWER. What do you do when you get disconnected? If you try to reopen a stream does it fail? Any error codes?

Also is Live Caption enabled? Click volume key and look at bottom icon.

philburk avatar Jun 02 '21 21:06 philburk

@philburk

What do you do when you get disconnected? If you try to reopen a stream does it fail? Any error codes?

when we switch from BT SA to Radio, BT loses focus hence tried to pause and flush the stream. but the stream is been disconnected already. 03-29 13:31:24.508 4651 5422 D AAudio : AAudioStream_requestPause(s#2) called 03-29 13:31:24.508 4651 5422 D AAudioStream: setState(s#2) from 13 to 5 03-29 13:31:24.508 4651 5422 E AAudioStream: setState(2) tried to set to 5 but already DISCONNECTED 03-29 13:31:24.508 4651 5422 D droid.bluetoot: PlayerBase::pause() from IPlayer 03-29 13:31:24.508 4651 5422 D AAudio : AAudioStream_requestFlush(s#2) called 03-29 13:31:24.508 4651 5422 E AAudio : can only flush stream when PAUSED, OPEN or STOPPED, state = AAUDIO_STREAM_STATE_DISCONNECTED

when we switch back to BT from Radio, BT tries to start the stream and failed. 03-29 13:31:26.905 4651 5422 D AAudio : AAudioStream_requestStart(s#2) called -------------- 03-29 13:31:26.905 4651 5422 W AAudioStream: systemStart() stream is dead, state = AAUDIO_STREAM_STATE_DISCONNECTED 03-29 13:31:26.905 4651 5422 D AAudio : AAudioStream_requestStart(s#2) returned -895 ---------

we are still trying to figure out why the stream is disconnecting in first place. we are not trying to reconnect the stream.

FYI, from what we understand the BT SA code located at https://android.googlesource.com/platform/system/bt/+/refs/heads/master/btif/ is AOSP code base.

is Live Caption enabled? we don't have this.

darshanbr avatar Jun 03 '21 12:06 darshanbr

we are still trying to figure out why the stream is disconnecting in first place.

When a routing change occurs your AAudio stream will get disconnected. This is because the data path has changed and the old framesPerBurst and other stream characteristics are invalid. The error callback will be called and the stream will be closed.

If you want to reopen the stream then you can do that.

OpenSL ES will automatically reroute the stream. But that often results in a broken stream with badly sized buffers. So AAudio just disconnects the stream.

https://github.com/google/oboe/blob/master/docs/FullGuide.md#disconnected-audio-stream

philburk avatar Jun 08 '21 21:06 philburk