Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 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 <cutils/config_utils.h> |
| 21 | #include <cutils/misc.h> |
| 22 | #include <utils/Timers.h> |
| 23 | #include <utils/Errors.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | #include <utils/SortedVector.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 26 | #include "AudioPolicyInterface.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 27 | |
| 28 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 29 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | #define MAX_DEVICE_ADDRESS_LEN 20 |
| 34 | // Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB |
| 35 | #define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5 |
| 36 | // Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB |
| 37 | #define SONIFICATION_HEADSET_VOLUME_MIN 0.016 |
| 38 | // Time in milliseconds during which we consider that music is still active after a music |
| 39 | // track was stopped - see computeVolume() |
| 40 | #define SONIFICATION_HEADSET_MUSIC_DELAY 5000 |
| 41 | // Time in milliseconds after media stopped playing during which we consider that the |
| 42 | // sonification should be as unobtrusive as during the time media was playing. |
| 43 | #define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000 |
| 44 | // Time in milliseconds during witch some streams are muted while the audio path |
| 45 | // is switched |
| 46 | #define MUTE_TIME_MS 2000 |
| 47 | |
| 48 | #define NUM_TEST_OUTPUTS 5 |
| 49 | |
| 50 | #define NUM_VOL_CURVE_KNEES 2 |
| 51 | |
| 52 | // Default minimum length allowed for offloading a compressed track |
| 53 | // Can be overridden by the audio.offload.min.duration.secs property |
| 54 | #define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60 |
| 55 | |
| 56 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 57 | // AudioPolicyManager implements audio policy manager behavior common to all platforms. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
| 59 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 60 | class AudioPolicyManager: public AudioPolicyInterface |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 61 | #ifdef AUDIO_POLICY_TEST |
| 62 | , public Thread |
| 63 | #endif //AUDIO_POLICY_TEST |
| 64 | { |
| 65 | |
| 66 | public: |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 67 | AudioPolicyManager(AudioPolicyClientInterface *clientInterface); |
| 68 | virtual ~AudioPolicyManager(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 69 | |
| 70 | // AudioPolicyInterface |
| 71 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 72 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 73 | const char *device_address); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 74 | virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 75 | const char *device_address); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 76 | virtual void setPhoneState(audio_mode_t state); |
| 77 | virtual void setForceUse(audio_policy_force_use_t usage, |
| 78 | audio_policy_forced_cfg_t config); |
| 79 | virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 80 | virtual void setSystemProperty(const char* property, const char* value); |
| 81 | virtual status_t initCheck(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 82 | virtual audio_io_handle_t getOutput(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 83 | uint32_t samplingRate, |
| 84 | audio_format_t format, |
| 85 | audio_channel_mask_t channelMask, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 86 | audio_output_flags_t flags, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | const audio_offload_info_t *offloadInfo); |
| 88 | virtual status_t startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 89 | audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 90 | int session = 0); |
| 91 | virtual status_t stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 92 | audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 93 | int session = 0); |
| 94 | virtual void releaseOutput(audio_io_handle_t output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 95 | virtual audio_io_handle_t getInput(audio_source_t inputSource, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 96 | uint32_t samplingRate, |
| 97 | audio_format_t format, |
| 98 | audio_channel_mask_t channelMask, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 99 | audio_in_acoustics_t acoustics); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | |
| 101 | // indicates to the audio policy manager that the input starts being used. |
| 102 | virtual status_t startInput(audio_io_handle_t input); |
| 103 | |
| 104 | // indicates to the audio policy manager that the input stops being used. |
| 105 | virtual status_t stopInput(audio_io_handle_t input); |
| 106 | virtual void releaseInput(audio_io_handle_t input); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 107 | virtual void initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | int indexMin, |
| 109 | int indexMax); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 110 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 111 | int index, |
| 112 | audio_devices_t device); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 113 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 114 | int *index, |
| 115 | audio_devices_t device); |
| 116 | |
| 117 | // return the strategy corresponding to a given stream type |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 118 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 119 | |
| 120 | // return the enabled output devices for the given stream type |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 121 | virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 122 | |
| 123 | virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL); |
| 124 | virtual status_t registerEffect(const effect_descriptor_t *desc, |
| 125 | audio_io_handle_t io, |
| 126 | uint32_t strategy, |
| 127 | int session, |
| 128 | int id); |
| 129 | virtual status_t unregisterEffect(int id); |
| 130 | virtual status_t setEffectEnabled(int id, bool enabled); |
| 131 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 132 | virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 133 | // return whether a stream is playing remotely, override to change the definition of |
| 134 | // local/remote playback, used for instance by notification manager to not make |
| 135 | // media players lose audio focus when not playing locally |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 136 | virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 137 | virtual bool isSourceActive(audio_source_t source) const; |
| 138 | |
| 139 | virtual status_t dump(int fd); |
| 140 | |
| 141 | virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo); |
| 142 | |
| 143 | protected: |
| 144 | |
| 145 | enum routing_strategy { |
| 146 | STRATEGY_MEDIA, |
| 147 | STRATEGY_PHONE, |
| 148 | STRATEGY_SONIFICATION, |
| 149 | STRATEGY_SONIFICATION_RESPECTFUL, |
| 150 | STRATEGY_DTMF, |
| 151 | STRATEGY_ENFORCED_AUDIBLE, |
| 152 | NUM_STRATEGIES |
| 153 | }; |
| 154 | |
| 155 | // 4 points to define the volume attenuation curve, each characterized by the volume |
| 156 | // index (from 0 to 100) at which they apply, and the attenuation in dB at that index. |
| 157 | // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl() |
| 158 | |
| 159 | enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4}; |
| 160 | |
| 161 | class VolumeCurvePoint |
| 162 | { |
| 163 | public: |
| 164 | int mIndex; |
| 165 | float mDBAttenuation; |
| 166 | }; |
| 167 | |
| 168 | // device categories used for volume curve management. |
| 169 | enum device_category { |
| 170 | DEVICE_CATEGORY_HEADSET, |
| 171 | DEVICE_CATEGORY_SPEAKER, |
| 172 | DEVICE_CATEGORY_EARPIECE, |
| 173 | DEVICE_CATEGORY_CNT |
| 174 | }; |
| 175 | |
| 176 | class IOProfile; |
| 177 | |
| 178 | class HwModule { |
| 179 | public: |
| 180 | HwModule(const char *name); |
| 181 | ~HwModule(); |
| 182 | |
| 183 | void dump(int fd); |
| 184 | |
| 185 | const char *const mName; // base name of the audio HW module (primary, a2dp ...) |
| 186 | audio_module_handle_t mHandle; |
| 187 | Vector <IOProfile *> mOutputProfiles; // output profiles exposed by this module |
| 188 | Vector <IOProfile *> mInputProfiles; // input profiles exposed by this module |
| 189 | }; |
| 190 | |
| 191 | // the IOProfile class describes the capabilities of an output or input stream. |
| 192 | // It is currently assumed that all combination of listed parameters are supported. |
| 193 | // It is used by the policy manager to determine if an output or input is suitable for |
| 194 | // a given use case, open/close it accordingly and connect/disconnect audio tracks |
| 195 | // to/from it. |
| 196 | class IOProfile |
| 197 | { |
| 198 | public: |
| 199 | IOProfile(HwModule *module); |
| 200 | ~IOProfile(); |
| 201 | |
| 202 | bool isCompatibleProfile(audio_devices_t device, |
| 203 | uint32_t samplingRate, |
| 204 | audio_format_t format, |
| 205 | audio_channel_mask_t channelMask, |
| 206 | audio_output_flags_t flags) const; |
| 207 | |
| 208 | void dump(int fd); |
| 209 | |
| 210 | // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats |
| 211 | // indicates the supported parameters should be read from the output stream |
| 212 | // after it is opened for the first time |
| 213 | Vector <uint32_t> mSamplingRates; // supported sampling rates |
| 214 | Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks |
| 215 | Vector <audio_format_t> mFormats; // supported audio formats |
| 216 | audio_devices_t mSupportedDevices; // supported devices (devices this output can be |
| 217 | // routed to) |
| 218 | audio_output_flags_t mFlags; // attribute flags (e.g primary output, |
| 219 | // direct output...). For outputs only. |
| 220 | HwModule *mModule; // audio HW module exposing this I/O stream |
| 221 | }; |
| 222 | |
| 223 | // default volume curve |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 224 | static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 225 | // default volume curve for media strategy |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 226 | static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 227 | // volume curve for media strategy on speakers |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 228 | static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 229 | // volume curve for sonification strategy on speakers |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 230 | static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 231 | static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT]; |
| 232 | static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 233 | static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT]; |
| 234 | static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 235 | static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 236 | static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 237 | // default volume curves per stream and device category. See initializeVolumeCurves() |
| 238 | static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT]; |
| 239 | |
| 240 | // descriptor for audio outputs. Used to maintain current configuration of each opened audio output |
| 241 | // and keep track of the usage of this output by each audio stream type. |
| 242 | class AudioOutputDescriptor |
| 243 | { |
| 244 | public: |
| 245 | AudioOutputDescriptor(const IOProfile *profile); |
| 246 | |
| 247 | status_t dump(int fd); |
| 248 | |
| 249 | audio_devices_t device() const; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 250 | void changeRefCount(audio_stream_type_t stream, int delta); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 251 | |
| 252 | bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); } |
| 253 | audio_devices_t supportedDevices(); |
| 254 | uint32_t latency(); |
| 255 | bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc); |
| 256 | bool isActive(uint32_t inPastMs = 0) const; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 257 | bool isStreamActive(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 258 | uint32_t inPastMs = 0, |
| 259 | nsecs_t sysTime = 0) const; |
| 260 | bool isStrategyActive(routing_strategy strategy, |
| 261 | uint32_t inPastMs = 0, |
| 262 | nsecs_t sysTime = 0) const; |
| 263 | |
| 264 | audio_io_handle_t mId; // output handle |
| 265 | uint32_t mSamplingRate; // |
| 266 | audio_format_t mFormat; // |
| 267 | audio_channel_mask_t mChannelMask; // output configuration |
| 268 | uint32_t mLatency; // |
| 269 | audio_output_flags_t mFlags; // |
| 270 | audio_devices_t mDevice; // current device this output is routed to |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 271 | uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output |
| 272 | nsecs_t mStopTime[AUDIO_STREAM_CNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 273 | AudioOutputDescriptor *mOutput1; // used by duplicated outputs: first output |
| 274 | AudioOutputDescriptor *mOutput2; // used by duplicated outputs: second output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 275 | float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume |
| 276 | int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 277 | const IOProfile *mProfile; // I/O profile this output derives from |
| 278 | bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible |
| 279 | // device selection. See checkDeviceMuteStrategies() |
| 280 | uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only) |
| 281 | }; |
| 282 | |
| 283 | // descriptor for audio inputs. Used to maintain current configuration of each opened audio input |
| 284 | // and keep track of the usage of this input. |
| 285 | class AudioInputDescriptor |
| 286 | { |
| 287 | public: |
| 288 | AudioInputDescriptor(const IOProfile *profile); |
| 289 | |
| 290 | status_t dump(int fd); |
| 291 | |
| 292 | uint32_t mSamplingRate; // |
| 293 | audio_format_t mFormat; // input configuration |
| 294 | audio_channel_mask_t mChannelMask; // |
| 295 | audio_devices_t mDevice; // current device this input is routed to |
| 296 | uint32_t mRefCount; // number of AudioRecord clients using this output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 297 | audio_source_t mInputSource; // input source selected by application (mediarecorder.h) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 298 | const IOProfile *mProfile; // I/O profile this output derives from |
| 299 | }; |
| 300 | |
| 301 | // stream descriptor used for volume control |
| 302 | class StreamDescriptor |
| 303 | { |
| 304 | public: |
| 305 | StreamDescriptor(); |
| 306 | |
| 307 | int getVolumeIndex(audio_devices_t device); |
| 308 | void dump(int fd); |
| 309 | |
| 310 | int mIndexMin; // min volume index |
| 311 | int mIndexMax; // max volume index |
| 312 | KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device |
| 313 | bool mCanBeMuted; // true is the stream can be muted |
| 314 | |
| 315 | const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT]; |
| 316 | }; |
| 317 | |
| 318 | // stream descriptor used for volume control |
| 319 | class EffectDescriptor |
| 320 | { |
| 321 | public: |
| 322 | |
| 323 | status_t dump(int fd); |
| 324 | |
| 325 | int mIo; // io the effect is attached to |
| 326 | routing_strategy mStrategy; // routing strategy the effect is associated to |
| 327 | int mSession; // audio session the effect is on |
| 328 | effect_descriptor_t mDesc; // effect descriptor |
| 329 | bool mEnabled; // enabled state: CPU load being used or not |
| 330 | }; |
| 331 | |
| 332 | void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc); |
| 333 | |
| 334 | // return the strategy corresponding to a given stream type |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 335 | static routing_strategy getStrategy(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 336 | |
| 337 | // return appropriate device for streams handled by the specified strategy according to current |
| 338 | // phone state, connected devices... |
| 339 | // if fromCache is true, the device is returned from mDeviceForStrategy[], |
| 340 | // otherwise it is determine by current state |
| 341 | // (device connected,phone state, force use, a2dp output...) |
| 342 | // This allows to: |
| 343 | // 1 speed up process when the state is stable (when starting or stopping an output) |
| 344 | // 2 access to either current device selection (fromCache == true) or |
| 345 | // "future" device selection (fromCache == false) when called from a context |
| 346 | // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND |
| 347 | // before updateDevicesAndOutputs() is called. |
| 348 | virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy, |
| 349 | bool fromCache); |
| 350 | |
| 351 | // change the route of the specified output. Returns the number of ms we have slept to |
| 352 | // allow new routing to take effect in certain cases. |
| 353 | uint32_t setOutputDevice(audio_io_handle_t output, |
| 354 | audio_devices_t device, |
| 355 | bool force = false, |
| 356 | int delayMs = 0); |
| 357 | |
| 358 | // select input device corresponding to requested audio source |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 359 | virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | |
| 361 | // return io handle of active input or 0 if no input is active |
| 362 | // Only considers inputs from physical devices (e.g. main mic, headset mic) when |
| 363 | // ignoreVirtualInputs is true. |
| 364 | audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true); |
| 365 | |
| 366 | // initialize volume curves for each strategy and device category |
| 367 | void initializeVolumeCurves(); |
| 368 | |
| 369 | // compute the actual volume for a given stream according to the requested index and a particular |
| 370 | // device |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 371 | virtual float computeVolume(audio_stream_type_t stream, int index, |
| 372 | audio_io_handle_t output, audio_devices_t device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 373 | |
| 374 | // check that volume change is permitted, compute and send new volume to audio hardware |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 375 | status_t checkAndSetVolume(audio_stream_type_t stream, int index, audio_io_handle_t output, |
| 376 | audio_devices_t device, int delayMs = 0, bool force = false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 377 | |
| 378 | // apply all stream volumes to the specified output and device |
| 379 | void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false); |
| 380 | |
| 381 | // Mute or unmute all streams handled by the specified strategy on the specified output |
| 382 | void setStrategyMute(routing_strategy strategy, |
| 383 | bool on, |
| 384 | audio_io_handle_t output, |
| 385 | int delayMs = 0, |
| 386 | audio_devices_t device = (audio_devices_t)0); |
| 387 | |
| 388 | // Mute or unmute the stream on the specified output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 389 | void setStreamMute(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 390 | bool on, |
| 391 | audio_io_handle_t output, |
| 392 | int delayMs = 0, |
| 393 | audio_devices_t device = (audio_devices_t)0); |
| 394 | |
| 395 | // handle special cases for sonification strategy while in call: mute streams or replace by |
| 396 | // a special tone in the device used for communication |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 397 | void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 398 | |
| 399 | // true if device is in a telephony or VoIP call |
| 400 | virtual bool isInCall(); |
| 401 | |
| 402 | // true if given state represents a device in a telephony or VoIP call |
| 403 | virtual bool isStateInCall(int state); |
| 404 | |
| 405 | // when a device is connected, checks if an open output can be routed |
| 406 | // to this device. If none is open, tries to open one of the available outputs. |
| 407 | // Returns an output suitable to this device or 0. |
| 408 | // when a device is disconnected, checks if an output is not used any more and |
| 409 | // returns its handle if any. |
| 410 | // transfers the audio tracks and effects from one output thread to another accordingly. |
| 411 | status_t checkOutputsForDevice(audio_devices_t device, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 412 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 413 | SortedVector<audio_io_handle_t>& outputs, |
| 414 | const String8 paramStr); |
| 415 | |
| 416 | // close an output and its companion duplicating output. |
| 417 | void closeOutput(audio_io_handle_t output); |
| 418 | |
| 419 | // checks and if necessary changes outputs used for all strategies. |
| 420 | // must be called every time a condition that affects the output choice for a given strategy |
| 421 | // changes: connected device, phone state, force use... |
| 422 | // Must be called before updateDevicesAndOutputs() |
| 423 | void checkOutputForStrategy(routing_strategy strategy); |
| 424 | |
| 425 | // Same as checkOutputForStrategy() but for a all strategies in order of priority |
| 426 | void checkOutputForAllStrategies(); |
| 427 | |
| 428 | // manages A2DP output suspend/restore according to phone state and BT SCO usage |
| 429 | void checkA2dpSuspend(); |
| 430 | |
| 431 | // returns the A2DP output handle if it is open or 0 otherwise |
| 432 | audio_io_handle_t getA2dpOutput(); |
| 433 | |
| 434 | // selects the most appropriate device on output for current state |
| 435 | // must be called every time a condition that affects the device choice for a given output is |
| 436 | // changed: connected device, phone state, force use, output start, output stop.. |
| 437 | // see getDeviceForStrategy() for the use of fromCache parameter |
| 438 | |
| 439 | audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache); |
| 440 | // updates cache of device used by all strategies (mDeviceForStrategy[]) |
| 441 | // must be called every time a condition that affects the device choice for a given strategy is |
| 442 | // changed: connected device, phone state, force use... |
| 443 | // cached values are used by getDeviceForStrategy() if parameter fromCache is true. |
| 444 | // Must be called after checkOutputForAllStrategies() |
| 445 | |
| 446 | void updateDevicesAndOutputs(); |
| 447 | |
| 448 | virtual uint32_t getMaxEffectsCpuLoad(); |
| 449 | virtual uint32_t getMaxEffectsMemory(); |
| 450 | #ifdef AUDIO_POLICY_TEST |
| 451 | virtual bool threadLoop(); |
| 452 | void exit(); |
| 453 | int testOutputIndex(audio_io_handle_t output); |
| 454 | #endif //AUDIO_POLICY_TEST |
| 455 | |
| 456 | status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled); |
| 457 | |
| 458 | // returns the category the device belongs to with regard to volume curve management |
| 459 | static device_category getDeviceCategory(audio_devices_t device); |
| 460 | |
| 461 | // extract one device relevant for volume control from multiple device selection |
| 462 | static audio_devices_t getDeviceForVolume(audio_devices_t device); |
| 463 | |
| 464 | SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device, |
| 465 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs); |
| 466 | bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
| 467 | SortedVector<audio_io_handle_t>& outputs2); |
| 468 | |
| 469 | // mute/unmute strategies using an incompatible device combination |
| 470 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 471 | // if unmuting, unmute only after the specified delay |
| 472 | // Returns the number of ms waited |
| 473 | uint32_t checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc, |
| 474 | audio_devices_t prevDevice, |
| 475 | uint32_t delayMs); |
| 476 | |
| 477 | audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 478 | audio_output_flags_t flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 479 | IOProfile *getInputProfile(audio_devices_t device, |
| 480 | uint32_t samplingRate, |
| 481 | audio_format_t format, |
| 482 | audio_channel_mask_t channelMask); |
| 483 | IOProfile *getProfileForDirectOutput(audio_devices_t device, |
| 484 | uint32_t samplingRate, |
| 485 | audio_format_t format, |
| 486 | audio_channel_mask_t channelMask, |
| 487 | audio_output_flags_t flags); |
| 488 | |
| 489 | audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs); |
| 490 | |
| 491 | bool isNonOffloadableEffectEnabled(); |
| 492 | |
| 493 | // |
| 494 | // Audio policy configuration file parsing (audio_policy.conf) |
| 495 | // |
| 496 | static uint32_t stringToEnum(const struct StringToEnum *table, |
| 497 | size_t size, |
| 498 | const char *name); |
| 499 | static bool stringToBool(const char *value); |
| 500 | static audio_output_flags_t parseFlagNames(char *name); |
| 501 | static audio_devices_t parseDeviceNames(char *name); |
| 502 | void loadSamplingRates(char *name, IOProfile *profile); |
| 503 | void loadFormats(char *name, IOProfile *profile); |
| 504 | void loadOutChannels(char *name, IOProfile *profile); |
| 505 | void loadInChannels(char *name, IOProfile *profile); |
| 506 | status_t loadOutput(cnode *root, HwModule *module); |
| 507 | status_t loadInput(cnode *root, HwModule *module); |
| 508 | void loadHwModule(cnode *root); |
| 509 | void loadHwModules(cnode *root); |
| 510 | void loadGlobalConfig(cnode *root); |
| 511 | status_t loadAudioPolicyConfig(const char *path); |
| 512 | void defaultAudioPolicyConfig(void); |
| 513 | |
| 514 | |
| 515 | AudioPolicyClientInterface *mpClientInterface; // audio policy client interface |
| 516 | audio_io_handle_t mPrimaryOutput; // primary output handle |
| 517 | // list of descriptors for outputs currently opened |
| 518 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs; |
| 519 | // copy of mOutputs before setDeviceConnectionState() opens new outputs |
| 520 | // reset to mOutputs when updateDevicesAndOutputs() is called. |
| 521 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs; |
| 522 | DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors |
| 523 | audio_devices_t mAvailableOutputDevices; // bit field of all available output devices |
| 524 | audio_devices_t mAvailableInputDevices; // bit field of all available input devices |
| 525 | // without AUDIO_DEVICE_BIT_IN to allow direct bit |
| 526 | // field comparisons |
| 527 | int mPhoneState; // current phone state |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 528 | audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 529 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 530 | StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | String8 mA2dpDeviceAddress; // A2DP device MAC address |
| 532 | String8 mScoDeviceAddress; // SCO device MAC address |
| 533 | String8 mUsbCardAndDevice; // USB audio ALSA card and device numbers: |
| 534 | // card=<card_number>;device=<><device_number> |
| 535 | bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected |
| 536 | audio_devices_t mDeviceForStrategy[NUM_STRATEGIES]; |
| 537 | float mLastVoiceVolume; // last voice volume value sent to audio HAL |
| 538 | |
| 539 | // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units |
| 540 | static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000; |
| 541 | // Maximum memory allocated to audio effects in KB |
| 542 | static const uint32_t MAX_EFFECTS_MEMORY = 512; |
| 543 | uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects |
| 544 | uint32_t mTotalEffectsMemory; // current memory used by effects |
| 545 | KeyedVector<int, EffectDescriptor *> mEffects; // list of registered audio effects |
| 546 | bool mA2dpSuspended; // true if A2DP output is suspended |
| 547 | bool mHasA2dp; // true on platforms with support for bluetooth A2DP |
| 548 | bool mHasUsb; // true on platforms with support for USB audio |
| 549 | bool mHasRemoteSubmix; // true on platforms with support for remote presentation of a submix |
| 550 | audio_devices_t mAttachedOutputDevices; // output devices always available on the platform |
| 551 | audio_devices_t mDefaultOutputDevice; // output device selected by default at boot time |
| 552 | // (must be in mAttachedOutputDevices) |
| 553 | bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path |
| 554 | // to boost soft sounds, used to adjust volume curves accordingly |
| 555 | |
| 556 | Vector <HwModule *> mHwModules; |
| 557 | |
| 558 | #ifdef AUDIO_POLICY_TEST |
| 559 | Mutex mLock; |
| 560 | Condition mWaitWorkCV; |
| 561 | |
| 562 | int mCurOutput; |
| 563 | bool mDirectOutput; |
| 564 | audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; |
| 565 | int mTestInput; |
| 566 | uint32_t mTestDevice; |
| 567 | uint32_t mTestSamplingRate; |
| 568 | uint32_t mTestFormat; |
| 569 | uint32_t mTestChannels; |
| 570 | uint32_t mTestLatencyMs; |
| 571 | #endif //AUDIO_POLICY_TEST |
| 572 | |
| 573 | private: |
| 574 | static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, |
| 575 | int indexInUi); |
| 576 | // updates device caching and output for streams that can influence the |
| 577 | // routing of notifications |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 578 | void handleNotificationRoutingForStream(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | static bool isVirtualInputDevice(audio_devices_t device); |
| 580 | }; |
| 581 | |
| 582 | }; |