blob: b965411838dd2c48df6ac4a733857f23fd226c48 [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,
Paul McLean466dc8e2015-04-17 13:15:36 -0600134 audio_port_handle_t selectedDeviceId,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800135 input_type_t *inputType);
136
137 // indicates to the audio policy manager that the input starts being used.
138 virtual status_t startInput(audio_io_handle_t input,
139 audio_session_t session);
140
141 // indicates to the audio policy manager that the input stops being used.
142 virtual status_t stopInput(audio_io_handle_t input,
143 audio_session_t session);
144 virtual void releaseInput(audio_io_handle_t input,
145 audio_session_t session);
146 virtual void closeAllInputs();
147 virtual void initStreamVolume(audio_stream_type_t stream,
148 int indexMin,
149 int indexMax);
150 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
151 int index,
152 audio_devices_t device);
153 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
154 int *index,
155 audio_devices_t device);
156
157 // return the strategy corresponding to a given stream type
158 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
159 // return the strategy corresponding to the given audio attributes
160 virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
161
162 // return the enabled output devices for the given stream type
163 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
164
165 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
166 virtual status_t registerEffect(const effect_descriptor_t *desc,
167 audio_io_handle_t io,
168 uint32_t strategy,
169 int session,
170 int id);
François Gaffie45ed3b02015-03-19 10:35:14 +0100171 virtual status_t unregisterEffect(int id)
172 {
173 return mEffects.unregisterEffect(id);
174 }
175 virtual status_t setEffectEnabled(int id, bool enabled)
176 {
177 return mEffects.setEffectEnabled(id, enabled);
178 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800179
Eric Laurentc75307b2015-03-17 15:29:32 -0700180 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800181 // return whether a stream is playing remotely, override to change the definition of
182 // local/remote playback, used for instance by notification manager to not make
183 // media players lose audio focus when not playing locally
184 // For the base implementation, "remotely" means playing during screen mirroring which
185 // uses an output for playback with a non-empty, non "0" address.
Eric Laurentc75307b2015-03-17 15:29:32 -0700186 virtual bool isStreamActiveRemotely(audio_stream_type_t stream,
187 uint32_t inPastMs = 0) const;
188
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800189 virtual bool isSourceActive(audio_source_t source) const;
190
191 virtual status_t dump(int fd);
192
193 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
194
195 virtual status_t listAudioPorts(audio_port_role_t role,
196 audio_port_type_t type,
197 unsigned int *num_ports,
198 struct audio_port *ports,
199 unsigned int *generation);
200 virtual status_t getAudioPort(struct audio_port *port);
201 virtual status_t createAudioPatch(const struct audio_patch *patch,
202 audio_patch_handle_t *handle,
203 uid_t uid);
204 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
205 uid_t uid);
206 virtual status_t listAudioPatches(unsigned int *num_patches,
207 struct audio_patch *patches,
208 unsigned int *generation);
209 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
210 virtual void clearAudioPatches(uid_t uid);
211
212 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
213 audio_io_handle_t *ioHandle,
214 audio_devices_t *device);
215
François Gaffiedf372692015-03-19 10:43:27 +0100216 virtual status_t releaseSoundTriggerSession(audio_session_t session)
217 {
218 return mSoundTriggerSessions.releaseSession(session);
219 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800220
221 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
222 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
223
Eric Laurent554a2772015-04-10 11:29:24 -0700224 virtual status_t startAudioSource(const struct audio_port_config *source,
225 const audio_attributes_t *attributes,
226 audio_io_handle_t *handle);
227 virtual status_t stopAudioSource(audio_io_handle_t handle);
228
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800229 // Audio policy configuration file parsing (audio_policy.conf)
230 // TODO candidates to be moved to ConfigParsingUtils
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800231 void defaultAudioPolicyConfig(void);
232
François Gaffie2110e042015-03-24 08:41:51 +0100233 // return the strategy corresponding to a given stream type
234 routing_strategy getStrategy(audio_stream_type_t stream) const;
235
Paul McLeanaa981192015-03-21 09:55:15 -0700236protected:
Paul McLean466dc8e2015-04-17 13:15:36 -0600237 class SessionRoute : public RefBase {
Paul McLeanaa981192015-03-21 09:55:15 -0700238 public:
Paul McLean466dc8e2015-04-17 13:15:36 -0600239 // For Input (Source) routes, use STREAM_TYPE_NA ("NA" = "not applicable)for the
240 // streamType argument
241 static const audio_stream_type_t STREAM_TYPE_NA = AUDIO_STREAM_DEFAULT;
242
243 // For Output (Sink) routes, use SOURCE_TYPE_NA ("NA" = "not applicable") for the
244 // source argument
245
246 static const audio_source_t SOURCE_TYPE_NA = AUDIO_SOURCE_DEFAULT;
247
Paul McLeanaa981192015-03-21 09:55:15 -0700248 SessionRoute(audio_session_t session,
249 audio_stream_type_t streamType,
Paul McLean466dc8e2015-04-17 13:15:36 -0600250 audio_source_t source,
Paul McLeanaa981192015-03-21 09:55:15 -0700251 sp<DeviceDescriptor> deviceDescriptor)
Paul McLean466dc8e2015-04-17 13:15:36 -0600252 : mSession(session),
253 mDeviceDescriptor(deviceDescriptor),
254 mRefCount(0),
255 mActivityCount(0),
256 mChanged(false),
257 mStreamType(streamType),
258 mSource(source) {}
Paul McLeanaa981192015-03-21 09:55:15 -0700259
260 audio_session_t mSession;
Paul McLeanaa981192015-03-21 09:55:15 -0700261
262 sp<DeviceDescriptor> mDeviceDescriptor;
263
Paul McLean466dc8e2015-04-17 13:15:36 -0600264 void log(const char* prefix);
265
Paul McLeanaa981192015-03-21 09:55:15 -0700266 // "reference" counting
Eric Laurent493404d2015-04-21 15:07:36 -0700267 int mRefCount; // +/- on references
268 int mActivityCount; // +/- on start/stop
269 bool mChanged;
Paul McLean466dc8e2015-04-17 13:15:36 -0600270
271 // for outputs
272 const audio_stream_type_t mStreamType;
273
274 // for inputs
275 const audio_source_t mSource;
Paul McLeanaa981192015-03-21 09:55:15 -0700276 };
277
Paul McLean466dc8e2015-04-17 13:15:36 -0600278 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute>> {
279 public:
280 // These constants identify the SessionRoutMap as holding EITHER input routes,
281 // or output routes. An error will occur if an attempt is made to add a SessionRoute
282 // object with mStreamType == STREAM_TYPE_NA (i.e. an input SessionRoute) to a
283 // SessionRoutMap that is marked for output (i.e. mMapType == SESSION_ROUTE_MAP_OUTPUT)
284 // and similarly for output SessionRoutes and Input SessionRouteMaps.
285 typedef enum {
286 MAPTYPE_INPUT = 0,
287 MAPTYPE_OUTPUT = 1
288 } session_route_map_type_t;
289
290 SessionRouteMap(session_route_map_type_t mapType) :
291 mMapType(mapType) {
292 }
293
Paul McLeanaa981192015-03-21 09:55:15 -0700294 bool hasRoute(audio_session_t session);
Paul McLeanaa981192015-03-21 09:55:15 -0700295 void removeRoute(audio_session_t session);
296
297 int incRouteActivity(audio_session_t session);
298 int decRouteActivity(audio_session_t session);
Eric Laurent493404d2015-04-21 15:07:36 -0700299 bool hasRouteChanged(audio_session_t session); // also clears the changed flag
Paul McLeanaa981192015-03-21 09:55:15 -0700300 void log(const char* caption);
Paul McLean466dc8e2015-04-17 13:15:36 -0600301
302 // Specify an Output(Sink) route by passing SessionRoute::SOURCE_TYPE_NA in the
303 // source argument.
304 // Specify an Input(Source) rout by passing SessionRoute::AUDIO_STREAM_DEFAULT
305 // in the streamType argument.
306 void addRoute(audio_session_t session,
307 audio_stream_type_t streamType,
308 audio_source_t source,
309 sp<DeviceDescriptor> deviceDescriptor);
310
311 private:
312 // Used to mark a SessionRoute as for either inputs (mMapType == kSessionRouteMap_Input)
313 // or outputs (mMapType == kSessionRouteMap_Output)
314 const session_route_map_type_t mMapType;
Paul McLeanaa981192015-03-21 09:55:15 -0700315 };
316
François Gaffie2110e042015-03-24 08:41:51 +0100317 // From AudioPolicyManagerObserver
318 virtual const AudioPatchCollection &getAudioPatches() const
319 {
320 return mAudioPatches;
321 }
322 virtual const SoundTriggerSessionCollection &getSoundTriggerSessionCollection() const
323 {
324 return mSoundTriggerSessions;
325 }
326 virtual const AudioPolicyMixCollection &getAudioPolicyMixCollection() const
327 {
328 return mPolicyMixes;
329 }
Eric Laurentc75307b2015-03-17 15:29:32 -0700330 virtual const SwAudioOutputCollection &getOutputs() const
François Gaffie2110e042015-03-24 08:41:51 +0100331 {
332 return mOutputs;
333 }
334 virtual const AudioInputCollection &getInputs() const
335 {
336 return mInputs;
337 }
338 virtual const DeviceVector &getAvailableOutputDevices() const
339 {
340 return mAvailableOutputDevices;
341 }
342 virtual const DeviceVector &getAvailableInputDevices() const
343 {
344 return mAvailableInputDevices;
345 }
346 virtual StreamDescriptorCollection &getStreamDescriptors()
347 {
348 return mStreams;
349 }
350 virtual const sp<DeviceDescriptor> &getDefaultOutputDevice() const
351 {
352 return mDefaultOutputDevice;
353 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800354protected:
Eric Laurentc75307b2015-03-17 15:29:32 -0700355 void addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc);
François Gaffie53615e22015-03-19 09:24:12 +0100356 void removeOutput(audio_io_handle_t output);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800357 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
358
359 // return appropriate device for streams handled by the specified strategy according to current
360 // phone state, connected devices...
361 // if fromCache is true, the device is returned from mDeviceForStrategy[],
362 // otherwise it is determine by current state
363 // (device connected,phone state, force use, a2dp output...)
364 // This allows to:
365 // 1 speed up process when the state is stable (when starting or stopping an output)
366 // 2 access to either current device selection (fromCache == true) or
367 // "future" device selection (fromCache == false) when called from a context
368 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
369 // before updateDevicesAndOutputs() is called.
370 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
371 bool fromCache);
372
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700373 bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
374 uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
375
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800376 // change the route of the specified output. Returns the number of ms we have slept to
377 // allow new routing to take effect in certain cases.
Eric Laurentc75307b2015-03-17 15:29:32 -0700378 virtual uint32_t setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800379 audio_devices_t device,
380 bool force = false,
381 int delayMs = 0,
382 audio_patch_handle_t *patchHandle = NULL,
383 const char* address = NULL);
Eric Laurentc75307b2015-03-17 15:29:32 -0700384 status_t resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800385 int delayMs = 0,
386 audio_patch_handle_t *patchHandle = NULL);
387 status_t setInputDevice(audio_io_handle_t input,
388 audio_devices_t device,
389 bool force = false,
390 audio_patch_handle_t *patchHandle = NULL);
391 status_t resetInputDevice(audio_io_handle_t input,
392 audio_patch_handle_t *patchHandle = NULL);
393
394 // select input device corresponding to requested audio source
395 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
396
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800397 // compute the actual volume for a given stream according to the requested index and a particular
398 // device
Eric Laurentc75307b2015-03-17 15:29:32 -0700399 virtual float computeVolume(audio_stream_type_t stream,
400 int index,
401 audio_devices_t device);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800402
403 // check that volume change is permitted, compute and send new volume to audio hardware
404 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
Eric Laurentc75307b2015-03-17 15:29:32 -0700405 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800406 audio_devices_t device,
407 int delayMs = 0, bool force = false);
408
409 // apply all stream volumes to the specified output and device
Eric Laurentc75307b2015-03-17 15:29:32 -0700410 void applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc,
411 audio_devices_t device, int delayMs = 0, bool force = false);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800412
413 // Mute or unmute all streams handled by the specified strategy on the specified output
414 void setStrategyMute(routing_strategy strategy,
415 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700416 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800417 int delayMs = 0,
418 audio_devices_t device = (audio_devices_t)0);
419
420 // Mute or unmute the stream on the specified output
421 void setStreamMute(audio_stream_type_t stream,
422 bool on,
Eric Laurentc75307b2015-03-17 15:29:32 -0700423 const sp<AudioOutputDescriptor>& outputDesc,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800424 int delayMs = 0,
425 audio_devices_t device = (audio_devices_t)0);
426
427 // handle special cases for sonification strategy while in call: mute streams or replace by
428 // a special tone in the device used for communication
429 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
430
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700431 audio_mode_t getPhoneState();
432
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800433 // true if device is in a telephony or VoIP call
434 virtual bool isInCall();
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800435 // true if given state represents a device in a telephony or VoIP call
436 virtual bool isStateInCall(int state);
437
438 // when a device is connected, checks if an open output can be routed
439 // to this device. If none is open, tries to open one of the available outputs.
440 // Returns an output suitable to this device or 0.
441 // when a device is disconnected, checks if an output is not used any more and
442 // returns its handle if any.
443 // transfers the audio tracks and effects from one output thread to another accordingly.
444 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
445 audio_policy_dev_state_t state,
446 SortedVector<audio_io_handle_t>& outputs,
447 const String8 address);
448
449 status_t checkInputsForDevice(audio_devices_t device,
450 audio_policy_dev_state_t state,
451 SortedVector<audio_io_handle_t>& inputs,
452 const String8 address);
453
454 // close an output and its companion duplicating output.
455 void closeOutput(audio_io_handle_t output);
456
457 // close an input.
458 void closeInput(audio_io_handle_t input);
459
460 // checks and if necessary changes outputs used for all strategies.
461 // must be called every time a condition that affects the output choice for a given strategy
462 // changes: connected device, phone state, force use...
463 // Must be called before updateDevicesAndOutputs()
464 void checkOutputForStrategy(routing_strategy strategy);
465
466 // Same as checkOutputForStrategy() but for a all strategies in order of priority
467 void checkOutputForAllStrategies();
468
469 // manages A2DP output suspend/restore according to phone state and BT SCO usage
470 void checkA2dpSuspend();
471
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800472 // selects the most appropriate device on output for current state
473 // must be called every time a condition that affects the device choice for a given output is
474 // changed: connected device, phone state, force use, output start, output stop..
475 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurentc75307b2015-03-17 15:29:32 -0700476 audio_devices_t getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc,
477 bool fromCache);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800478
479 // updates cache of device used by all strategies (mDeviceForStrategy[])
480 // must be called every time a condition that affects the device choice for a given strategy is
481 // changed: connected device, phone state, force use...
482 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
483 // Must be called after checkOutputForAllStrategies()
484 void updateDevicesAndOutputs();
485
486 // selects the most appropriate device on input for current state
487 audio_devices_t getNewInputDevice(audio_io_handle_t input);
488
François Gaffie45ed3b02015-03-19 10:35:14 +0100489 virtual uint32_t getMaxEffectsCpuLoad()
490 {
491 return mEffects.getMaxEffectsCpuLoad();
492 }
493
494 virtual uint32_t getMaxEffectsMemory()
495 {
496 return mEffects.getMaxEffectsMemory();
497 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800498#ifdef AUDIO_POLICY_TEST
499 virtual bool threadLoop();
500 void exit();
501 int testOutputIndex(audio_io_handle_t output);
502#endif //AUDIO_POLICY_TEST
503
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800504 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurentc75307b2015-03-17 15:29:32 -0700505 SwAudioOutputCollection openOutputs);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800506 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
507 SortedVector<audio_io_handle_t>& outputs2);
508
509 // mute/unmute strategies using an incompatible device combination
510 // if muting, wait for the audio in pcm buffer to be drained before proceeding
511 // if unmuting, unmute only after the specified delay
512 // Returns the number of ms waited
513 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
514 audio_devices_t prevDevice,
515 uint32_t delayMs);
516
517 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
518 audio_output_flags_t flags,
519 audio_format_t format);
Andy Hungf129b032015-04-07 13:45:50 -0700520 // samplingRate, format, channelMask are in/out and so may be modified
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800521 sp<IOProfile> getInputProfile(audio_devices_t device,
522 String8 address,
523 uint32_t& samplingRate,
Andy Hungf129b032015-04-07 13:45:50 -0700524 audio_format_t& format,
525 audio_channel_mask_t& channelMask,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800526 audio_input_flags_t flags);
527 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
528 uint32_t samplingRate,
529 audio_format_t format,
530 audio_channel_mask_t channelMask,
531 audio_output_flags_t flags);
532
533 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
534
François Gaffie53615e22015-03-19 09:24:12 +0100535 virtual status_t addAudioPatch(audio_patch_handle_t handle, const sp<AudioPatch>& patch)
536 {
537 return mAudioPatches.addAudioPatch(handle, patch);
538 }
539 virtual status_t removeAudioPatch(audio_patch_handle_t handle)
540 {
541 return mAudioPatches.removeAudioPatch(handle);
542 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800543
François Gaffie53615e22015-03-19 09:24:12 +0100544 audio_devices_t availablePrimaryOutputDevices() const
545 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700546 return mPrimaryOutput->supportedDevices() & mAvailableOutputDevices.types();
François Gaffie53615e22015-03-19 09:24:12 +0100547 }
548 audio_devices_t availablePrimaryInputDevices() const
549 {
Eric Laurentc75307b2015-03-17 15:29:32 -0700550 return mAvailableInputDevices.getDevicesFromHwModule(mPrimaryOutput->getModuleHandle());
François Gaffie53615e22015-03-19 09:24:12 +0100551 }
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800552
553 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
554
Eric Laurent493404d2015-04-21 15:07:36 -0700555 // if argument "device" is different from AUDIO_DEVICE_NONE, startSource() will force
556 // the re-evaluation of the output device.
Eric Laurentc75307b2015-03-17 15:29:32 -0700557 status_t startSource(sp<AudioOutputDescriptor> outputDesc,
558 audio_stream_type_t stream,
559 audio_devices_t device,
560 uint32_t *delayMs);
561 status_t stopSource(sp<AudioOutputDescriptor> outputDesc,
562 audio_stream_type_t stream);
563
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800564 uid_t mUidCached;
565 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
Eric Laurentc75307b2015-03-17 15:29:32 -0700566 sp<SwAudioOutputDescriptor> mPrimaryOutput; // primary output descriptor
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800567 // list of descriptors for outputs currently opened
Eric Laurentc75307b2015-03-17 15:29:32 -0700568
569 SwAudioOutputCollection mOutputs;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800570 // copy of mOutputs before setDeviceConnectionState() opens new outputs
571 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurentc75307b2015-03-17 15:29:32 -0700572 SwAudioOutputCollection mPreviousOutputs;
François Gaffie53615e22015-03-19 09:24:12 +0100573 AudioInputCollection mInputs; // list of input descriptors
Eric Laurentc75307b2015-03-17 15:29:32 -0700574
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800575 DeviceVector mAvailableOutputDevices; // all available output devices
576 DeviceVector mAvailableInputDevices; // all available input devices
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800577
Paul McLean466dc8e2015-04-17 13:15:36 -0600578 SessionRouteMap mOutputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_OUTPUT);
579 SessionRouteMap mInputRoutes = SessionRouteMap(SessionRouteMap::MAPTYPE_INPUT);
Paul McLeanaa981192015-03-21 09:55:15 -0700580
François Gaffiedfd74092015-03-19 12:10:59 +0100581 StreamDescriptorCollection mStreams; // stream descriptors for volume control
582 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800583 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
François Gaffiedfd74092015-03-19 12:10:59 +0100584 float mLastVoiceVolume; // last voice volume value sent to audio HAL
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800585
François Gaffie45ed3b02015-03-19 10:35:14 +0100586 EffectDescriptorCollection mEffects; // list of registered audio effects
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800587 bool mA2dpSuspended; // true if A2DP output is suspended
588 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
589 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
590 // to boost soft sounds, used to adjust volume curves accordingly
591
François Gaffie53615e22015-03-19 09:24:12 +0100592 HwModuleCollection mHwModules;
593
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800594 volatile int32_t mAudioPortGeneration;
595
François Gaffie53615e22015-03-19 09:24:12 +0100596 AudioPatchCollection mAudioPatches;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800597
François Gaffiedf372692015-03-19 10:43:27 +0100598 SoundTriggerSessionCollection mSoundTriggerSessions;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800599
600 sp<AudioPatch> mCallTxPatch;
601 sp<AudioPatch> mCallRxPatch;
602
603 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
604 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
605 enum {
606 STARTING_OUTPUT,
607 STARTING_BEACON,
608 STOPPING_OUTPUT,
609 STOPPING_BEACON
610 };
611 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
612 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
613 bool mBeaconMuted; // has STREAM_TTS been muted
614
François Gaffie036e1e92015-03-19 10:16:24 +0100615 AudioPolicyMixCollection mPolicyMixes; // list of registered mixes
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800616
617#ifdef AUDIO_POLICY_TEST
618 Mutex mLock;
619 Condition mWaitWorkCV;
620
621 int mCurOutput;
622 bool mDirectOutput;
623 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
624 int mTestInput;
625 uint32_t mTestDevice;
626 uint32_t mTestSamplingRate;
627 uint32_t mTestFormat;
628 uint32_t mTestChannels;
629 uint32_t mTestLatencyMs;
630#endif //AUDIO_POLICY_TEST
631
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800632 uint32_t nextAudioPortGeneration();
Jean-Michel Trivi887a9ed2015-03-31 18:02:24 -0700633
634 // Audio Policy Engine Interface.
635 AudioPolicyManagerInterface *mEngine;
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800636private:
637 // updates device caching and output for streams that can influence the
638 // routing of notifications
639 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800640 // find the outputs on a given output descriptor that have the given address.
641 // to be called on an AudioOutputDescriptor whose supported devices (as defined
642 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
643 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
644 // where addresses are used to distinguish between one connected device and another.
Eric Laurentc75307b2015-03-17 15:29:32 -0700645 void findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/,
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800646 const audio_devices_t device /*in*/,
647 const String8 address /*in*/,
648 SortedVector<audio_io_handle_t>& outputs /*out*/);
649 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
650 // internal method to return the output handle for the given device and format
651 audio_io_handle_t getOutputForDevice(
652 audio_devices_t device,
653 audio_session_t session,
654 audio_stream_type_t stream,
655 uint32_t samplingRate,
656 audio_format_t format,
657 audio_channel_mask_t channelMask,
658 audio_output_flags_t flags,
659 const audio_offload_info_t *offloadInfo);
660 // internal function to derive a stream type value from audio attributes
661 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800662 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
663 // returns 0 if no mute/unmute event happened, the largest latency of the device where
664 // the mute/unmute happened
665 uint32_t handleEventForBeacon(int event);
666 uint32_t setBeaconMute(bool mute);
667 bool isValidAttributes(const audio_attributes_t *paa);
668
669 // select input device corresponding to requested audio source and return associated policy
670 // mix if any. Calls getDeviceForInputSource().
671 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
672 AudioMix **policyMix = NULL);
673
674 // Called by setDeviceConnectionState().
675 status_t setDeviceConnectionStateInt(audio_devices_t device,
676 audio_policy_dev_state_t state,
677 const char *device_address,
678 const char *device_name);
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800679};
680
681};