blob: 37faac2cbbdb66bec34c5acf759767bd406822a1 [file] [log] [blame]
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001/*
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 Gaffiead3183e2015-03-18 16:55:35 +010017#pragma once
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080018
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>
Andy Hung2ddee192015-12-18 17:34:44 -080027#include <media/AudioParameter.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080028#include <media/AudioPolicy.h>
29#include "AudioPolicyInterface.h"
30
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -070031#include <AudioPolicyManagerInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010032#include <AudioPolicyManagerObserver.h>
François Gaffie98cc1912015-03-18 17:52:40 +010033#include <AudioGain.h>
34#include <AudioPort.h>
35#include <AudioPatch.h>
36#include <ConfigParsingUtils.h>
37#include <DeviceDescriptor.h>
38#include <IOProfile.h>
39#include <HwModule.h>
40#include <AudioInputDescriptor.h>
41#include <AudioOutputDescriptor.h>
François Gaffie036e1e92015-03-19 10:16:24 +010042#include <AudioPolicyMix.h>
François Gaffie45ed3b02015-03-19 10:35:14 +010043#include <EffectDescriptor.h>
François Gaffiedf372692015-03-19 10:43:27 +010044#include <SoundTriggerSession.h>
François Gaffiedfd74092015-03-19 12:10:59 +010045#include <StreamDescriptor.h>
François Gaffie54c06592015-07-08 09:46:05 +020046#include <SessionRoute.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080047
48namespace android {
49
50// ----------------------------------------------------------------------------
51
52// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
53#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
Eric Laurentffbc80f2015-03-18 18:30:19 -070054#define SONIFICATION_HEADSET_VOLUME_FACTOR_DB (-6)
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080055// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
56#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
Eric Laurentffbc80f2015-03-18 18:30:19 -070057#define SONIFICATION_HEADSET_VOLUME_MIN_DB (-36)
58
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080059// Time in milliseconds during which we consider that music is still active after a music
60// track was stopped - see computeVolume()
61#define SONIFICATION_HEADSET_MUSIC_DELAY 5000
François Gaffie2110e042015-03-24 08:41:51 +010062
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080063// Time in milliseconds during witch some streams are muted while the audio path
64// is switched
65#define MUTE_TIME_MS 2000
66
67#define NUM_TEST_OUTPUTS 5
68
69#define NUM_VOL_CURVE_KNEES 2
70
71// Default minimum length allowed for offloading a compressed track
72// Can be overridden by the audio.offload.min.duration.secs property
73#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
74
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080075// ----------------------------------------------------------------------------
76// AudioPolicyManager implements audio policy manager behavior common to all platforms.
77// ----------------------------------------------------------------------------
78
François Gaffie2110e042015-03-24 08:41:51 +010079class AudioPolicyManager : public AudioPolicyInterface, public AudioPolicyManagerObserver
80
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080081#ifdef AUDIO_POLICY_TEST
82 , public Thread
83#endif //AUDIO_POLICY_TEST
84{
85
86public:
87 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
88 virtual ~AudioPolicyManager();
89
90 // AudioPolicyInterface
91 virtual status_t setDeviceConnectionState(audio_devices_t device,
92 audio_policy_dev_state_t state,
93 const char *device_address,
94 const char *device_name);
95 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
96 const char *device_address);
97 virtual void setPhoneState(audio_mode_t state);
98 virtual void setForceUse(audio_policy_force_use_t usage,
99 audio_policy_forced_cfg_t config);
100 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
François Gaffie2110e042015-03-24 08:41:51 +0100101
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800102 virtual void setSystemProperty(const char* property, const char* value);
103 virtual status_t initCheck();
104 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
105 uint32_t samplingRate,
106 audio_format_t format,
107 audio_channel_mask_t channelMask,
108 audio_output_flags_t flags,
109 const audio_offload_info_t *offloadInfo);
110 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
111 audio_io_handle_t *output,
112 audio_session_t session,
113 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700114 uid_t uid,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800115 uint32_t samplingRate,
116 audio_format_t format,
117 audio_channel_mask_t channelMask,
118 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700119 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800120 const audio_offload_info_t *offloadInfo);
121 virtual status_t startOutput(audio_io_handle_t output,
122 audio_stream_type_t stream,
123 audio_session_t session);
124 virtual status_t stopOutput(audio_io_handle_t output,
125 audio_stream_type_t stream,
126 audio_session_t session);
127 virtual void releaseOutput(audio_io_handle_t output,
128 audio_stream_type_t stream,
129 audio_session_t session);
130 virtual status_t getInputForAttr(const audio_attributes_t *attr,
131 audio_io_handle_t *input,
132 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700133 uid_t uid,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800134 uint32_t samplingRate,
135 audio_format_t format,
136 audio_channel_mask_t channelMask,
137 audio_input_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600138 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800139 input_type_t *inputType);
140
141 // indicates to the audio policy manager that the input starts being used.
142 virtual status_t startInput(audio_io_handle_t input,
143 audio_session_t session);
144
145 // indicates to the audio policy manager that the input stops being used.
146 virtual status_t stopInput(audio_io_handle_t input,
147 audio_session_t session);
148 virtual void releaseInput(audio_io_handle_t input,
149 audio_session_t session);
150 virtual void closeAllInputs();
151 virtual void initStreamVolume(audio_stream_type_t stream,
152 int indexMin,
153 int indexMax);
154 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
155 int index,
156 audio_devices_t device);
157 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
158 int *index,
159 audio_devices_t device);
160
161 // return the strategy corresponding to a given stream type
162 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
163 // return the strategy corresponding to the given audio attributes
164 virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
165
166 // return the enabled output devices for the given stream type
167 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
168
169 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
170 virtual status_t registerEffect(const effect_descriptor_t *desc,
171 audio_io_handle_t io,
172 uint32_t strategy,
173 int session,
174 int id);
François Gaffie45ed3b02015-03-19 10:35:14 +0100175 virtual status_t unregisterEffect(int id)
176 {
177 return mEffects.unregisterEffect(id);
178 }
179 virtual status_t setEffectEnabled(int id, bool enabled)
180 {
181 return mEffects.setEffectEnabled(id, enabled);
182 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800183
Eric Laurentc75307b2015-03-17 15:29:32 -0700184 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800185 // return whether a stream is playing remotely, override to change the definition of
186 // local/remote playback, used for instance by notification manager to not make
187 // media players lose audio focus when not playing locally
188 // For the base implementation, "remotely" means playing during screen mirroring which
189 // uses an output for playback with a non-empty, non "0" address.
Eric Laurentc75307b2015-03-17 15:29:32 -0700190 virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
191 uint32_t inPastMs = 0) const;
192
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800193 virtual bool isSourceActive(audio_source_t source) const;
194
195 virtual status_t dump(int fd);
196
197 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
198
199 virtual status_t listAudioPorts(audio_port_role_t role,
200 audio_port_type_t type,
201 unsigned int *num_ports,
202 struct audio_port *ports,
203 unsigned int *generation);
204 virtual status_t getAudioPort(struct audio_port *port);
205 virtual status_t createAudioPatch(const struct audio_patch *patch,
206 audio_patch_handle_t *handle,
207 uid_t uid);
208 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
209 uid_t uid);
210 virtual status_t listAudioPatches(unsigned int *num_patches,
211 struct audio_patch *patches,
212 unsigned int *generation);
213 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800214
Eric Laurentd60560a2015-04-10 11:31:20 -0700215 virtual void releaseResourcesForUid(uid_t uid);
216
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800217 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
218 audio_io_handle_t *ioHandle,
219 audio_devices_t *device);
220
François Gaffiedf372692015-03-19 10:43:27 +0100221 virtual status_t releaseSoundTriggerSession(audio_session_t session)
222 {
223 return mSoundTriggerSessions.releaseSession(session);
224 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800225
226 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
227 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
228
Eric Laurent554a2772015-04-10 11:29:24 -0700229 virtual status_t startAudioSource(const struct audio_port_config *source,
230 const audio_attributes_t *attributes,
Eric Laurentd60560a2015-04-10 11:31:20 -0700231 audio_io_handle_t *handle,
232 uid_t uid);
Eric Laurent554a2772015-04-10 11:29:24 -0700233 virtual status_t stopAudioSource(audio_io_handle_t handle);
234
Andy Hung2ddee192015-12-18 17:34:44 -0800235 virtual status_t setMasterMono(bool mono);
236 virtual status_t getMasterMono(bool *mono);
237
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800238 // Audio policy configuration file parsing (audio_policy.conf)
239 // TODO candidates to be moved to ConfigParsingUtils
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800240 void defaultAudioPolicyConfig(void);
241
François Gaffie2110e042015-03-24 08:41:51 +0100242 // return the strategy corresponding to a given stream type
243 routing_strategy getStrategy(audio_stream_type_t stream) const;
244
Paul McLeanaa981192015-03-21 09:55:15 -0700245protected:
François Gaffie2110e042015-03-24 08:41:51 +0100246 // From AudioPolicyManagerObserver
247 virtual const AudioPatchCollection &getAudioPatches() const
248 {
249 return mAudioPatches;
250 }
251 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
252 {
253 return mSoundTriggerSessions;
254 }
255 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
256 {
257 return mPolicyMixes;
258 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700259 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100260 {
261 return mOutputs;
262 }
263 virtual const AudioInputCollection &getInputs() const
264 {
265 return mInputs;
266 }
267 virtual const DeviceVector &getAvailableOutputDevices() const
268 {
269 return mAvailableOutputDevices;
270 }
271 virtual const DeviceVector &getAvailableInputDevices() const
272 {
273 return mAvailableInputDevices;
274 }
275 virtual StreamDescriptorCollection &getStreamDescriptors()
276 {
277 return mStreams;
278 }
279 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
280 {
281 return mDefaultOutputDevice;
282 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800283protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700284 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100285 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800286 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
287
288 // return appropriate device for streams handled by the specified strategy according to current
289 // phone state, connected devices...
290 // if fromCache is true, the device is returned from mDeviceForStrategy[],
291 // otherwise it is determine by current state
292 // (device connected,phone state, force use, a2dp output...)
293 // This allows to:
294 // 1 speed up process when the state is stable (when starting or stopping an output)
295 // 2 access to either current device selection (fromCache == true) or
296 // "future" device selection (fromCache == false) when called from a context
297 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
298 // before updateDevicesAndOutputs() is called.
299 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
300 bool fromCache);
301
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700302 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
303 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
304
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800305 // change the route of the specified output. Returns the number of ms we have slept to
306 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700307 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800308 audio_devices_t device,
309 bool force = false,
310 int delayMs = 0,
311 audio_patch_handle_t *patchHandle = NULL,
312 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700313 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800314 int delayMs = 0,
315 audio_patch_handle_t *patchHandle = NULL);
316 status_t setInputDevice(audio_io_handle_t input,
317 audio_devices_t device,
318 bool force = false,
319 audio_patch_handle_t *patchHandle = NULL);
320 status_t resetInputDevice(audio_io_handle_t input,
321 audio_patch_handle_t *patchHandle = NULL);
322
323 // select input device corresponding to requested audio source
324 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
325
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800326 // compute the actual volume for a given stream according to the requested index and a particular
327 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700328 virtual float computeVolume(audio_stream_type_t stream,
329 int index,
330 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800331
332 // check that volume change is permitted, compute and send new volume to audio hardware
333 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700334 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800335 audio_devices_t device,
336 int delayMs = 0, bool force = false);
337
338 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700339 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
340 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800341
342 // Mute or unmute all streams handled by the specified strategy on the specified output
343 void setStrategyMute(routing_strategy strategy,
344 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700345 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800346 int delayMs = 0,
347 audio_devices_t device = (audio_devices_t)0);
348
349 // Mute or unmute the stream on the specified output
350 void setStreamMute(audio_stream_type_t stream,
351 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700352 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800353 int delayMs = 0,
354 audio_devices_t device = (audio_devices_t)0);
355
356 // handle special cases for sonification strategy while in call: mute streams or replace by
357 // a special tone in the device used for communication
358 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
359
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700360 audio_mode_t getPhoneState();
361
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800362 // true if device is in a telephony or VoIP call
363 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800364 // true if given state represents a device in a telephony or VoIP call
365 virtual bool isStateInCall(int state);
366
367 // when a device is connected, checks if an open output can be routed
368 // to this device. If none is open, tries to open one of the available outputs.
369 // Returns an output suitable to this device or 0.
370 // when a device is disconnected, checks if an output is not used any more and
371 // returns its handle if any.
372 // transfers the audio tracks and effects from one output thread to another accordingly.
373 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
374 audio_policy_dev_state_t state,
375 SortedVector<audio_io_handle_t>& outputs,
376 const String8 address);
377
Paul McLean9080a4c2015-06-18 08:24:02 -0700378 status_t checkInputsForDevice(const sp<DeviceDescriptor> devDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800379 audio_policy_dev_state_t state,
380 SortedVector<audio_io_handle_t>& inputs,
381 const String8 address);
382
383 // close an output and its companion duplicating output.
384 void closeOutput(audio_io_handle_t output);
385
386 // close an input.
387 void closeInput(audio_io_handle_t input);
388
389 // checks and if necessary changes outputs used for all strategies.
390 // must be called every time a condition that affects the output choice for a given strategy
391 // changes: connected device, phone state, force use...
392 // Must be called before updateDevicesAndOutputs()
393 void checkOutputForStrategy(routing_strategy strategy);
394
395 // Same as checkOutputForStrategy() but for a all strategies in order of priority
396 void checkOutputForAllStrategies();
397
398 // manages A2DP output suspend/restore according to phone state and BT SCO usage
399 void checkA2dpSuspend();
400
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800401 // selects the most appropriate device on output for current state
402 // must be called every time a condition that affects the device choice for a given output is
403 // changed: connected device, phone state, force use, output start, output stop..
404 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700405 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
406 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800407
408 // updates cache of device used by all strategies (mDeviceForStrategy[])
409 // must be called every time a condition that affects the device choice for a given strategy is
410 // changed: connected device, phone state, force use...
411 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
412 // Must be called after checkOutputForAllStrategies()
413 void updateDevicesAndOutputs();
414
415 // selects the most appropriate device on input for current state
416 audio_devices_t getNewInputDevice(audio_io_handle_t input);
417
François Gaffie45ed3b02015-03-19 10:35:14 +0100418 virtual uint32_t getMaxEffectsCpuLoad()
419 {
420 return mEffects.getMaxEffectsCpuLoad();
421 }
422
423 virtual uint32_t getMaxEffectsMemory()
424 {
425 return mEffects.getMaxEffectsMemory();
426 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800427#ifdef AUDIO_POLICY_TEST
428 virtual bool threadLoop();
429 void exit();
430 int testOutputIndex(audio_io_handle_t output);
431#endif //AUDIO_POLICY_TEST
432
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800433 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700434 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800435 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
436 SortedVector<audio_io_handle_t>& outputs2);
437
438 // mute/unmute strategies using an incompatible device combination
439 // if muting, wait for the audio in pcm buffer to be drained before proceeding
440 // if unmuting, unmute only after the specified delay
441 // Returns the number of ms waited
442 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
443 audio_devices_t prevDevice,
444 uint32_t delayMs);
445
446 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
447 audio_output_flags_t flags,
448 audio_format_t format);
Andy Hungf129b032015-04-07 13:45:50 -0700449 // samplingRate, format, channelMask are in/out and so may be modified
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800450 sp<IOProfile> getInputProfile(audio_devices_t device,
451 String8 address,
452 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -0700453 audio_format_t& format,
454 audio_channel_mask_t& channelMask,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800455 audio_input_flags_t flags);
456 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
457 uint32_t samplingRate,
458 audio_format_t format,
459 audio_channel_mask_t channelMask,
460 audio_output_flags_t flags);
461
462 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
463
François Gaffie53615e22015-03-19 09:24:12 +0100464 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
465 {
466 return mAudioPatches.addAudioPatch(handle, patch);
467 }
468 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
469 {
470 return mAudioPatches.removeAudioPatch(handle);
471 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800472
François Gaffie53615e22015-03-19 09:24:12 +0100473 audio_devices_t availablePrimaryOutputDevices() const
474 {
Eric Laurent87ffa392015-05-22 10:32:38 -0700475 if (!hasPrimaryOutput()) {
476 return AUDIO_DEVICE_NONE;
477 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700478 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100479 }
480 audio_devices_t availablePrimaryInputDevices() const
481 {
Eric Laurent87ffa392015-05-22 10:32:38 -0700482 if (!hasPrimaryOutput()) {
483 return AUDIO_DEVICE_NONE;
484 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700485 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100486 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800487
488 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
489
Eric Laurent493404d2015-04-21 15:07:36 -0700490 // if argument "device" is different from AUDIO_DEVICE_NONE, startSource() will force
491 // the re-evaluation of the output device.
Eric Laurentc75307b2015-03-17 15:29:32 -0700492 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
493 audio_stream_type_t stream,
494 audio_devices_t device,
495 uint32_t *delayMs);
496 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700497 audio_stream_type_t stream,
498 bool forceDeviceUpdate);
499
500 void clearAudioPatches(uid_t uid);
501 void clearSessionRoutes(uid_t uid);
502 void checkStrategyRoute(routing_strategy strategy, audio_io_handle_t ouptutToSkip);
Eric Laurentc75307b2015-03-17 15:29:32 -0700503
Eric Laurent87ffa392015-05-22 10:32:38 -0700504 status_t hasPrimaryOutput() const { return mPrimaryOutput != 0; }
505
Eric Laurentd60560a2015-04-10 11:31:20 -0700506 status_t connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc);
507 status_t disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc);
508
509 sp<AudioSourceDescriptor> getSourceForStrategyOnOutput(audio_io_handle_t output,
510 routing_strategy strategy);
511
512 void cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc);
513
514 void clearAudioSources(uid_t uid);
515
516
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800517 uid_t mUidCached;
518 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700519 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800520 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700521
522 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800523 // copy of mOutputs before setDeviceConnectionState() opens new outputs
524 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700525 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100526 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700527
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800528 DeviceVector mAvailableOutputDevices; // all available output devices
529 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800530
Paul McLean466dc8e2015-04-17 13:15:36 -0600531 SessionRouteMap mOutputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_OUTPUT);
532 SessionRouteMap mInputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_INPUT);
Paul McLeanaa981192015-03-21 09:55:15 -0700533
François Gaffiedfd74092015-03-19 12:10:59 +0100534 StreamDescriptorCollection mStreams; // stream descriptors for volume control
535 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800536 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100537 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800538
François Gaffie45ed3b02015-03-19 10:35:14 +0100539 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800540 bool mA2dpSuspended; // true if A2DP output is suspended
541 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
542 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
543 // to boost soft sounds, used to adjust volume curves accordingly
544
François Gaffie53615e22015-03-19 09:24:12 +0100545 HwModuleCollection mHwModules;
546
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800547 volatile int32_t mAudioPortGeneration;
548
François Gaffie53615e22015-03-19 09:24:12 +0100549 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800550
François Gaffiedf372692015-03-19 10:43:27 +0100551 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800552
553 sp<AudioPatch> mCallTxPatch;
554 sp<AudioPatch> mCallRxPatch;
555
Eric Laurentd60560a2015-04-10 11:31:20 -0700556 HwAudioOutputCollection mHwOutputs;
557 AudioSourceCollection mAudioSources;
558
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800559 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
560 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
561 enum {
562 STARTING_OUTPUT,
563 STARTING_BEACON,
564 STOPPING_OUTPUT,
565 STOPPING_BEACON
566 };
567 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
568 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
569 bool mBeaconMuted; // has STREAM_TTS been muted
Eric Laurent9459fb02015-08-12 18:36:32 -0700570 bool mTtsOutputAvailable; // true if a dedicated output for TTS stream is available
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800571
Andy Hung2ddee192015-12-18 17:34:44 -0800572 bool mMasterMono; // true if we wish to force all outputs to mono
François Gaffie036e1e92015-03-19 10:16:24 +0100573 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800574
575#ifdef AUDIO_POLICY_TEST
576 Mutex mLock;
577 Condition mWaitWorkCV;
578
579 int mCurOutput;
580 bool mDirectOutput;
581 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
582 int mTestInput;
583 uint32_t mTestDevice;
584 uint32_t mTestSamplingRate;
585 uint32_t mTestFormat;
586 uint32_t mTestChannels;
587 uint32_t mTestLatencyMs;
588#endif //AUDIO_POLICY_TEST
589
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800590 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700591
592 // Audio Policy Engine Interface.
593 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800594private:
François Gaffie112b0af2015-11-19 16:13:25 +0100595 // If any, resolve any "dynamic" fields of an Audio Profiles collection
596 void updateAudioProfiles(audio_io_handle_t ioHandle, AudioProfileVector &profiles);
597
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800598 // updates device caching and output for streams that can influence the
599 // routing of notifications
600 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800601 // find the outputs on a given output descriptor that have the given address.
602 // to be called on an AudioOutputDescriptor whose supported devices (as defined
603 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
604 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
605 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700606 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800607 const audio_devices_t device /*in*/,
608 const String8 address /*in*/,
609 SortedVector<audio_io_handle_t>& outputs /*out*/);
610 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
611 // internal method to return the output handle for the given device and format
612 audio_io_handle_t getOutputForDevice(
613 audio_devices_t device,
614 audio_session_t session,
615 audio_stream_type_t stream,
616 uint32_t samplingRate,
617 audio_format_t format,
618 audio_channel_mask_t channelMask,
619 audio_output_flags_t flags,
620 const audio_offload_info_t *offloadInfo);
Eric Laurent599c7582015-12-07 18:05:55 -0800621 // internal method to return the input handle for the given device and format
622 audio_io_handle_t getInputForDevice(audio_devices_t device,
623 String8 address,
624 audio_session_t session,
625 uid_t uid,
626 audio_source_t inputSource,
627 uint32_t samplingRate,
628 audio_format_t format,
629 audio_channel_mask_t channelMask,
630 audio_input_flags_t flags,
631 AudioMix *policyMix);
632
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800633 // internal function to derive a stream type value from audio attributes
634 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800635 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
636 // returns 0 if no mute/unmute event happened, the largest latency of the device where
637 // the mute/unmute happened
638 uint32_t handleEventForBeacon(int event);
639 uint32_t setBeaconMute(bool mute);
640 bool isValidAttributes(const audio_attributes_t *paa);
641
642 // select input device corresponding to requested audio source and return associated policy
643 // mix if any. Calls getDeviceForInputSource().
644 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
645 AudioMix **policyMix = NULL);
646
647 // Called by setDeviceConnectionState().
648 status_t setDeviceConnectionStateInt(audio_devices_t device,
649 audio_policy_dev_state_t state,
650 const char *device_address,
651 const char *device_name);
Andy Hung2ddee192015-12-18 17:34:44 -0800652 void updateMono(audio_io_handle_t output) {
653 AudioParameter param;
654 param.addInt(String8(AUDIO_PARAMETER_MONO_OUTPUT), (int)mMasterMono);
655 mpClientInterface->setParameters(output, param.toString());
656 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800657};
658
659};