blob: 9baeeb6f9fa9ede95a963a406ae1f720b2713261 [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
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 Gaffie2110e042015-03-24 08:41:51 +010057
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080058// 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 Trivi56ec4ff2015-01-23 16:45:18 -080070// ----------------------------------------------------------------------------
71// AudioPolicyManager implements audio policy manager behavior common to all platforms.
72// ----------------------------------------------------------------------------
73
François Gaffie2110e042015-03-24 08:41:51 +010074class AudioPolicyManager : public AudioPolicyInterface, public AudioPolicyManagerObserver
75
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080076#ifdef AUDIO_POLICY_TEST
77 , public Thread
78#endif //AUDIO_POLICY_TEST
79{
80
81public:
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 Gaffie2110e042015-03-24 08:41:51 +010096
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080097 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 McLeanaa981192015-03-21 09:55:15 -0700113 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800114 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 Gaffie45ed3b02015-03-19 10:35:14 +0100167 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 Trivi56ec4ff2015-01-23 16:45:18 -0800175
Eric Laurentc75307b2015-03-17 15:29:32 -0700176 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800177 // return whether a stream is playing remotely, override to change the definition of
178 // local/remote playback, used for instance by notification manager to not make
179 // media players lose audio focus when not playing locally
180 // For the base implementation, "remotely" means playing during screen mirroring which
181 // uses an output for playback with a non-empty, non "0" address.
Eric Laurentc75307b2015-03-17 15:29:32 -0700182 virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
183 uint32_t inPastMs = 0) const;
184
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800185 virtual bool isSourceActive(audio_source_t source) const;
186
187 virtual status_t dump(int fd);
188
189 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
190
191 virtual status_t listAudioPorts(audio_port_role_t role,
192 audio_port_type_t type,
193 unsigned int *num_ports,
194 struct audio_port *ports,
195 unsigned int *generation);
196 virtual status_t getAudioPort(struct audio_port *port);
197 virtual status_t createAudioPatch(const struct audio_patch *patch,
198 audio_patch_handle_t *handle,
199 uid_t uid);
200 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
201 uid_t uid);
202 virtual status_t listAudioPatches(unsigned int *num_patches,
203 struct audio_patch *patches,
204 unsigned int *generation);
205 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
206 virtual void clearAudioPatches(uid_t uid);
207
208 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
209 audio_io_handle_t *ioHandle,
210 audio_devices_t *device);
211
François Gaffiedf372692015-03-19 10:43:27 +0100212 virtual status_t releaseSoundTriggerSession(audio_session_t session)
213 {
214 return mSoundTriggerSessions.releaseSession(session);
215 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800216
217 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
218 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
219
220 // Audio policy configuration file parsing (audio_policy.conf)
221 // TODO candidates to be moved to ConfigParsingUtils
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800222 void defaultAudioPolicyConfig(void);
223
François Gaffie2110e042015-03-24 08:41:51 +0100224 // return the strategy corresponding to a given stream type
225 routing_strategy getStrategy(audio_stream_type_t stream) const;
226
Paul McLeanaa981192015-03-21 09:55:15 -0700227protected:
228 class SessionRoute : public RefBase
229 {
230 public:
231 friend class SessionRouteMap;
232 SessionRoute(audio_session_t session,
233 audio_stream_type_t streamType,
234 sp<DeviceDescriptor> deviceDescriptor)
235 : mSession(session),
236 mStreamType(streamType),
237 mDeviceDescriptor(deviceDescriptor),
238 mRefCount(0),
239 mActivityCount(0) {}
240
241 audio_session_t mSession;
242 audio_stream_type_t mStreamType;
243
244 sp<DeviceDescriptor> mDeviceDescriptor;
245
246 // "reference" counting
247 int mRefCount; // +/- on references
248 int mActivityCount; // +/- on start/stop
249
250 void log(const char* prefix);
251 };
252
253 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>>
254 {
255 public:
256 bool hasRoute(audio_session_t session);
257 void addRoute(audio_session_t session, audio_stream_type_t streamType,
258 sp<DeviceDescriptor> deviceDescriptor);
259 void removeRoute(audio_session_t session);
260
261 int incRouteActivity(audio_session_t session);
262 int decRouteActivity(audio_session_t session);
263
264 void log(const char* caption);
265 };
266
François Gaffie2110e042015-03-24 08:41:51 +0100267 // From AudioPolicyManagerObserver
268 virtual const AudioPatchCollection &getAudioPatches() const
269 {
270 return mAudioPatches;
271 }
272 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
273 {
274 return mSoundTriggerSessions;
275 }
276 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
277 {
278 return mPolicyMixes;
279 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700280 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100281 {
282 return mOutputs;
283 }
284 virtual const AudioInputCollection &getInputs() const
285 {
286 return mInputs;
287 }
288 virtual const DeviceVector &getAvailableOutputDevices() const
289 {
290 return mAvailableOutputDevices;
291 }
292 virtual const DeviceVector &getAvailableInputDevices() const
293 {
294 return mAvailableInputDevices;
295 }
296 virtual StreamDescriptorCollection &getStreamDescriptors()
297 {
298 return mStreams;
299 }
300 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
301 {
302 return mDefaultOutputDevice;
303 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800304protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700305 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100306 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800307 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
308
309 // return appropriate device for streams handled by the specified strategy according to current
310 // phone state, connected devices...
311 // if fromCache is true, the device is returned from mDeviceForStrategy[],
312 // otherwise it is determine by current state
313 // (device connected,phone state, force use, a2dp output...)
314 // This allows to:
315 // 1 speed up process when the state is stable (when starting or stopping an output)
316 // 2 access to either current device selection (fromCache == true) or
317 // "future" device selection (fromCache == false) when called from a context
318 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
319 // before updateDevicesAndOutputs() is called.
320 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
321 bool fromCache);
322
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700323 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
324 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
325
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800326 // change the route of the specified output. Returns the number of ms we have slept to
327 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700328 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800329 audio_devices_t device,
330 bool force = false,
331 int delayMs = 0,
332 audio_patch_handle_t *patchHandle = NULL,
333 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700334 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800335 int delayMs = 0,
336 audio_patch_handle_t *patchHandle = NULL);
337 status_t setInputDevice(audio_io_handle_t input,
338 audio_devices_t device,
339 bool force = false,
340 audio_patch_handle_t *patchHandle = NULL);
341 status_t resetInputDevice(audio_io_handle_t input,
342 audio_patch_handle_t *patchHandle = NULL);
343
344 // select input device corresponding to requested audio source
345 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
346
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800347 // compute the actual volume for a given stream according to the requested index and a particular
348 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700349 virtual float computeVolume(audio_stream_type_t stream,
350 int index,
351 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800352
353 // check that volume change is permitted, compute and send new volume to audio hardware
354 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700355 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800356 audio_devices_t device,
357 int delayMs = 0, bool force = false);
358
359 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700360 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
361 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800362
363 // Mute or unmute all streams handled by the specified strategy on the specified output
364 void setStrategyMute(routing_strategy strategy,
365 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700366 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800367 int delayMs = 0,
368 audio_devices_t device = (audio_devices_t)0);
369
370 // Mute or unmute the stream on the specified output
371 void setStreamMute(audio_stream_type_t stream,
372 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700373 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800374 int delayMs = 0,
375 audio_devices_t device = (audio_devices_t)0);
376
377 // handle special cases for sonification strategy while in call: mute streams or replace by
378 // a special tone in the device used for communication
379 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
380
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700381 audio_mode_t getPhoneState();
382
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800383 // true if device is in a telephony or VoIP call
384 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800385 // true if given state represents a device in a telephony or VoIP call
386 virtual bool isStateInCall(int state);
387
388 // when a device is connected, checks if an open output can be routed
389 // to this device. If none is open, tries to open one of the available outputs.
390 // Returns an output suitable to this device or 0.
391 // when a device is disconnected, checks if an output is not used any more and
392 // returns its handle if any.
393 // transfers the audio tracks and effects from one output thread to another accordingly.
394 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
395 audio_policy_dev_state_t state,
396 SortedVector<audio_io_handle_t>& outputs,
397 const String8 address);
398
399 status_t checkInputsForDevice(audio_devices_t device,
400 audio_policy_dev_state_t state,
401 SortedVector<audio_io_handle_t>& inputs,
402 const String8 address);
403
404 // close an output and its companion duplicating output.
405 void closeOutput(audio_io_handle_t output);
406
407 // close an input.
408 void closeInput(audio_io_handle_t input);
409
410 // checks and if necessary changes outputs used for all strategies.
411 // must be called every time a condition that affects the output choice for a given strategy
412 // changes: connected device, phone state, force use...
413 // Must be called before updateDevicesAndOutputs()
414 void checkOutputForStrategy(routing_strategy strategy);
415
416 // Same as checkOutputForStrategy() but for a all strategies in order of priority
417 void checkOutputForAllStrategies();
418
419 // manages A2DP output suspend/restore according to phone state and BT SCO usage
420 void checkA2dpSuspend();
421
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800422 // selects the most appropriate device on output for current state
423 // must be called every time a condition that affects the device choice for a given output is
424 // changed: connected device, phone state, force use, output start, output stop..
425 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700426 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
427 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800428
429 // updates cache of device used by all strategies (mDeviceForStrategy[])
430 // must be called every time a condition that affects the device choice for a given strategy is
431 // changed: connected device, phone state, force use...
432 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
433 // Must be called after checkOutputForAllStrategies()
434 void updateDevicesAndOutputs();
435
436 // selects the most appropriate device on input for current state
437 audio_devices_t getNewInputDevice(audio_io_handle_t input);
438
François Gaffie45ed3b02015-03-19 10:35:14 +0100439 virtual uint32_t getMaxEffectsCpuLoad()
440 {
441 return mEffects.getMaxEffectsCpuLoad();
442 }
443
444 virtual uint32_t getMaxEffectsMemory()
445 {
446 return mEffects.getMaxEffectsMemory();
447 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800448#ifdef AUDIO_POLICY_TEST
449 virtual bool threadLoop();
450 void exit();
451 int testOutputIndex(audio_io_handle_t output);
452#endif //AUDIO_POLICY_TEST
453
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800454 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700455 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800456 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
457 SortedVector<audio_io_handle_t>& outputs2);
458
459 // mute/unmute strategies using an incompatible device combination
460 // if muting, wait for the audio in pcm buffer to be drained before proceeding
461 // if unmuting, unmute only after the specified delay
462 // Returns the number of ms waited
463 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
464 audio_devices_t prevDevice,
465 uint32_t delayMs);
466
467 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
468 audio_output_flags_t flags,
469 audio_format_t format);
470 // samplingRate parameter is an in/out and so may be modified
471 sp<IOProfile> getInputProfile(audio_devices_t device,
472 String8 address,
473 uint32_t& samplingRate,
474 audio_format_t format,
475 audio_channel_mask_t channelMask,
476 audio_input_flags_t flags);
477 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
478 uint32_t samplingRate,
479 audio_format_t format,
480 audio_channel_mask_t channelMask,
481 audio_output_flags_t flags);
482
483 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
484
François Gaffie53615e22015-03-19 09:24:12 +0100485 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
486 {
487 return mAudioPatches.addAudioPatch(handle, patch);
488 }
489 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
490 {
491 return mAudioPatches.removeAudioPatch(handle);
492 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800493
François Gaffie53615e22015-03-19 09:24:12 +0100494 audio_devices_t availablePrimaryOutputDevices() const
495 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700496 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100497 }
498 audio_devices_t availablePrimaryInputDevices() const
499 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700500 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100501 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800502
503 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
504
Eric Laurentc75307b2015-03-17 15:29:32 -0700505 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
506 audio_stream_type_t stream,
507 audio_devices_t device,
508 uint32_t *delayMs);
509 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
510 audio_stream_type_t stream);
511
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800512 uid_t mUidCached;
513 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700514 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800515 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700516
517 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800518 // copy of mOutputs before setDeviceConnectionState() opens new outputs
519 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700520 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100521 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700522
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800523 DeviceVector mAvailableOutputDevices; // all available output devices
524 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800525
Paul McLeanaa981192015-03-21 09:55:15 -0700526 SessionRouteMap mOutputRoutes;
527 SessionRouteMap mInputRoutes;
528
François Gaffiedfd74092015-03-19 12:10:59 +0100529 StreamDescriptorCollection mStreams; // stream descriptors for volume control
530 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800531 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100532 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800533
François Gaffie45ed3b02015-03-19 10:35:14 +0100534 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800535 bool mA2dpSuspended; // true if A2DP output is suspended
536 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
537 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
538 // to boost soft sounds, used to adjust volume curves accordingly
539
François Gaffie53615e22015-03-19 09:24:12 +0100540 HwModuleCollection mHwModules;
541
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800542 volatile int32_t mAudioPortGeneration;
543
François Gaffie53615e22015-03-19 09:24:12 +0100544 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800545
François Gaffiedf372692015-03-19 10:43:27 +0100546 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800547
548 sp<AudioPatch> mCallTxPatch;
549 sp<AudioPatch> mCallRxPatch;
550
551 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
552 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
553 enum {
554 STARTING_OUTPUT,
555 STARTING_BEACON,
556 STOPPING_OUTPUT,
557 STOPPING_BEACON
558 };
559 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
560 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
561 bool mBeaconMuted; // has STREAM_TTS been muted
562
François Gaffie036e1e92015-03-19 10:16:24 +0100563 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800564
565#ifdef AUDIO_POLICY_TEST
566 Mutex mLock;
567 Condition mWaitWorkCV;
568
569 int mCurOutput;
570 bool mDirectOutput;
571 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
572 int mTestInput;
573 uint32_t mTestDevice;
574 uint32_t mTestSamplingRate;
575 uint32_t mTestFormat;
576 uint32_t mTestChannels;
577 uint32_t mTestLatencyMs;
578#endif //AUDIO_POLICY_TEST
579
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800580 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700581
582 // Audio Policy Engine Interface.
583 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800584private:
585 // updates device caching and output for streams that can influence the
586 // routing of notifications
587 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800588 // find the outputs on a given output descriptor that have the given address.
589 // to be called on an AudioOutputDescriptor whose supported devices (as defined
590 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
591 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
592 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700593 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800594 const audio_devices_t device /*in*/,
595 const String8 address /*in*/,
596 SortedVector<audio_io_handle_t>& outputs /*out*/);
597 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
598 // internal method to return the output handle for the given device and format
599 audio_io_handle_t getOutputForDevice(
600 audio_devices_t device,
601 audio_session_t session,
602 audio_stream_type_t stream,
603 uint32_t samplingRate,
604 audio_format_t format,
605 audio_channel_mask_t channelMask,
606 audio_output_flags_t flags,
607 const audio_offload_info_t *offloadInfo);
608 // internal function to derive a stream type value from audio attributes
609 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800610 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
611 // returns 0 if no mute/unmute event happened, the largest latency of the device where
612 // the mute/unmute happened
613 uint32_t handleEventForBeacon(int event);
614 uint32_t setBeaconMute(bool mute);
615 bool isValidAttributes(const audio_attributes_t *paa);
616
617 // select input device corresponding to requested audio source and return associated policy
618 // mix if any. Calls getDeviceForInputSource().
619 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
620 AudioMix **policyMix = NULL);
621
622 // Called by setDeviceConnectionState().
623 status_t setDeviceConnectionStateInt(audio_devices_t device,
624 audio_policy_dev_state_t state,
625 const char *device_address,
626 const char *device_name);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800627};
628
629};