blob: 146a7af3358128e7b6db968ea92e82cfcca5eb59 [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>
27#include <media/AudioPolicy.h>
28#include "AudioPolicyInterface.h"
29
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -070030#include <AudioPolicyManagerInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010031#include <AudioPolicyManagerObserver.h>
François Gaffie98cc1912015-03-18 17:52:40 +010032#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 Gaffie036e1e92015-03-19 10:16:24 +010041#include <AudioPolicyMix.h>
François Gaffie45ed3b02015-03-19 10:35:14 +010042#include <EffectDescriptor.h>
François Gaffiedf372692015-03-19 10:43:27 +010043#include <SoundTriggerSession.h>
François Gaffiedfd74092015-03-19 12:10:59 +010044#include <StreamDescriptor.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080045
46namespace 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 Laurentffbc80f2015-03-18 18:30:19 -070052#define SONIFICATION_HEADSET_VOLUME_FACTOR_DB (-6)
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080053// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
54#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
Eric Laurentffbc80f2015-03-18 18:30:19 -070055#define SONIFICATION_HEADSET_VOLUME_MIN_DB (-36)
56
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080057// 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 Gaffie2110e042015-03-24 08:41:51 +010060
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080061// 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 Trivi56ec4ff2015-01-23 16:45:18 -080073// ----------------------------------------------------------------------------
74// AudioPolicyManager implements audio policy manager behavior common to all platforms.
75// ----------------------------------------------------------------------------
76
François Gaffie2110e042015-03-24 08:41:51 +010077class AudioPolicyManager : public AudioPolicyInterface, public AudioPolicyManagerObserver
78
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080079#ifdef AUDIO_POLICY_TEST
80 , public Thread
81#endif //AUDIO_POLICY_TEST
82{
83
84public:
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 Gaffie2110e042015-03-24 08:41:51 +010099
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800100 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 McLeanaa981192015-03-21 09:55:15 -0700116 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800117 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 Gaffie45ed3b02015-03-19 10:35:14 +0100170 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 Trivi56ec4ff2015-01-23 16:45:18 -0800178
Eric Laurentc75307b2015-03-17 15:29:32 -0700179 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800180 // 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 Laurentc75307b2015-03-17 15:29:32 -0700185 virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
186 uint32_t inPastMs = 0) const;
187
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800188 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 Gaffiedf372692015-03-19 10:43:27 +0100215 virtual status_t releaseSoundTriggerSession(audio_session_t session)
216 {
217 return mSoundTriggerSessions.releaseSession(session);
218 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800219
220 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
221 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
222
Eric Laurent554a2772015-04-10 11:29:24 -0700223 virtual status_t startAudioSource(const struct audio_port_config *source,
224 const audio_attributes_t *attributes,
225 audio_io_handle_t *handle);
226 virtual status_t stopAudioSource(audio_io_handle_t handle);
227
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800228 // Audio policy configuration file parsing (audio_policy.conf)
229 // TODO candidates to be moved to ConfigParsingUtils
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800230 void defaultAudioPolicyConfig(void);
231
François Gaffie2110e042015-03-24 08:41:51 +0100232 // return the strategy corresponding to a given stream type
233 routing_strategy getStrategy(audio_stream_type_t stream) const;
234
Paul McLeanaa981192015-03-21 09:55:15 -0700235protected:
236 class SessionRoute : public RefBase
237 {
238 public:
239 friend class SessionRouteMap;
240 SessionRoute(audio_session_t session,
241 audio_stream_type_t streamType,
242 sp<DeviceDescriptor> deviceDescriptor)
243 : mSession(session),
244 mStreamType(streamType),
245 mDeviceDescriptor(deviceDescriptor),
246 mRefCount(0),
247 mActivityCount(0) {}
248
249 audio_session_t mSession;
250 audio_stream_type_t mStreamType;
251
252 sp<DeviceDescriptor> mDeviceDescriptor;
253
254 // "reference" counting
255 int mRefCount; // +/- on references
256 int mActivityCount; // +/- on start/stop
257
258 void log(const char* prefix);
259 };
260
261 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>>
262 {
263 public:
264 bool hasRoute(audio_session_t session);
265 void addRoute(audio_session_t session, audio_stream_type_t streamType,
266 sp<DeviceDescriptor> deviceDescriptor);
267 void removeRoute(audio_session_t session);
268
269 int incRouteActivity(audio_session_t session);
270 int decRouteActivity(audio_session_t session);
271
272 void log(const char* caption);
273 };
274
François Gaffie2110e042015-03-24 08:41:51 +0100275 // From AudioPolicyManagerObserver
276 virtual const AudioPatchCollection &getAudioPatches() const
277 {
278 return mAudioPatches;
279 }
280 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
281 {
282 return mSoundTriggerSessions;
283 }
284 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
285 {
286 return mPolicyMixes;
287 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700288 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100289 {
290 return mOutputs;
291 }
292 virtual const AudioInputCollection &getInputs() const
293 {
294 return mInputs;
295 }
296 virtual const DeviceVector &getAvailableOutputDevices() const
297 {
298 return mAvailableOutputDevices;
299 }
300 virtual const DeviceVector &getAvailableInputDevices() const
301 {
302 return mAvailableInputDevices;
303 }
304 virtual StreamDescriptorCollection &getStreamDescriptors()
305 {
306 return mStreams;
307 }
308 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
309 {
310 return mDefaultOutputDevice;
311 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800312protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700313 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100314 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800315 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
316
317 // return appropriate device for streams handled by the specified strategy according to current
318 // phone state, connected devices...
319 // if fromCache is true, the device is returned from mDeviceForStrategy[],
320 // otherwise it is determine by current state
321 // (device connected,phone state, force use, a2dp output...)
322 // This allows to:
323 // 1 speed up process when the state is stable (when starting or stopping an output)
324 // 2 access to either current device selection (fromCache == true) or
325 // "future" device selection (fromCache == false) when called from a context
326 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
327 // before updateDevicesAndOutputs() is called.
328 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
329 bool fromCache);
330
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700331 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
332 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
333
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800334 // change the route of the specified output. Returns the number of ms we have slept to
335 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700336 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800337 audio_devices_t device,
338 bool force = false,
339 int delayMs = 0,
340 audio_patch_handle_t *patchHandle = NULL,
341 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700342 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800343 int delayMs = 0,
344 audio_patch_handle_t *patchHandle = NULL);
345 status_t setInputDevice(audio_io_handle_t input,
346 audio_devices_t device,
347 bool force = false,
348 audio_patch_handle_t *patchHandle = NULL);
349 status_t resetInputDevice(audio_io_handle_t input,
350 audio_patch_handle_t *patchHandle = NULL);
351
352 // select input device corresponding to requested audio source
353 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
354
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800355 // compute the actual volume for a given stream according to the requested index and a particular
356 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700357 virtual float computeVolume(audio_stream_type_t stream,
358 int index,
359 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800360
361 // check that volume change is permitted, compute and send new volume to audio hardware
362 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700363 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800364 audio_devices_t device,
365 int delayMs = 0, bool force = false);
366
367 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700368 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
369 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800370
371 // Mute or unmute all streams handled by the specified strategy on the specified output
372 void setStrategyMute(routing_strategy strategy,
373 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700374 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800375 int delayMs = 0,
376 audio_devices_t device = (audio_devices_t)0);
377
378 // Mute or unmute the stream on the specified output
379 void setStreamMute(audio_stream_type_t stream,
380 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700381 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800382 int delayMs = 0,
383 audio_devices_t device = (audio_devices_t)0);
384
385 // handle special cases for sonification strategy while in call: mute streams or replace by
386 // a special tone in the device used for communication
387 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
388
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700389 audio_mode_t getPhoneState();
390
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800391 // true if device is in a telephony or VoIP call
392 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800393 // true if given state represents a device in a telephony or VoIP call
394 virtual bool isStateInCall(int state);
395
396 // when a device is connected, checks if an open output can be routed
397 // to this device. If none is open, tries to open one of the available outputs.
398 // Returns an output suitable to this device or 0.
399 // when a device is disconnected, checks if an output is not used any more and
400 // returns its handle if any.
401 // transfers the audio tracks and effects from one output thread to another accordingly.
402 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
403 audio_policy_dev_state_t state,
404 SortedVector<audio_io_handle_t>& outputs,
405 const String8 address);
406
407 status_t checkInputsForDevice(audio_devices_t device,
408 audio_policy_dev_state_t state,
409 SortedVector<audio_io_handle_t>& inputs,
410 const String8 address);
411
412 // close an output and its companion duplicating output.
413 void closeOutput(audio_io_handle_t output);
414
415 // close an input.
416 void closeInput(audio_io_handle_t input);
417
418 // checks and if necessary changes outputs used for all strategies.
419 // must be called every time a condition that affects the output choice for a given strategy
420 // changes: connected device, phone state, force use...
421 // Must be called before updateDevicesAndOutputs()
422 void checkOutputForStrategy(routing_strategy strategy);
423
424 // Same as checkOutputForStrategy() but for a all strategies in order of priority
425 void checkOutputForAllStrategies();
426
427 // manages A2DP output suspend/restore according to phone state and BT SCO usage
428 void checkA2dpSuspend();
429
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800430 // selects the most appropriate device on output for current state
431 // must be called every time a condition that affects the device choice for a given output is
432 // changed: connected device, phone state, force use, output start, output stop..
433 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700434 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
435 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800436
437 // updates cache of device used by all strategies (mDeviceForStrategy[])
438 // must be called every time a condition that affects the device choice for a given strategy is
439 // changed: connected device, phone state, force use...
440 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
441 // Must be called after checkOutputForAllStrategies()
442 void updateDevicesAndOutputs();
443
444 // selects the most appropriate device on input for current state
445 audio_devices_t getNewInputDevice(audio_io_handle_t input);
446
François Gaffie45ed3b02015-03-19 10:35:14 +0100447 virtual uint32_t getMaxEffectsCpuLoad()
448 {
449 return mEffects.getMaxEffectsCpuLoad();
450 }
451
452 virtual uint32_t getMaxEffectsMemory()
453 {
454 return mEffects.getMaxEffectsMemory();
455 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800456#ifdef AUDIO_POLICY_TEST
457 virtual bool threadLoop();
458 void exit();
459 int testOutputIndex(audio_io_handle_t output);
460#endif //AUDIO_POLICY_TEST
461
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800462 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700463 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800464 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
465 SortedVector<audio_io_handle_t>& outputs2);
466
467 // mute/unmute strategies using an incompatible device combination
468 // if muting, wait for the audio in pcm buffer to be drained before proceeding
469 // if unmuting, unmute only after the specified delay
470 // Returns the number of ms waited
471 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
472 audio_devices_t prevDevice,
473 uint32_t delayMs);
474
475 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
476 audio_output_flags_t flags,
477 audio_format_t format);
Andy Hungf129b032015-04-07 13:45:50 -0700478 // samplingRate, format, channelMask are in/out and so may be modified
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800479 sp<IOProfile> getInputProfile(audio_devices_t device,
480 String8 address,
481 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -0700482 audio_format_t& format,
483 audio_channel_mask_t& channelMask,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800484 audio_input_flags_t flags);
485 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
486 uint32_t samplingRate,
487 audio_format_t format,
488 audio_channel_mask_t channelMask,
489 audio_output_flags_t flags);
490
491 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
492
François Gaffie53615e22015-03-19 09:24:12 +0100493 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
494 {
495 return mAudioPatches.addAudioPatch(handle, patch);
496 }
497 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
498 {
499 return mAudioPatches.removeAudioPatch(handle);
500 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800501
François Gaffie53615e22015-03-19 09:24:12 +0100502 audio_devices_t availablePrimaryOutputDevices() const
503 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700504 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100505 }
506 audio_devices_t availablePrimaryInputDevices() const
507 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700508 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100509 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800510
511 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
512
Eric Laurentc75307b2015-03-17 15:29:32 -0700513 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
514 audio_stream_type_t stream,
515 audio_devices_t device,
516 uint32_t *delayMs);
517 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
518 audio_stream_type_t stream);
519
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800520 uid_t mUidCached;
521 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700522 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800523 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700524
525 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800526 // copy of mOutputs before setDeviceConnectionState() opens new outputs
527 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700528 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100529 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700530
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800531 DeviceVector mAvailableOutputDevices; // all available output devices
532 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800533
Paul McLeanaa981192015-03-21 09:55:15 -0700534 SessionRouteMap mOutputRoutes;
535 SessionRouteMap mInputRoutes;
536
François Gaffiedfd74092015-03-19 12:10:59 +0100537 StreamDescriptorCollection mStreams; // stream descriptors for volume control
538 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800539 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100540 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800541
François Gaffie45ed3b02015-03-19 10:35:14 +0100542 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800543 bool mA2dpSuspended; // true if A2DP output is suspended
544 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
545 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
546 // to boost soft sounds, used to adjust volume curves accordingly
547
François Gaffie53615e22015-03-19 09:24:12 +0100548 HwModuleCollection mHwModules;
549
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800550 volatile int32_t mAudioPortGeneration;
551
François Gaffie53615e22015-03-19 09:24:12 +0100552 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800553
François Gaffiedf372692015-03-19 10:43:27 +0100554 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800555
556 sp<AudioPatch> mCallTxPatch;
557 sp<AudioPatch> mCallRxPatch;
558
559 // 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
570
François Gaffie036e1e92015-03-19 10:16:24 +0100571 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800572
573#ifdef AUDIO_POLICY_TEST
574 Mutex mLock;
575 Condition mWaitWorkCV;
576
577 int mCurOutput;
578 bool mDirectOutput;
579 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
580 int mTestInput;
581 uint32_t mTestDevice;
582 uint32_t mTestSamplingRate;
583 uint32_t mTestFormat;
584 uint32_t mTestChannels;
585 uint32_t mTestLatencyMs;
586#endif //AUDIO_POLICY_TEST
587
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800588 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700589
590 // Audio Policy Engine Interface.
591 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800592private:
593 // updates device caching and output for streams that can influence the
594 // routing of notifications
595 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800596 // find the outputs on a given output descriptor that have the given address.
597 // to be called on an AudioOutputDescriptor whose supported devices (as defined
598 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
599 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
600 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700601 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800602 const audio_devices_t device /*in*/,
603 const String8 address /*in*/,
604 SortedVector<audio_io_handle_t>& outputs /*out*/);
605 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
606 // internal method to return the output handle for the given device and format
607 audio_io_handle_t getOutputForDevice(
608 audio_devices_t device,
609 audio_session_t session,
610 audio_stream_type_t stream,
611 uint32_t samplingRate,
612 audio_format_t format,
613 audio_channel_mask_t channelMask,
614 audio_output_flags_t flags,
615 const audio_offload_info_t *offloadInfo);
616 // internal function to derive a stream type value from audio attributes
617 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800618 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
619 // returns 0 if no mute/unmute event happened, the largest latency of the device where
620 // the mute/unmute happened
621 uint32_t handleEventForBeacon(int event);
622 uint32_t setBeaconMute(bool mute);
623 bool isValidAttributes(const audio_attributes_t *paa);
624
625 // select input device corresponding to requested audio source and return associated policy
626 // mix if any. Calls getDeviceForInputSource().
627 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
628 AudioMix **policyMix = NULL);
629
630 // Called by setDeviceConnectionState().
631 status_t setDeviceConnectionStateInt(audio_devices_t device,
632 audio_policy_dev_state_t state,
633 const char *device_address,
634 const char *device_name);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800635};
636
637};