WebAPI/AudioChannels

This API introduces the concept of a hierarchy of audio channels. The channels are prioritized as to allow "silencing all channels with priority lower than X".

The problems that we are trying to solve are:

  • When the user answers a phone call, the sound from all apps should be silenced
  • The alarm clock shouldn't be muted even if normal audio is muted. This to prevent the user oversleeping due to having muted the phone the previous day.
  • When the user leaves an app, under normal circumstances the app should be muted.
  • Some apps need to be able to opt in to not getting muted when the user leaves the app, such as the music player app or the radio app.
  • When the volume keys are used it should change the volume for different audio types depending on context. For example while in the alarm app, the volume keys should adjust the alarm volume and not the "normal" volume.
  • When a video app starts playing audio, background music should be muted while video is playing.

See also: https://etherpad.mozilla.org/sound-stream-types

The channels are:

  • normal: UI sounds, web content, music, radio
  • content: music, video.
  • notification: New email, incoming SMS
  • alarm: Alarm clock, calendar alarms
  • ringer: Incoming phone calls.
  • telephony: Phone calls, voip calls.
  • publicnotification: Forced camera shutter sounds. This will not be in V1.

Whenever an audio channel is used, or lower priority channels are automatically paused. The only exception to this is that "normal" and "content" has the same priority which means that if the "content" channel is used, it's simply mixed with audio from the "normal" channel.

If two apps try to use the "content" channel at the same time, the foreground app wins. If both of the apps are background apps, then the last app to try to use the channel wins.

We'll have separate mute and volume settings per channel. We'll additionally have a volume and mute setting for a "headphones" channel. The "headphones" channel is used both for normal headphones as well as for bluetooth headsets.

For now all sounds are directed through headphones/headset when they are plugged in. We discussed possibly making the alarm sound through both headphones and speaker, but we deemed this a non-v1 feature. Whenever

For now all audio channels except "telephony" never use the built-in earpiece. I.e. they always use the speaker or headphones/headset. We might introduce using the built-in earpiece for "normal" sounds in a future version.

When the volume up/down buttons on a bluetooth headset is pressed, we'll treat that exactly as when the on-device volume up/down buttons are pressed.

Application API

interface AudioChannelManager : EventTarget {
 // We might not need this headphones section for v1. 
 readonly attribute boolean headphones;
 attribute EventHandler onheadphoneschange; // Always fired before audio start playing through the new channel

 attribute boolean telephonySpeaker; // Not in V1 Makes the "telephony" channel go through the speaker.
}

We'll additionally introduce a new attribute "mozchannel" on <audio> and <video> which selects which audio channel will be used for any played audio.

partial interface HTMLMediaElement {
 attribute DOMString mozchannel; // Or are we using some other name?

 readonly attribute boolean mozchannelpaused; // Returns true if this element is currently paused due to the channel being paused.
 // Should these events only fire if the element is currently played?
 attribute EventTarget onmozchannelpaused;
 attribute EventTarget onmozchannelunpaused;
}

The mozchannelpaused property can change both as a result of an app being put in the background, or as a result of the channel being paused due to a higher priority channel being used.

System and Browser API changes

We need to add settings for controlling the volume and the mute to any iframe mozbrowser. The proposal is to add this set of new properties to the browserAPI (for simplicity in webIDL):

interface BrowserAPI {

 // With this it's possible to mute all the MediaElement for this mozbrowser iframe.
 attribute boolean audioMuted;
 // range: 0.0 to 1.0 - a custom volume
 attribute double audioVolume;
 // list of active audio channels
 readonly attribute DOMString[] activeAudioChannels;
 // if this app actually playing?
 readonly attribute boolean audioActive;
 // horrible name :(
 // when this attribute is set to true, any normal channel will be 'converted'
 // to content channel. This fixes the problem of the visibility.
 // Task of the system app is to set this boolean to true before changing the visibility to false
 // for the foreground app. Implementing this, we can revert all the hacks we implemented for the
 // visibility and normal channels...
 attribute boolean normalAudioChannelToContent;

}

In addition we want to emit events when activeAudioChannels and audioActive attributes change.

All of these changes are meant to be built on top of the existing AudioChannelService. Doing that we keep the logic in gecko, but we allow custom settings:

1. the browser can let play websites as such as spotify, grooveshark and so also when the device is locked, setting normalAudioChannelToContent to true. 2. the system app can mark the foreground app, setting normalAudioChannelToContent to true before changing the visibility 3. the settings app can have configurations ad-hoc for the audio management and apps

Settings

There's multiple different settings for audio volume:

  • audio.volume.content: Affects the "content" and "normal" audio channels. It's an integer between 0 and 15 where 0 means muted.
  • audio.volume.notification: Affects the "notification" and "ringer" channels. It's an integer between 0 and 15.
  • audio.volume.alarm: Affects the "alarm" channel. It's an integer between 0 and 15.
  • audio.volume.telephony: Affects the "telephony" channel. It's an integer between 0 and 5.
  • audio.volume.bt_sco: Volume when bluetooth headset is plugged in. It's an integer between 0 and 15.

Note that headphones do not use the same audio setting as bluetooth. Instead headphones use the same volume as when headphones are not plugged in.

Security model

In order to get access to anything more than the "normal" channel, the application needs to enumerate these channels in the permissions property in the app manifest. So something like:

permissions: {
  ...
  audio: {
    channels: ["notification", "alarm"]
  },
  ...
}

This would enable the app to play sound through both the "notification" channel and the "alarm" channel.

I'm not sure if we need to have prompts if non-privileged apps try to use channels beyond the "normal" or "content" channels.