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 | |
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(""), |
| 188 | mChannelMask(AUDIO_CHANNEL_NONE), mId(0) {} |
| 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); |
| 250 | |
| 251 | // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats |
| 252 | // indicates the supported parameters should be read from the output stream |
| 253 | // after it is opened for the first time |
| 254 | Vector <uint32_t> mSamplingRates; // supported sampling rates |
| 255 | Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks |
| 256 | Vector <audio_format_t> mFormats; // supported audio formats |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 257 | DeviceVector mSupportedDevices; // supported devices |
| 258 | // (devices this output can be routed to) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 259 | audio_output_flags_t mFlags; // attribute flags (e.g primary output, |
| 260 | // direct output...). For outputs only. |
| 261 | HwModule *mModule; // audio HW module exposing this I/O stream |
| 262 | }; |
| 263 | |
| 264 | // default volume curve |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 265 | static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | // default volume curve for media strategy |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 267 | static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | // volume curve for media strategy on speakers |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 269 | static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 270 | // volume curve for sonification strategy on speakers |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 271 | static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 272 | static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT]; |
| 273 | static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 274 | static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT]; |
| 275 | static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 276 | static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT]; |
| 277 | static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 278 | // default volume curves per stream and device category. See initializeVolumeCurves() |
| 279 | static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT]; |
| 280 | |
| 281 | // descriptor for audio outputs. Used to maintain current configuration of each opened audio output |
| 282 | // and keep track of the usage of this output by each audio stream type. |
| 283 | class AudioOutputDescriptor |
| 284 | { |
| 285 | public: |
| 286 | AudioOutputDescriptor(const IOProfile *profile); |
| 287 | |
| 288 | status_t dump(int fd); |
| 289 | |
| 290 | audio_devices_t device() const; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 291 | void changeRefCount(audio_stream_type_t stream, int delta); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | |
| 293 | bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); } |
| 294 | audio_devices_t supportedDevices(); |
| 295 | uint32_t latency(); |
| 296 | bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc); |
| 297 | bool isActive(uint32_t inPastMs = 0) const; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 298 | bool isStreamActive(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 | uint32_t inPastMs = 0, |
| 300 | nsecs_t sysTime = 0) const; |
| 301 | bool isStrategyActive(routing_strategy strategy, |
| 302 | uint32_t inPastMs = 0, |
| 303 | nsecs_t sysTime = 0) const; |
| 304 | |
| 305 | audio_io_handle_t mId; // output handle |
| 306 | uint32_t mSamplingRate; // |
| 307 | audio_format_t mFormat; // |
| 308 | audio_channel_mask_t mChannelMask; // output configuration |
| 309 | uint32_t mLatency; // |
| 310 | audio_output_flags_t mFlags; // |
| 311 | audio_devices_t mDevice; // current device this output is routed to |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 312 | uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output |
| 313 | nsecs_t mStopTime[AUDIO_STREAM_CNT]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | AudioOutputDescriptor *mOutput1; // used by duplicated outputs: first output |
| 315 | AudioOutputDescriptor *mOutput2; // used by duplicated outputs: second output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 316 | float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume |
| 317 | int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 318 | const IOProfile *mProfile; // I/O profile this output derives from |
| 319 | bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible |
| 320 | // device selection. See checkDeviceMuteStrategies() |
| 321 | uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only) |
| 322 | }; |
| 323 | |
| 324 | // descriptor for audio inputs. Used to maintain current configuration of each opened audio input |
| 325 | // and keep track of the usage of this input. |
| 326 | class AudioInputDescriptor |
| 327 | { |
| 328 | public: |
| 329 | AudioInputDescriptor(const IOProfile *profile); |
| 330 | |
| 331 | status_t dump(int fd); |
| 332 | |
| 333 | uint32_t mSamplingRate; // |
| 334 | audio_format_t mFormat; // input configuration |
| 335 | audio_channel_mask_t mChannelMask; // |
| 336 | audio_devices_t mDevice; // current device this input is routed to |
| 337 | uint32_t mRefCount; // number of AudioRecord clients using this output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 338 | audio_source_t mInputSource; // input source selected by application (mediarecorder.h) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 339 | const IOProfile *mProfile; // I/O profile this output derives from |
| 340 | }; |
| 341 | |
| 342 | // stream descriptor used for volume control |
| 343 | class StreamDescriptor |
| 344 | { |
| 345 | public: |
| 346 | StreamDescriptor(); |
| 347 | |
| 348 | int getVolumeIndex(audio_devices_t device); |
| 349 | void dump(int fd); |
| 350 | |
| 351 | int mIndexMin; // min volume index |
| 352 | int mIndexMax; // max volume index |
| 353 | KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device |
| 354 | bool mCanBeMuted; // true is the stream can be muted |
| 355 | |
| 356 | const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT]; |
| 357 | }; |
| 358 | |
| 359 | // stream descriptor used for volume control |
| 360 | class EffectDescriptor |
| 361 | { |
| 362 | public: |
| 363 | |
| 364 | status_t dump(int fd); |
| 365 | |
| 366 | int mIo; // io the effect is attached to |
| 367 | routing_strategy mStrategy; // routing strategy the effect is associated to |
| 368 | int mSession; // audio session the effect is on |
| 369 | effect_descriptor_t mDesc; // effect descriptor |
| 370 | bool mEnabled; // enabled state: CPU load being used or not |
| 371 | }; |
| 372 | |
| 373 | void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc); |
| 374 | |
| 375 | // return the strategy corresponding to a given stream type |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 376 | static routing_strategy getStrategy(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 377 | |
| 378 | // return appropriate device for streams handled by the specified strategy according to current |
| 379 | // phone state, connected devices... |
| 380 | // if fromCache is true, the device is returned from mDeviceForStrategy[], |
| 381 | // otherwise it is determine by current state |
| 382 | // (device connected,phone state, force use, a2dp output...) |
| 383 | // This allows to: |
| 384 | // 1 speed up process when the state is stable (when starting or stopping an output) |
| 385 | // 2 access to either current device selection (fromCache == true) or |
| 386 | // "future" device selection (fromCache == false) when called from a context |
| 387 | // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND |
| 388 | // before updateDevicesAndOutputs() is called. |
| 389 | virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy, |
| 390 | bool fromCache); |
| 391 | |
| 392 | // change the route of the specified output. Returns the number of ms we have slept to |
| 393 | // allow new routing to take effect in certain cases. |
| 394 | uint32_t setOutputDevice(audio_io_handle_t output, |
| 395 | audio_devices_t device, |
| 396 | bool force = false, |
| 397 | int delayMs = 0); |
| 398 | |
| 399 | // select input device corresponding to requested audio source |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 400 | virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 401 | |
| 402 | // return io handle of active input or 0 if no input is active |
| 403 | // Only considers inputs from physical devices (e.g. main mic, headset mic) when |
| 404 | // ignoreVirtualInputs is true. |
| 405 | audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true); |
| 406 | |
| 407 | // initialize volume curves for each strategy and device category |
| 408 | void initializeVolumeCurves(); |
| 409 | |
| 410 | // compute the actual volume for a given stream according to the requested index and a particular |
| 411 | // device |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 412 | virtual float computeVolume(audio_stream_type_t stream, int index, |
| 413 | audio_io_handle_t output, audio_devices_t device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 414 | |
| 415 | // 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] | 416 | status_t checkAndSetVolume(audio_stream_type_t stream, int index, audio_io_handle_t output, |
| 417 | audio_devices_t device, int delayMs = 0, bool force = false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 418 | |
| 419 | // apply all stream volumes to the specified output and device |
| 420 | void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false); |
| 421 | |
| 422 | // Mute or unmute all streams handled by the specified strategy on the specified output |
| 423 | void setStrategyMute(routing_strategy strategy, |
| 424 | bool on, |
| 425 | audio_io_handle_t output, |
| 426 | int delayMs = 0, |
| 427 | audio_devices_t device = (audio_devices_t)0); |
| 428 | |
| 429 | // Mute or unmute the stream on the specified output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 430 | void setStreamMute(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 431 | bool on, |
| 432 | audio_io_handle_t output, |
| 433 | int delayMs = 0, |
| 434 | audio_devices_t device = (audio_devices_t)0); |
| 435 | |
| 436 | // handle special cases for sonification strategy while in call: mute streams or replace by |
| 437 | // a special tone in the device used for communication |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 438 | void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 439 | |
| 440 | // true if device is in a telephony or VoIP call |
| 441 | virtual bool isInCall(); |
| 442 | |
| 443 | // true if given state represents a device in a telephony or VoIP call |
| 444 | virtual bool isStateInCall(int state); |
| 445 | |
| 446 | // when a device is connected, checks if an open output can be routed |
| 447 | // to this device. If none is open, tries to open one of the available outputs. |
| 448 | // Returns an output suitable to this device or 0. |
| 449 | // when a device is disconnected, checks if an output is not used any more and |
| 450 | // returns its handle if any. |
| 451 | // transfers the audio tracks and effects from one output thread to another accordingly. |
| 452 | status_t checkOutputsForDevice(audio_devices_t device, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 453 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 454 | SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 455 | const String8 address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 456 | |
| 457 | // close an output and its companion duplicating output. |
| 458 | void closeOutput(audio_io_handle_t output); |
| 459 | |
| 460 | // checks and if necessary changes outputs used for all strategies. |
| 461 | // must be called every time a condition that affects the output choice for a given strategy |
| 462 | // changes: connected device, phone state, force use... |
| 463 | // Must be called before updateDevicesAndOutputs() |
| 464 | void checkOutputForStrategy(routing_strategy strategy); |
| 465 | |
| 466 | // Same as checkOutputForStrategy() but for a all strategies in order of priority |
| 467 | void checkOutputForAllStrategies(); |
| 468 | |
| 469 | // manages A2DP output suspend/restore according to phone state and BT SCO usage |
| 470 | void checkA2dpSuspend(); |
| 471 | |
| 472 | // returns the A2DP output handle if it is open or 0 otherwise |
| 473 | audio_io_handle_t getA2dpOutput(); |
| 474 | |
| 475 | // selects the most appropriate device on output for current state |
| 476 | // must be called every time a condition that affects the device choice for a given output is |
| 477 | // changed: connected device, phone state, force use, output start, output stop.. |
| 478 | // see getDeviceForStrategy() for the use of fromCache parameter |
| 479 | |
| 480 | audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache); |
| 481 | // updates cache of device used by all strategies (mDeviceForStrategy[]) |
| 482 | // must be called every time a condition that affects the device choice for a given strategy is |
| 483 | // changed: connected device, phone state, force use... |
| 484 | // cached values are used by getDeviceForStrategy() if parameter fromCache is true. |
| 485 | // Must be called after checkOutputForAllStrategies() |
| 486 | |
| 487 | void updateDevicesAndOutputs(); |
| 488 | |
| 489 | virtual uint32_t getMaxEffectsCpuLoad(); |
| 490 | virtual uint32_t getMaxEffectsMemory(); |
| 491 | #ifdef AUDIO_POLICY_TEST |
| 492 | virtual bool threadLoop(); |
| 493 | void exit(); |
| 494 | int testOutputIndex(audio_io_handle_t output); |
| 495 | #endif //AUDIO_POLICY_TEST |
| 496 | |
| 497 | status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled); |
| 498 | |
| 499 | // returns the category the device belongs to with regard to volume curve management |
| 500 | static device_category getDeviceCategory(audio_devices_t device); |
| 501 | |
| 502 | // extract one device relevant for volume control from multiple device selection |
| 503 | static audio_devices_t getDeviceForVolume(audio_devices_t device); |
| 504 | |
| 505 | SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device, |
| 506 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs); |
| 507 | bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
| 508 | SortedVector<audio_io_handle_t>& outputs2); |
| 509 | |
| 510 | // mute/unmute strategies using an incompatible device combination |
| 511 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 512 | // if unmuting, unmute only after the specified delay |
| 513 | // Returns the number of ms waited |
| 514 | uint32_t checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc, |
| 515 | audio_devices_t prevDevice, |
| 516 | uint32_t delayMs); |
| 517 | |
| 518 | audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 519 | audio_output_flags_t flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 520 | IOProfile *getInputProfile(audio_devices_t device, |
| 521 | uint32_t samplingRate, |
| 522 | audio_format_t format, |
| 523 | audio_channel_mask_t channelMask); |
| 524 | IOProfile *getProfileForDirectOutput(audio_devices_t device, |
| 525 | uint32_t samplingRate, |
| 526 | audio_format_t format, |
| 527 | audio_channel_mask_t channelMask, |
| 528 | audio_output_flags_t flags); |
| 529 | |
| 530 | audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs); |
| 531 | |
| 532 | bool isNonOffloadableEffectEnabled(); |
| 533 | |
| 534 | // |
| 535 | // Audio policy configuration file parsing (audio_policy.conf) |
| 536 | // |
| 537 | static uint32_t stringToEnum(const struct StringToEnum *table, |
| 538 | size_t size, |
| 539 | const char *name); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 540 | static const char *enumToString(const struct StringToEnum *table, |
| 541 | size_t size, |
| 542 | uint32_t value); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 543 | static bool stringToBool(const char *value); |
| 544 | static audio_output_flags_t parseFlagNames(char *name); |
| 545 | static audio_devices_t parseDeviceNames(char *name); |
| 546 | void loadSamplingRates(char *name, IOProfile *profile); |
| 547 | void loadFormats(char *name, IOProfile *profile); |
| 548 | void loadOutChannels(char *name, IOProfile *profile); |
| 549 | void loadInChannels(char *name, IOProfile *profile); |
| 550 | status_t loadOutput(cnode *root, HwModule *module); |
| 551 | status_t loadInput(cnode *root, HwModule *module); |
| 552 | void loadHwModule(cnode *root); |
| 553 | void loadHwModules(cnode *root); |
| 554 | void loadGlobalConfig(cnode *root); |
| 555 | status_t loadAudioPolicyConfig(const char *path); |
| 556 | void defaultAudioPolicyConfig(void); |
| 557 | |
| 558 | |
| 559 | AudioPolicyClientInterface *mpClientInterface; // audio policy client interface |
| 560 | audio_io_handle_t mPrimaryOutput; // primary output handle |
| 561 | // list of descriptors for outputs currently opened |
| 562 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs; |
| 563 | // copy of mOutputs before setDeviceConnectionState() opens new outputs |
| 564 | // reset to mOutputs when updateDevicesAndOutputs() is called. |
| 565 | DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs; |
| 566 | DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 567 | DeviceVector mAvailableOutputDevices; // bit field of all available output devices |
| 568 | DeviceVector mAvailableInputDevices; // bit field of all available input devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 569 | // without AUDIO_DEVICE_BIT_IN to allow direct bit |
| 570 | // field comparisons |
| 571 | int mPhoneState; // current phone state |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 572 | 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] | 573 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 574 | StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 575 | bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected |
| 576 | audio_devices_t mDeviceForStrategy[NUM_STRATEGIES]; |
| 577 | float mLastVoiceVolume; // last voice volume value sent to audio HAL |
| 578 | |
| 579 | // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units |
| 580 | static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000; |
| 581 | // Maximum memory allocated to audio effects in KB |
| 582 | static const uint32_t MAX_EFFECTS_MEMORY = 512; |
| 583 | uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects |
| 584 | uint32_t mTotalEffectsMemory; // current memory used by effects |
| 585 | KeyedVector<int, EffectDescriptor *> mEffects; // list of registered audio effects |
| 586 | bool mA2dpSuspended; // true if A2DP output is suspended |
| 587 | bool mHasA2dp; // true on platforms with support for bluetooth A2DP |
| 588 | bool mHasUsb; // true on platforms with support for USB audio |
| 589 | bool mHasRemoteSubmix; // true on platforms with support for remote presentation of a submix |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 590 | sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path |
| 592 | // to boost soft sounds, used to adjust volume curves accordingly |
| 593 | |
| 594 | Vector <HwModule *> mHwModules; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 595 | volatile int32_t mNextUniqueId; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 596 | |
| 597 | #ifdef AUDIO_POLICY_TEST |
| 598 | Mutex mLock; |
| 599 | Condition mWaitWorkCV; |
| 600 | |
| 601 | int mCurOutput; |
| 602 | bool mDirectOutput; |
| 603 | audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; |
| 604 | int mTestInput; |
| 605 | uint32_t mTestDevice; |
| 606 | uint32_t mTestSamplingRate; |
| 607 | uint32_t mTestFormat; |
| 608 | uint32_t mTestChannels; |
| 609 | uint32_t mTestLatencyMs; |
| 610 | #endif //AUDIO_POLICY_TEST |
| 611 | |
| 612 | private: |
| 613 | static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, |
| 614 | int indexInUi); |
| 615 | // updates device caching and output for streams that can influence the |
| 616 | // routing of notifications |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 617 | void handleNotificationRoutingForStream(audio_stream_type_t stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | static bool isVirtualInputDevice(audio_devices_t device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame^] | 619 | uint32_t nextUniqueId(); |
| 620 | // converts device address to string sent to audio HAL via setParameters |
| 621 | static String8 addressToParameter(audio_devices_t device, const String8 address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | }; |