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