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