Home | History | Annotate | Download | only in hardware_legacy
      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_AUDIOPOLICYINTERFACE_H
     18 #define ANDROID_AUDIOPOLICYINTERFACE_H
     19 
     20 #include <media/AudioSystem.h>
     21 #include <media/ToneGenerator.h>
     22 #include <utils/String8.h>
     23 
     24 namespace android {
     25 
     26 
     27 // ----------------------------------------------------------------------------
     28 
     29 // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication interfaces
     30 // between the platform specific audio policy manager and Android generic audio policy manager.
     31 // The platform specific audio policy manager must implement methods of the AudioPolicyInterface class.
     32 // This implementation makes use of the AudioPolicyClientInterface to control the activity and
     33 // configuration of audio input and output streams.
     34 //
     35 // The platform specific audio policy manager is in charge of the audio routing and volume control
     36 // policies for a given platform.
     37 // The main roles of this module are:
     38 //   - keep track of current system state (removable device connections, phone state, user requests...).
     39 //   System state changes and user actions are notified to audio policy manager with methods of the AudioPolicyInterface.
     40 //   - process getOutput() queries received when AudioTrack objects are created: Those queries
     41 //   return a handler on an output that has been selected, configured and opened by the audio policy manager and that
     42 //   must be used by the AudioTrack when registering to the AudioFlinger with the createTrack() method.
     43 //   When the AudioTrack object is released, a putOutput() query is received and the audio policy manager can decide
     44 //   to close or reconfigure the output depending on other streams using this output and current system state.
     45 //   - similarly process getInput() and putInput() queries received from AudioRecord objects and configure audio inputs.
     46 //   - process volume control requests: the stream volume is converted from an index value (received from UI) to a float value
     47 //   applicable to each output as a function of platform specific settings and current output route (destination device). It
     48 //   also make sure that streams are not muted if not allowed (e.g. camera shutter sound in some countries).
     49 //
     50 // The platform specific audio policy manager is provided as a shared library by platform vendors (as for libaudio.so)
     51 // and is linked with libaudioflinger.so
     52 
     53 
     54 //    Audio Policy Manager Interface
     55 class AudioPolicyInterface
     56 {
     57 
     58 public:
     59     virtual ~AudioPolicyInterface() {}
     60     //
     61     // configuration functions
     62     //
     63 
     64     // indicate a change in device connection status
     65     virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
     66                                           AudioSystem::device_connection_state state,
     67                                           const char *device_address) = 0;
     68     // retreive a device connection status
     69     virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
     70                                                                           const char *device_address) = 0;
     71     // indicate a change in phone state. Valid phones states are defined by AudioSystem::audio_mode
     72     virtual void setPhoneState(int state) = 0;
     73     // indicate a change in ringer mode
     74     virtual void setRingerMode(uint32_t mode, uint32_t mask) = 0;
     75     // force using a specific device category for the specified usage
     76     virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) = 0;
     77     // retreive current device category forced for a given usage
     78     virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage) = 0;
     79     // set a system property (e.g. camera sound always audible)
     80     virtual void setSystemProperty(const char* property, const char* value) = 0;
     81 
     82 
     83     //
     84     // Audio routing query functions
     85     //
     86 
     87     // request an output appriate for playback of the supplied stream type and parameters
     88     virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
     89                                         uint32_t samplingRate = 0,
     90                                         uint32_t format = AudioSystem::FORMAT_DEFAULT,
     91                                         uint32_t channels = 0,
     92                                         AudioSystem::output_flags flags = AudioSystem::OUTPUT_FLAG_INDIRECT) = 0;
     93     // indicates to the audio policy manager that the output starts being used by corresponding stream.
     94     virtual status_t startOutput(audio_io_handle_t output,
     95                                  AudioSystem::stream_type stream,
     96                                  int session = 0) = 0;
     97     // indicates to the audio policy manager that the output stops being used by corresponding stream.
     98     virtual status_t stopOutput(audio_io_handle_t output,
     99                                 AudioSystem::stream_type stream,
    100                                 int session = 0) = 0;
    101     // releases the output.
    102     virtual void releaseOutput(audio_io_handle_t output) = 0;
    103 
    104     // request an input appriate for record from the supplied device with supplied parameters.
    105     virtual audio_io_handle_t getInput(int inputSource,
    106                                     uint32_t samplingRate = 0,
    107                                     uint32_t Format = AudioSystem::FORMAT_DEFAULT,
    108                                     uint32_t channels = 0,
    109                                     AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0) = 0;
    110     // indicates to the audio policy manager that the input starts being used.
    111     virtual status_t startInput(audio_io_handle_t input) = 0;
    112     // indicates to the audio policy manager that the input stops being used.
    113     virtual status_t stopInput(audio_io_handle_t input) = 0;
    114     // releases the input.
    115     virtual void releaseInput(audio_io_handle_t input) = 0;
    116 
    117     //
    118     // volume control functions
    119     //
    120 
    121     // initialises stream volume conversion parameters by specifying volume index range.
    122     virtual void initStreamVolume(AudioSystem::stream_type stream,
    123                                       int indexMin,
    124                                       int indexMax) = 0;
    125 
    126     // sets the new stream volume at a level corresponding to the supplied index
    127     virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index) = 0;
    128     // retreive current volume index for the specified stream
    129     virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index) = 0;
    130 
    131     // return the strategy corresponding to a given stream type
    132     virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream) = 0;
    133 
    134     // Audio effect management
    135     virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc) = 0;
    136     virtual status_t registerEffect(effect_descriptor_t *desc,
    137                                     audio_io_handle_t output,
    138                                     uint32_t strategy,
    139                                     int session,
    140                                     int id) = 0;
    141     virtual status_t unregisterEffect(int id) = 0;
    142 
    143     //dump state
    144     virtual status_t    dump(int fd) = 0;
    145 };
    146 
    147 
    148 // Audio Policy client Interface
    149 class AudioPolicyClientInterface
    150 {
    151 public:
    152     virtual ~AudioPolicyClientInterface() {}
    153 
    154     //
    155     // Audio output Control functions
    156     //
    157 
    158     // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
    159     // in case the audio policy manager has no specific requirements for the output being opened.
    160     // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
    161     // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
    162     virtual audio_io_handle_t openOutput(uint32_t *pDevices,
    163                                     uint32_t *pSamplingRate,
    164                                     uint32_t *pFormat,
    165                                     uint32_t *pChannels,
    166                                     uint32_t *pLatencyMs,
    167                                     AudioSystem::output_flags flags) = 0;
    168     // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
    169     // a special mixer thread in the AudioFlinger.
    170     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0;
    171     // closes the output stream
    172     virtual status_t closeOutput(audio_io_handle_t output) = 0;
    173     // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
    174     // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
    175     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
    176     // restores a suspended output.
    177     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
    178 
    179     //
    180     // Audio input Control functions
    181     //
    182 
    183     // opens an audio input
    184     virtual audio_io_handle_t openInput(uint32_t *pDevices,
    185                                     uint32_t *pSamplingRate,
    186                                     uint32_t *pFormat,
    187                                     uint32_t *pChannels,
    188                                     uint32_t acoustics) = 0;
    189     // closes an audio input
    190     virtual status_t closeInput(audio_io_handle_t input) = 0;
    191     //
    192     // misc control functions
    193     //
    194 
    195     // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
    196     // for each output (destination device) it is attached to.
    197     virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0;
    198 
    199     // reroute a given stream type to the specified output
    200     virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output) = 0;
    201 
    202     // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
    203     virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0;
    204     // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
    205     virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0;
    206 
    207     // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
    208     // over a telephony device during a phone call.
    209     virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0;
    210     virtual status_t stopTone() = 0;
    211 
    212     // set down link audio volume.
    213     virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0;
    214 
    215     // move effect to the specified output
    216     virtual status_t moveEffects(int session,
    217                                      audio_io_handle_t srcOutput,
    218                                      audio_io_handle_t dstOutput) = 0;
    219 
    220 };
    221 
    222 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface);
    223 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface);
    224 
    225 
    226 }; // namespace android
    227 
    228 #endif // ANDROID_AUDIOPOLICYINTERFACE_H
    229