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