Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [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 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 17 | #pragma once |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <cutils/config_utils.h> |
| 22 | #include <cutils/misc.h> |
| 23 | #include <utils/Timers.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/KeyedVector.h> |
| 26 | #include <utils/SortedVector.h> |
| 27 | #include <media/AudioPolicy.h> |
| 28 | #include "AudioPolicyInterface.h" |
| 29 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 30 | #include <AudioPolicyManagerInterface.h> |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 31 | #include <AudioPolicyManagerObserver.h> |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 32 | #include <AudioGain.h> |
| 33 | #include <AudioPort.h> |
| 34 | #include <AudioPatch.h> |
| 35 | #include <ConfigParsingUtils.h> |
| 36 | #include <DeviceDescriptor.h> |
| 37 | #include <IOProfile.h> |
| 38 | #include <HwModule.h> |
| 39 | #include <AudioInputDescriptor.h> |
| 40 | #include <AudioOutputDescriptor.h> |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 41 | #include <AudioPolicyMix.h> |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 42 | #include <EffectDescriptor.h> |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 43 | #include <SoundTriggerSession.h> |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 44 | #include <StreamDescriptor.h> |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | // Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB |
| 51 | #define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5 |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 52 | #define SONIFICATION_HEADSET_VOLUME_FACTOR_DB (-6) |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 53 | // Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB |
| 54 | #define SONIFICATION_HEADSET_VOLUME_MIN 0.016 |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 55 | #define SONIFICATION_HEADSET_VOLUME_MIN_DB (-36) |
| 56 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 57 | // Time in milliseconds during which we consider that music is still active after a music |
| 58 | // track was stopped - see computeVolume() |
| 59 | #define SONIFICATION_HEADSET_MUSIC_DELAY 5000 |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 60 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 61 | // Time in milliseconds during witch some streams are muted while the audio path |
| 62 | // is switched |
| 63 | #define MUTE_TIME_MS 2000 |
| 64 | |
| 65 | #define NUM_TEST_OUTPUTS 5 |
| 66 | |
| 67 | #define NUM_VOL_CURVE_KNEES 2 |
| 68 | |
| 69 | // Default minimum length allowed for offloading a compressed track |
| 70 | // Can be overridden by the audio.offload.min.duration.secs property |
| 71 | #define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60 |
| 72 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 73 | // ---------------------------------------------------------------------------- |
| 74 | // AudioPolicyManager implements audio policy manager behavior common to all platforms. |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 77 | class AudioPolicyManager : public AudioPolicyInterface, public AudioPolicyManagerObserver |
| 78 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 79 | #ifdef AUDIO_POLICY_TEST |
| 80 | , public Thread |
| 81 | #endif //AUDIO_POLICY_TEST |
| 82 | { |
| 83 | |
| 84 | public: |
| 85 | AudioPolicyManager(AudioPolicyClientInterface *clientInterface); |
| 86 | virtual ~AudioPolicyManager(); |
| 87 | |
| 88 | // AudioPolicyInterface |
| 89 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 90 | audio_policy_dev_state_t state, |
| 91 | const char *device_address, |
| 92 | const char *device_name); |
| 93 | virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device, |
| 94 | const char *device_address); |
| 95 | virtual void setPhoneState(audio_mode_t state); |
| 96 | virtual void setForceUse(audio_policy_force_use_t usage, |
| 97 | audio_policy_forced_cfg_t config); |
| 98 | virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 99 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 100 | virtual void setSystemProperty(const char* property, const char* value); |
| 101 | virtual status_t initCheck(); |
| 102 | virtual audio_io_handle_t getOutput(audio_stream_type_t stream, |
| 103 | uint32_t samplingRate, |
| 104 | audio_format_t format, |
| 105 | audio_channel_mask_t channelMask, |
| 106 | audio_output_flags_t flags, |
| 107 | const audio_offload_info_t *offloadInfo); |
| 108 | virtual status_t getOutputForAttr(const audio_attributes_t *attr, |
| 109 | audio_io_handle_t *output, |
| 110 | audio_session_t session, |
| 111 | audio_stream_type_t *stream, |
| 112 | uint32_t samplingRate, |
| 113 | audio_format_t format, |
| 114 | audio_channel_mask_t channelMask, |
| 115 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 116 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 117 | const audio_offload_info_t *offloadInfo); |
| 118 | virtual status_t startOutput(audio_io_handle_t output, |
| 119 | audio_stream_type_t stream, |
| 120 | audio_session_t session); |
| 121 | virtual status_t stopOutput(audio_io_handle_t output, |
| 122 | audio_stream_type_t stream, |
| 123 | audio_session_t session); |
| 124 | virtual void releaseOutput(audio_io_handle_t output, |
| 125 | audio_stream_type_t stream, |
| 126 | audio_session_t session); |
| 127 | virtual status_t getInputForAttr(const audio_attributes_t *attr, |
| 128 | audio_io_handle_t *input, |
| 129 | audio_session_t session, |
| 130 | uint32_t samplingRate, |
| 131 | audio_format_t format, |
| 132 | audio_channel_mask_t channelMask, |
| 133 | audio_input_flags_t flags, |
| 134 | input_type_t *inputType); |
| 135 | |
| 136 | // indicates to the audio policy manager that the input starts being used. |
| 137 | virtual status_t startInput(audio_io_handle_t input, |
| 138 | audio_session_t session); |
| 139 | |
| 140 | // indicates to the audio policy manager that the input stops being used. |
| 141 | virtual status_t stopInput(audio_io_handle_t input, |
| 142 | audio_session_t session); |
| 143 | virtual void releaseInput(audio_io_handle_t input, |
| 144 | audio_session_t session); |
| 145 | virtual void closeAllInputs(); |
| 146 | virtual void initStreamVolume(audio_stream_type_t stream, |
| 147 | int indexMin, |
| 148 | int indexMax); |
| 149 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, |
| 150 | int index, |
| 151 | audio_devices_t device); |
| 152 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, |
| 153 | int *index, |
| 154 | audio_devices_t device); |
| 155 | |
| 156 | // return the strategy corresponding to a given stream type |
| 157 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
| 158 | // return the strategy corresponding to the given audio attributes |
| 159 | virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr); |
| 160 | |
| 161 | // return the enabled output devices for the given stream type |
| 162 | virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); |
| 163 | |
| 164 | virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL); |
| 165 | virtual status_t registerEffect(const effect_descriptor_t *desc, |
| 166 | audio_io_handle_t io, |
| 167 | uint32_t strategy, |
| 168 | int session, |
| 169 | int id); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 170 | virtual status_t unregisterEffect(int id) |
| 171 | { |
| 172 | return mEffects.unregisterEffect(id); |
| 173 | } |
| 174 | virtual status_t setEffectEnabled(int id, bool enabled) |
| 175 | { |
| 176 | return mEffects.setEffectEnabled(id, enabled); |
| 177 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 178 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 179 | virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 180 | // return whether a stream is playing remotely, override to change the definition of |
| 181 | // local/remote playback, used for instance by notification manager to not make |
| 182 | // media players lose audio focus when not playing locally |
| 183 | // For the base implementation, "remotely" means playing during screen mirroring which |
| 184 | // uses an output for playback with a non-empty, non "0" address. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 | virtual bool isStreamActiveRemotely(audio_stream_type_t stream, |
| 186 | uint32_t inPastMs = 0) const; |
| 187 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 188 | virtual bool isSourceActive(audio_source_t source) const; |
| 189 | |
| 190 | virtual status_t dump(int fd); |
| 191 | |
| 192 | virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo); |
| 193 | |
| 194 | virtual status_t listAudioPorts(audio_port_role_t role, |
| 195 | audio_port_type_t type, |
| 196 | unsigned int *num_ports, |
| 197 | struct audio_port *ports, |
| 198 | unsigned int *generation); |
| 199 | virtual status_t getAudioPort(struct audio_port *port); |
| 200 | virtual status_t createAudioPatch(const struct audio_patch *patch, |
| 201 | audio_patch_handle_t *handle, |
| 202 | uid_t uid); |
| 203 | virtual status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 204 | uid_t uid); |
| 205 | virtual status_t listAudioPatches(unsigned int *num_patches, |
| 206 | struct audio_patch *patches, |
| 207 | unsigned int *generation); |
| 208 | virtual status_t setAudioPortConfig(const struct audio_port_config *config); |
| 209 | virtual void clearAudioPatches(uid_t uid); |
| 210 | |
| 211 | virtual status_t acquireSoundTriggerSession(audio_session_t *session, |
| 212 | audio_io_handle_t *ioHandle, |
| 213 | audio_devices_t *device); |
| 214 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 215 | virtual status_t releaseSoundTriggerSession(audio_session_t session) |
| 216 | { |
| 217 | return mSoundTriggerSessions.releaseSession(session); |
| 218 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 219 | |
| 220 | virtual status_t registerPolicyMixes(Vector<AudioMix> mixes); |
| 221 | virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes); |
| 222 | |
| 223 | // Audio policy configuration file parsing (audio_policy.conf) |
| 224 | // TODO candidates to be moved to ConfigParsingUtils |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 225 | void defaultAudioPolicyConfig(void); |
| 226 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 227 | // return the strategy corresponding to a given stream type |
| 228 | routing_strategy getStrategy(audio_stream_type_t stream) const; |
| 229 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 230 | protected: |
| 231 | class SessionRoute : public RefBase |
| 232 | { |
| 233 | public: |
| 234 | friend class SessionRouteMap; |
| 235 | SessionRoute(audio_session_t session, |
| 236 | audio_stream_type_t streamType, |
| 237 | sp<DeviceDescriptor> deviceDescriptor) |
| 238 | : mSession(session), |
| 239 | mStreamType(streamType), |
| 240 | mDeviceDescriptor(deviceDescriptor), |
| 241 | mRefCount(0), |
| 242 | mActivityCount(0) {} |
| 243 | |
| 244 | audio_session_t mSession; |
| 245 | audio_stream_type_t mStreamType; |
| 246 | |
| 247 | sp<DeviceDescriptor> mDeviceDescriptor; |
| 248 | |
| 249 | // "reference" counting |
| 250 | int mRefCount; // +/- on references |
| 251 | int mActivityCount; // +/- on start/stop |
| 252 | |
| 253 | void log(const char* prefix); |
| 254 | }; |
| 255 | |
| 256 | class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>> |
| 257 | { |
| 258 | public: |
| 259 | bool hasRoute(audio_session_t session); |
| 260 | void addRoute(audio_session_t session, audio_stream_type_t streamType, |
| 261 | sp<DeviceDescriptor> deviceDescriptor); |
| 262 | void removeRoute(audio_session_t session); |
| 263 | |
| 264 | int incRouteActivity(audio_session_t session); |
| 265 | int decRouteActivity(audio_session_t session); |
| 266 | |
| 267 | void log(const char* caption); |
| 268 | }; |
| 269 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 270 | // From AudioPolicyManagerObserver |
| 271 | virtual const AudioPatchCollection &getAudioPatches() const |
| 272 | { |
| 273 | return mAudioPatches; |
| 274 | } |
| 275 | virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const |
| 276 | { |
| 277 | return mSoundTriggerSessions; |
| 278 | } |
| 279 | virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const |
| 280 | { |
| 281 | return mPolicyMixes; |
| 282 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 283 | virtual const SwAudioOutputCollection &getOutputs() const |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 284 | { |
| 285 | return mOutputs; |
| 286 | } |
| 287 | virtual const AudioInputCollection &getInputs() const |
| 288 | { |
| 289 | return mInputs; |
| 290 | } |
| 291 | virtual const DeviceVector &getAvailableOutputDevices() const |
| 292 | { |
| 293 | return mAvailableOutputDevices; |
| 294 | } |
| 295 | virtual const DeviceVector &getAvailableInputDevices() const |
| 296 | { |
| 297 | return mAvailableInputDevices; |
| 298 | } |
| 299 | virtual StreamDescriptorCollection &getStreamDescriptors() |
| 300 | { |
| 301 | return mStreams; |
| 302 | } |
| 303 | virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const |
| 304 | { |
| 305 | return mDefaultOutputDevice; |
| 306 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 307 | protected: |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 308 | void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 309 | void removeOutput(audio_io_handle_t output); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 310 | void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc); |
| 311 | |
| 312 | // return appropriate device for streams handled by the specified strategy according to current |
| 313 | // phone state, connected devices... |
| 314 | // if fromCache is true, the device is returned from mDeviceForStrategy[], |
| 315 | // otherwise it is determine by current state |
| 316 | // (device connected,phone state, force use, a2dp output...) |
| 317 | // This allows to: |
| 318 | // 1 speed up process when the state is stable (when starting or stopping an output) |
| 319 | // 2 access to either current device selection (fromCache == true) or |
| 320 | // "future" device selection (fromCache == false) when called from a context |
| 321 | // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND |
| 322 | // before updateDevicesAndOutputs() is called. |
| 323 | virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy, |
| 324 | bool fromCache); |
| 325 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 326 | bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy, |
| 327 | uint32_t inPastMs = 0, nsecs_t sysTime = 0) const; |
| 328 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 329 | // change the route of the specified output. Returns the number of ms we have slept to |
| 330 | // allow new routing to take effect in certain cases. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 331 | virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 332 | audio_devices_t device, |
| 333 | bool force = false, |
| 334 | int delayMs = 0, |
| 335 | audio_patch_handle_t *patchHandle = NULL, |
| 336 | const char* address = NULL); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 337 | status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 338 | int delayMs = 0, |
| 339 | audio_patch_handle_t *patchHandle = NULL); |
| 340 | status_t setInputDevice(audio_io_handle_t input, |
| 341 | audio_devices_t device, |
| 342 | bool force = false, |
| 343 | audio_patch_handle_t *patchHandle = NULL); |
| 344 | status_t resetInputDevice(audio_io_handle_t input, |
| 345 | audio_patch_handle_t *patchHandle = NULL); |
| 346 | |
| 347 | // select input device corresponding to requested audio source |
| 348 | virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource); |
| 349 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 350 | // compute the actual volume for a given stream according to the requested index and a particular |
| 351 | // device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 352 | virtual float computeVolume(audio_stream_type_t stream, |
| 353 | int index, |
| 354 | audio_devices_t device); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 355 | |
| 356 | // check that volume change is permitted, compute and send new volume to audio hardware |
| 357 | virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 358 | const sp<AudioOutputDescriptor>& outputDesc, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 359 | audio_devices_t device, |
| 360 | int delayMs = 0, bool force = false); |
| 361 | |
| 362 | // apply all stream volumes to the specified output and device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 363 | void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 364 | audio_devices_t device, int delayMs = 0, bool force = false); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 365 | |
| 366 | // Mute or unmute all streams handled by the specified strategy on the specified output |
| 367 | void setStrategyMute(routing_strategy strategy, |
| 368 | bool on, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 369 | const sp<AudioOutputDescriptor>& outputDesc, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 370 | int delayMs = 0, |
| 371 | audio_devices_t device = (audio_devices_t)0); |
| 372 | |
| 373 | // Mute or unmute the stream on the specified output |
| 374 | void setStreamMute(audio_stream_type_t stream, |
| 375 | bool on, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 376 | const sp<AudioOutputDescriptor>& outputDesc, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 377 | int delayMs = 0, |
| 378 | audio_devices_t device = (audio_devices_t)0); |
| 379 | |
| 380 | // handle special cases for sonification strategy while in call: mute streams or replace by |
| 381 | // a special tone in the device used for communication |
| 382 | void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange); |
| 383 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 384 | audio_mode_t getPhoneState(); |
| 385 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 386 | // true if device is in a telephony or VoIP call |
| 387 | virtual bool isInCall(); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 388 | // true if given state represents a device in a telephony or VoIP call |
| 389 | virtual bool isStateInCall(int state); |
| 390 | |
| 391 | // when a device is connected, checks if an open output can be routed |
| 392 | // to this device. If none is open, tries to open one of the available outputs. |
| 393 | // Returns an output suitable to this device or 0. |
| 394 | // when a device is disconnected, checks if an output is not used any more and |
| 395 | // returns its handle if any. |
| 396 | // transfers the audio tracks and effects from one output thread to another accordingly. |
| 397 | status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
| 398 | audio_policy_dev_state_t state, |
| 399 | SortedVector<audio_io_handle_t>& outputs, |
| 400 | const String8 address); |
| 401 | |
| 402 | status_t checkInputsForDevice(audio_devices_t device, |
| 403 | audio_policy_dev_state_t state, |
| 404 | SortedVector<audio_io_handle_t>& inputs, |
| 405 | const String8 address); |
| 406 | |
| 407 | // close an output and its companion duplicating output. |
| 408 | void closeOutput(audio_io_handle_t output); |
| 409 | |
| 410 | // close an input. |
| 411 | void closeInput(audio_io_handle_t input); |
| 412 | |
| 413 | // checks and if necessary changes outputs used for all strategies. |
| 414 | // must be called every time a condition that affects the output choice for a given strategy |
| 415 | // changes: connected device, phone state, force use... |
| 416 | // Must be called before updateDevicesAndOutputs() |
| 417 | void checkOutputForStrategy(routing_strategy strategy); |
| 418 | |
| 419 | // Same as checkOutputForStrategy() but for a all strategies in order of priority |
| 420 | void checkOutputForAllStrategies(); |
| 421 | |
| 422 | // manages A2DP output suspend/restore according to phone state and BT SCO usage |
| 423 | void checkA2dpSuspend(); |
| 424 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 425 | // selects the most appropriate device on output for current state |
| 426 | // must be called every time a condition that affects the device choice for a given output is |
| 427 | // changed: connected device, phone state, force use, output start, output stop.. |
| 428 | // see getDeviceForStrategy() for the use of fromCache parameter |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 429 | audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 430 | bool fromCache); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 431 | |
| 432 | // updates cache of device used by all strategies (mDeviceForStrategy[]) |
| 433 | // must be called every time a condition that affects the device choice for a given strategy is |
| 434 | // changed: connected device, phone state, force use... |
| 435 | // cached values are used by getDeviceForStrategy() if parameter fromCache is true. |
| 436 | // Must be called after checkOutputForAllStrategies() |
| 437 | void updateDevicesAndOutputs(); |
| 438 | |
| 439 | // selects the most appropriate device on input for current state |
| 440 | audio_devices_t getNewInputDevice(audio_io_handle_t input); |
| 441 | |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 442 | virtual uint32_t getMaxEffectsCpuLoad() |
| 443 | { |
| 444 | return mEffects.getMaxEffectsCpuLoad(); |
| 445 | } |
| 446 | |
| 447 | virtual uint32_t getMaxEffectsMemory() |
| 448 | { |
| 449 | return mEffects.getMaxEffectsMemory(); |
| 450 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 451 | #ifdef AUDIO_POLICY_TEST |
| 452 | virtual bool threadLoop(); |
| 453 | void exit(); |
| 454 | int testOutputIndex(audio_io_handle_t output); |
| 455 | #endif //AUDIO_POLICY_TEST |
| 456 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 457 | SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 458 | SwAudioOutputCollection openOutputs); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 459 | bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
| 460 | SortedVector<audio_io_handle_t>& outputs2); |
| 461 | |
| 462 | // mute/unmute strategies using an incompatible device combination |
| 463 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 464 | // if unmuting, unmute only after the specified delay |
| 465 | // Returns the number of ms waited |
| 466 | virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
| 467 | audio_devices_t prevDevice, |
| 468 | uint32_t delayMs); |
| 469 | |
| 470 | audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
| 471 | audio_output_flags_t flags, |
| 472 | audio_format_t format); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame^] | 473 | // samplingRate, format, channelMask are in/out and so may be modified |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 474 | sp<IOProfile> getInputProfile(audio_devices_t device, |
| 475 | String8 address, |
| 476 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame^] | 477 | audio_format_t& format, |
| 478 | audio_channel_mask_t& channelMask, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 479 | audio_input_flags_t flags); |
| 480 | sp<IOProfile> getProfileForDirectOutput(audio_devices_t device, |
| 481 | uint32_t samplingRate, |
| 482 | audio_format_t format, |
| 483 | audio_channel_mask_t channelMask, |
| 484 | audio_output_flags_t flags); |
| 485 | |
| 486 | audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs); |
| 487 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 488 | virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch) |
| 489 | { |
| 490 | return mAudioPatches.addAudioPatch(handle, patch); |
| 491 | } |
| 492 | virtual status_t removeAudioPatch(audio_patch_handle_t handle) |
| 493 | { |
| 494 | return mAudioPatches.removeAudioPatch(handle); |
| 495 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 496 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 497 | audio_devices_t availablePrimaryOutputDevices() const |
| 498 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 499 | return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 500 | } |
| 501 | audio_devices_t availablePrimaryInputDevices() const |
| 502 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 503 | return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle()); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 504 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 505 | |
| 506 | void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0); |
| 507 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 508 | status_t startSource(sp<AudioOutputDescriptor> outputDesc, |
| 509 | audio_stream_type_t stream, |
| 510 | audio_devices_t device, |
| 511 | uint32_t *delayMs); |
| 512 | status_t stopSource(sp<AudioOutputDescriptor> outputDesc, |
| 513 | audio_stream_type_t stream); |
| 514 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 515 | uid_t mUidCached; |
| 516 | AudioPolicyClientInterface *mpClientInterface; // audio policy client interface |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 517 | sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 518 | // list of descriptors for outputs currently opened |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 519 | |
| 520 | SwAudioOutputCollection mOutputs; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 521 | // copy of mOutputs before setDeviceConnectionState() opens new outputs |
| 522 | // reset to mOutputs when updateDevicesAndOutputs() is called. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 523 | SwAudioOutputCollection mPreviousOutputs; |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 524 | AudioInputCollection mInputs; // list of input descriptors |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 525 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 526 | DeviceVector mAvailableOutputDevices; // all available output devices |
| 527 | DeviceVector mAvailableInputDevices; // all available input devices |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 528 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 529 | SessionRouteMap mOutputRoutes; |
| 530 | SessionRouteMap mInputRoutes; |
| 531 | |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 532 | StreamDescriptorCollection mStreams; // stream descriptors for volume control |
| 533 | bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 534 | audio_devices_t mDeviceForStrategy[NUM_STRATEGIES]; |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 535 | float mLastVoiceVolume; // last voice volume value sent to audio HAL |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 536 | |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 537 | EffectDescriptorCollection mEffects; // list of registered audio effects |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 538 | bool mA2dpSuspended; // true if A2DP output is suspended |
| 539 | sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time |
| 540 | bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path |
| 541 | // to boost soft sounds, used to adjust volume curves accordingly |
| 542 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 543 | HwModuleCollection mHwModules; |
| 544 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 545 | volatile int32_t mAudioPortGeneration; |
| 546 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 547 | AudioPatchCollection mAudioPatches; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 548 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 549 | SoundTriggerSessionCollection mSoundTriggerSessions; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 550 | |
| 551 | sp<AudioPatch> mCallTxPatch; |
| 552 | sp<AudioPatch> mCallRxPatch; |
| 553 | |
| 554 | // for supporting "beacon" streams, i.e. streams that only play on speaker, and never |
| 555 | // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing |
| 556 | enum { |
| 557 | STARTING_OUTPUT, |
| 558 | STARTING_BEACON, |
| 559 | STOPPING_OUTPUT, |
| 560 | STOPPING_BEACON |
| 561 | }; |
| 562 | uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon |
| 563 | uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams |
| 564 | bool mBeaconMuted; // has STREAM_TTS been muted |
| 565 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 566 | AudioPolicyMixCollection mPolicyMixes; // list of registered mixes |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 567 | |
| 568 | #ifdef AUDIO_POLICY_TEST |
| 569 | Mutex mLock; |
| 570 | Condition mWaitWorkCV; |
| 571 | |
| 572 | int mCurOutput; |
| 573 | bool mDirectOutput; |
| 574 | audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; |
| 575 | int mTestInput; |
| 576 | uint32_t mTestDevice; |
| 577 | uint32_t mTestSamplingRate; |
| 578 | uint32_t mTestFormat; |
| 579 | uint32_t mTestChannels; |
| 580 | uint32_t mTestLatencyMs; |
| 581 | #endif //AUDIO_POLICY_TEST |
| 582 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 583 | uint32_t nextAudioPortGeneration(); |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 584 | |
| 585 | // Audio Policy Engine Interface. |
| 586 | AudioPolicyManagerInterface *mEngine; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 587 | private: |
| 588 | // updates device caching and output for streams that can influence the |
| 589 | // routing of notifications |
| 590 | void handleNotificationRoutingForStream(audio_stream_type_t stream); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 591 | // find the outputs on a given output descriptor that have the given address. |
| 592 | // to be called on an AudioOutputDescriptor whose supported devices (as defined |
| 593 | // in mProfile->mSupportedDevices) matches the device whose address is to be matched. |
| 594 | // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one |
| 595 | // where addresses are used to distinguish between one connected device and another. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 596 | void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 597 | const audio_devices_t device /*in*/, |
| 598 | const String8 address /*in*/, |
| 599 | SortedVector<audio_io_handle_t>& outputs /*out*/); |
| 600 | uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; } |
| 601 | // internal method to return the output handle for the given device and format |
| 602 | audio_io_handle_t getOutputForDevice( |
| 603 | audio_devices_t device, |
| 604 | audio_session_t session, |
| 605 | audio_stream_type_t stream, |
| 606 | uint32_t samplingRate, |
| 607 | audio_format_t format, |
| 608 | audio_channel_mask_t channelMask, |
| 609 | audio_output_flags_t flags, |
| 610 | const audio_offload_info_t *offloadInfo); |
| 611 | // internal function to derive a stream type value from audio attributes |
| 612 | audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 613 | // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON |
| 614 | // returns 0 if no mute/unmute event happened, the largest latency of the device where |
| 615 | // the mute/unmute happened |
| 616 | uint32_t handleEventForBeacon(int event); |
| 617 | uint32_t setBeaconMute(bool mute); |
| 618 | bool isValidAttributes(const audio_attributes_t *paa); |
| 619 | |
| 620 | // select input device corresponding to requested audio source and return associated policy |
| 621 | // mix if any. Calls getDeviceForInputSource(). |
| 622 | audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource, |
| 623 | AudioMix **policyMix = NULL); |
| 624 | |
| 625 | // Called by setDeviceConnectionState(). |
| 626 | status_t setDeviceConnectionStateInt(audio_devices_t device, |
| 627 | audio_policy_dev_state_t state, |
| 628 | const char *device_address, |
| 629 | const char *device_name); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 630 | }; |
| 631 | |
| 632 | }; |