blob: 4214131fc4c304f7502751161b1dd3bedff3c68c [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 Laurent275e8e92014-11-30 15:14:47 -080026#include <media/AudioPolicy.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070027#include "AudioPolicyInterface.h"
Eric Laurente552edb2014-03-10 17:42:56 -070028
29
Eric Laurent3b73df72014-03-11 09:06:29 -070030namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070031
32// ----------------------------------------------------------------------------
33
Eric Laurente552edb2014-03-10 17:42:56 -070034// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
35#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
36// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
37#define SONIFICATION_HEADSET_VOLUME_MIN 0.016
38// Time in milliseconds during which we consider that music is still active after a music
39// track was stopped - see computeVolume()
40#define SONIFICATION_HEADSET_MUSIC_DELAY 5000
41// Time in milliseconds after media stopped playing during which we consider that the
42// sonification should be as unobtrusive as during the time media was playing.
43#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
44// Time in milliseconds during witch some streams are muted while the audio path
45// is switched
46#define MUTE_TIME_MS 2000
47
48#define NUM_TEST_OUTPUTS 5
49
50#define NUM_VOL_CURVE_KNEES 2
51
52// Default minimum length allowed for offloading a compressed track
53// Can be overridden by the audio.offload.min.duration.secs property
54#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
55
Eric Laurent1e693b52014-07-09 15:03:28 -070056#define MAX_MIXER_SAMPLING_RATE 48000
Andy Hung9a605382014-07-28 16:16:31 -070057#define MAX_MIXER_CHANNEL_COUNT 8
Eric Laurent1e693b52014-07-09 15:03:28 -070058
Eric Laurente552edb2014-03-10 17:42:56 -070059// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -070060// AudioPolicyManager implements audio policy manager behavior common to all platforms.
Eric Laurente552edb2014-03-10 17:42:56 -070061// ----------------------------------------------------------------------------
62
Eric Laurente0720872014-03-11 09:30:41 -070063class AudioPolicyManager: public AudioPolicyInterface
Eric Laurente552edb2014-03-10 17:42:56 -070064#ifdef AUDIO_POLICY_TEST
65 , public Thread
66#endif //AUDIO_POLICY_TEST
67{
68
69public:
Eric Laurente0720872014-03-11 09:30:41 -070070 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
71 virtual ~AudioPolicyManager();
Eric Laurente552edb2014-03-10 17:42:56 -070072
73 // AudioPolicyInterface
74 virtual status_t setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -070075 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -070076 const char *device_address);
Eric Laurent3b73df72014-03-11 09:06:29 -070077 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -070078 const char *device_address);
Eric Laurent3b73df72014-03-11 09:06:29 -070079 virtual void setPhoneState(audio_mode_t state);
80 virtual void setForceUse(audio_policy_force_use_t usage,
81 audio_policy_forced_cfg_t config);
82 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurente552edb2014-03-10 17:42:56 -070083 virtual void setSystemProperty(const char* property, const char* value);
84 virtual status_t initCheck();
Eric Laurent3b73df72014-03-11 09:06:29 -070085 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -070086 uint32_t samplingRate,
87 audio_format_t format,
88 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -070089 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -070090 const audio_offload_info_t *offloadInfo);
Eric Laurente83b55d2014-11-14 10:06:21 -080091 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
92 audio_io_handle_t *output,
93 audio_session_t session,
94 audio_stream_type_t *stream,
95 uint32_t samplingRate,
96 audio_format_t format,
97 audio_channel_mask_t channelMask,
98 audio_output_flags_t flags,
99 const audio_offload_info_t *offloadInfo);
Eric Laurente552edb2014-03-10 17:42:56 -0700100 virtual status_t startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700101 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800102 audio_session_t session);
Eric Laurente552edb2014-03-10 17:42:56 -0700103 virtual status_t stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700104 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800105 audio_session_t session);
106 virtual void releaseOutput(audio_io_handle_t output,
107 audio_stream_type_t stream,
108 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800109 virtual status_t getInputForAttr(const audio_attributes_t *attr,
110 audio_io_handle_t *input,
111 audio_session_t session,
112 uint32_t samplingRate,
113 audio_format_t format,
114 audio_channel_mask_t channelMask,
115 audio_input_flags_t flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700116
117 // indicates to the audio policy manager that the input starts being used.
Eric Laurent4dc68062014-07-28 17:26:49 -0700118 virtual status_t startInput(audio_io_handle_t input,
119 audio_session_t session);
Eric Laurente552edb2014-03-10 17:42:56 -0700120
121 // indicates to the audio policy manager that the input stops being used.
Eric Laurent4dc68062014-07-28 17:26:49 -0700122 virtual status_t stopInput(audio_io_handle_t input,
123 audio_session_t session);
124 virtual void releaseInput(audio_io_handle_t input,
125 audio_session_t session);
Eric Laurentd4692962014-05-05 18:13:44 -0700126 virtual void closeAllInputs();
Eric Laurent3b73df72014-03-11 09:06:29 -0700127 virtual void initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700128 int indexMin,
129 int indexMax);
Eric Laurent3b73df72014-03-11 09:06:29 -0700130 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700131 int index,
132 audio_devices_t device);
Eric Laurent3b73df72014-03-11 09:06:29 -0700133 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700134 int *index,
135 audio_devices_t device);
136
137 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700138 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700139 // return the strategy corresponding to the given audio attributes
140 virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
Eric Laurente552edb2014-03-10 17:42:56 -0700141
142 // return the enabled output devices for the given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700143 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700144
145 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
146 virtual status_t registerEffect(const effect_descriptor_t *desc,
147 audio_io_handle_t io,
148 uint32_t strategy,
149 int session,
150 int id);
151 virtual status_t unregisterEffect(int id);
152 virtual status_t setEffectEnabled(int id, bool enabled);
153
Eric Laurent3b73df72014-03-11 09:06:29 -0700154 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700155 // return whether a stream is playing remotely, override to change the definition of
156 // local/remote playback, used for instance by notification manager to not make
157 // media players lose audio focus when not playing locally
Eric Laurent3b73df72014-03-11 09:06:29 -0700158 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700159 virtual bool isSourceActive(audio_source_t source) const;
160
161 virtual status_t dump(int fd);
162
163 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
164
Eric Laurent6a94d692014-05-20 11:18:06 -0700165 virtual status_t listAudioPorts(audio_port_role_t role,
166 audio_port_type_t type,
167 unsigned int *num_ports,
168 struct audio_port *ports,
169 unsigned int *generation);
170 virtual status_t getAudioPort(struct audio_port *port);
171 virtual status_t createAudioPatch(const struct audio_patch *patch,
172 audio_patch_handle_t *handle,
173 uid_t uid);
174 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
175 uid_t uid);
176 virtual status_t listAudioPatches(unsigned int *num_patches,
177 struct audio_patch *patches,
178 unsigned int *generation);
179 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
180 virtual void clearAudioPatches(uid_t uid);
181
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700182 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
183 audio_io_handle_t *ioHandle,
184 audio_devices_t *device);
185
186 virtual status_t releaseSoundTriggerSession(audio_session_t session);
187
Eric Laurent275e8e92014-11-30 15:14:47 -0800188 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
189 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
190
Eric Laurente552edb2014-03-10 17:42:56 -0700191protected:
192
193 enum routing_strategy {
194 STRATEGY_MEDIA,
195 STRATEGY_PHONE,
196 STRATEGY_SONIFICATION,
197 STRATEGY_SONIFICATION_RESPECTFUL,
198 STRATEGY_DTMF,
199 STRATEGY_ENFORCED_AUDIBLE,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700200 STRATEGY_TRANSMITTED_THROUGH_SPEAKER,
Eric Laurent223fd5c2014-11-11 13:43:36 -0800201 STRATEGY_ACCESSIBILITY,
202 STRATEGY_REROUTING,
Eric Laurente552edb2014-03-10 17:42:56 -0700203 NUM_STRATEGIES
204 };
205
206 // 4 points to define the volume attenuation curve, each characterized by the volume
207 // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
208 // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
209
210 enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
211
212 class VolumeCurvePoint
213 {
214 public:
215 int mIndex;
216 float mDBAttenuation;
217 };
218
219 // device categories used for volume curve management.
220 enum device_category {
221 DEVICE_CATEGORY_HEADSET,
222 DEVICE_CATEGORY_SPEAKER,
223 DEVICE_CATEGORY_EARPIECE,
Jon Eklundac29afa2014-07-28 16:06:06 -0500224 DEVICE_CATEGORY_EXT_MEDIA,
Eric Laurente552edb2014-03-10 17:42:56 -0700225 DEVICE_CATEGORY_CNT
226 };
227
Eric Laurent1afeecb2014-05-14 08:52:28 -0700228 class HwModule;
Eric Laurente552edb2014-03-10 17:42:56 -0700229
Eric Laurent1afeecb2014-05-14 08:52:28 -0700230 class AudioGain: public RefBase
231 {
Eric Laurente552edb2014-03-10 17:42:56 -0700232 public:
Eric Laurenta121f902014-06-03 13:32:54 -0700233 AudioGain(int index, bool useInChannelMask);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700234 virtual ~AudioGain() {}
Eric Laurente552edb2014-03-10 17:42:56 -0700235
Eric Laurent1afeecb2014-05-14 08:52:28 -0700236 void dump(int fd, int spaces, int index) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700237
Eric Laurenta121f902014-06-03 13:32:54 -0700238 void getDefaultConfig(struct audio_gain_config *config);
239 status_t checkConfig(const struct audio_gain_config *config);
240 int mIndex;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700241 struct audio_gain mGain;
Eric Laurenta121f902014-06-03 13:32:54 -0700242 bool mUseInChannelMask;
Eric Laurent951f4552014-05-20 10:48:17 -0700243 };
244
Eric Laurent1f2f2232014-06-02 12:01:23 -0700245 class AudioPort: public virtual RefBase
Eric Laurent951f4552014-05-20 10:48:17 -0700246 {
247 public:
Eric Laurent1afeecb2014-05-14 08:52:28 -0700248 AudioPort(const String8& name, audio_port_type_t type,
Eric Laurenta121f902014-06-03 13:32:54 -0700249 audio_port_role_t role, const sp<HwModule>& module);
Eric Laurent951f4552014-05-20 10:48:17 -0700250 virtual ~AudioPort() {}
251
252 virtual void toAudioPort(struct audio_port *port) const;
253
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700254 void importAudioPort(const sp<AudioPort> port);
255 void clearCapabilities();
256
Eric Laurent951f4552014-05-20 10:48:17 -0700257 void loadSamplingRates(char *name);
258 void loadFormats(char *name);
259 void loadOutChannels(char *name);
260 void loadInChannels(char *name);
261
Eric Laurent1afeecb2014-05-14 08:52:28 -0700262 audio_gain_mode_t loadGainMode(char *name);
Eric Laurenta121f902014-06-03 13:32:54 -0700263 void loadGain(cnode *root, int index);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700264 void loadGains(cnode *root);
265
Glenn Kastencbd48022014-07-24 13:46:44 -0700266 // searches for an exact match
267 status_t checkExactSamplingRate(uint32_t samplingRate) const;
268 // searches for a compatible match, and returns the best match via updatedSamplingRate
269 status_t checkCompatibleSamplingRate(uint32_t samplingRate,
270 uint32_t *updatedSamplingRate) const;
271 // searches for an exact match
272 status_t checkExactChannelMask(audio_channel_mask_t channelMask) const;
273 // searches for a compatible match, currently implemented for input channel masks only
274 status_t checkCompatibleChannelMask(audio_channel_mask_t channelMask) const;
Eric Laurenta121f902014-06-03 13:32:54 -0700275 status_t checkFormat(audio_format_t format) const;
276 status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
277
Eric Laurent1e693b52014-07-09 15:03:28 -0700278 uint32_t pickSamplingRate() const;
279 audio_channel_mask_t pickChannelMask() const;
280 audio_format_t pickFormat() const;
281
282 static const audio_format_t sPcmFormatCompareTable[];
283 static int compareFormats(audio_format_t format1, audio_format_t format2);
284
Eric Laurent1afeecb2014-05-14 08:52:28 -0700285 void dump(int fd, int spaces) const;
286
287 String8 mName;
288 audio_port_type_t mType;
289 audio_port_role_t mRole;
Eric Laurenta121f902014-06-03 13:32:54 -0700290 bool mUseInChannelMask;
Eric Laurent951f4552014-05-20 10:48:17 -0700291 // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
292 // indicates the supported parameters should be read from the output stream
293 // after it is opened for the first time
294 Vector <uint32_t> mSamplingRates; // supported sampling rates
295 Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
296 Vector <audio_format_t> mFormats; // supported audio formats
Eric Laurent1afeecb2014-05-14 08:52:28 -0700297 Vector < sp<AudioGain> > mGains; // gain controllers
Eric Laurent1f2f2232014-06-02 12:01:23 -0700298 sp<HwModule> mModule; // audio HW module exposing this I/O stream
Eric Laurent5dbe4712014-09-19 19:04:57 -0700299 uint32_t mFlags; // attribute flags (e.g primary output,
300 // direct output...).
Eric Laurent951f4552014-05-20 10:48:17 -0700301 };
302
Eric Laurent1f2f2232014-06-02 12:01:23 -0700303 class AudioPortConfig: public virtual RefBase
304 {
305 public:
306 AudioPortConfig();
307 virtual ~AudioPortConfig() {}
308
Eric Laurenta121f902014-06-03 13:32:54 -0700309 status_t applyAudioPortConfig(const struct audio_port_config *config,
310 struct audio_port_config *backupConfig = NULL);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700311 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
312 const struct audio_port_config *srcConfig = NULL) const = 0;
Marco Nelissen961ec212014-08-25 15:58:39 -0700313 virtual sp<AudioPort> getAudioPort() const = 0;
Eric Laurent1f2f2232014-06-02 12:01:23 -0700314 uint32_t mSamplingRate;
315 audio_format_t mFormat;
316 audio_channel_mask_t mChannelMask;
317 struct audio_gain_config mGain;
318 };
319
320
Eric Laurent6a94d692014-05-20 11:18:06 -0700321 class AudioPatch: public RefBase
322 {
323 public:
324 AudioPatch(audio_patch_handle_t handle,
325 const struct audio_patch *patch, uid_t uid) :
326 mHandle(handle), mPatch(*patch), mUid(uid), mAfPatchHandle(0) {}
327
Eric Laurent4d416952014-08-10 14:07:09 -0700328 status_t dump(int fd, int spaces, int index) const;
329
Eric Laurent6a94d692014-05-20 11:18:06 -0700330 audio_patch_handle_t mHandle;
331 struct audio_patch mPatch;
332 uid_t mUid;
333 audio_patch_handle_t mAfPatchHandle;
334 };
Eric Laurent951f4552014-05-20 10:48:17 -0700335
Eric Laurent1f2f2232014-06-02 12:01:23 -0700336 class DeviceDescriptor: public AudioPort, public AudioPortConfig
Eric Laurent951f4552014-05-20 10:48:17 -0700337 {
338 public:
Eric Laurent1f2f2232014-06-02 12:01:23 -0700339 DeviceDescriptor(const String8& name, audio_devices_t type);
Eric Laurent951f4552014-05-20 10:48:17 -0700340
Eric Laurent951f4552014-05-20 10:48:17 -0700341 virtual ~DeviceDescriptor() {}
342
343 bool equals(const sp<DeviceDescriptor>& other) const;
Eric Laurent1f2f2232014-06-02 12:01:23 -0700344 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700345 const struct audio_port_config *srcConfig = NULL) const;
Marco Nelissen961ec212014-08-25 15:58:39 -0700346 virtual sp<AudioPort> getAudioPort() const { return (AudioPort*) this; }
Eric Laurent1afeecb2014-05-14 08:52:28 -0700347
Eric Laurent951f4552014-05-20 10:48:17 -0700348 virtual void toAudioPort(struct audio_port *port) const;
349
Eric Laurent1afeecb2014-05-14 08:52:28 -0700350 status_t dump(int fd, int spaces, int index) const;
Eric Laurent951f4552014-05-20 10:48:17 -0700351
352 audio_devices_t mDeviceType;
353 String8 mAddress;
Eric Laurent951f4552014-05-20 10:48:17 -0700354 audio_port_handle_t mId;
355 };
356
357 class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
358 {
359 public:
360 DeviceVector() : SortedVector(), mDeviceTypes(AUDIO_DEVICE_NONE) {}
361
362 ssize_t add(const sp<DeviceDescriptor>& item);
363 ssize_t remove(const sp<DeviceDescriptor>& item);
364 ssize_t indexOf(const sp<DeviceDescriptor>& item) const;
365
366 audio_devices_t types() const { return mDeviceTypes; }
367
368 void loadDevicesFromType(audio_devices_t types);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700369 void loadDevicesFromName(char *name, const DeviceVector& declaredDevices);
370
Eric Laurent951f4552014-05-20 10:48:17 -0700371 sp<DeviceDescriptor> getDevice(audio_devices_t type, String8 address) const;
372 DeviceVector getDevicesFromType(audio_devices_t types) const;
Eric Laurent6a94d692014-05-20 11:18:06 -0700373 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700374 sp<DeviceDescriptor> getDeviceFromName(const String8& name) const;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700375 DeviceVector getDevicesFromTypeAddr(audio_devices_t type, String8 address)
376 const;
Eric Laurent951f4552014-05-20 10:48:17 -0700377
378 private:
379 void refreshTypes();
380 audio_devices_t mDeviceTypes;
Eric Laurente552edb2014-03-10 17:42:56 -0700381 };
382
383 // the IOProfile class describes the capabilities of an output or input stream.
384 // It is currently assumed that all combination of listed parameters are supported.
385 // It is used by the policy manager to determine if an output or input is suitable for
386 // a given use case, open/close it accordingly and connect/disconnect audio tracks
387 // to/from it.
Eric Laurent951f4552014-05-20 10:48:17 -0700388 class IOProfile : public AudioPort
Eric Laurente552edb2014-03-10 17:42:56 -0700389 {
390 public:
Eric Laurent1f2f2232014-06-02 12:01:23 -0700391 IOProfile(const String8& name, audio_port_role_t role, const sp<HwModule>& module);
Eric Laurent951f4552014-05-20 10:48:17 -0700392 virtual ~IOProfile();
Eric Laurente552edb2014-03-10 17:42:56 -0700393
Glenn Kastencbd48022014-07-24 13:46:44 -0700394 // This method is used for both output and input.
395 // If parameter updatedSamplingRate is non-NULL, it is assigned the actual sample rate.
396 // For input, flags is interpreted as audio_input_flags_t.
397 // TODO: merge audio_output_flags_t and audio_input_flags_t.
Eric Laurente552edb2014-03-10 17:42:56 -0700398 bool isCompatibleProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -0800399 String8 address,
Eric Laurente552edb2014-03-10 17:42:56 -0700400 uint32_t samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700401 uint32_t *updatedSamplingRate,
Eric Laurente552edb2014-03-10 17:42:56 -0700402 audio_format_t format,
403 audio_channel_mask_t channelMask,
Eric Laurent5dbe4712014-09-19 19:04:57 -0700404 uint32_t flags) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700405
406 void dump(int fd);
Eric Laurentd4692962014-05-05 18:13:44 -0700407 void log();
Eric Laurente552edb2014-03-10 17:42:56 -0700408
Eric Laurent3a4311c2014-03-17 12:00:47 -0700409 DeviceVector mSupportedDevices; // supported devices
410 // (devices this output can be routed to)
Eric Laurente552edb2014-03-10 17:42:56 -0700411 };
412
Eric Laurent1e693b52014-07-09 15:03:28 -0700413 class HwModule : public RefBase
414 {
Eric Laurent1afeecb2014-05-14 08:52:28 -0700415 public:
416 HwModule(const char *name);
417 ~HwModule();
418
419 status_t loadOutput(cnode *root);
420 status_t loadInput(cnode *root);
421 status_t loadDevice(cnode *root);
422
Eric Laurent275e8e92014-11-30 15:14:47 -0800423 status_t addOutputProfile(String8 name, const audio_config_t *config,
424 audio_devices_t device, String8 address);
425 status_t removeOutputProfile(String8 name);
426 status_t addInputProfile(String8 name, const audio_config_t *config,
427 audio_devices_t device, String8 address);
428 status_t removeInputProfile(String8 name);
429
Eric Laurent1afeecb2014-05-14 08:52:28 -0700430 void dump(int fd);
431
Eric Laurenteb108a42014-06-06 14:56:52 -0700432 const char *const mName; // base name of the audio HW module (primary, a2dp ...)
433 uint32_t mHalVersion; // audio HAL API version
434 audio_module_handle_t mHandle;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700435 Vector < sp<IOProfile> > mOutputProfiles; // output profiles exposed by this module
436 Vector < sp<IOProfile> > mInputProfiles; // input profiles exposed by this module
437 DeviceVector mDeclaredDevices; // devices declared in audio_policy.conf
438
439 };
440
Eric Laurente552edb2014-03-10 17:42:56 -0700441 // default volume curve
Eric Laurente0720872014-03-11 09:30:41 -0700442 static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700443 // default volume curve for media strategy
Eric Laurente0720872014-03-11 09:30:41 -0700444 static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Jon Eklundac29afa2014-07-28 16:06:06 -0500445 // volume curve for non-media audio on ext media outputs (HDMI, Line, etc)
446 static const VolumeCurvePoint sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700447 // volume curve for media strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700448 static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -0700449 static const VolumeCurvePoint sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700450 // volume curve for sonification strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700451 static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
452 static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
453 static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
454 static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
455 static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
456 static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
457 static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700458 static const VolumeCurvePoint sLinearVolumeCurve[AudioPolicyManager::VOLCNT];
459 static const VolumeCurvePoint sSilentVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurent223fd5c2014-11-11 13:43:36 -0800460 static const VolumeCurvePoint sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700461 // default volume curves per stream and device category. See initializeVolumeCurves()
462 static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
463
464 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
465 // and keep track of the usage of this output by each audio stream type.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700466 class AudioOutputDescriptor: public AudioPortConfig
Eric Laurente552edb2014-03-10 17:42:56 -0700467 {
468 public:
Eric Laurent951f4552014-05-20 10:48:17 -0700469 AudioOutputDescriptor(const sp<IOProfile>& profile);
Eric Laurente552edb2014-03-10 17:42:56 -0700470
471 status_t dump(int fd);
472
473 audio_devices_t device() const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700474 void changeRefCount(audio_stream_type_t stream, int delta);
Eric Laurente552edb2014-03-10 17:42:56 -0700475
476 bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
477 audio_devices_t supportedDevices();
478 uint32_t latency();
Eric Laurent1f2f2232014-06-02 12:01:23 -0700479 bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700480 bool isActive(uint32_t inPastMs = 0) const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700481 bool isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700482 uint32_t inPastMs = 0,
483 nsecs_t sysTime = 0) const;
484 bool isStrategyActive(routing_strategy strategy,
485 uint32_t inPastMs = 0,
486 nsecs_t sysTime = 0) const;
487
Eric Laurent1f2f2232014-06-02 12:01:23 -0700488 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700489 const struct audio_port_config *srcConfig = NULL) const;
Marco Nelissen961ec212014-08-25 15:58:39 -0700490 virtual sp<AudioPort> getAudioPort() const { return mProfile; }
Eric Laurent951f4552014-05-20 10:48:17 -0700491 void toAudioPort(struct audio_port *port) const;
492
493 audio_port_handle_t mId;
494 audio_io_handle_t mIoHandle; // output handle
Eric Laurente552edb2014-03-10 17:42:56 -0700495 uint32_t mLatency; //
496 audio_output_flags_t mFlags; //
497 audio_devices_t mDevice; // current device this output is routed to
Eric Laurent275e8e92014-11-30 15:14:47 -0800498 String8 mPolicyMixAddress; // non empty or "0" when used by a dynamic policy
Eric Laurent951f4552014-05-20 10:48:17 -0700499 audio_patch_handle_t mPatchHandle;
Eric Laurent3b73df72014-03-11 09:06:29 -0700500 uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
501 nsecs_t mStopTime[AUDIO_STREAM_CNT];
Eric Laurent1f2f2232014-06-02 12:01:23 -0700502 sp<AudioOutputDescriptor> mOutput1; // used by duplicated outputs: first output
503 sp<AudioOutputDescriptor> mOutput2; // used by duplicated outputs: second output
Eric Laurent3b73df72014-03-11 09:06:29 -0700504 float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume
505 int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter
Eric Laurent951f4552014-05-20 10:48:17 -0700506 const sp<IOProfile> mProfile; // I/O profile this output derives from
Eric Laurente552edb2014-03-10 17:42:56 -0700507 bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
508 // device selection. See checkDeviceMuteStrategies()
509 uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
510 };
511
512 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
513 // and keep track of the usage of this input.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700514 class AudioInputDescriptor: public AudioPortConfig
Eric Laurente552edb2014-03-10 17:42:56 -0700515 {
516 public:
Eric Laurent951f4552014-05-20 10:48:17 -0700517 AudioInputDescriptor(const sp<IOProfile>& profile);
Eric Laurente552edb2014-03-10 17:42:56 -0700518
519 status_t dump(int fd);
520
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700521 audio_port_handle_t mId;
522 audio_io_handle_t mIoHandle; // input handle
523 audio_devices_t mDevice; // current device this input is routed to
524 audio_patch_handle_t mPatchHandle;
525 uint32_t mRefCount; // number of AudioRecord clients using
526 // this input
527 uint32_t mOpenRefCount;
528 audio_source_t mInputSource; // input source selected by application
529 //(mediarecorder.h)
530 const sp<IOProfile> mProfile; // I/O profile this output derives from
Eric Laurent275e8e92014-11-30 15:14:47 -0800531 // audio sessions attached to this input and the
532 // corresponding device address
533 DefaultKeyedVector<audio_session_t, String8> mSessions;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700534 bool mIsSoundTrigger; // used by a soundtrigger capture
Eric Laurent951f4552014-05-20 10:48:17 -0700535
Eric Laurent1f2f2232014-06-02 12:01:23 -0700536 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700537 const struct audio_port_config *srcConfig = NULL) const;
Marco Nelissen961ec212014-08-25 15:58:39 -0700538 virtual sp<AudioPort> getAudioPort() const { return mProfile; }
Eric Laurent951f4552014-05-20 10:48:17 -0700539 void toAudioPort(struct audio_port *port) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700540 };
541
542 // stream descriptor used for volume control
543 class StreamDescriptor
544 {
545 public:
546 StreamDescriptor();
547
548 int getVolumeIndex(audio_devices_t device);
549 void dump(int fd);
550
551 int mIndexMin; // min volume index
552 int mIndexMax; // max volume index
553 KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device
554 bool mCanBeMuted; // true is the stream can be muted
555
556 const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
557 };
558
559 // stream descriptor used for volume control
Eric Laurent1f2f2232014-06-02 12:01:23 -0700560 class EffectDescriptor : public RefBase
Eric Laurente552edb2014-03-10 17:42:56 -0700561 {
562 public:
563
564 status_t dump(int fd);
565
566 int mIo; // io the effect is attached to
567 routing_strategy mStrategy; // routing strategy the effect is associated to
568 int mSession; // audio session the effect is on
569 effect_descriptor_t mDesc; // effect descriptor
570 bool mEnabled; // enabled state: CPU load being used or not
571 };
572
Eric Laurent1f2f2232014-06-02 12:01:23 -0700573 void addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc);
574 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700575
576 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700577 static routing_strategy getStrategy(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700578
579 // return appropriate device for streams handled by the specified strategy according to current
580 // phone state, connected devices...
581 // if fromCache is true, the device is returned from mDeviceForStrategy[],
582 // otherwise it is determine by current state
583 // (device connected,phone state, force use, a2dp output...)
584 // This allows to:
585 // 1 speed up process when the state is stable (when starting or stopping an output)
586 // 2 access to either current device selection (fromCache == true) or
587 // "future" device selection (fromCache == false) when called from a context
588 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
589 // before updateDevicesAndOutputs() is called.
590 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
591 bool fromCache);
592
593 // change the route of the specified output. Returns the number of ms we have slept to
594 // allow new routing to take effect in certain cases.
595 uint32_t setOutputDevice(audio_io_handle_t output,
596 audio_devices_t device,
597 bool force = false,
Eric Laurent6a94d692014-05-20 11:18:06 -0700598 int delayMs = 0,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700599 audio_patch_handle_t *patchHandle = NULL,
600 const char* address = NULL);
Eric Laurent951f4552014-05-20 10:48:17 -0700601 status_t resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -0700602 int delayMs = 0,
603 audio_patch_handle_t *patchHandle = NULL);
Eric Laurent951f4552014-05-20 10:48:17 -0700604 status_t setInputDevice(audio_io_handle_t input,
605 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -0700606 bool force = false,
607 audio_patch_handle_t *patchHandle = NULL);
608 status_t resetInputDevice(audio_io_handle_t input,
609 audio_patch_handle_t *patchHandle = NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700610
611 // select input device corresponding to requested audio source
Eric Laurent275e8e92014-11-30 15:14:47 -0800612 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource,
613 String8 *address = NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700614
615 // return io handle of active input or 0 if no input is active
616 // Only considers inputs from physical devices (e.g. main mic, headset mic) when
617 // ignoreVirtualInputs is true.
618 audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
619
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700620 uint32_t activeInputsCount() const;
621
Eric Laurente552edb2014-03-10 17:42:56 -0700622 // initialize volume curves for each strategy and device category
623 void initializeVolumeCurves();
624
625 // compute the actual volume for a given stream according to the requested index and a particular
626 // device
Eric Laurent3b73df72014-03-11 09:06:29 -0700627 virtual float computeVolume(audio_stream_type_t stream, int index,
628 audio_io_handle_t output, audio_devices_t device);
Eric Laurente552edb2014-03-10 17:42:56 -0700629
630 // check that volume change is permitted, compute and send new volume to audio hardware
Hochi Huang327cb702014-09-21 09:47:31 +0800631 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
632 audio_io_handle_t output,
633 audio_devices_t device,
634 int delayMs = 0, bool force = false);
Eric Laurente552edb2014-03-10 17:42:56 -0700635
636 // apply all stream volumes to the specified output and device
637 void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
638
639 // Mute or unmute all streams handled by the specified strategy on the specified output
640 void setStrategyMute(routing_strategy strategy,
641 bool on,
642 audio_io_handle_t output,
643 int delayMs = 0,
644 audio_devices_t device = (audio_devices_t)0);
645
646 // Mute or unmute the stream on the specified output
Eric Laurent3b73df72014-03-11 09:06:29 -0700647 void setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700648 bool on,
649 audio_io_handle_t output,
650 int delayMs = 0,
651 audio_devices_t device = (audio_devices_t)0);
652
653 // handle special cases for sonification strategy while in call: mute streams or replace by
654 // a special tone in the device used for communication
Eric Laurent3b73df72014-03-11 09:06:29 -0700655 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
Eric Laurente552edb2014-03-10 17:42:56 -0700656
657 // true if device is in a telephony or VoIP call
658 virtual bool isInCall();
659
660 // true if given state represents a device in a telephony or VoIP call
661 virtual bool isStateInCall(int state);
662
663 // when a device is connected, checks if an open output can be routed
664 // to this device. If none is open, tries to open one of the available outputs.
665 // Returns an output suitable to this device or 0.
666 // when a device is disconnected, checks if an output is not used any more and
667 // returns its handle if any.
668 // transfers the audio tracks and effects from one output thread to another accordingly.
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700669 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
Eric Laurent3b73df72014-03-11 09:06:29 -0700670 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700671 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -0700672 const String8 address);
Eric Laurente552edb2014-03-10 17:42:56 -0700673
Eric Laurentd4692962014-05-05 18:13:44 -0700674 status_t checkInputsForDevice(audio_devices_t device,
675 audio_policy_dev_state_t state,
676 SortedVector<audio_io_handle_t>& inputs,
677 const String8 address);
678
Eric Laurente552edb2014-03-10 17:42:56 -0700679 // close an output and its companion duplicating output.
680 void closeOutput(audio_io_handle_t output);
681
Eric Laurent05b90f82014-08-27 15:32:29 -0700682 // close an input.
683 void closeInput(audio_io_handle_t input);
684
Eric Laurente552edb2014-03-10 17:42:56 -0700685 // checks and if necessary changes outputs used for all strategies.
686 // must be called every time a condition that affects the output choice for a given strategy
687 // changes: connected device, phone state, force use...
688 // Must be called before updateDevicesAndOutputs()
689 void checkOutputForStrategy(routing_strategy strategy);
690
691 // Same as checkOutputForStrategy() but for a all strategies in order of priority
692 void checkOutputForAllStrategies();
693
694 // manages A2DP output suspend/restore according to phone state and BT SCO usage
695 void checkA2dpSuspend();
696
697 // returns the A2DP output handle if it is open or 0 otherwise
698 audio_io_handle_t getA2dpOutput();
699
700 // selects the most appropriate device on output for current state
701 // must be called every time a condition that affects the device choice for a given output is
702 // changed: connected device, phone state, force use, output start, output stop..
703 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurent951f4552014-05-20 10:48:17 -0700704 audio_devices_t getNewOutputDevice(audio_io_handle_t output, bool fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -0700705
Eric Laurente552edb2014-03-10 17:42:56 -0700706 // updates cache of device used by all strategies (mDeviceForStrategy[])
707 // must be called every time a condition that affects the device choice for a given strategy is
708 // changed: connected device, phone state, force use...
709 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
710 // Must be called after checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -0700711 void updateDevicesAndOutputs();
712
Eric Laurent951f4552014-05-20 10:48:17 -0700713 // selects the most appropriate device on input for current state
714 audio_devices_t getNewInputDevice(audio_io_handle_t input);
715
Eric Laurente552edb2014-03-10 17:42:56 -0700716 virtual uint32_t getMaxEffectsCpuLoad();
717 virtual uint32_t getMaxEffectsMemory();
718#ifdef AUDIO_POLICY_TEST
719 virtual bool threadLoop();
720 void exit();
721 int testOutputIndex(audio_io_handle_t output);
722#endif //AUDIO_POLICY_TEST
723
Eric Laurent1f2f2232014-06-02 12:01:23 -0700724 status_t setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled);
Eric Laurente552edb2014-03-10 17:42:56 -0700725
726 // returns the category the device belongs to with regard to volume curve management
727 static device_category getDeviceCategory(audio_devices_t device);
728
729 // extract one device relevant for volume control from multiple device selection
730 static audio_devices_t getDeviceForVolume(audio_devices_t device);
731
732 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -0700733 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -0700734 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
735 SortedVector<audio_io_handle_t>& outputs2);
736
737 // mute/unmute strategies using an incompatible device combination
738 // if muting, wait for the audio in pcm buffer to be drained before proceeding
739 // if unmuting, unmute only after the specified delay
740 // Returns the number of ms waited
Hochi Huang18f2f902014-12-03 15:23:36 +0800741 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -0700742 audio_devices_t prevDevice,
743 uint32_t delayMs);
744
745 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700746 audio_output_flags_t flags,
747 audio_format_t format);
Glenn Kastencbd48022014-07-24 13:46:44 -0700748 // samplingRate parameter is an in/out and so may be modified
Eric Laurent951f4552014-05-20 10:48:17 -0700749 sp<IOProfile> getInputProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -0800750 String8 address,
751 uint32_t& samplingRate,
752 audio_format_t format,
753 audio_channel_mask_t channelMask,
754 audio_input_flags_t flags);
Eric Laurent951f4552014-05-20 10:48:17 -0700755 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700756 uint32_t samplingRate,
757 audio_format_t format,
758 audio_channel_mask_t channelMask,
759 audio_output_flags_t flags);
760
761 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
762
763 bool isNonOffloadableEffectEnabled();
764
Eric Laurent6a94d692014-05-20 11:18:06 -0700765 status_t addAudioPatch(audio_patch_handle_t handle,
766 const sp<AudioPatch>& patch);
767 status_t removeAudioPatch(audio_patch_handle_t handle);
768
Eric Laurent1f2f2232014-06-02 12:01:23 -0700769 sp<AudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
770 sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
771 sp<HwModule> getModuleForDevice(audio_devices_t device) const;
772 sp<HwModule> getModuleFromName(const char *name) const;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700773 audio_devices_t availablePrimaryOutputDevices();
774 audio_devices_t availablePrimaryInputDevices();
775
776 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
777
Eric Laurente552edb2014-03-10 17:42:56 -0700778 //
779 // Audio policy configuration file parsing (audio_policy.conf)
780 //
781 static uint32_t stringToEnum(const struct StringToEnum *table,
782 size_t size,
783 const char *name);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700784 static const char *enumToString(const struct StringToEnum *table,
785 size_t size,
786 uint32_t value);
Eric Laurente552edb2014-03-10 17:42:56 -0700787 static bool stringToBool(const char *value);
Eric Laurent5dbe4712014-09-19 19:04:57 -0700788 static uint32_t parseOutputFlagNames(char *name);
789 static uint32_t parseInputFlagNames(char *name);
Eric Laurente552edb2014-03-10 17:42:56 -0700790 static audio_devices_t parseDeviceNames(char *name);
Eric Laurente552edb2014-03-10 17:42:56 -0700791 void loadHwModule(cnode *root);
792 void loadHwModules(cnode *root);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700793 void loadGlobalConfig(cnode *root, const sp<HwModule>& module);
Eric Laurente552edb2014-03-10 17:42:56 -0700794 status_t loadAudioPolicyConfig(const char *path);
795 void defaultAudioPolicyConfig(void);
796
797
Eric Laurent6a94d692014-05-20 11:18:06 -0700798 uid_t mUidCached;
Eric Laurente552edb2014-03-10 17:42:56 -0700799 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
800 audio_io_handle_t mPrimaryOutput; // primary output handle
801 // list of descriptors for outputs currently opened
Eric Laurent1f2f2232014-06-02 12:01:23 -0700802 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700803 // copy of mOutputs before setDeviceConnectionState() opens new outputs
804 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700805 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs;
806 DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs; // list of input descriptors
Eric Laurent1afeecb2014-05-14 08:52:28 -0700807 DeviceVector mAvailableOutputDevices; // all available output devices
808 DeviceVector mAvailableInputDevices; // all available input devices
Eric Laurente552edb2014-03-10 17:42:56 -0700809 int mPhoneState; // current phone state
Eric Laurent3b73df72014-03-11 09:06:29 -0700810 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration
Eric Laurente552edb2014-03-10 17:42:56 -0700811
Eric Laurent3b73df72014-03-11 09:06:29 -0700812 StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control
Eric Laurente552edb2014-03-10 17:42:56 -0700813 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
814 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
815 float mLastVoiceVolume; // last voice volume value sent to audio HAL
816
817 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
818 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
819 // Maximum memory allocated to audio effects in KB
820 static const uint32_t MAX_EFFECTS_MEMORY = 512;
821 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
822 uint32_t mTotalEffectsMemory; // current memory used by effects
Eric Laurent1f2f2232014-06-02 12:01:23 -0700823 KeyedVector<int, sp<EffectDescriptor> > mEffects; // list of registered audio effects
Eric Laurente552edb2014-03-10 17:42:56 -0700824 bool mA2dpSuspended; // true if A2DP output is suspended
Eric Laurent3a4311c2014-03-17 12:00:47 -0700825 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
Eric Laurente552edb2014-03-10 17:42:56 -0700826 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
827 // to boost soft sounds, used to adjust volume curves accordingly
828
Eric Laurent1f2f2232014-06-02 12:01:23 -0700829 Vector < sp<HwModule> > mHwModules;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700830 volatile int32_t mNextUniqueId;
Eric Laurent6a94d692014-05-20 11:18:06 -0700831 volatile int32_t mAudioPortGeneration;
832
833 DefaultKeyedVector<audio_patch_handle_t, sp<AudioPatch> > mAudioPatches;
Eric Laurente552edb2014-03-10 17:42:56 -0700834
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700835 DefaultKeyedVector<audio_session_t, audio_io_handle_t> mSoundTriggerSessions;
836
Eric Laurentc2730ba2014-07-20 15:47:07 -0700837 sp<AudioPatch> mCallTxPatch;
838 sp<AudioPatch> mCallRxPatch;
839
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700840 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
841 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
842 enum {
843 STARTING_OUTPUT,
844 STARTING_BEACON,
845 STOPPING_OUTPUT,
846 STOPPING_BEACON
847 };
848 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
849 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
850 bool mBeaconMuted; // has STREAM_TTS been muted
851
Eric Laurent275e8e92014-11-30 15:14:47 -0800852 // custom mix entry in mPolicyMixes
853 class AudioPolicyMix : public RefBase {
854 public:
855 AudioPolicyMix() {}
856
857 AudioMix mMix; // Audio policy mix descriptor
858 sp<AudioOutputDescriptor> mOutput; // Corresponding output stream
859 };
860 DefaultKeyedVector<String8, sp<AudioPolicyMix> > mPolicyMixes; // list of registered mixes
861
862
Eric Laurente552edb2014-03-10 17:42:56 -0700863#ifdef AUDIO_POLICY_TEST
864 Mutex mLock;
865 Condition mWaitWorkCV;
866
867 int mCurOutput;
868 bool mDirectOutput;
869 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
870 int mTestInput;
871 uint32_t mTestDevice;
872 uint32_t mTestSamplingRate;
873 uint32_t mTestFormat;
874 uint32_t mTestChannels;
875 uint32_t mTestLatencyMs;
876#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -0700877 static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
878 int indexInUi);
Hochi Huang327cb702014-09-21 09:47:31 +0800879private:
Eric Laurente552edb2014-03-10 17:42:56 -0700880 // updates device caching and output for streams that can influence the
881 // routing of notifications
Eric Laurent3b73df72014-03-11 09:06:29 -0700882 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700883 static bool isVirtualInputDevice(audio_devices_t device);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700884 static bool deviceDistinguishesOnAddress(audio_devices_t device);
885 // find the outputs on a given output descriptor that have the given address.
886 // to be called on an AudioOutputDescriptor whose supported devices (as defined
887 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
888 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
889 // where addresses are used to distinguish between one connected device and another.
890 void findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
891 const String8 address /*in*/,
892 SortedVector<audio_io_handle_t>& outputs /*out*/);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700893 uint32_t nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700894 uint32_t nextAudioPortGeneration();
895 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700896 // internal method to return the output handle for the given device and format
897 audio_io_handle_t getOutputForDevice(
898 audio_devices_t device,
Eric Laurente83b55d2014-11-14 10:06:21 -0800899 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700900 audio_stream_type_t stream,
901 uint32_t samplingRate,
902 audio_format_t format,
903 audio_channel_mask_t channelMask,
904 audio_output_flags_t flags,
905 const audio_offload_info_t *offloadInfo);
906 // internal function to derive a stream type value from audio attributes
907 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700908 // return true if any output is playing anything besides the stream to ignore
909 bool isAnyOutputActive(audio_stream_type_t streamToIgnore);
910 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
911 // returns 0 if no mute/unmute event happened, the largest latency of the device where
912 // the mute/unmute happened
913 uint32_t handleEventForBeacon(int event);
914 uint32_t setBeaconMute(bool mute);
Eric Laurente83b55d2014-11-14 10:06:21 -0800915 bool isValidAttributes(const audio_attributes_t *paa);
Eric Laurente552edb2014-03-10 17:42:56 -0700916};
917
918};