Home | History | Annotate | Download | only in audiopolicy
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_AUDIOPOLICY_INTERFACE_H
     18 #define ANDROID_AUDIOPOLICY_INTERFACE_H
     19 
     20 #include <media/AudioSystem.h>
     21 #include <media/AudioPolicy.h>
     22 #include <utils/String8.h>
     23 
     24 namespace android {
     25 
     26 // ----------------------------------------------------------------------------
     27 
     28 // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication interfaces
     29 // between the platform specific audio policy manager and Android generic audio policy manager.
     30 // The platform specific audio policy manager must implement methods of the AudioPolicyInterface class.
     31 // This implementation makes use of the AudioPolicyClientInterface to control the activity and
     32 // configuration of audio input and output streams.
     33 //
     34 // The platform specific audio policy manager is in charge of the audio routing and volume control
     35 // policies for a given platform.
     36 // The main roles of this module are:
     37 //   - keep track of current system state (removable device connections, phone state, user requests...).
     38 //   System state changes and user actions are notified to audio policy manager with methods of the AudioPolicyInterface.
     39 //   - process getOutput() queries received when AudioTrack objects are created: Those queries
     40 //   return a handler on an output that has been selected, configured and opened by the audio policy manager and that
     41 //   must be used by the AudioTrack when registering to the AudioFlinger with the createTrack() method.
     42 //   When the AudioTrack object is released, a putOutput() query is received and the audio policy manager can decide
     43 //   to close or reconfigure the output depending on other streams using this output and current system state.
     44 //   - similarly process getInput() and putInput() queries received from AudioRecord objects and configure audio inputs.
     45 //   - process volume control requests: the stream volume is converted from an index value (received from UI) to a float value
     46 //   applicable to each output as a function of platform specific settings and current output route (destination device). It
     47 //   also make sure that streams are not muted if not allowed (e.g. camera shutter sound in some countries).
     48 //
     49 // The platform specific audio policy manager is provided as a shared library by platform vendors (as for libaudio.so)
     50 // and is linked with libaudioflinger.so
     51 
     52 
     53 //    Audio Policy Manager Interface
     54 class AudioPolicyInterface
     55 {
     56 
     57 public:
     58     typedef enum {
     59         API_INPUT_INVALID = -1,
     60         API_INPUT_LEGACY  = 0,// e.g. audio recording from a microphone
     61         API_INPUT_MIX_CAPTURE,// used for "remote submix", capture of the media to play it remotely
     62         API_INPUT_MIX_EXT_POLICY_REROUTE,// used for platform audio rerouting, where mixes are
     63                                          // handled by external and dynamically installed
     64                                          // policies which reroute audio mixes
     65         API_INPUT_TELEPHONY_RX, // used for capture from telephony RX path
     66     } input_type_t;
     67 
     68     enum {
     69         API_INPUT_CONCURRENCY_NONE = 0,
     70         API_INPUT_CONCURRENCY_CALL = (1 << 0),      // Concurrency with a call
     71         API_INPUT_CONCURRENCY_CAPTURE = (1 << 1),   // Concurrency with another capture
     72         API_INPUT_CONCURRENCY_HOTWORD = (1 << 2),   // Concurrency with a hotword
     73         API_INPUT_CONCURRENCY_PREEMPT = (1 << 3),   // pre-empted someone
     74                 // NB: preempt is marked on a successful return, others are on failing calls
     75         API_INPUT_CONCURRENCY_LAST = (1 << 4),
     76 
     77         API_INPUT_CONCURRENCY_ALL = (API_INPUT_CONCURRENCY_LAST - 1),
     78     };
     79 
     80     typedef uint32_t concurrency_type__mask_t;
     81 
     82 public:
     83     virtual ~AudioPolicyInterface() {}
     84     //
     85     // configuration functions
     86     //
     87 
     88     // indicate a change in device connection status
     89     virtual status_t setDeviceConnectionState(audio_devices_t device,
     90                                               audio_policy_dev_state_t state,
     91                                               const char *device_address,
     92                                               const char *device_name) = 0;
     93     // retrieve a device connection status
     94     virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
     95                                                                           const char *device_address) = 0;
     96     // indicate a change in device configuration
     97     virtual status_t handleDeviceConfigChange(audio_devices_t device,
     98                                               const char *device_address,
     99                                               const char *device_name) = 0;
    100     // indicate a change in phone state. Valid phones states are defined by audio_mode_t
    101     virtual void setPhoneState(audio_mode_t state) = 0;
    102     // force using a specific device category for the specified usage
    103     virtual void setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) = 0;
    104     // retrieve current device category forced for a given usage
    105     virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) = 0;
    106     // set a system property (e.g. camera sound always audible)
    107     virtual void setSystemProperty(const char* property, const char* value) = 0;
    108     // check proper initialization
    109     virtual status_t initCheck() = 0;
    110 
    111     //
    112     // Audio routing query functions
    113     //
    114 
    115     // request an output appropriate for playback of the supplied stream type and parameters
    116     virtual audio_io_handle_t getOutput(audio_stream_type_t stream) = 0;
    117     virtual status_t getOutputForAttr(const audio_attributes_t *attr,
    118                                         audio_io_handle_t *output,
    119                                         audio_session_t session,
    120                                         audio_stream_type_t *stream,
    121                                         uid_t uid,
    122                                         const audio_config_t *config,
    123                                         audio_output_flags_t *flags,
    124                                         audio_port_handle_t *selectedDeviceId,
    125                                         audio_port_handle_t *portId) = 0;
    126     // indicates to the audio policy manager that the output starts being used by corresponding stream.
    127     virtual status_t startOutput(audio_io_handle_t output,
    128                                  audio_stream_type_t stream,
    129                                  audio_session_t session) = 0;
    130     // indicates to the audio policy manager that the output stops being used by corresponding stream.
    131     virtual status_t stopOutput(audio_io_handle_t output,
    132                                 audio_stream_type_t stream,
    133                                 audio_session_t session) = 0;
    134     // releases the output.
    135     virtual void releaseOutput(audio_io_handle_t output,
    136                                audio_stream_type_t stream,
    137                                audio_session_t session) = 0;
    138 
    139     // request an input appropriate for record from the supplied device with supplied parameters.
    140     virtual status_t getInputForAttr(const audio_attributes_t *attr,
    141                                      audio_io_handle_t *input,
    142                                      audio_session_t session,
    143                                      uid_t uid,
    144                                      const audio_config_base_t *config,
    145                                      audio_input_flags_t flags,
    146                                      audio_port_handle_t *selectedDeviceId,
    147                                      input_type_t *inputType,
    148                                      audio_port_handle_t *portId) = 0;
    149     // indicates to the audio policy manager that the input starts being used.
    150     virtual status_t startInput(audio_io_handle_t input,
    151                                 audio_session_t session,
    152                                 bool silenced,
    153                                 concurrency_type__mask_t *concurrency) = 0;
    154     // indicates to the audio policy manager that the input stops being used.
    155     virtual status_t stopInput(audio_io_handle_t input,
    156                                audio_session_t session) = 0;
    157     // releases the input.
    158     virtual void releaseInput(audio_io_handle_t input,
    159                               audio_session_t session) = 0;
    160 
    161     //
    162     // volume control functions
    163     //
    164 
    165     // initialises stream volume conversion parameters by specifying volume index range.
    166     virtual void initStreamVolume(audio_stream_type_t stream,
    167                                       int indexMin,
    168                                       int indexMax) = 0;
    169 
    170     // sets the new stream volume at a level corresponding to the supplied index for the
    171     // supplied device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME means
    172     // setting volume for all devices
    173     virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
    174                                           int index,
    175                                           audio_devices_t device) = 0;
    176 
    177     // retrieve current volume index for the specified stream and the
    178     // specified device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME means
    179     // querying the volume of the active device.
    180     virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
    181                                           int *index,
    182                                           audio_devices_t device) = 0;
    183 
    184     // return the strategy corresponding to a given stream type
    185     virtual uint32_t getStrategyForStream(audio_stream_type_t stream) = 0;
    186 
    187     // return the enabled output devices for the given stream type
    188     virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream) = 0;
    189 
    190     // Audio effect management
    191     virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc) = 0;
    192     virtual status_t registerEffect(const effect_descriptor_t *desc,
    193                                     audio_io_handle_t io,
    194                                     uint32_t strategy,
    195                                     int session,
    196                                     int id) = 0;
    197     virtual status_t unregisterEffect(int id) = 0;
    198     virtual status_t setEffectEnabled(int id, bool enabled) = 0;
    199 
    200     virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const = 0;
    201     virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
    202                                         uint32_t inPastMs = 0) const = 0;
    203     virtual bool isSourceActive(audio_source_t source) const = 0;
    204 
    205     //dump state
    206     virtual status_t    dump(int fd) = 0;
    207 
    208     virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo) = 0;
    209 
    210     virtual status_t listAudioPorts(audio_port_role_t role,
    211                                     audio_port_type_t type,
    212                                     unsigned int *num_ports,
    213                                     struct audio_port *ports,
    214                                     unsigned int *generation) = 0;
    215     virtual status_t getAudioPort(struct audio_port *port) = 0;
    216     virtual status_t createAudioPatch(const struct audio_patch *patch,
    217                                        audio_patch_handle_t *handle,
    218                                        uid_t uid) = 0;
    219     virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
    220                                           uid_t uid) = 0;
    221     virtual status_t listAudioPatches(unsigned int *num_patches,
    222                                       struct audio_patch *patches,
    223                                       unsigned int *generation) = 0;
    224     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
    225     virtual void releaseResourcesForUid(uid_t uid) = 0;
    226 
    227     virtual status_t acquireSoundTriggerSession(audio_session_t *session,
    228                                            audio_io_handle_t *ioHandle,
    229                                            audio_devices_t *device) = 0;
    230 
    231     virtual status_t releaseSoundTriggerSession(audio_session_t session) = 0;
    232 
    233     virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes) = 0;
    234     virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes) = 0;
    235 
    236     virtual status_t startAudioSource(const struct audio_port_config *source,
    237                                       const audio_attributes_t *attributes,
    238                                       audio_patch_handle_t *handle,
    239                                       uid_t uid) = 0;
    240     virtual status_t stopAudioSource(audio_patch_handle_t handle) = 0;
    241 
    242     virtual status_t setMasterMono(bool mono) = 0;
    243     virtual status_t getMasterMono(bool *mono) = 0;
    244 
    245     virtual float    getStreamVolumeDB(
    246                 audio_stream_type_t stream, int index, audio_devices_t device) = 0;
    247 
    248     virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
    249                                         audio_format_t *surroundFormats,
    250                                         bool *surroundFormatsEnabled,
    251                                         bool reported) = 0;
    252     virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) = 0;
    253 
    254     virtual void     setRecordSilenced(uid_t uid, bool silenced);
    255 };
    256 
    257 
    258 // Audio Policy client Interface
    259 class AudioPolicyClientInterface
    260 {
    261 public:
    262     virtual ~AudioPolicyClientInterface() {}
    263 
    264     //
    265     // Audio HW module functions
    266     //
    267 
    268     // loads a HW module.
    269     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
    270 
    271     //
    272     // Audio output Control functions
    273     //
    274 
    275     // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
    276     // in case the audio policy manager has no specific requirements for the output being opened.
    277     // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
    278     // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
    279     virtual status_t openOutput(audio_module_handle_t module,
    280                                 audio_io_handle_t *output,
    281                                 audio_config_t *config,
    282                                 audio_devices_t *devices,
    283                                 const String8& address,
    284                                 uint32_t *latencyMs,
    285                                 audio_output_flags_t flags) = 0;
    286     // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
    287     // a special mixer thread in the AudioFlinger.
    288     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0;
    289     // closes the output stream
    290     virtual status_t closeOutput(audio_io_handle_t output) = 0;
    291     // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
    292     // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
    293     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
    294     // restores a suspended output.
    295     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
    296 
    297     //
    298     // Audio input Control functions
    299     //
    300 
    301     // opens an audio input
    302     virtual status_t openInput(audio_module_handle_t module,
    303                                audio_io_handle_t *input,
    304                                audio_config_t *config,
    305                                audio_devices_t *device,
    306                                const String8& address,
    307                                audio_source_t source,
    308                                audio_input_flags_t flags) = 0;
    309     // closes an audio input
    310     virtual status_t closeInput(audio_io_handle_t input) = 0;
    311     //
    312     // misc control functions
    313     //
    314 
    315     // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
    316     // for each output (destination device) it is attached to.
    317     virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0;
    318 
    319     // invalidate a stream type, causing a reroute to an unspecified new output
    320     virtual status_t invalidateStream(audio_stream_type_t stream) = 0;
    321 
    322     // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
    323     virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0;
    324     // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
    325     virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0;
    326 
    327     // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
    328     // over a telephony device during a phone call.
    329     virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream) = 0;
    330     virtual status_t stopTone() = 0;
    331 
    332     // set down link audio volume.
    333     virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0;
    334 
    335     // move effect to the specified output
    336     virtual status_t moveEffects(audio_session_t session,
    337                                      audio_io_handle_t srcOutput,
    338                                      audio_io_handle_t dstOutput) = 0;
    339 
    340     /* Create a patch between several source and sink ports */
    341     virtual status_t createAudioPatch(const struct audio_patch *patch,
    342                                        audio_patch_handle_t *handle,
    343                                        int delayMs) = 0;
    344 
    345     /* Release a patch */
    346     virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
    347                                        int delayMs) = 0;
    348 
    349     /* Set audio port configuration */
    350     virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs) = 0;
    351 
    352     virtual void onAudioPortListUpdate() = 0;
    353 
    354     virtual void onAudioPatchListUpdate() = 0;
    355 
    356     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
    357 
    358     virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state) = 0;
    359 
    360     virtual void onRecordingConfigurationUpdate(int event,
    361                     const record_client_info_t *clientInfo,
    362                     const struct audio_config_base *clientConfig,
    363                     const struct audio_config_base *deviceConfig,
    364                     audio_patch_handle_t patchHandle) = 0;
    365 };
    366 
    367 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface);
    368 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface);
    369 
    370 
    371 } // namespace android
    372 
    373 #endif // ANDROID_AUDIOPOLICY_INTERFACE_H
    374