blob: 0dd015a10101709ee3f8a2a38cfaf489cffcce5a [file] [log] [blame]
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07001/*
Mingming Yin0670f162014-06-12 16:05:49 -07002 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -07003 * Not a contribution.
4 *
5 * Copyright (C) 2009 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20
21#include <stdint.h>
22#include <sys/types.h>
Mingming Yin0670f162014-06-12 16:05:49 -070023#include <cutils/config_utils.h>
24#include <cutils/misc.h>
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070025#include <utils/Timers.h>
26#include <utils/Errors.h>
27#include <utils/KeyedVector.h>
Mingming Yin0670f162014-06-12 16:05:49 -070028#include <utils/SortedVector.h>
29#include "AudioPolicyInterface.h"
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070030
31
Mingming Yin0670f162014-06-12 16:05:49 -070032namespace android {
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070033
34// ----------------------------------------------------------------------------
35
Mingming Yin0670f162014-06-12 16:05:49 -070036// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
37#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
38// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
39#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
40// Time in milliseconds during which we consider that music is still active after a music
41// track was stopped - see computeVolume()
42#define SONIFICATION_HEADSET_MUSIC_DELAY 5000
43// Time in milliseconds after media stopped playing during which we consider that the
44// sonification should be as unobtrusive as during the time media was playing.
45#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
46// Time in milliseconds during witch some streams are muted while the audio path
47// is switched
48#define MUTE_TIME_MS 2000
49
50#define NUM_TEST_OUTPUTS 5
51
52#define NUM_VOL_CURVE_KNEES 2
53
54// Default minimum length allowed for offloading a compressed track
55// Can be overridden by the audio.offload.min.duration.secs property
56#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
57
58// ----------------------------------------------------------------------------
59// AudioPolicyManager implements audio policy manager behavior common to all platforms.
60// ----------------------------------------------------------------------------
61
62class AudioPolicyManager: public AudioPolicyInterface
63#ifdef AUDIO_POLICY_TEST
64 , public Thread
65#endif //AUDIO_POLICY_TEST
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070066{
67
68public:
Mingming Yin0670f162014-06-12 16:05:49 -070069 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
70 virtual ~AudioPolicyManager();
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -070071
Mingming Yin0670f162014-06-12 16:05:49 -070072 // AudioPolicyInterface
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070073 virtual status_t setDeviceConnectionState(audio_devices_t device,
Mingming Yin0670f162014-06-12 16:05:49 -070074 audio_policy_dev_state_t state,
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070075 const char *device_address);
Mingming Yin0670f162014-06-12 16:05:49 -070076 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
77 const char *device_address);
78 virtual void setPhoneState(audio_mode_t state);
79 virtual void setForceUse(audio_policy_force_use_t usage,
80 audio_policy_forced_cfg_t config);
81 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
82 virtual void setSystemProperty(const char* property, const char* value);
83 virtual status_t initCheck();
84 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -070085 uint32_t samplingRate,
Mingming Yin0670f162014-06-12 16:05:49 -070086 audio_format_t format,
87 audio_channel_mask_t channelMask,
88 audio_output_flags_t flags,
89 const audio_offload_info_t *offloadInfo);
90 virtual status_t startOutput(audio_io_handle_t output,
91 audio_stream_type_t stream,
92 int session = 0);
93 virtual status_t stopOutput(audio_io_handle_t output,
94 audio_stream_type_t stream,
95 int session = 0);
96 virtual void releaseOutput(audio_io_handle_t output);
97 virtual audio_io_handle_t getInput(audio_source_t inputSource,
98 uint32_t samplingRate,
99 audio_format_t format,
100 audio_channel_mask_t channelMask,
101 audio_in_acoustics_t acoustics);
102
103 // indicates to the audio policy manager that the input starts being used.
104 virtual status_t startInput(audio_io_handle_t input);
105
106 // indicates to the audio policy manager that the input stops being used.
107 virtual status_t stopInput(audio_io_handle_t input);
108 virtual void releaseInput(audio_io_handle_t input);
109 virtual void closeAllInputs();
110 virtual void initStreamVolume(audio_stream_type_t stream,
111 int indexMin,
112 int indexMax);
113 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
114 int index,
115 audio_devices_t device);
116 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
117 int *index,
118 audio_devices_t device);
119
120 // return the strategy corresponding to a given stream type
121 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
122
123 // return the enabled output devices for the given stream type
124 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
125
126 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
127 virtual status_t registerEffect(const effect_descriptor_t *desc,
128 audio_io_handle_t io,
129 uint32_t strategy,
130 int session,
131 int id);
132 virtual status_t unregisterEffect(int id);
133 virtual status_t setEffectEnabled(int id, bool enabled);
134
135 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
136 // return whether a stream is playing remotely, override to change the definition of
137 // local/remote playback, used for instance by notification manager to not make
138 // media players lose audio focus when not playing locally
139 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
140 virtual bool isSourceActive(audio_source_t source) const;
141
142 virtual status_t dump(int fd);
Naresh Tanniru36c08932014-01-27 18:40:53 +0530143
144 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
145
Mingming Yin0670f162014-06-12 16:05:49 -0700146 virtual status_t listAudioPorts(audio_port_role_t role,
147 audio_port_type_t type,
148 unsigned int *num_ports,
149 struct audio_port *ports,
150 unsigned int *generation);
151 virtual status_t getAudioPort(struct audio_port *port);
152 virtual status_t createAudioPatch(const struct audio_patch *patch,
153 audio_patch_handle_t *handle,
154 uid_t uid);
155 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
156 uid_t uid);
157 virtual status_t listAudioPatches(unsigned int *num_patches,
158 struct audio_patch *patches,
159 unsigned int *generation);
160 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
161 virtual void clearAudioPatches(uid_t uid);
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +0530162
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700163protected:
Mingming Yin0670f162014-06-12 16:05:49 -0700164
165 enum routing_strategy {
166 STRATEGY_MEDIA,
167 STRATEGY_PHONE,
168 STRATEGY_SONIFICATION,
169 STRATEGY_SONIFICATION_RESPECTFUL,
170 STRATEGY_DTMF,
171 STRATEGY_ENFORCED_AUDIBLE,
172 NUM_STRATEGIES
173 };
174
175 // 4 points to define the volume attenuation curve, each characterized by the volume
176 // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
177 // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
178
179 enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
180
181 class VolumeCurvePoint
182 {
183 public:
184 int mIndex;
185 float mDBAttenuation;
186 };
187
188 // device categories used for volume curve management.
189 enum device_category {
190 DEVICE_CATEGORY_HEADSET,
191 DEVICE_CATEGORY_SPEAKER,
192 DEVICE_CATEGORY_EARPIECE,
193 DEVICE_CATEGORY_CNT
194 };
195
196 class HwModule;
197
198 class AudioGain: public RefBase
199 {
200 public:
201 AudioGain();
202 virtual ~AudioGain() {}
203
204 void dump(int fd, int spaces, int index) const;
205
206 struct audio_gain mGain;
207 };
208
209 class AudioPort: public RefBase
210 {
211 public:
212 AudioPort(const String8& name, audio_port_type_t type,
213 audio_port_role_t role, HwModule *module) :
214 mName(name), mType(type), mRole(role), mModule(module) {}
215 virtual ~AudioPort() {}
216
217 virtual void toAudioPort(struct audio_port *port) const;
218
219 void loadSamplingRates(char *name);
220 void loadFormats(char *name);
221 void loadOutChannels(char *name);
222 void loadInChannels(char *name);
223
224 audio_gain_mode_t loadGainMode(char *name);
225 void loadGain(cnode *root);
226 void loadGains(cnode *root);
227
228 void dump(int fd, int spaces) const;
229
230 String8 mName;
231 audio_port_type_t mType;
232 audio_port_role_t mRole;
233 // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
234 // indicates the supported parameters should be read from the output stream
235 // after it is opened for the first time
236 Vector <uint32_t> mSamplingRates; // supported sampling rates
237 Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
238 Vector <audio_format_t> mFormats; // supported audio formats
239 Vector < sp<AudioGain> > mGains; // gain controllers
240 HwModule *mModule; // audio HW module exposing this I/O stream
241 };
242
243 class AudioPatch: public RefBase
244 {
245 public:
246 AudioPatch(audio_patch_handle_t handle,
247 const struct audio_patch *patch, uid_t uid) :
248 mHandle(handle), mPatch(*patch), mUid(uid), mAfPatchHandle(0) {}
249
250 audio_patch_handle_t mHandle;
251 struct audio_patch mPatch;
252 uid_t mUid;
253 audio_patch_handle_t mAfPatchHandle;
254 };
255
256 class DeviceDescriptor: public AudioPort
257 {
258 public:
259 DeviceDescriptor(const String8& name, audio_devices_t type, String8 address,
260 audio_channel_mask_t channelMask) :
261 AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
262 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
263 AUDIO_PORT_ROLE_SOURCE,
264 NULL),
265 mDeviceType(type), mAddress(address),
266 mChannelMask(channelMask), mId(0) {}
267
268 DeviceDescriptor(String8 name, audio_devices_t type) :
269 AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
270 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
271 AUDIO_PORT_ROLE_SOURCE,
272 NULL),
273 mDeviceType(type), mAddress(""),
274 mChannelMask(0), mId(0) {}
275 virtual ~DeviceDescriptor() {}
276
277 bool equals(const sp<DeviceDescriptor>& other) const;
278 void toAudioPortConfig(struct audio_port_config *dstConfig,
279 const struct audio_port_config *srcConfig = NULL) const;
280
281 virtual void toAudioPort(struct audio_port *port) const;
282
283 status_t dump(int fd, int spaces, int index) const;
284
285 audio_devices_t mDeviceType;
286 String8 mAddress;
287 audio_channel_mask_t mChannelMask;
288 audio_port_handle_t mId;
289 };
290
291 class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
292 {
293 public:
294 DeviceVector() : SortedVector(), mDeviceTypes(AUDIO_DEVICE_NONE) {}
295
296 ssize_t add(const sp<DeviceDescriptor>& item);
297 ssize_t remove(const sp<DeviceDescriptor>& item);
298 ssize_t indexOf(const sp<DeviceDescriptor>& item) const;
299
300 audio_devices_t types() const { return mDeviceTypes; }
301
302 void loadDevicesFromType(audio_devices_t types);
303 void loadDevicesFromName(char *name, const DeviceVector& declaredDevices);
304
305 sp<DeviceDescriptor> getDevice(audio_devices_t type, String8 address) const;
306 DeviceVector getDevicesFromType(audio_devices_t types) const;
307 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
308 sp<DeviceDescriptor> getDeviceFromName(const String8& name) const;
309
310 private:
311 void refreshTypes();
312 audio_devices_t mDeviceTypes;
313 };
314
315 // the IOProfile class describes the capabilities of an output or input stream.
316 // It is currently assumed that all combination of listed parameters are supported.
317 // It is used by the policy manager to determine if an output or input is suitable for
318 // a given use case, open/close it accordingly and connect/disconnect audio tracks
319 // to/from it.
320 class IOProfile : public AudioPort
321 {
322 public:
323 IOProfile(const String8& name, audio_port_role_t role, HwModule *module);
324 virtual ~IOProfile();
325
326 bool isCompatibleProfile(audio_devices_t device,
327 uint32_t samplingRate,
328 audio_format_t format,
329 audio_channel_mask_t channelMask,
330 audio_output_flags_t flags) const;
331
332 void dump(int fd);
333 void log();
334
335 DeviceVector mSupportedDevices; // supported devices
336 // (devices this output can be routed to)
337 audio_output_flags_t mFlags; // attribute flags (e.g primary output,
338 // direct output...). For outputs only.
339 };
340
341 class HwModule {
342 public:
343 HwModule(const char *name);
344 ~HwModule();
345
346 status_t loadOutput(cnode *root);
347 status_t loadInput(cnode *root);
348 status_t loadDevice(cnode *root);
349
350 void dump(int fd);
351
352 const char *const mName; // base name of the audio HW module (primary, a2dp ...)
353 audio_module_handle_t mHandle;
354 Vector < sp<IOProfile> > mOutputProfiles; // output profiles exposed by this module
355 Vector < sp<IOProfile> > mInputProfiles; // input profiles exposed by this module
356 DeviceVector mDeclaredDevices; // devices declared in audio_policy.conf
357
358 };
359
360 // default volume curve
361 static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
362 // default volume curve for media strategy
363 static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
364 // volume curve for media strategy on speakers
365 static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
366 // volume curve for sonification strategy on speakers
367 static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
368 static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
369 static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
370 static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
371 static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
372 static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
373 static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
374 // default volume curves per stream and device category. See initializeVolumeCurves()
375 static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
376
377 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
378 // and keep track of the usage of this output by each audio stream type.
379 class AudioOutputDescriptor
380 {
381 public:
382 AudioOutputDescriptor(const sp<IOProfile>& profile);
383
384 status_t dump(int fd);
385
386 audio_devices_t device() const;
387 void changeRefCount(audio_stream_type_t stream, int delta);
388
389 bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
390 audio_devices_t supportedDevices();
391 uint32_t latency();
392 bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc);
393 bool isActive(uint32_t inPastMs = 0) const;
394 bool isStreamActive(audio_stream_type_t stream,
395 uint32_t inPastMs = 0,
396 nsecs_t sysTime = 0) const;
397 bool isStrategyActive(routing_strategy strategy,
398 uint32_t inPastMs = 0,
399 nsecs_t sysTime = 0) const;
400
401 void toAudioPortConfig(struct audio_port_config *dstConfig,
402 const struct audio_port_config *srcConfig = NULL) const;
403 void toAudioPort(struct audio_port *port) const;
404
405 audio_port_handle_t mId;
406 audio_io_handle_t mIoHandle; // output handle
407 uint32_t mSamplingRate; //
408 audio_format_t mFormat; //
409 audio_channel_mask_t mChannelMask; // output configuration
410 uint32_t mLatency; //
411 audio_output_flags_t mFlags; //
412 audio_devices_t mDevice; // current device this output is routed to
413 audio_patch_handle_t mPatchHandle;
414 uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
415 nsecs_t mStopTime[AUDIO_STREAM_CNT];
416 AudioOutputDescriptor *mOutput1; // used by duplicated outputs: first output
417 AudioOutputDescriptor *mOutput2; // used by duplicated outputs: second output
418 float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume
419 int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter
420 const sp<IOProfile> mProfile; // I/O profile this output derives from
421 bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
422 // device selection. See checkDeviceMuteStrategies()
423 uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
424 };
425
426 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
427 // and keep track of the usage of this input.
428 class AudioInputDescriptor
429 {
430 public:
431 AudioInputDescriptor(const sp<IOProfile>& profile);
432
433 status_t dump(int fd);
434
435 audio_port_handle_t mId;
436 audio_io_handle_t mIoHandle; // input handle
437 uint32_t mSamplingRate; //
438 audio_format_t mFormat; // input configuration
439 audio_channel_mask_t mChannelMask; //
440 audio_devices_t mDevice; // current device this input is routed to
441 audio_patch_handle_t mPatchHandle;
442 uint32_t mRefCount; // number of AudioRecord clients using this output
443 audio_source_t mInputSource; // input source selected by application (mediarecorder.h)
444 const sp<IOProfile> mProfile; // I/O profile this output derives from
445
446 void toAudioPortConfig(struct audio_port_config *dstConfig,
447 const struct audio_port_config *srcConfig = NULL) const;
448 void toAudioPort(struct audio_port *port) const;
449 };
450
451 // stream descriptor used for volume control
452 class StreamDescriptor
453 {
454 public:
455 StreamDescriptor();
456
457 int getVolumeIndex(audio_devices_t device);
458 void dump(int fd);
459
460 int mIndexMin; // min volume index
461 int mIndexMax; // max volume index
462 KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device
463 bool mCanBeMuted; // true is the stream can be muted
464
465 const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
466 };
467
468 // stream descriptor used for volume control
469 class EffectDescriptor
470 {
471 public:
472
473 status_t dump(int fd);
474
475 int mIo; // io the effect is attached to
476 routing_strategy mStrategy; // routing strategy the effect is associated to
477 int mSession; // audio session the effect is on
478 effect_descriptor_t mDesc; // effect descriptor
479 bool mEnabled; // enabled state: CPU load being used or not
480 };
481
482 void addOutput(audio_io_handle_t output, AudioOutputDescriptor *outputDesc);
483 void addInput(audio_io_handle_t input, AudioInputDescriptor *inputDesc);
484
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700485 // return the strategy corresponding to a given stream type
Mingming Yin0670f162014-06-12 16:05:49 -0700486 static routing_strategy getStrategy(audio_stream_type_t stream);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700487
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700488 // return appropriate device for streams handled by the specified strategy according to current
489 // phone state, connected devices...
490 // if fromCache is true, the device is returned from mDeviceForStrategy[],
491 // otherwise it is determine by current state
492 // (device connected,phone state, force use, a2dp output...)
493 // This allows to:
494 // 1 speed up process when the state is stable (when starting or stopping an output)
495 // 2 access to either current device selection (fromCache == true) or
496 // "future" device selection (fromCache == false) when called from a context
497 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
498 // before updateDevicesAndOutputs() is called.
499 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
Mingming Yin0670f162014-06-12 16:05:49 -0700500 bool fromCache);
501
502 // change the route of the specified output. Returns the number of ms we have slept to
503 // allow new routing to take effect in certain cases.
504 uint32_t setOutputDevice(audio_io_handle_t output,
505 audio_devices_t device,
506 bool force = false,
507 int delayMs = 0,
508 audio_patch_handle_t *patchHandle = NULL);
509 status_t resetOutputDevice(audio_io_handle_t output,
510 int delayMs = 0,
511 audio_patch_handle_t *patchHandle = NULL);
512 status_t setInputDevice(audio_io_handle_t input,
513 audio_devices_t device,
514 bool force = false,
515 audio_patch_handle_t *patchHandle = NULL);
516 status_t resetInputDevice(audio_io_handle_t input,
517 audio_patch_handle_t *patchHandle = NULL);
518
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700519 // select input device corresponding to requested audio source
Mingming Yin0670f162014-06-12 16:05:49 -0700520 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
521
522 // return io handle of active input or 0 if no input is active
523 // Only considers inputs from physical devices (e.g. main mic, headset mic) when
524 // ignoreVirtualInputs is true.
525 audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
526
527 // initialize volume curves for each strategy and device category
528 void initializeVolumeCurves();
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700529
530 // compute the actual volume for a given stream according to the requested index and a particular
531 // device
Mingming Yin0670f162014-06-12 16:05:49 -0700532 virtual float computeVolume(audio_stream_type_t stream, int index,
533 audio_io_handle_t output, audio_devices_t device);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700534
535 // check that volume change is permitted, compute and send new volume to audio hardware
Mingming Yin0670f162014-06-12 16:05:49 -0700536 status_t checkAndSetVolume(audio_stream_type_t stream, int index, audio_io_handle_t output,
537 audio_devices_t device, int delayMs = 0, bool force = false);
538
539 // apply all stream volumes to the specified output and device
540 void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
541
542 // Mute or unmute all streams handled by the specified strategy on the specified output
543 void setStrategyMute(routing_strategy strategy,
544 bool on,
545 audio_io_handle_t output,
546 int delayMs = 0,
547 audio_devices_t device = (audio_devices_t)0);
548
549 // Mute or unmute the stream on the specified output
550 void setStreamMute(audio_stream_type_t stream,
551 bool on,
552 audio_io_handle_t output,
553 int delayMs = 0,
554 audio_devices_t device = (audio_devices_t)0);
555
556 // handle special cases for sonification strategy while in call: mute streams or replace by
557 // a special tone in the device used for communication
558 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
559
560 // true if device is in a telephony or VoIP call
561 virtual bool isInCall();
562
563 // true if given state represents a device in a telephony or VoIP call
564 virtual bool isStateInCall(int state);
565
566 // when a device is connected, checks if an open output can be routed
567 // to this device. If none is open, tries to open one of the available outputs.
568 // Returns an output suitable to this device or 0.
569 // when a device is disconnected, checks if an output is not used any more and
570 // returns its handle if any.
571 // transfers the audio tracks and effects from one output thread to another accordingly.
572 status_t checkOutputsForDevice(audio_devices_t device,
573 audio_policy_dev_state_t state,
574 SortedVector<audio_io_handle_t>& outputs,
575 const String8 address);
576
577 status_t checkInputsForDevice(audio_devices_t device,
578 audio_policy_dev_state_t state,
579 SortedVector<audio_io_handle_t>& inputs,
580 const String8 address);
581
582 // close an output and its companion duplicating output.
583 void closeOutput(audio_io_handle_t output);
584
585 // checks and if necessary changes outputs used for all strategies.
586 // must be called every time a condition that affects the output choice for a given strategy
587 // changes: connected device, phone state, force use...
588 // Must be called before updateDevicesAndOutputs()
589 void checkOutputForStrategy(routing_strategy strategy);
590
591 // Same as checkOutputForStrategy() but for a all strategies in order of priority
592 void checkOutputForAllStrategies();
593
594 // manages A2DP output suspend/restore according to phone state and BT SCO usage
595 void checkA2dpSuspend();
596
597 // returns the A2DP output handle if it is open or 0 otherwise
598 audio_io_handle_t getA2dpOutput();
599
600 // selects the most appropriate device on output for current state
601 // must be called every time a condition that affects the device choice for a given output is
602 // changed: connected device, phone state, force use, output start, output stop..
603 // see getDeviceForStrategy() for the use of fromCache parameter
604 audio_devices_t getNewOutputDevice(audio_io_handle_t output, bool fromCache);
605
606 // updates cache of device used by all strategies (mDeviceForStrategy[])
607 // must be called every time a condition that affects the device choice for a given strategy is
608 // changed: connected device, phone state, force use...
609 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
610 // Must be called after checkOutputForAllStrategies()
611 void updateDevicesAndOutputs();
612
613 // selects the most appropriate device on input for current state
614 audio_devices_t getNewInputDevice(audio_io_handle_t input);
615
616 virtual uint32_t getMaxEffectsCpuLoad();
617 virtual uint32_t getMaxEffectsMemory();
618#ifdef AUDIO_POLICY_TEST
619 virtual bool threadLoop();
620 void exit();
621 int testOutputIndex(audio_io_handle_t output);
622#endif //AUDIO_POLICY_TEST
623
624 status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled);
Ravi Kumar Alamanda88d28cb2013-10-15 16:59:57 -0700625
626 // returns the category the device belongs to with regard to volume curve management
627 static device_category getDeviceCategory(audio_devices_t device);
628
Mingming Yin0670f162014-06-12 16:05:49 -0700629 // extract one device relevant for volume control from multiple device selection
630 static audio_devices_t getDeviceForVolume(audio_devices_t device);
Tanya Finkelde496d82014-03-05 23:59:45 +0200631
Mingming Yin0670f162014-06-12 16:05:49 -0700632 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
633 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs);
634 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
635 SortedVector<audio_io_handle_t>& outputs2);
Tanya Finkelde496d82014-03-05 23:59:45 +0200636
Mingming Yin0670f162014-06-12 16:05:49 -0700637 // mute/unmute strategies using an incompatible device combination
638 // if muting, wait for the audio in pcm buffer to be drained before proceeding
639 // if unmuting, unmute only after the specified delay
640 // Returns the number of ms waited
641 uint32_t checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
642 audio_devices_t prevDevice,
643 uint32_t delayMs);
644
645 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
646 audio_output_flags_t flags);
647 sp<IOProfile> getInputProfile(audio_devices_t device,
648 uint32_t samplingRate,
649 audio_format_t format,
650 audio_channel_mask_t channelMask);
651 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
652 uint32_t samplingRate,
653 audio_format_t format,
654 audio_channel_mask_t channelMask,
655 audio_output_flags_t flags);
656
657 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
658
659 bool isNonOffloadableEffectEnabled();
660
661 status_t addAudioPatch(audio_patch_handle_t handle,
662 const sp<AudioPatch>& patch);
663 status_t removeAudioPatch(audio_patch_handle_t handle);
664
665 AudioOutputDescriptor *getOutputFromId(audio_port_handle_t id) const;
666 AudioInputDescriptor *getInputFromId(audio_port_handle_t id) const;
667 HwModule *getModuleForDevice(audio_devices_t device) const;
668 HwModule *getModuleFromName(const char *name) const;
669 //
670 // Audio policy configuration file parsing (audio_policy.conf)
671 //
672 static uint32_t stringToEnum(const struct StringToEnum *table,
673 size_t size,
674 const char *name);
675 static const char *enumToString(const struct StringToEnum *table,
676 size_t size,
677 uint32_t value);
678 static bool stringToBool(const char *value);
679 static audio_output_flags_t parseFlagNames(char *name);
680 static audio_devices_t parseDeviceNames(char *name);
681 void loadHwModule(cnode *root);
682 void loadHwModules(cnode *root);
683 void loadGlobalConfig(cnode *root, HwModule *module);
684 status_t loadAudioPolicyConfig(const char *path);
685 void defaultAudioPolicyConfig(void);
686
687
688 uid_t mUidCached;
689 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
690 audio_io_handle_t mPrimaryOutput; // primary output handle
691 // list of descriptors for outputs currently opened
692 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;
693 // copy of mOutputs before setDeviceConnectionState() opens new outputs
694 // reset to mOutputs when updateDevicesAndOutputs() is called.
695 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs;
696 DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors
697 DeviceVector mAvailableOutputDevices; // all available output devices
698 DeviceVector mAvailableInputDevices; // all available input devices
699 int mPhoneState; // current phone state
700 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration
701
702 StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control
703 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
704 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
705 float mLastVoiceVolume; // last voice volume value sent to audio HAL
706
707 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
708 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
709 // Maximum memory allocated to audio effects in KB
710 static const uint32_t MAX_EFFECTS_MEMORY = 512;
711 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
712 uint32_t mTotalEffectsMemory; // current memory used by effects
713 KeyedVector<int, EffectDescriptor *> mEffects; // list of registered audio effects
714 bool mA2dpSuspended; // true if A2DP output is suspended
715 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
716 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
717 // to boost soft sounds, used to adjust volume curves accordingly
718
719 Vector <HwModule *> mHwModules;
720 volatile int32_t mNextUniqueId;
721 volatile int32_t mAudioPortGeneration;
722
723 DefaultKeyedVector<audio_patch_handle_t, sp<AudioPatch> > mAudioPatches;
724
725#ifdef AUDIO_POLICY_TEST
726 Mutex mLock;
727 Condition mWaitWorkCV;
728
729 int mCurOutput;
730 bool mDirectOutput;
731 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
732 int mTestInput;
733 uint32_t mTestDevice;
734 uint32_t mTestSamplingRate;
735 uint32_t mTestFormat;
736 uint32_t mTestChannels;
737 uint32_t mTestLatencyMs;
738#endif //AUDIO_POLICY_TEST
Tanya Finkelde496d82014-03-05 23:59:45 +0200739
Pavan Chikkala785b6932014-03-24 18:58:11 +0530740private:
Mingming Yin0670f162014-06-12 16:05:49 -0700741 static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
742 int indexInUi);
743 // updates device caching and output for streams that can influence the
744 // routing of notifications
745 void handleNotificationRoutingForStream(audio_stream_type_t stream);
746 static bool isVirtualInputDevice(audio_devices_t device);
747 uint32_t nextUniqueId();
748 uint32_t nextAudioPortGeneration();
749 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
750 // converts device address to string sent to audio HAL via setParameters
751 static String8 addressToParameter(audio_devices_t device, const String8 address);
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700752};
Mingming Yin0670f162014-06-12 16:05:49 -0700753
Ravi Kumar Alamanda89a81422013-10-08 23:47:55 -0700754};