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 18 #include <stdint.h> 19 #include <sys/types.h> 20 #include <utils/Timers.h> 21 #include <utils/Errors.h> 22 #include <utils/KeyedVector.h> 23 #include <hardware_legacy/AudioPolicyInterface.h> 24 25 26 namespace android { 27 28 // ---------------------------------------------------------------------------- 29 30 #define MAX_DEVICE_ADDRESS_LEN 20 31 // Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB 32 #define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5 33 // Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB 34 #define SONIFICATION_HEADSET_VOLUME_MIN 0.016 35 // Time in seconds during which we consider that music is still active after a music 36 // track was stopped - see computeVolume() 37 #define SONIFICATION_HEADSET_MUSIC_DELAY 5 38 // Time in milliseconds during witch some streams are muted while the audio path 39 // is switched 40 #define MUTE_TIME_MS 2000 41 42 #define NUM_TEST_OUTPUTS 5 43 44 // ---------------------------------------------------------------------------- 45 // AudioPolicyManagerBase implements audio policy manager behavior common to all platforms. 46 // Each platform must implement an AudioPolicyManager class derived from AudioPolicyManagerBase 47 // and override methods for which the platform specific behavior differs from the implementation 48 // in AudioPolicyManagerBase. Even if no specific behavior is required, the AudioPolicyManager 49 // class must be implemented as well as the class factory function createAudioPolicyManager() 50 // and provided in a shared library libaudiopolicy.so. 51 // ---------------------------------------------------------------------------- 52 53 class AudioPolicyManagerBase: public AudioPolicyInterface 54 #ifdef AUDIO_POLICY_TEST 55 , public Thread 56 #endif //AUDIO_POLICY_TEST 57 { 58 59 public: 60 AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface); 61 virtual ~AudioPolicyManagerBase(); 62 63 // AudioPolicyInterface 64 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device, 65 AudioSystem::device_connection_state state, 66 const char *device_address); 67 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device, 68 const char *device_address); 69 virtual void setPhoneState(int state); 70 virtual void setRingerMode(uint32_t mode, uint32_t mask); 71 virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config); 72 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage); 73 virtual void setSystemProperty(const char* property, const char* value); 74 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, 75 uint32_t samplingRate = 0, 76 uint32_t format = AudioSystem::FORMAT_DEFAULT, 77 uint32_t channels = 0, 78 AudioSystem::output_flags flags = 79 AudioSystem::OUTPUT_FLAG_INDIRECT); 80 virtual status_t startOutput(audio_io_handle_t output, 81 AudioSystem::stream_type stream, 82 int session = 0); 83 virtual status_t stopOutput(audio_io_handle_t output, 84 AudioSystem::stream_type stream, 85 int session = 0); 86 virtual void releaseOutput(audio_io_handle_t output); 87 virtual audio_io_handle_t getInput(int inputSource, 88 uint32_t samplingRate, 89 uint32_t format, 90 uint32_t channels, 91 AudioSystem::audio_in_acoustics acoustics); 92 // indicates to the audio policy manager that the input starts being used. 93 virtual status_t startInput(audio_io_handle_t input); 94 // indicates to the audio policy manager that the input stops being used. 95 virtual status_t stopInput(audio_io_handle_t input); 96 virtual void releaseInput(audio_io_handle_t input); 97 virtual void initStreamVolume(AudioSystem::stream_type stream, 98 int indexMin, 99 int indexMax); 100 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index); 101 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index); 102 103 // return the strategy corresponding to a given stream type 104 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream); 105 106 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); 107 virtual status_t registerEffect(effect_descriptor_t *desc, 108 audio_io_handle_t output, 109 uint32_t strategy, 110 int session, 111 int id); 112 virtual status_t unregisterEffect(int id); 113 114 virtual status_t dump(int fd); 115 116 protected: 117 118 enum routing_strategy { 119 STRATEGY_MEDIA, 120 STRATEGY_PHONE, 121 STRATEGY_SONIFICATION, 122 STRATEGY_DTMF, 123 NUM_STRATEGIES 124 }; 125 126 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output 127 // and keep track of the usage of this output by each audio stream type. 128 class AudioOutputDescriptor 129 { 130 public: 131 AudioOutputDescriptor(); 132 133 status_t dump(int fd); 134 135 uint32_t device(); 136 void changeRefCount(AudioSystem::stream_type, int delta); 137 uint32_t refCount(); 138 uint32_t strategyRefCount(routing_strategy strategy); 139 bool isUsedByStrategy(routing_strategy strategy) { return (strategyRefCount(strategy) != 0);} 140 bool isDuplicated() { return (mOutput1 != NULL && mOutput2 != NULL); } 141 142 audio_io_handle_t mId; // output handle 143 uint32_t mSamplingRate; // 144 uint32_t mFormat; // 145 uint32_t mChannels; // output configuration 146 uint32_t mLatency; // 147 AudioSystem::output_flags mFlags; // 148 uint32_t mDevice; // current device this output is routed to 149 uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output 150 AudioOutputDescriptor *mOutput1; // used by duplicated outputs: first output 151 AudioOutputDescriptor *mOutput2; // used by duplicated outputs: second output 152 float mCurVolume[AudioSystem::NUM_STREAM_TYPES]; // current stream volume 153 int mMuteCount[AudioSystem::NUM_STREAM_TYPES]; // mute request counter 154 }; 155 156 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input 157 // and keep track of the usage of this input. 158 class AudioInputDescriptor 159 { 160 public: 161 AudioInputDescriptor(); 162 163 status_t dump(int fd); 164 165 uint32_t mSamplingRate; // 166 uint32_t mFormat; // input configuration 167 uint32_t mChannels; // 168 AudioSystem::audio_in_acoustics mAcoustics; // 169 uint32_t mDevice; // current device this input is routed to 170 uint32_t mRefCount; // number of AudioRecord clients using this output 171 int mInputSource; // input source selected by application (mediarecorder.h) 172 }; 173 174 // stream descriptor used for volume control 175 class StreamDescriptor 176 { 177 public: 178 StreamDescriptor() 179 : mIndexMin(0), mIndexMax(1), mIndexCur(1), mCanBeMuted(true) {} 180 181 void dump(char* buffer, size_t size); 182 183 int mIndexMin; // min volume index 184 int mIndexMax; // max volume index 185 int mIndexCur; // current volume index 186 bool mCanBeMuted; // true is the stream can be muted 187 }; 188 189 // stream descriptor used for volume control 190 class EffectDescriptor 191 { 192 public: 193 194 status_t dump(int fd); 195 196 int mOutput; // output the effect is attached to 197 routing_strategy mStrategy; // routing strategy the effect is associated to 198 int mSession; // audio session the effect is on 199 effect_descriptor_t mDesc; // effect descriptor 200 }; 201 202 void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc); 203 204 // return the strategy corresponding to a given stream type 205 static routing_strategy getStrategy(AudioSystem::stream_type stream); 206 // return appropriate device for streams handled by the specified strategy according to current 207 // phone state, connected devices... 208 // if fromCache is true, the device is returned from mDeviceForStrategy[], otherwise it is determined 209 // by current state (device connected, phone state, force use, a2dp output...) 210 // This allows to: 211 // 1 speed up process when the state is stable (when starting or stopping an output) 212 // 2 access to either current device selection (fromCache == true) or 213 // "future" device selection (fromCache == false) when called from a context 214 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND 215 // before updateDeviceForStrategy() is called. 216 virtual uint32_t getDeviceForStrategy(routing_strategy strategy, bool fromCache = true); 217 // change the route of the specified output 218 void setOutputDevice(audio_io_handle_t output, uint32_t device, bool force = false, int delayMs = 0); 219 // select input device corresponding to requested audio source 220 virtual uint32_t getDeviceForInputSource(int inputSource); 221 // return io handle of active input or 0 if no input is active 222 audio_io_handle_t getActiveInput(); 223 // compute the actual volume for a given stream according to the requested index and a particular 224 // device 225 virtual float computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device); 226 // check that volume change is permitted, compute and send new volume to audio hardware 227 status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false); 228 // apply all stream volumes to the specified output and device 229 void applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs = 0); 230 // Mute or unmute all streams handled by the specified strategy on the specified output 231 void setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs = 0); 232 // Mute or unmute the stream on the specified output 233 void setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs = 0); 234 // handle special cases for sonification strategy while in call: mute streams or replace by 235 // a special tone in the device used for communication 236 void handleIncallSonification(int stream, bool starting, bool stateChange); 237 // true is current platform implements a back microphone 238 virtual bool hasBackMicrophone() const { return false; } 239 240 #ifdef WITH_A2DP 241 // true is current platform supports suplication of notifications and ringtones over A2DP output 242 virtual bool a2dpUsedForSonification() const { return true; } 243 status_t handleA2dpConnection(AudioSystem::audio_devices device, 244 const char *device_address); 245 status_t handleA2dpDisconnection(AudioSystem::audio_devices device, 246 const char *device_address); 247 void closeA2dpOutputs(); 248 // checks and if necessary changes output (a2dp, duplicated or hardware) used for all strategies. 249 // must be called every time a condition that affects the output choice for a given strategy is 250 // changed: connected device, phone state, force use... 251 // Must be called before updateDeviceForStrategy() 252 void checkOutputForStrategy(routing_strategy strategy); 253 // Same as checkOutputForStrategy() but for a all strategies in order of priority 254 void checkOutputForAllStrategies(); 255 256 #endif 257 // selects the most appropriate device on output for current state 258 // must be called every time a condition that affects the device choice for a given output is 259 // changed: connected device, phone state, force use, output start, output stop.. 260 // see getDeviceForStrategy() for the use of fromCache parameter 261 uint32_t getNewDevice(audio_io_handle_t output, bool fromCache = true); 262 // updates cache of device used by all strategies (mDeviceForStrategy[]) 263 // must be called every time a condition that affects the device choice for a given strategy is 264 // changed: connected device, phone state, force use... 265 // cached values are used by getDeviceForStrategy() if parameter fromCache is true. 266 // Must be called after checkOutputForAllStrategies() 267 void updateDeviceForStrategy(); 268 // true if current platform requires a specific output to be opened for this particular 269 // set of parameters. This function is called by getOutput() and is implemented by platform 270 // specific audio policy manager. 271 virtual bool needsDirectOuput(AudioSystem::stream_type stream, 272 uint32_t samplingRate, 273 uint32_t format, 274 uint32_t channels, 275 AudioSystem::output_flags flags, 276 uint32_t device); 277 virtual uint32_t getMaxEffectsCpuLoad(); 278 virtual uint32_t getMaxEffectsMemory(); 279 #ifdef AUDIO_POLICY_TEST 280 virtual bool threadLoop(); 281 void exit(); 282 int testOutputIndex(audio_io_handle_t output); 283 #endif //AUDIO_POLICY_TEST 284 285 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface 286 audio_io_handle_t mHardwareOutput; // hardware output handler 287 audio_io_handle_t mA2dpOutput; // A2DP output handler 288 audio_io_handle_t mDuplicatedOutput; // duplicated output handler: outputs to hardware and A2DP. 289 290 KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs; // list of output descriptors 291 KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors 292 uint32_t mAvailableOutputDevices; // bit field of all available output devices 293 uint32_t mAvailableInputDevices; // bit field of all available input devices 294 int mPhoneState; // current phone state 295 uint32_t mRingerMode; // current ringer mode 296 AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE]; // current forced use configuration 297 298 StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES]; // stream descriptors for volume control 299 String8 mA2dpDeviceAddress; // A2DP device MAC address 300 String8 mScoDeviceAddress; // SCO device MAC address 301 nsecs_t mMusicStopTime; // time when last music stream was stopped 302 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected 303 uint32_t mDeviceForStrategy[NUM_STRATEGIES]; 304 305 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units 306 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000; 307 // Maximum memory allocated to audio effects in KB 308 static const uint32_t MAX_EFFECTS_MEMORY = 512; 309 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects 310 uint32_t mTotalEffectsMemory; // current memory used by effects 311 KeyedVector<int, EffectDescriptor *> mEffects; // list of registered audio effects 312 313 #ifdef AUDIO_POLICY_TEST 314 Mutex mLock; 315 Condition mWaitWorkCV; 316 317 int mCurOutput; 318 bool mDirectOutput; 319 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; 320 int mTestInput; 321 uint32_t mTestDevice; 322 uint32_t mTestSamplingRate; 323 uint32_t mTestFormat; 324 uint32_t mTestChannels; 325 uint32_t mTestLatencyMs; 326 #endif //AUDIO_POLICY_TEST 327 }; 328 329 }; 330