blob: 521f6c42f1973770958e8de48c5356aec1ee6885 [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),
Eric Laurent493404d2015-04-21 15:07:36 -0700247 mActivityCount(0),
248 mChanged(false) {}
249
250 void log(const char* prefix);
Paul McLeanaa981192015-03-21 09:55:15 -0700251
252 audio_session_t mSession;
253 audio_stream_type_t mStreamType;
254
255 sp<DeviceDescriptor> mDeviceDescriptor;
256
257 // "reference" counting
Eric Laurent493404d2015-04-21 15:07:36 -0700258 int mRefCount; // +/- on references
259 int mActivityCount; // +/- on start/stop
260 bool mChanged;
Paul McLeanaa981192015-03-21 09:55:15 -0700261 };
262
263 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>>
264 {
265 public:
266 bool hasRoute(audio_session_t session);
267 void addRoute(audio_session_t session, audio_stream_type_t streamType,
268 sp<DeviceDescriptor> deviceDescriptor);
269 void removeRoute(audio_session_t session);
270
271 int incRouteActivity(audio_session_t session);
272 int decRouteActivity(audio_session_t session);
Eric Laurent493404d2015-04-21 15:07:36 -0700273 bool hasRouteChanged(audio_session_t session); // also clears the changed flag
Paul McLeanaa981192015-03-21 09:55:15 -0700274 void log(const char* caption);
275 };
276
François Gaffie2110e042015-03-24 08:41:51 +0100277 // From AudioPolicyManagerObserver
278 virtual const AudioPatchCollection &getAudioPatches() const
279 {
280 return mAudioPatches;
281 }
282 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
283 {
284 return mSoundTriggerSessions;
285 }
286 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
287 {
288 return mPolicyMixes;
289 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700290 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100291 {
292 return mOutputs;
293 }
294 virtual const AudioInputCollection &getInputs() const
295 {
296 return mInputs;
297 }
298 virtual const DeviceVector &getAvailableOutputDevices() const
299 {
300 return mAvailableOutputDevices;
301 }
302 virtual const DeviceVector &getAvailableInputDevices() const
303 {
304 return mAvailableInputDevices;
305 }
306 virtual StreamDescriptorCollection &getStreamDescriptors()
307 {
308 return mStreams;
309 }
310 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
311 {
312 return mDefaultOutputDevice;
313 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800314protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700315 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100316 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800317 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
318
319 // return appropriate device for streams handled by the specified strategy according to current
320 // phone state, connected devices...
321 // if fromCache is true, the device is returned from mDeviceForStrategy[],
322 // otherwise it is determine by current state
323 // (device connected,phone state, force use, a2dp output...)
324 // This allows to:
325 // 1 speed up process when the state is stable (when starting or stopping an output)
326 // 2 access to either current device selection (fromCache == true) or
327 // "future" device selection (fromCache == false) when called from a context
328 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
329 // before updateDevicesAndOutputs() is called.
330 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
331 bool fromCache);
332
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700333 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
334 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
335
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800336 // change the route of the specified output. Returns the number of ms we have slept to
337 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700338 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800339 audio_devices_t device,
340 bool force = false,
341 int delayMs = 0,
342 audio_patch_handle_t *patchHandle = NULL,
343 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700344 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800345 int delayMs = 0,
346 audio_patch_handle_t *patchHandle = NULL);
347 status_t setInputDevice(audio_io_handle_t input,
348 audio_devices_t device,
349 bool force = false,
350 audio_patch_handle_t *patchHandle = NULL);
351 status_t resetInputDevice(audio_io_handle_t input,
352 audio_patch_handle_t *patchHandle = NULL);
353
354 // select input device corresponding to requested audio source
355 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
356
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800357 // compute the actual volume for a given stream according to the requested index and a particular
358 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700359 virtual float computeVolume(audio_stream_type_t stream,
360 int index,
361 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800362
363 // check that volume change is permitted, compute and send new volume to audio hardware
364 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700365 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800366 audio_devices_t device,
367 int delayMs = 0, bool force = false);
368
369 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700370 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
371 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800372
373 // Mute or unmute all streams handled by the specified strategy on the specified output
374 void setStrategyMute(routing_strategy strategy,
375 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700376 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800377 int delayMs = 0,
378 audio_devices_t device = (audio_devices_t)0);
379
380 // Mute or unmute the stream on the specified output
381 void setStreamMute(audio_stream_type_t stream,
382 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700383 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800384 int delayMs = 0,
385 audio_devices_t device = (audio_devices_t)0);
386
387 // handle special cases for sonification strategy while in call: mute streams or replace by
388 // a special tone in the device used for communication
389 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
390
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700391 audio_mode_t getPhoneState();
392
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800393 // true if device is in a telephony or VoIP call
394 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800395 // true if given state represents a device in a telephony or VoIP call
396 virtual bool isStateInCall(int state);
397
398 // when a device is connected, checks if an open output can be routed
399 // to this device. If none is open, tries to open one of the available outputs.
400 // Returns an output suitable to this device or 0.
401 // when a device is disconnected, checks if an output is not used any more and
402 // returns its handle if any.
403 // transfers the audio tracks and effects from one output thread to another accordingly.
404 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
405 audio_policy_dev_state_t state,
406 SortedVector<audio_io_handle_t>& outputs,
407 const String8 address);
408
409 status_t checkInputsForDevice(audio_devices_t device,
410 audio_policy_dev_state_t state,
411 SortedVector<audio_io_handle_t>& inputs,
412 const String8 address);
413
414 // close an output and its companion duplicating output.
415 void closeOutput(audio_io_handle_t output);
416
417 // close an input.
418 void closeInput(audio_io_handle_t input);
419
420 // checks and if necessary changes outputs used for all strategies.
421 // must be called every time a condition that affects the output choice for a given strategy
422 // changes: connected device, phone state, force use...
423 // Must be called before updateDevicesAndOutputs()
424 void checkOutputForStrategy(routing_strategy strategy);
425
426 // Same as checkOutputForStrategy() but for a all strategies in order of priority
427 void checkOutputForAllStrategies();
428
429 // manages A2DP output suspend/restore according to phone state and BT SCO usage
430 void checkA2dpSuspend();
431
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800432 // selects the most appropriate device on output for current state
433 // must be called every time a condition that affects the device choice for a given output is
434 // changed: connected device, phone state, force use, output start, output stop..
435 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700436 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
437 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800438
439 // updates cache of device used by all strategies (mDeviceForStrategy[])
440 // must be called every time a condition that affects the device choice for a given strategy is
441 // changed: connected device, phone state, force use...
442 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
443 // Must be called after checkOutputForAllStrategies()
444 void updateDevicesAndOutputs();
445
446 // selects the most appropriate device on input for current state
447 audio_devices_t getNewInputDevice(audio_io_handle_t input);
448
François Gaffie45ed3b02015-03-19 10:35:14 +0100449 virtual uint32_t getMaxEffectsCpuLoad()
450 {
451 return mEffects.getMaxEffectsCpuLoad();
452 }
453
454 virtual uint32_t getMaxEffectsMemory()
455 {
456 return mEffects.getMaxEffectsMemory();
457 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800458#ifdef AUDIO_POLICY_TEST
459 virtual bool threadLoop();
460 void exit();
461 int testOutputIndex(audio_io_handle_t output);
462#endif //AUDIO_POLICY_TEST
463
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800464 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700465 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800466 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
467 SortedVector<audio_io_handle_t>& outputs2);
468
469 // mute/unmute strategies using an incompatible device combination
470 // if muting, wait for the audio in pcm buffer to be drained before proceeding
471 // if unmuting, unmute only after the specified delay
472 // Returns the number of ms waited
473 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
474 audio_devices_t prevDevice,
475 uint32_t delayMs);
476
477 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
478 audio_output_flags_t flags,
479 audio_format_t format);
Andy Hungf129b032015-04-07 13:45:50 -0700480 // samplingRate, format, channelMask are in/out and so may be modified
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800481 sp<IOProfile> getInputProfile(audio_devices_t device,
482 String8 address,
483 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -0700484 audio_format_t& format,
485 audio_channel_mask_t& channelMask,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800486 audio_input_flags_t flags);
487 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
488 uint32_t samplingRate,
489 audio_format_t format,
490 audio_channel_mask_t channelMask,
491 audio_output_flags_t flags);
492
493 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
494
François Gaffie53615e22015-03-19 09:24:12 +0100495 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
496 {
497 return mAudioPatches.addAudioPatch(handle, patch);
498 }
499 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
500 {
501 return mAudioPatches.removeAudioPatch(handle);
502 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800503
François Gaffie53615e22015-03-19 09:24:12 +0100504 audio_devices_t availablePrimaryOutputDevices() const
505 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700506 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100507 }
508 audio_devices_t availablePrimaryInputDevices() const
509 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700510 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100511 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800512
513 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
514
Eric Laurent493404d2015-04-21 15:07:36 -0700515 // if argument "device" is different from AUDIO_DEVICE_NONE, startSource() will force
516 // the re-evaluation of the output device.
Eric Laurentc75307b2015-03-17 15:29:32 -0700517 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
518 audio_stream_type_t stream,
519 audio_devices_t device,
520 uint32_t *delayMs);
521 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
522 audio_stream_type_t stream);
523
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800524 uid_t mUidCached;
525 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700526 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800527 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700528
529 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800530 // copy of mOutputs before setDeviceConnectionState() opens new outputs
531 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700532 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100533 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700534
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800535 DeviceVector mAvailableOutputDevices; // all available output devices
536 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800537
Paul McLeanaa981192015-03-21 09:55:15 -0700538 SessionRouteMap mOutputRoutes;
539 SessionRouteMap mInputRoutes;
540
François Gaffiedfd74092015-03-19 12:10:59 +0100541 StreamDescriptorCollection mStreams; // stream descriptors for volume control
542 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800543 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100544 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800545
François Gaffie45ed3b02015-03-19 10:35:14 +0100546 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800547 bool mA2dpSuspended; // true if A2DP output is suspended
548 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
549 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
550 // to boost soft sounds, used to adjust volume curves accordingly
551
François Gaffie53615e22015-03-19 09:24:12 +0100552 HwModuleCollection mHwModules;
553
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800554 volatile int32_t mAudioPortGeneration;
555
François Gaffie53615e22015-03-19 09:24:12 +0100556 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800557
François Gaffiedf372692015-03-19 10:43:27 +0100558 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800559
560 sp<AudioPatch> mCallTxPatch;
561 sp<AudioPatch> mCallRxPatch;
562
563 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
564 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
565 enum {
566 STARTING_OUTPUT,
567 STARTING_BEACON,
568 STOPPING_OUTPUT,
569 STOPPING_BEACON
570 };
571 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
572 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
573 bool mBeaconMuted; // has STREAM_TTS been muted
574
François Gaffie036e1e92015-03-19 10:16:24 +0100575 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800576
577#ifdef AUDIO_POLICY_TEST
578 Mutex mLock;
579 Condition mWaitWorkCV;
580
581 int mCurOutput;
582 bool mDirectOutput;
583 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
584 int mTestInput;
585 uint32_t mTestDevice;
586 uint32_t mTestSamplingRate;
587 uint32_t mTestFormat;
588 uint32_t mTestChannels;
589 uint32_t mTestLatencyMs;
590#endif //AUDIO_POLICY_TEST
591
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800592 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700593
594 // Audio Policy Engine Interface.
595 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800596private:
597 // updates device caching and output for streams that can influence the
598 // routing of notifications
599 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800600 // find the outputs on a given output descriptor that have the given address.
601 // to be called on an AudioOutputDescriptor whose supported devices (as defined
602 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
603 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
604 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700605 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800606 const audio_devices_t device /*in*/,
607 const String8 address /*in*/,
608 SortedVector<audio_io_handle_t>& outputs /*out*/);
609 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
610 // internal method to return the output handle for the given device and format
611 audio_io_handle_t getOutputForDevice(
612 audio_devices_t device,
613 audio_session_t session,
614 audio_stream_type_t stream,
615 uint32_t samplingRate,
616 audio_format_t format,
617 audio_channel_mask_t channelMask,
618 audio_output_flags_t flags,
619 const audio_offload_info_t *offloadInfo);
620 // internal function to derive a stream type value from audio attributes
621 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800622 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
623 // returns 0 if no mute/unmute event happened, the largest latency of the device where
624 // the mute/unmute happened
625 uint32_t handleEventForBeacon(int event);
626 uint32_t setBeaconMute(bool mute);
627 bool isValidAttributes(const audio_attributes_t *paa);
628
629 // select input device corresponding to requested audio source and return associated policy
630 // mix if any. Calls getDeviceForInputSource().
631 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
632 AudioMix **policyMix = NULL);
633
634 // Called by setDeviceConnectionState().
635 status_t setDeviceConnectionStateInt(audio_devices_t device,
636 audio_policy_dev_state_t state,
637 const char *device_address,
638 const char *device_name);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800639};
640
641};