"Routing" an audio stream to a particular device

359 views
Skip to first unread message

Jeffrey Blattman

unread,
Jun 6, 2018, 6:24:47 PM6/6/18
to android-platform
I say "routing" in quotes as I'm not sure that doesn't mean something else in AOSP. 

What I'd like to do is change the behavior of which streams are audible on which devices. For example, I'd like to make the notification stream only audible on the speaker, regardless if there is a headset, USB audio, bluetooth audio, etc. connected.

I'm expecting to somewhere find some mapping from streams to audio devices, but I don't see that. I found audio_policy_configurations.xml that doesn't seem to describe it. I found audio_policy_volumes.xml, which looked promising. I tried changing things like,

    <volume stream="AUDIO_STREAM_NOTIFICATION" deviceCategory="DEVICE_CATEGORY_EXT_MEDIA"
                                               ref="SILENT_VOLUME_CURVE"/>

But it did not seem to have an effect.

Thanks.

Jeffrey Blattman

unread,
Jun 7, 2018, 1:59:59 PM6/7/18
to android-platform
Answering my own question. Hope it helps someone else.
  • Usages map to stream types. Usages are a newer concept that are supposed to replace explicit use of streams, but as of Oreo, the system just statically maps the usage to a legacy stream.
  • Streams map to strategies.
  • Strategies map to (logical) devices.
So for example, if the app chooses usage USAGE_NOTIFICATION_RINGTONE, this gets mapped to stream type STREAM_DTMF. That stream gets mapped to strategy STRATEGY_DTMF.

The magic then happens in frameworks/av/service/audiopolicy/enginedefault/src/Engine.cpp in method getDeviceForStrategyInt(). This method has a load of hand-coded logic for deciding the device for the strategy. So for example, for a test I wanted to block everything but USAGE_NOTIFICATION_RINGTONE from going to USB audio, I put this at the end of that method:

    if (strategy != STRATEGY_ACCESSIBILITY) {
      if ((device & AUDIO_DEVICE_OUT_USB_DEVICE) ||
            (device & AUDIO_DEVICE_OUT_USB_ACCESSORY) ||
            (device & AUDIO_DEVICE_OUT_USB_HEADSET)) {
          device = AUDIO_DEVICE_NONE;
      }
    }

As a side note, I found, in the "examples" folder, some files with the extends .pfw (e.g., device_for_strategy_media.pfw) that appear to be a way of defining a strategy to device mapping via configuration. However, our image doesn't actually install any of these files so it's not clear to me if this is functional or to what degree it's functional.

Jeffrey Blattman

unread,
Jun 7, 2018, 2:03:42 PM6/7/18
to android-platform
Error, should be "STRATEGY_DTMF".

On Thursday, June 7, 2018 at 10:59:59 AM UTC-7, Jeffrey Blattman wrote:
    if (strategy != STRATEGY_ACCESSIBILITY) {

Reply all
Reply to author
Forward
0 new messages