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, AudioSystem::stream_type stream) = 0; 95 // indicates to the audio policy manager that the output stops being used by corresponding stream. 96 virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream) = 0; 97 // releases the output. 98 virtual void releaseOutput(audio_io_handle_t output) = 0; 99 100 // request an input appriate for record from the supplied device with supplied parameters. 101 virtual audio_io_handle_t getInput(int inputSource, 102 uint32_t samplingRate = 0, 103 uint32_t Format = AudioSystem::FORMAT_DEFAULT, 104 uint32_t channels = 0, 105 AudioSystem::audio_in_acoustics acoustics = (AudioSystem::audio_in_acoustics)0) = 0; 106 // indicates to the audio policy manager that the input starts being used. 107 virtual status_t startInput(audio_io_handle_t input) = 0; 108 // indicates to the audio policy manager that the input stops being used. 109 virtual status_t stopInput(audio_io_handle_t input) = 0; 110 // releases the input. 111 virtual void releaseInput(audio_io_handle_t input) = 0; 112 113 // 114 // volume control functions 115 // 116 117 // initialises stream volume conversion parameters by specifying volume index range. 118 virtual void initStreamVolume(AudioSystem::stream_type stream, 119 int indexMin, 120 int indexMax) = 0; 121 122 // sets the new stream volume at a level corresponding to the supplied index 123 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index) = 0; 124 // retreive current volume index for the specified stream 125 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index) = 0; 126 127 //dump state 128 virtual status_t dump(int fd) = 0; 129 }; 130 131 132 // Audio Policy client Interface 133 class AudioPolicyClientInterface 134 { 135 public: 136 virtual ~AudioPolicyClientInterface() {} 137 138 // 139 // Audio output Control functions 140 // 141 142 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values 143 // in case the audio policy manager has no specific requirements for the output being opened. 144 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. 145 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. 146 virtual audio_io_handle_t openOutput(uint32_t *pDevices, 147 uint32_t *pSamplingRate, 148 uint32_t *pFormat, 149 uint32_t *pChannels, 150 uint32_t *pLatencyMs, 151 AudioSystem::output_flags flags) = 0; 152 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by 153 // a special mixer thread in the AudioFlinger. 154 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2) = 0; 155 // closes the output stream 156 virtual status_t closeOutput(audio_io_handle_t output) = 0; 157 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in 158 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. 159 virtual status_t suspendOutput(audio_io_handle_t output) = 0; 160 // restores a suspended output. 161 virtual status_t restoreOutput(audio_io_handle_t output) = 0; 162 163 // 164 // Audio input Control functions 165 // 166 167 // opens an audio input 168 virtual audio_io_handle_t openInput(uint32_t *pDevices, 169 uint32_t *pSamplingRate, 170 uint32_t *pFormat, 171 uint32_t *pChannels, 172 uint32_t acoustics) = 0; 173 // closes an audio input 174 virtual status_t closeInput(audio_io_handle_t input) = 0; 175 // 176 // misc control functions 177 // 178 179 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes 180 // for each output (destination device) it is attached to. 181 virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0) = 0; 182 183 // reroute a given stream type to the specified output 184 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output) = 0; 185 186 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. 187 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0) = 0; 188 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. 189 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0; 190 191 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing 192 // over a telephony device during a phone call. 193 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0; 194 virtual status_t stopTone() = 0; 195 196 // set down link audio volume. 197 virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0; 198 }; 199 200 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface); 201 extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface); 202 203 204 }; // namespace android 205 206 #endif // ANDROID_AUDIOPOLICYINTERFACE_H 207