blob: 8a631ba46da172697615b362b7d3f8db37db1f70 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
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
17
18#include <stdint.h>
19#include <sys/types.h>
20#include <cutils/config_utils.h>
21#include <cutils/misc.h>
22#include <utils/Timers.h>
23#include <utils/Errors.h>
24#include <utils/KeyedVector.h>
25#include <utils/SortedVector.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070026#include "AudioPolicyInterface.h"
Eric Laurente552edb2014-03-10 17:42:56 -070027
28
Eric Laurent3b73df72014-03-11 09:06:29 -070029namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070030
31// ----------------------------------------------------------------------------
32
Eric Laurente552edb2014-03-10 17:42:56 -070033// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
34#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
35// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
36#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
37// Time in milliseconds during which we consider that music is still active after a music
38// track was stopped - see computeVolume()
39#define SONIFICATION_HEADSET_MUSIC_DELAY 5000
40// Time in milliseconds after media stopped playing during which we consider that the
41// sonification should be as unobtrusive as during the time media was playing.
42#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
43// Time in milliseconds during witch some streams are muted while the audio path
44// is switched
45#define MUTE_TIME_MS 2000
46
47#define NUM_TEST_OUTPUTS 5
48
49#define NUM_VOL_CURVE_KNEES 2
50
51// Default minimum length allowed for offloading a compressed track
52// Can be overridden by the audio.offload.min.duration.secs property
53#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
54
55// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -070056// AudioPolicyManager implements audio policy manager behavior common to all platforms.
Eric Laurente552edb2014-03-10 17:42:56 -070057// ----------------------------------------------------------------------------
58
Eric Laurente0720872014-03-11 09:30:41 -070059class AudioPolicyManager: public AudioPolicyInterface
Eric Laurente552edb2014-03-10 17:42:56 -070060#ifdef AUDIO_POLICY_TEST
61 , public Thread
62#endif //AUDIO_POLICY_TEST
63{
64
65public:
Eric Laurente0720872014-03-11 09:30:41 -070066 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
67 virtual ~AudioPolicyManager();
Eric Laurente552edb2014-03-10 17:42:56 -070068
69 // AudioPolicyInterface
70 virtual status_t setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -070071 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -070072 const char *device_address);
Eric Laurent3b73df72014-03-11 09:06:29 -070073 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -070074 const char *device_address);
Eric Laurent3b73df72014-03-11 09:06:29 -070075 virtual void setPhoneState(audio_mode_t state);
76 virtual void setForceUse(audio_policy_force_use_t usage,
77 audio_policy_forced_cfg_t config);
78 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurente552edb2014-03-10 17:42:56 -070079 virtual void setSystemProperty(const char* property, const char* value);
80 virtual status_t initCheck();
Eric Laurent3b73df72014-03-11 09:06:29 -070081 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -070082 uint32_t samplingRate,
83 audio_format_t format,
84 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -070085 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -070086 const audio_offload_info_t *offloadInfo);
87 virtual status_t startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -070088 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -070089 int session = 0);
90 virtual status_t stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -070091 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -070092 int session = 0);
93 virtual void releaseOutput(audio_io_handle_t output);
Eric Laurent3b73df72014-03-11 09:06:29 -070094 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Eric Laurente552edb2014-03-10 17:42:56 -070095 uint32_t samplingRate,
96 audio_format_t format,
97 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -070098 audio_in_acoustics_t acoustics);
Eric Laurente552edb2014-03-10 17:42:56 -070099
100 // indicates to the audio policy manager that the input starts being used.
101 virtual status_t startInput(audio_io_handle_t input);
102
103 // indicates to the audio policy manager that the input stops being used.
104 virtual status_t stopInput(audio_io_handle_t input);
105 virtual void releaseInput(audio_io_handle_t input);
Eric Laurent3b73df72014-03-11 09:06:29 -0700106 virtual void initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700107 int indexMin,
108 int indexMax);
Eric Laurent3b73df72014-03-11 09:06:29 -0700109 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700110 int index,
111 audio_devices_t device);
Eric Laurent3b73df72014-03-11 09:06:29 -0700112 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700113 int *index,
114 audio_devices_t device);
115
116 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700117 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700118
119 // return the enabled output devices for the given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700120 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700121
122 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
123 virtual status_t registerEffect(const effect_descriptor_t *desc,
124 audio_io_handle_t io,
125 uint32_t strategy,
126 int session,
127 int id);
128 virtual status_t unregisterEffect(int id);
129 virtual status_t setEffectEnabled(int id, bool enabled);
130
Eric Laurent3b73df72014-03-11 09:06:29 -0700131 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700132 // return whether a stream is playing remotely, override to change the definition of
133 // local/remote playback, used for instance by notification manager to not make
134 // media players lose audio focus when not playing locally
Eric Laurent3b73df72014-03-11 09:06:29 -0700135 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700136 virtual bool isSourceActive(audio_source_t source) const;
137
138 virtual status_t dump(int fd);
139
140 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
141
142protected:
143
144 enum routing_strategy {
145 STRATEGY_MEDIA,
146 STRATEGY_PHONE,
147 STRATEGY_SONIFICATION,
148 STRATEGY_SONIFICATION_RESPECTFUL,
149 STRATEGY_DTMF,
150 STRATEGY_ENFORCED_AUDIBLE,
151 NUM_STRATEGIES
152 };
153
154 // 4 points to define the volume attenuation curve, each characterized by the volume
155 // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
156 // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
157
158 enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
159
160 class VolumeCurvePoint
161 {
162 public:
163 int mIndex;
164 float mDBAttenuation;
165 };
166
167 // device categories used for volume curve management.
168 enum device_category {
169 DEVICE_CATEGORY_HEADSET,
170 DEVICE_CATEGORY_SPEAKER,
171 DEVICE_CATEGORY_EARPIECE,
172 DEVICE_CATEGORY_CNT
173 };
174
175 class IOProfile;
176
Eric Laurent3a4311c2014-03-17 12:00:47 -0700177 class DeviceDescriptor: public RefBase
178 {
179 public:
180 DeviceDescriptor(audio_devices_t type, String8 address,
181 audio_channel_mask_t channelMask) :
182 mType(type), mAddress(address),
183 mChannelMask(channelMask), mId(0) {}
184
185 DeviceDescriptor(audio_devices_t type) :
186 mType(type), mAddress(""),
Eric Laurent2f8a36f2014-03-26 19:05:55 -0700187 mChannelMask(0), mId(0) {}
Eric Laurent3a4311c2014-03-17 12:00:47 -0700188
189 status_t dump(int fd, int spaces) const;
190 static void dumpHeader(int fd, int spaces);
191
192 bool equals(const sp<DeviceDescriptor>& other) const;
193
194 audio_devices_t mType;
195 String8 mAddress;
196 audio_channel_mask_t mChannelMask;
197 uint32_t mId;
198 };
199
200 class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
201 {
202 public:
203 DeviceVector() : SortedVector(), mTypes(AUDIO_DEVICE_NONE) {}
204
205 ssize_t add(const sp<DeviceDescriptor>& item);
206 ssize_t remove(const sp<DeviceDescriptor>& item);
207 ssize_t indexOf(const sp<DeviceDescriptor>& item) const;
208
209 audio_devices_t types() const { return mTypes; }
210
211 void loadDevicesFromType(audio_devices_t types);
212
213 private:
214 void refreshTypes();
215 audio_devices_t mTypes;
216 };
217
Eric Laurente552edb2014-03-10 17:42:56 -0700218 class HwModule {
219 public:
220 HwModule(const char *name);
221 ~HwModule();
222
223 void dump(int fd);
224
225 const char *const mName; // base name of the audio HW module (primary, a2dp ...)
226 audio_module_handle_t mHandle;
227 Vector <IOProfile *> mOutputProfiles; // output profiles exposed by this module
228 Vector <IOProfile *> mInputProfiles; // input profiles exposed by this module
229 };
230
231 // the IOProfile class describes the capabilities of an output or input stream.
232 // It is currently assumed that all combination of listed parameters are supported.
233 // It is used by the policy manager to determine if an output or input is suitable for
234 // a given use case, open/close it accordingly and connect/disconnect audio tracks
235 // to/from it.
236 class IOProfile
237 {
238 public:
239 IOProfile(HwModule *module);
240 ~IOProfile();
241
242 bool isCompatibleProfile(audio_devices_t device,
243 uint32_t samplingRate,
244 audio_format_t format,
245 audio_channel_mask_t channelMask,
246 audio_output_flags_t flags) const;
247
248 void dump(int fd);
249
250 // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
251 // indicates the supported parameters should be read from the output stream
252 // after it is opened for the first time
253 Vector <uint32_t> mSamplingRates; // supported sampling rates
254 Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
255 Vector <audio_format_t> mFormats; // supported audio formats
Eric Laurent3a4311c2014-03-17 12:00:47 -0700256 DeviceVector mSupportedDevices; // supported devices
257 // (devices this output can be routed to)
Eric Laurente552edb2014-03-10 17:42:56 -0700258 audio_output_flags_t mFlags; // attribute flags (e.g primary output,
259 // direct output...). For outputs only.
260 HwModule *mModule; // audio HW module exposing this I/O stream
261 };
262
263 // default volume curve
Eric Laurente0720872014-03-11 09:30:41 -0700264 static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700265 // default volume curve for media strategy
Eric Laurente0720872014-03-11 09:30:41 -0700266 static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700267 // volume curve for media strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700268 static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700269 // volume curve for sonification strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700270 static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
271 static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
272 static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
273 static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
274 static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
275 static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
276 static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700277 // default volume curves per stream and device category. See initializeVolumeCurves()
278 static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
279
280 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
281 // and keep track of the usage of this output by each audio stream type.
282 class AudioOutputDescriptor
283 {
284 public:
285 AudioOutputDescriptor(const IOProfile *profile);
286
287 status_t dump(int fd);
288
289 audio_devices_t device() const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700290 void changeRefCount(audio_stream_type_t stream, int delta);
Eric Laurente552edb2014-03-10 17:42:56 -0700291
292 bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
293 audio_devices_t supportedDevices();
294 uint32_t latency();
295 bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc);
296 bool isActive(uint32_t inPastMs = 0) const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700297 bool isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700298 uint32_t inPastMs = 0,
299 nsecs_t sysTime = 0) const;
300 bool isStrategyActive(routing_strategy strategy,
301 uint32_t inPastMs = 0,
302 nsecs_t sysTime = 0) const;
303
304 audio_io_handle_t mId; // output handle
305 uint32_t mSamplingRate; //
306 audio_format_t mFormat; //
307 audio_channel_mask_t mChannelMask; // output configuration
308 uint32_t mLatency; //
309 audio_output_flags_t mFlags; //
310 audio_devices_t mDevice; // current device this output is routed to
Eric Laurent3b73df72014-03-11 09:06:29 -0700311 uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
312 nsecs_t mStopTime[AUDIO_STREAM_CNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700313 AudioOutputDescriptor *mOutput1; // used by duplicated outputs: first output
314 AudioOutputDescriptor *mOutput2; // used by duplicated outputs: second output
Eric Laurent3b73df72014-03-11 09:06:29 -0700315 float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume
316 int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter
Eric Laurente552edb2014-03-10 17:42:56 -0700317 const IOProfile *mProfile; // I/O profile this output derives from
318 bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
319 // device selection. See checkDeviceMuteStrategies()
320 uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
321 };
322
323 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
324 // and keep track of the usage of this input.
325 class AudioInputDescriptor
326 {
327 public:
328 AudioInputDescriptor(const IOProfile *profile);
329
330 status_t dump(int fd);
331
332 uint32_t mSamplingRate; //
333 audio_format_t mFormat; // input configuration
334 audio_channel_mask_t mChannelMask; //
335 audio_devices_t mDevice; // current device this input is routed to
336 uint32_t mRefCount; // number of AudioRecord clients using this output
Eric Laurent3b73df72014-03-11 09:06:29 -0700337 audio_source_t mInputSource; // input source selected by application (mediarecorder.h)
Eric Laurente552edb2014-03-10 17:42:56 -0700338 const IOProfile *mProfile; // I/O profile this output derives from
339 };
340
341 // stream descriptor used for volume control
342 class StreamDescriptor
343 {
344 public:
345 StreamDescriptor();
346
347 int getVolumeIndex(audio_devices_t device);
348 void dump(int fd);
349
350 int mIndexMin; // min volume index
351 int mIndexMax; // max volume index
352 KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device
353 bool mCanBeMuted; // true is the stream can be muted
354
355 const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
356 };
357
358 // stream descriptor used for volume control
359 class EffectDescriptor
360 {
361 public:
362
363 status_t dump(int fd);
364
365 int mIo; // io the effect is attached to
366 routing_strategy mStrategy; // routing strategy the effect is associated to
367 int mSession; // audio session the effect is on
368 effect_descriptor_t mDesc; // effect descriptor
369 bool mEnabled; // enabled state: CPU load being used or not
370 };
371
372 void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc);
373
374 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700375 static routing_strategy getStrategy(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700376
377 // return appropriate device for streams handled by the specified strategy according to current
378 // phone state, connected devices...
379 // if fromCache is true, the device is returned from mDeviceForStrategy[],
380 // otherwise it is determine by current state
381 // (device connected,phone state, force use, a2dp output...)
382 // This allows to:
383 // 1 speed up process when the state is stable (when starting or stopping an output)
384 // 2 access to either current device selection (fromCache == true) or
385 // "future" device selection (fromCache == false) when called from a context
386 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
387 // before updateDevicesAndOutputs() is called.
388 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
389 bool fromCache);
390
391 // change the route of the specified output. Returns the number of ms we have slept to
392 // allow new routing to take effect in certain cases.
393 uint32_t setOutputDevice(audio_io_handle_t output,
394 audio_devices_t device,
395 bool force = false,
396 int delayMs = 0);
397
398 // select input device corresponding to requested audio source
Eric Laurent3b73df72014-03-11 09:06:29 -0700399 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -0700400
401 // return io handle of active input or 0 if no input is active
402 // Only considers inputs from physical devices (e.g. main mic, headset mic) when
403 // ignoreVirtualInputs is true.
404 audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
405
406 // initialize volume curves for each strategy and device category
407 void initializeVolumeCurves();
408
409 // compute the actual volume for a given stream according to the requested index and a particular
410 // device
Eric Laurent3b73df72014-03-11 09:06:29 -0700411 virtual float computeVolume(audio_stream_type_t stream, int index,
412 audio_io_handle_t output, audio_devices_t device);
Eric Laurente552edb2014-03-10 17:42:56 -0700413
414 // check that volume change is permitted, compute and send new volume to audio hardware
Eric Laurent3b73df72014-03-11 09:06:29 -0700415 status_t checkAndSetVolume(audio_stream_type_t stream, int index, audio_io_handle_t output,
416 audio_devices_t device, int delayMs = 0, bool force = false);
Eric Laurente552edb2014-03-10 17:42:56 -0700417
418 // apply all stream volumes to the specified output and device
419 void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
420
421 // Mute or unmute all streams handled by the specified strategy on the specified output
422 void setStrategyMute(routing_strategy strategy,
423 bool on,
424 audio_io_handle_t output,
425 int delayMs = 0,
426 audio_devices_t device = (audio_devices_t)0);
427
428 // Mute or unmute the stream on the specified output
Eric Laurent3b73df72014-03-11 09:06:29 -0700429 void setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700430 bool on,
431 audio_io_handle_t output,
432 int delayMs = 0,
433 audio_devices_t device = (audio_devices_t)0);
434
435 // handle special cases for sonification strategy while in call: mute streams or replace by
436 // a special tone in the device used for communication
Eric Laurent3b73df72014-03-11 09:06:29 -0700437 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
Eric Laurente552edb2014-03-10 17:42:56 -0700438
439 // true if device is in a telephony or VoIP call
440 virtual bool isInCall();
441
442 // true if given state represents a device in a telephony or VoIP call
443 virtual bool isStateInCall(int state);
444
445 // when a device is connected, checks if an open output can be routed
446 // to this device. If none is open, tries to open one of the available outputs.
447 // Returns an output suitable to this device or 0.
448 // when a device is disconnected, checks if an output is not used any more and
449 // returns its handle if any.
450 // transfers the audio tracks and effects from one output thread to another accordingly.
451 status_t checkOutputsForDevice(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700452 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700453 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -0700454 const String8 address);
Eric Laurente552edb2014-03-10 17:42:56 -0700455
456 // close an output and its companion duplicating output.
457 void closeOutput(audio_io_handle_t output);
458
459 // checks and if necessary changes outputs used for all strategies.
460 // must be called every time a condition that affects the output choice for a given strategy
461 // changes: connected device, phone state, force use...
462 // Must be called before updateDevicesAndOutputs()
463 void checkOutputForStrategy(routing_strategy strategy);
464
465 // Same as checkOutputForStrategy() but for a all strategies in order of priority
466 void checkOutputForAllStrategies();
467
468 // manages A2DP output suspend/restore according to phone state and BT SCO usage
469 void checkA2dpSuspend();
470
471 // returns the A2DP output handle if it is open or 0 otherwise
472 audio_io_handle_t getA2dpOutput();
473
474 // selects the most appropriate device on output for current state
475 // must be called every time a condition that affects the device choice for a given output is
476 // changed: connected device, phone state, force use, output start, output stop..
477 // see getDeviceForStrategy() for the use of fromCache parameter
478
479 audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache);
480 // updates cache of device used by all strategies (mDeviceForStrategy[])
481 // must be called every time a condition that affects the device choice for a given strategy is
482 // changed: connected device, phone state, force use...
483 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
484 // Must be called after checkOutputForAllStrategies()
485
486 void updateDevicesAndOutputs();
487
488 virtual uint32_t getMaxEffectsCpuLoad();
489 virtual uint32_t getMaxEffectsMemory();
490#ifdef AUDIO_POLICY_TEST
491 virtual bool threadLoop();
492 void exit();
493 int testOutputIndex(audio_io_handle_t output);
494#endif //AUDIO_POLICY_TEST
495
496 status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled);
497
498 // returns the category the device belongs to with regard to volume curve management
499 static device_category getDeviceCategory(audio_devices_t device);
500
501 // extract one device relevant for volume control from multiple device selection
502 static audio_devices_t getDeviceForVolume(audio_devices_t device);
503
504 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
505 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs);
506 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 uint32_t checkDeviceMuteStrategies(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,
Eric Laurent3b73df72014-03-11 09:06:29 -0700518 audio_output_flags_t flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700519 IOProfile *getInputProfile(audio_devices_t device,
520 uint32_t samplingRate,
521 audio_format_t format,
522 audio_channel_mask_t channelMask);
523 IOProfile *getProfileForDirectOutput(audio_devices_t device,
524 uint32_t samplingRate,
525 audio_format_t format,
526 audio_channel_mask_t channelMask,
527 audio_output_flags_t flags);
528
529 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
530
531 bool isNonOffloadableEffectEnabled();
532
533 //
534 // Audio policy configuration file parsing (audio_policy.conf)
535 //
536 static uint32_t stringToEnum(const struct StringToEnum *table,
537 size_t size,
538 const char *name);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700539 static const char *enumToString(const struct StringToEnum *table,
540 size_t size,
541 uint32_t value);
Eric Laurente552edb2014-03-10 17:42:56 -0700542 static bool stringToBool(const char *value);
543 static audio_output_flags_t parseFlagNames(char *name);
544 static audio_devices_t parseDeviceNames(char *name);
545 void loadSamplingRates(char *name, IOProfile *profile);
546 void loadFormats(char *name, IOProfile *profile);
547 void loadOutChannels(char *name, IOProfile *profile);
548 void loadInChannels(char *name, IOProfile *profile);
549 status_t loadOutput(cnode *root, HwModule *module);
550 status_t loadInput(cnode *root, HwModule *module);
551 void loadHwModule(cnode *root);
552 void loadHwModules(cnode *root);
553 void loadGlobalConfig(cnode *root);
554 status_t loadAudioPolicyConfig(const char *path);
555 void defaultAudioPolicyConfig(void);
556
557
558 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
559 audio_io_handle_t mPrimaryOutput; // primary output handle
560 // list of descriptors for outputs currently opened
561 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;
562 // copy of mOutputs before setDeviceConnectionState() opens new outputs
563 // reset to mOutputs when updateDevicesAndOutputs() is called.
564 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs;
565 DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors
Eric Laurent3a4311c2014-03-17 12:00:47 -0700566 DeviceVector mAvailableOutputDevices; // bit field of all available output devices
567 DeviceVector mAvailableInputDevices; // bit field of all available input devices
Eric Laurente552edb2014-03-10 17:42:56 -0700568 // without AUDIO_DEVICE_BIT_IN to allow direct bit
569 // field comparisons
570 int mPhoneState; // current phone state
Eric Laurent3b73df72014-03-11 09:06:29 -0700571 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration
Eric Laurente552edb2014-03-10 17:42:56 -0700572
Eric Laurent3b73df72014-03-11 09:06:29 -0700573 StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control
Eric Laurente552edb2014-03-10 17:42:56 -0700574 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
575 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
576 float mLastVoiceVolume; // last voice volume value sent to audio HAL
577
578 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
579 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
580 // Maximum memory allocated to audio effects in KB
581 static const uint32_t MAX_EFFECTS_MEMORY = 512;
582 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
583 uint32_t mTotalEffectsMemory; // current memory used by effects
584 KeyedVector<int, EffectDescriptor *> mEffects; // list of registered audio effects
585 bool mA2dpSuspended; // true if A2DP output is suspended
Eric Laurent3a4311c2014-03-17 12:00:47 -0700586 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
Eric Laurente552edb2014-03-10 17:42:56 -0700587 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
588 // to boost soft sounds, used to adjust volume curves accordingly
589
590 Vector <HwModule *> mHwModules;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700591 volatile int32_t mNextUniqueId;
Eric Laurente552edb2014-03-10 17:42:56 -0700592
593#ifdef AUDIO_POLICY_TEST
594 Mutex mLock;
595 Condition mWaitWorkCV;
596
597 int mCurOutput;
598 bool mDirectOutput;
599 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
600 int mTestInput;
601 uint32_t mTestDevice;
602 uint32_t mTestSamplingRate;
603 uint32_t mTestFormat;
604 uint32_t mTestChannels;
605 uint32_t mTestLatencyMs;
606#endif //AUDIO_POLICY_TEST
607
608private:
609 static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
610 int indexInUi);
611 // updates device caching and output for streams that can influence the
612 // routing of notifications
Eric Laurent3b73df72014-03-11 09:06:29 -0700613 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700614 static bool isVirtualInputDevice(audio_devices_t device);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700615 uint32_t nextUniqueId();
616 // converts device address to string sent to audio HAL via setParameters
617 static String8 addressToParameter(audio_devices_t device, const String8 address);
Eric Laurente552edb2014-03-10 17:42:56 -0700618};
619
620};