blob: 3e3c6625cb6cbb9bf25bea91ddef90648b53e2a8 [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,
Paul McLeane743a472015-01-28 11:07:31 -080076 const char *device_address,
77 const char *device_name);
Eric Laurent3b73df72014-03-11 09:06:29 -070078 virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -070079 const char *device_address);
Eric Laurent3b73df72014-03-11 09:06:29 -070080 virtual void setPhoneState(audio_mode_t state);
81 virtual void setForceUse(audio_policy_force_use_t usage,
82 audio_policy_forced_cfg_t config);
83 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurente552edb2014-03-10 17:42:56 -070084 virtual void setSystemProperty(const char* property, const char* value);
85 virtual status_t initCheck();
Eric Laurent3b73df72014-03-11 09:06:29 -070086 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -070087 uint32_t samplingRate,
88 audio_format_t format,
89 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -070090 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -070091 const audio_offload_info_t *offloadInfo);
Eric Laurente83b55d2014-11-14 10:06:21 -080092 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
93 audio_io_handle_t *output,
94 audio_session_t session,
95 audio_stream_type_t *stream,
96 uint32_t samplingRate,
97 audio_format_t format,
98 audio_channel_mask_t channelMask,
99 audio_output_flags_t flags,
100 const audio_offload_info_t *offloadInfo);
Eric Laurente552edb2014-03-10 17:42:56 -0700101 virtual status_t startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700102 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800103 audio_session_t session);
Eric Laurente552edb2014-03-10 17:42:56 -0700104 virtual status_t stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700105 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800106 audio_session_t session);
107 virtual void releaseOutput(audio_io_handle_t output,
108 audio_stream_type_t stream,
109 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800110 virtual status_t getInputForAttr(const audio_attributes_t *attr,
111 audio_io_handle_t *input,
112 audio_session_t session,
113 uint32_t samplingRate,
114 audio_format_t format,
115 audio_channel_mask_t channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800116 audio_input_flags_t flags,
117 input_type_t *inputType);
Eric Laurente552edb2014-03-10 17:42:56 -0700118
119 // indicates to the audio policy manager that the input starts being used.
Eric Laurent4dc68062014-07-28 17:26:49 -0700120 virtual status_t startInput(audio_io_handle_t input,
121 audio_session_t session);
Eric Laurente552edb2014-03-10 17:42:56 -0700122
123 // indicates to the audio policy manager that the input stops being used.
Eric Laurent4dc68062014-07-28 17:26:49 -0700124 virtual status_t stopInput(audio_io_handle_t input,
125 audio_session_t session);
126 virtual void releaseInput(audio_io_handle_t input,
127 audio_session_t session);
Eric Laurentd4692962014-05-05 18:13:44 -0700128 virtual void closeAllInputs();
Eric Laurent3b73df72014-03-11 09:06:29 -0700129 virtual void initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700130 int indexMin,
131 int indexMax);
Eric Laurent3b73df72014-03-11 09:06:29 -0700132 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700133 int index,
134 audio_devices_t device);
Eric Laurent3b73df72014-03-11 09:06:29 -0700135 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700136 int *index,
137 audio_devices_t device);
138
139 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700140 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700141 // return the strategy corresponding to the given audio attributes
142 virtual uint32_t getStrategyForAttr(const audio_attributes_t *attr);
Eric Laurente552edb2014-03-10 17:42:56 -0700143
144 // return the enabled output devices for the given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700145 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700146
147 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
148 virtual status_t registerEffect(const effect_descriptor_t *desc,
149 audio_io_handle_t io,
150 uint32_t strategy,
151 int session,
152 int id);
153 virtual status_t unregisterEffect(int id);
154 virtual status_t setEffectEnabled(int id, bool enabled);
155
Eric Laurent3b73df72014-03-11 09:06:29 -0700156 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700157 // return whether a stream is playing remotely, override to change the definition of
158 // local/remote playback, used for instance by notification manager to not make
159 // media players lose audio focus when not playing locally
Jean-Michel Trivi1767df72014-12-09 18:11:49 -0800160 // For the base implementation, "remotely" means playing during screen mirroring which
161 // uses an output for playback with a non-empty, non "0" address.
Eric Laurent3b73df72014-03-11 09:06:29 -0700162 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700163 virtual bool isSourceActive(audio_source_t source) const;
164
165 virtual status_t dump(int fd);
166
167 virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
168
Eric Laurent6a94d692014-05-20 11:18:06 -0700169 virtual status_t listAudioPorts(audio_port_role_t role,
170 audio_port_type_t type,
171 unsigned int *num_ports,
172 struct audio_port *ports,
173 unsigned int *generation);
174 virtual status_t getAudioPort(struct audio_port *port);
175 virtual status_t createAudioPatch(const struct audio_patch *patch,
176 audio_patch_handle_t *handle,
177 uid_t uid);
178 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
179 uid_t uid);
180 virtual status_t listAudioPatches(unsigned int *num_patches,
181 struct audio_patch *patches,
182 unsigned int *generation);
183 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
184 virtual void clearAudioPatches(uid_t uid);
185
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700186 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
187 audio_io_handle_t *ioHandle,
188 audio_devices_t *device);
189
190 virtual status_t releaseSoundTriggerSession(audio_session_t session);
191
Eric Laurent275e8e92014-11-30 15:14:47 -0800192 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes);
193 virtual status_t unregisterPolicyMixes(Vector<AudioMix> mixes);
194
Eric Laurente552edb2014-03-10 17:42:56 -0700195protected:
196
197 enum routing_strategy {
198 STRATEGY_MEDIA,
199 STRATEGY_PHONE,
200 STRATEGY_SONIFICATION,
201 STRATEGY_SONIFICATION_RESPECTFUL,
202 STRATEGY_DTMF,
203 STRATEGY_ENFORCED_AUDIBLE,
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700204 STRATEGY_TRANSMITTED_THROUGH_SPEAKER,
Eric Laurent223fd5c2014-11-11 13:43:36 -0800205 STRATEGY_ACCESSIBILITY,
206 STRATEGY_REROUTING,
Eric Laurente552edb2014-03-10 17:42:56 -0700207 NUM_STRATEGIES
208 };
209
210 // 4 points to define the volume attenuation curve, each characterized by the volume
211 // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
212 // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
213
214 enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
215
216 class VolumeCurvePoint
217 {
218 public:
219 int mIndex;
220 float mDBAttenuation;
221 };
222
223 // device categories used for volume curve management.
224 enum device_category {
225 DEVICE_CATEGORY_HEADSET,
226 DEVICE_CATEGORY_SPEAKER,
227 DEVICE_CATEGORY_EARPIECE,
Jon Eklundac29afa2014-07-28 16:06:06 -0500228 DEVICE_CATEGORY_EXT_MEDIA,
Eric Laurente552edb2014-03-10 17:42:56 -0700229 DEVICE_CATEGORY_CNT
230 };
231
Eric Laurent1afeecb2014-05-14 08:52:28 -0700232 class HwModule;
Eric Laurente552edb2014-03-10 17:42:56 -0700233
Eric Laurent1afeecb2014-05-14 08:52:28 -0700234 class AudioGain: public RefBase
235 {
Eric Laurente552edb2014-03-10 17:42:56 -0700236 public:
Eric Laurenta121f902014-06-03 13:32:54 -0700237 AudioGain(int index, bool useInChannelMask);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700238 virtual ~AudioGain() {}
Eric Laurente552edb2014-03-10 17:42:56 -0700239
Eric Laurent1afeecb2014-05-14 08:52:28 -0700240 void dump(int fd, int spaces, int index) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700241
Eric Laurenta121f902014-06-03 13:32:54 -0700242 void getDefaultConfig(struct audio_gain_config *config);
243 status_t checkConfig(const struct audio_gain_config *config);
244 int mIndex;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700245 struct audio_gain mGain;
Eric Laurenta121f902014-06-03 13:32:54 -0700246 bool mUseInChannelMask;
Eric Laurent951f4552014-05-20 10:48:17 -0700247 };
248
Eric Laurent1f2f2232014-06-02 12:01:23 -0700249 class AudioPort: public virtual RefBase
Eric Laurent951f4552014-05-20 10:48:17 -0700250 {
251 public:
Eric Laurent1afeecb2014-05-14 08:52:28 -0700252 AudioPort(const String8& name, audio_port_type_t type,
Eric Laurenta121f902014-06-03 13:32:54 -0700253 audio_port_role_t role, const sp<HwModule>& module);
Eric Laurent951f4552014-05-20 10:48:17 -0700254 virtual ~AudioPort() {}
255
Paul McLeane743a472015-01-28 11:07:31 -0800256 audio_port_handle_t getHandle() { return mId; }
257
258 void attach(const sp<HwModule>& module);
259 bool isAttached() { return mId != 0; }
260
Eric Laurent951f4552014-05-20 10:48:17 -0700261 virtual void toAudioPort(struct audio_port *port) const;
262
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700263 void importAudioPort(const sp<AudioPort> port);
264 void clearCapabilities();
265
Eric Laurent951f4552014-05-20 10:48:17 -0700266 void loadSamplingRates(char *name);
267 void loadFormats(char *name);
268 void loadOutChannels(char *name);
269 void loadInChannels(char *name);
270
Eric Laurent1afeecb2014-05-14 08:52:28 -0700271 audio_gain_mode_t loadGainMode(char *name);
Eric Laurenta121f902014-06-03 13:32:54 -0700272 void loadGain(cnode *root, int index);
Eric Laurent1db89b92015-01-27 18:21:09 -0800273 virtual void loadGains(cnode *root);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700274
Glenn Kastencbd48022014-07-24 13:46:44 -0700275 // searches for an exact match
276 status_t checkExactSamplingRate(uint32_t samplingRate) const;
277 // searches for a compatible match, and returns the best match via updatedSamplingRate
278 status_t checkCompatibleSamplingRate(uint32_t samplingRate,
279 uint32_t *updatedSamplingRate) const;
280 // searches for an exact match
281 status_t checkExactChannelMask(audio_channel_mask_t channelMask) const;
282 // searches for a compatible match, currently implemented for input channel masks only
283 status_t checkCompatibleChannelMask(audio_channel_mask_t channelMask) const;
Eric Laurenta121f902014-06-03 13:32:54 -0700284 status_t checkFormat(audio_format_t format) const;
285 status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
286
Eric Laurent1e693b52014-07-09 15:03:28 -0700287 uint32_t pickSamplingRate() const;
288 audio_channel_mask_t pickChannelMask() const;
289 audio_format_t pickFormat() const;
290
291 static const audio_format_t sPcmFormatCompareTable[];
292 static int compareFormats(audio_format_t format1, audio_format_t format2);
293
Eric Laurent1afeecb2014-05-14 08:52:28 -0700294 void dump(int fd, int spaces) const;
295
296 String8 mName;
297 audio_port_type_t mType;
298 audio_port_role_t mRole;
Eric Laurenta121f902014-06-03 13:32:54 -0700299 bool mUseInChannelMask;
Eric Laurent951f4552014-05-20 10:48:17 -0700300 // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
301 // indicates the supported parameters should be read from the output stream
302 // after it is opened for the first time
303 Vector <uint32_t> mSamplingRates; // supported sampling rates
304 Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
305 Vector <audio_format_t> mFormats; // supported audio formats
Eric Laurent1afeecb2014-05-14 08:52:28 -0700306 Vector < sp<AudioGain> > mGains; // gain controllers
Eric Laurent1f2f2232014-06-02 12:01:23 -0700307 sp<HwModule> mModule; // audio HW module exposing this I/O stream
Eric Laurent5dbe4712014-09-19 19:04:57 -0700308 uint32_t mFlags; // attribute flags (e.g primary output,
309 // direct output...).
Paul McLeane743a472015-01-28 11:07:31 -0800310
311 protected:
312 //TODO - clarify the role of mId in this case, both an "attached" indicator
313 // and a unique ID for identifying a port to the (upcoming) selection API,
314 // and its relationship to the mId in AudioOutputDescriptor and AudioInputDescriptor.
315 audio_port_handle_t mId;
Eric Laurent951f4552014-05-20 10:48:17 -0700316 };
317
Eric Laurent1f2f2232014-06-02 12:01:23 -0700318 class AudioPortConfig: public virtual RefBase
319 {
320 public:
321 AudioPortConfig();
322 virtual ~AudioPortConfig() {}
323
Eric Laurenta121f902014-06-03 13:32:54 -0700324 status_t applyAudioPortConfig(const struct audio_port_config *config,
325 struct audio_port_config *backupConfig = NULL);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700326 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
327 const struct audio_port_config *srcConfig = NULL) const = 0;
Marco Nelissen961ec212014-08-25 15:58:39 -0700328 virtual sp<AudioPort> getAudioPort() const = 0;
Eric Laurent1f2f2232014-06-02 12:01:23 -0700329 uint32_t mSamplingRate;
330 audio_format_t mFormat;
331 audio_channel_mask_t mChannelMask;
332 struct audio_gain_config mGain;
333 };
334
335
Eric Laurent6a94d692014-05-20 11:18:06 -0700336 class AudioPatch: public RefBase
337 {
338 public:
339 AudioPatch(audio_patch_handle_t handle,
340 const struct audio_patch *patch, uid_t uid) :
341 mHandle(handle), mPatch(*patch), mUid(uid), mAfPatchHandle(0) {}
342
Eric Laurent4d416952014-08-10 14:07:09 -0700343 status_t dump(int fd, int spaces, int index) const;
344
Eric Laurent6a94d692014-05-20 11:18:06 -0700345 audio_patch_handle_t mHandle;
346 struct audio_patch mPatch;
347 uid_t mUid;
348 audio_patch_handle_t mAfPatchHandle;
349 };
Eric Laurent951f4552014-05-20 10:48:17 -0700350
Eric Laurent1f2f2232014-06-02 12:01:23 -0700351 class DeviceDescriptor: public AudioPort, public AudioPortConfig
Eric Laurent951f4552014-05-20 10:48:17 -0700352 {
353 public:
Eric Laurent1f2f2232014-06-02 12:01:23 -0700354 DeviceDescriptor(const String8& name, audio_devices_t type);
Eric Laurent951f4552014-05-20 10:48:17 -0700355
Eric Laurent951f4552014-05-20 10:48:17 -0700356 virtual ~DeviceDescriptor() {}
357
358 bool equals(const sp<DeviceDescriptor>& other) const;
Eric Laurent1db89b92015-01-27 18:21:09 -0800359
360 // AudioPortConfig
361 virtual sp<AudioPort> getAudioPort() const { return (AudioPort*) this; }
Eric Laurent1f2f2232014-06-02 12:01:23 -0700362 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700363 const struct audio_port_config *srcConfig = NULL) const;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700364
Eric Laurent1db89b92015-01-27 18:21:09 -0800365 // AudioPort
366 virtual void loadGains(cnode *root);
Eric Laurent951f4552014-05-20 10:48:17 -0700367 virtual void toAudioPort(struct audio_port *port) const;
368
Eric Laurent1afeecb2014-05-14 08:52:28 -0700369 status_t dump(int fd, int spaces, int index) const;
Eric Laurent951f4552014-05-20 10:48:17 -0700370
371 audio_devices_t mDeviceType;
372 String8 mAddress;
Paul McLeane743a472015-01-28 11:07:31 -0800373
374 static String8 emptyNameStr;
Eric Laurent951f4552014-05-20 10:48:17 -0700375 };
376
377 class DeviceVector : public SortedVector< sp<DeviceDescriptor> >
378 {
379 public:
380 DeviceVector() : SortedVector(), mDeviceTypes(AUDIO_DEVICE_NONE) {}
381
382 ssize_t add(const sp<DeviceDescriptor>& item);
383 ssize_t remove(const sp<DeviceDescriptor>& item);
384 ssize_t indexOf(const sp<DeviceDescriptor>& item) const;
385
386 audio_devices_t types() const { return mDeviceTypes; }
387
388 void loadDevicesFromType(audio_devices_t types);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700389 void loadDevicesFromName(char *name, const DeviceVector& declaredDevices);
390
Eric Laurent951f4552014-05-20 10:48:17 -0700391 sp<DeviceDescriptor> getDevice(audio_devices_t type, String8 address) const;
392 DeviceVector getDevicesFromType(audio_devices_t types) const;
Eric Laurent6a94d692014-05-20 11:18:06 -0700393 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700394 sp<DeviceDescriptor> getDeviceFromName(const String8& name) const;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700395 DeviceVector getDevicesFromTypeAddr(audio_devices_t type, String8 address)
396 const;
Eric Laurent951f4552014-05-20 10:48:17 -0700397
398 private:
399 void refreshTypes();
400 audio_devices_t mDeviceTypes;
Eric Laurente552edb2014-03-10 17:42:56 -0700401 };
402
403 // the IOProfile class describes the capabilities of an output or input stream.
404 // It is currently assumed that all combination of listed parameters are supported.
405 // It is used by the policy manager to determine if an output or input is suitable for
406 // a given use case, open/close it accordingly and connect/disconnect audio tracks
407 // to/from it.
Eric Laurent951f4552014-05-20 10:48:17 -0700408 class IOProfile : public AudioPort
Eric Laurente552edb2014-03-10 17:42:56 -0700409 {
410 public:
Eric Laurent1f2f2232014-06-02 12:01:23 -0700411 IOProfile(const String8& name, audio_port_role_t role, const sp<HwModule>& module);
Eric Laurent951f4552014-05-20 10:48:17 -0700412 virtual ~IOProfile();
Eric Laurente552edb2014-03-10 17:42:56 -0700413
Glenn Kastencbd48022014-07-24 13:46:44 -0700414 // This method is used for both output and input.
415 // If parameter updatedSamplingRate is non-NULL, it is assigned the actual sample rate.
416 // For input, flags is interpreted as audio_input_flags_t.
417 // TODO: merge audio_output_flags_t and audio_input_flags_t.
Eric Laurente552edb2014-03-10 17:42:56 -0700418 bool isCompatibleProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -0800419 String8 address,
Eric Laurente552edb2014-03-10 17:42:56 -0700420 uint32_t samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700421 uint32_t *updatedSamplingRate,
Eric Laurente552edb2014-03-10 17:42:56 -0700422 audio_format_t format,
423 audio_channel_mask_t channelMask,
Eric Laurent5dbe4712014-09-19 19:04:57 -0700424 uint32_t flags) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700425
426 void dump(int fd);
Eric Laurentd4692962014-05-05 18:13:44 -0700427 void log();
Eric Laurente552edb2014-03-10 17:42:56 -0700428
Eric Laurent3a4311c2014-03-17 12:00:47 -0700429 DeviceVector mSupportedDevices; // supported devices
430 // (devices this output can be routed to)
Eric Laurente552edb2014-03-10 17:42:56 -0700431 };
432
Eric Laurent1e693b52014-07-09 15:03:28 -0700433 class HwModule : public RefBase
434 {
Eric Laurent1afeecb2014-05-14 08:52:28 -0700435 public:
436 HwModule(const char *name);
437 ~HwModule();
438
439 status_t loadOutput(cnode *root);
440 status_t loadInput(cnode *root);
441 status_t loadDevice(cnode *root);
442
Eric Laurent275e8e92014-11-30 15:14:47 -0800443 status_t addOutputProfile(String8 name, const audio_config_t *config,
444 audio_devices_t device, String8 address);
445 status_t removeOutputProfile(String8 name);
446 status_t addInputProfile(String8 name, const audio_config_t *config,
447 audio_devices_t device, String8 address);
448 status_t removeInputProfile(String8 name);
449
Eric Laurent1afeecb2014-05-14 08:52:28 -0700450 void dump(int fd);
451
Eric Laurenteb108a42014-06-06 14:56:52 -0700452 const char *const mName; // base name of the audio HW module (primary, a2dp ...)
453 uint32_t mHalVersion; // audio HAL API version
454 audio_module_handle_t mHandle;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700455 Vector < sp<IOProfile> > mOutputProfiles; // output profiles exposed by this module
456 Vector < sp<IOProfile> > mInputProfiles; // input profiles exposed by this module
457 DeviceVector mDeclaredDevices; // devices declared in audio_policy.conf
458
459 };
460
Eric Laurente552edb2014-03-10 17:42:56 -0700461 // default volume curve
Eric Laurente0720872014-03-11 09:30:41 -0700462 static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700463 // default volume curve for media strategy
Eric Laurente0720872014-03-11 09:30:41 -0700464 static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Jon Eklundac29afa2014-07-28 16:06:06 -0500465 // volume curve for non-media audio on ext media outputs (HDMI, Line, etc)
466 static const VolumeCurvePoint sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700467 // volume curve for media strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700468 static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT];
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -0700469 static const VolumeCurvePoint sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700470 // volume curve for sonification strategy on speakers
Eric Laurente0720872014-03-11 09:30:41 -0700471 static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT];
472 static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT];
473 static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT];
474 static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT];
475 static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT];
476 static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
477 static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT];
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700478 static const VolumeCurvePoint sLinearVolumeCurve[AudioPolicyManager::VOLCNT];
479 static const VolumeCurvePoint sSilentVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurent223fd5c2014-11-11 13:43:36 -0800480 static const VolumeCurvePoint sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT];
Eric Laurente552edb2014-03-10 17:42:56 -0700481 // default volume curves per stream and device category. See initializeVolumeCurves()
482 static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
483
484 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
485 // and keep track of the usage of this output by each audio stream type.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700486 class AudioOutputDescriptor: public AudioPortConfig
Eric Laurente552edb2014-03-10 17:42:56 -0700487 {
488 public:
Eric Laurent951f4552014-05-20 10:48:17 -0700489 AudioOutputDescriptor(const sp<IOProfile>& profile);
Eric Laurente552edb2014-03-10 17:42:56 -0700490
491 status_t dump(int fd);
492
493 audio_devices_t device() const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700494 void changeRefCount(audio_stream_type_t stream, int delta);
Eric Laurente552edb2014-03-10 17:42:56 -0700495
496 bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
497 audio_devices_t supportedDevices();
498 uint32_t latency();
Eric Laurent1f2f2232014-06-02 12:01:23 -0700499 bool sharesHwModuleWith(const sp<AudioOutputDescriptor> outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700500 bool isActive(uint32_t inPastMs = 0) const;
Eric Laurent3b73df72014-03-11 09:06:29 -0700501 bool isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700502 uint32_t inPastMs = 0,
503 nsecs_t sysTime = 0) const;
504 bool isStrategyActive(routing_strategy strategy,
505 uint32_t inPastMs = 0,
506 nsecs_t sysTime = 0) const;
507
Eric Laurent1f2f2232014-06-02 12:01:23 -0700508 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700509 const struct audio_port_config *srcConfig = NULL) const;
Marco Nelissen961ec212014-08-25 15:58:39 -0700510 virtual sp<AudioPort> getAudioPort() const { return mProfile; }
Eric Laurent951f4552014-05-20 10:48:17 -0700511 void toAudioPort(struct audio_port *port) const;
512
513 audio_port_handle_t mId;
514 audio_io_handle_t mIoHandle; // output handle
Eric Laurente552edb2014-03-10 17:42:56 -0700515 uint32_t mLatency; //
516 audio_output_flags_t mFlags; //
517 audio_devices_t mDevice; // current device this output is routed to
Eric Laurentc722f302014-12-10 11:21:49 -0800518 AudioMix *mPolicyMix; // non NULL when used by a dynamic policy
Eric Laurent951f4552014-05-20 10:48:17 -0700519 audio_patch_handle_t mPatchHandle;
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 uint32_t mRefCount[AUDIO_STREAM_CNT]; // number of streams of each type using this output
521 nsecs_t mStopTime[AUDIO_STREAM_CNT];
Eric Laurent1f2f2232014-06-02 12:01:23 -0700522 sp<AudioOutputDescriptor> mOutput1; // used by duplicated outputs: first output
523 sp<AudioOutputDescriptor> mOutput2; // used by duplicated outputs: second output
Eric Laurent3b73df72014-03-11 09:06:29 -0700524 float mCurVolume[AUDIO_STREAM_CNT]; // current stream volume
525 int mMuteCount[AUDIO_STREAM_CNT]; // mute request counter
Eric Laurent951f4552014-05-20 10:48:17 -0700526 const sp<IOProfile> mProfile; // I/O profile this output derives from
Eric Laurente552edb2014-03-10 17:42:56 -0700527 bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
528 // device selection. See checkDeviceMuteStrategies()
529 uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
530 };
531
532 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
533 // and keep track of the usage of this input.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700534 class AudioInputDescriptor: public AudioPortConfig
Eric Laurente552edb2014-03-10 17:42:56 -0700535 {
536 public:
Eric Laurent951f4552014-05-20 10:48:17 -0700537 AudioInputDescriptor(const sp<IOProfile>& profile);
Eric Laurente552edb2014-03-10 17:42:56 -0700538
539 status_t dump(int fd);
540
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700541 audio_port_handle_t mId;
542 audio_io_handle_t mIoHandle; // input handle
543 audio_devices_t mDevice; // current device this input is routed to
Eric Laurentc722f302014-12-10 11:21:49 -0800544 AudioMix *mPolicyMix; // non NULL when used by a dynamic policy
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700545 audio_patch_handle_t mPatchHandle;
546 uint32_t mRefCount; // number of AudioRecord clients using
547 // this input
548 uint32_t mOpenRefCount;
549 audio_source_t mInputSource; // input source selected by application
550 //(mediarecorder.h)
551 const sp<IOProfile> mProfile; // I/O profile this output derives from
Eric Laurentc722f302014-12-10 11:21:49 -0800552 SortedVector<audio_session_t> mSessions; // audio sessions attached to this input
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700553 bool mIsSoundTrigger; // used by a soundtrigger capture
Eric Laurent951f4552014-05-20 10:48:17 -0700554
Eric Laurent1f2f2232014-06-02 12:01:23 -0700555 virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
Eric Laurent6a94d692014-05-20 11:18:06 -0700556 const struct audio_port_config *srcConfig = NULL) const;
Marco Nelissen961ec212014-08-25 15:58:39 -0700557 virtual sp<AudioPort> getAudioPort() const { return mProfile; }
Eric Laurent951f4552014-05-20 10:48:17 -0700558 void toAudioPort(struct audio_port *port) const;
Eric Laurente552edb2014-03-10 17:42:56 -0700559 };
560
561 // stream descriptor used for volume control
562 class StreamDescriptor
563 {
564 public:
565 StreamDescriptor();
566
567 int getVolumeIndex(audio_devices_t device);
568 void dump(int fd);
569
570 int mIndexMin; // min volume index
571 int mIndexMax; // max volume index
572 KeyedVector<audio_devices_t, int> mIndexCur; // current volume index per device
573 bool mCanBeMuted; // true is the stream can be muted
574
575 const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
576 };
577
578 // stream descriptor used for volume control
Eric Laurent1f2f2232014-06-02 12:01:23 -0700579 class EffectDescriptor : public RefBase
Eric Laurente552edb2014-03-10 17:42:56 -0700580 {
581 public:
582
583 status_t dump(int fd);
584
585 int mIo; // io the effect is attached to
586 routing_strategy mStrategy; // routing strategy the effect is associated to
587 int mSession; // audio session the effect is on
588 effect_descriptor_t mDesc; // effect descriptor
589 bool mEnabled; // enabled state: CPU load being used or not
590 };
591
Eric Laurent1f2f2232014-06-02 12:01:23 -0700592 void addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc);
593 void addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700594
595 // return the strategy corresponding to a given stream type
Eric Laurent3b73df72014-03-11 09:06:29 -0700596 static routing_strategy getStrategy(audio_stream_type_t stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700597
598 // return appropriate device for streams handled by the specified strategy according to current
599 // phone state, connected devices...
600 // if fromCache is true, the device is returned from mDeviceForStrategy[],
601 // otherwise it is determine by current state
602 // (device connected,phone state, force use, a2dp output...)
603 // This allows to:
604 // 1 speed up process when the state is stable (when starting or stopping an output)
605 // 2 access to either current device selection (fromCache == true) or
606 // "future" device selection (fromCache == false) when called from a context
607 // where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
608 // before updateDevicesAndOutputs() is called.
609 virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
610 bool fromCache);
611
612 // change the route of the specified output. Returns the number of ms we have slept to
613 // allow new routing to take effect in certain cases.
Hochi Huange6b8b272015-01-13 20:27:13 +0800614 virtual uint32_t setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -0700615 audio_devices_t device,
616 bool force = false,
Eric Laurent6a94d692014-05-20 11:18:06 -0700617 int delayMs = 0,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700618 audio_patch_handle_t *patchHandle = NULL,
619 const char* address = NULL);
Eric Laurent951f4552014-05-20 10:48:17 -0700620 status_t resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -0700621 int delayMs = 0,
622 audio_patch_handle_t *patchHandle = NULL);
Eric Laurent951f4552014-05-20 10:48:17 -0700623 status_t setInputDevice(audio_io_handle_t input,
624 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -0700625 bool force = false,
626 audio_patch_handle_t *patchHandle = NULL);
627 status_t resetInputDevice(audio_io_handle_t input,
628 audio_patch_handle_t *patchHandle = NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700629
630 // select input device corresponding to requested audio source
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800631 virtual audio_devices_t getDeviceForInputSource(audio_source_t inputSource);
Eric Laurente552edb2014-03-10 17:42:56 -0700632
633 // return io handle of active input or 0 if no input is active
634 // Only considers inputs from physical devices (e.g. main mic, headset mic) when
635 // ignoreVirtualInputs is true.
636 audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
637
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700638 uint32_t activeInputsCount() const;
639
Eric Laurente552edb2014-03-10 17:42:56 -0700640 // initialize volume curves for each strategy and device category
641 void initializeVolumeCurves();
642
643 // compute the actual volume for a given stream according to the requested index and a particular
644 // device
Eric Laurent3b73df72014-03-11 09:06:29 -0700645 virtual float computeVolume(audio_stream_type_t stream, int index,
646 audio_io_handle_t output, audio_devices_t device);
Eric Laurente552edb2014-03-10 17:42:56 -0700647
648 // check that volume change is permitted, compute and send new volume to audio hardware
Hochi Huang327cb702014-09-21 09:47:31 +0800649 virtual status_t checkAndSetVolume(audio_stream_type_t stream, int index,
650 audio_io_handle_t output,
651 audio_devices_t device,
652 int delayMs = 0, bool force = false);
Eric Laurente552edb2014-03-10 17:42:56 -0700653
654 // apply all stream volumes to the specified output and device
655 void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
656
657 // Mute or unmute all streams handled by the specified strategy on the specified output
658 void setStrategyMute(routing_strategy strategy,
659 bool on,
660 audio_io_handle_t output,
661 int delayMs = 0,
662 audio_devices_t device = (audio_devices_t)0);
663
664 // Mute or unmute the stream on the specified output
Eric Laurent3b73df72014-03-11 09:06:29 -0700665 void setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700666 bool on,
667 audio_io_handle_t output,
668 int delayMs = 0,
669 audio_devices_t device = (audio_devices_t)0);
670
671 // handle special cases for sonification strategy while in call: mute streams or replace by
672 // a special tone in the device used for communication
Eric Laurent3b73df72014-03-11 09:06:29 -0700673 void handleIncallSonification(audio_stream_type_t stream, bool starting, bool stateChange);
Eric Laurente552edb2014-03-10 17:42:56 -0700674
675 // true if device is in a telephony or VoIP call
676 virtual bool isInCall();
677
678 // true if given state represents a device in a telephony or VoIP call
679 virtual bool isStateInCall(int state);
680
681 // when a device is connected, checks if an open output can be routed
682 // to this device. If none is open, tries to open one of the available outputs.
683 // Returns an output suitable to this device or 0.
684 // when a device is disconnected, checks if an output is not used any more and
685 // returns its handle if any.
686 // transfers the audio tracks and effects from one output thread to another accordingly.
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700687 status_t checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
Eric Laurent3b73df72014-03-11 09:06:29 -0700688 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700689 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -0700690 const String8 address);
Eric Laurente552edb2014-03-10 17:42:56 -0700691
Eric Laurentd4692962014-05-05 18:13:44 -0700692 status_t checkInputsForDevice(audio_devices_t device,
693 audio_policy_dev_state_t state,
694 SortedVector<audio_io_handle_t>& inputs,
695 const String8 address);
696
Eric Laurente552edb2014-03-10 17:42:56 -0700697 // close an output and its companion duplicating output.
698 void closeOutput(audio_io_handle_t output);
699
Eric Laurent05b90f82014-08-27 15:32:29 -0700700 // close an input.
701 void closeInput(audio_io_handle_t input);
702
Eric Laurente552edb2014-03-10 17:42:56 -0700703 // checks and if necessary changes outputs used for all strategies.
704 // must be called every time a condition that affects the output choice for a given strategy
705 // changes: connected device, phone state, force use...
706 // Must be called before updateDevicesAndOutputs()
707 void checkOutputForStrategy(routing_strategy strategy);
708
709 // Same as checkOutputForStrategy() but for a all strategies in order of priority
710 void checkOutputForAllStrategies();
711
712 // manages A2DP output suspend/restore according to phone state and BT SCO usage
713 void checkA2dpSuspend();
714
715 // returns the A2DP output handle if it is open or 0 otherwise
716 audio_io_handle_t getA2dpOutput();
717
718 // selects the most appropriate device on output for current state
719 // must be called every time a condition that affects the device choice for a given output is
720 // changed: connected device, phone state, force use, output start, output stop..
721 // see getDeviceForStrategy() for the use of fromCache parameter
Eric Laurent951f4552014-05-20 10:48:17 -0700722 audio_devices_t getNewOutputDevice(audio_io_handle_t output, bool fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -0700723
Eric Laurente552edb2014-03-10 17:42:56 -0700724 // updates cache of device used by all strategies (mDeviceForStrategy[])
725 // must be called every time a condition that affects the device choice for a given strategy is
726 // changed: connected device, phone state, force use...
727 // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
728 // Must be called after checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -0700729 void updateDevicesAndOutputs();
730
Eric Laurent951f4552014-05-20 10:48:17 -0700731 // selects the most appropriate device on input for current state
732 audio_devices_t getNewInputDevice(audio_io_handle_t input);
733
Eric Laurente552edb2014-03-10 17:42:56 -0700734 virtual uint32_t getMaxEffectsCpuLoad();
735 virtual uint32_t getMaxEffectsMemory();
736#ifdef AUDIO_POLICY_TEST
737 virtual bool threadLoop();
738 void exit();
739 int testOutputIndex(audio_io_handle_t output);
740#endif //AUDIO_POLICY_TEST
741
Eric Laurent1f2f2232014-06-02 12:01:23 -0700742 status_t setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled);
Eric Laurente552edb2014-03-10 17:42:56 -0700743
744 // returns the category the device belongs to with regard to volume curve management
745 static device_category getDeviceCategory(audio_devices_t device);
746
747 // extract one device relevant for volume control from multiple device selection
748 static audio_devices_t getDeviceForVolume(audio_devices_t device);
749
750 SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -0700751 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs);
Eric Laurente552edb2014-03-10 17:42:56 -0700752 bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
753 SortedVector<audio_io_handle_t>& outputs2);
754
755 // mute/unmute strategies using an incompatible device combination
756 // if muting, wait for the audio in pcm buffer to be drained before proceeding
757 // if unmuting, unmute only after the specified delay
758 // Returns the number of ms waited
Hochi Huang18f2f902014-12-03 15:23:36 +0800759 virtual uint32_t checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -0700760 audio_devices_t prevDevice,
761 uint32_t delayMs);
762
763 audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -0700764 audio_output_flags_t flags,
765 audio_format_t format);
Glenn Kastencbd48022014-07-24 13:46:44 -0700766 // samplingRate parameter is an in/out and so may be modified
Eric Laurent951f4552014-05-20 10:48:17 -0700767 sp<IOProfile> getInputProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -0800768 String8 address,
769 uint32_t& samplingRate,
770 audio_format_t format,
771 audio_channel_mask_t channelMask,
772 audio_input_flags_t flags);
Eric Laurent951f4552014-05-20 10:48:17 -0700773 sp<IOProfile> getProfileForDirectOutput(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700774 uint32_t samplingRate,
775 audio_format_t format,
776 audio_channel_mask_t channelMask,
777 audio_output_flags_t flags);
778
779 audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
780
781 bool isNonOffloadableEffectEnabled();
782
Hochi Huang045e7102014-12-10 22:03:37 +0800783 virtual status_t addAudioPatch(audio_patch_handle_t handle,
Eric Laurent6a94d692014-05-20 11:18:06 -0700784 const sp<AudioPatch>& patch);
Hochi Huang045e7102014-12-10 22:03:37 +0800785 virtual status_t removeAudioPatch(audio_patch_handle_t handle);
Eric Laurent6a94d692014-05-20 11:18:06 -0700786
Eric Laurent1f2f2232014-06-02 12:01:23 -0700787 sp<AudioOutputDescriptor> getOutputFromId(audio_port_handle_t id) const;
788 sp<AudioInputDescriptor> getInputFromId(audio_port_handle_t id) const;
789 sp<HwModule> getModuleForDevice(audio_devices_t device) const;
790 sp<HwModule> getModuleFromName(const char *name) const;
Eric Laurentc2730ba2014-07-20 15:47:07 -0700791 audio_devices_t availablePrimaryOutputDevices();
792 audio_devices_t availablePrimaryInputDevices();
793
794 void updateCallRouting(audio_devices_t rxDevice, int delayMs = 0);
795
Eric Laurente552edb2014-03-10 17:42:56 -0700796 //
797 // Audio policy configuration file parsing (audio_policy.conf)
798 //
799 static uint32_t stringToEnum(const struct StringToEnum *table,
800 size_t size,
801 const char *name);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700802 static const char *enumToString(const struct StringToEnum *table,
803 size_t size,
804 uint32_t value);
Eric Laurente552edb2014-03-10 17:42:56 -0700805 static bool stringToBool(const char *value);
Eric Laurent5dbe4712014-09-19 19:04:57 -0700806 static uint32_t parseOutputFlagNames(char *name);
807 static uint32_t parseInputFlagNames(char *name);
Eric Laurente552edb2014-03-10 17:42:56 -0700808 static audio_devices_t parseDeviceNames(char *name);
Eric Laurente552edb2014-03-10 17:42:56 -0700809 void loadHwModule(cnode *root);
810 void loadHwModules(cnode *root);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700811 void loadGlobalConfig(cnode *root, const sp<HwModule>& module);
Eric Laurente552edb2014-03-10 17:42:56 -0700812 status_t loadAudioPolicyConfig(const char *path);
813 void defaultAudioPolicyConfig(void);
814
815
Eric Laurent6a94d692014-05-20 11:18:06 -0700816 uid_t mUidCached;
Eric Laurente552edb2014-03-10 17:42:56 -0700817 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface
818 audio_io_handle_t mPrimaryOutput; // primary output handle
819 // list of descriptors for outputs currently opened
Eric Laurent1f2f2232014-06-02 12:01:23 -0700820 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700821 // copy of mOutputs before setDeviceConnectionState() opens new outputs
822 // reset to mOutputs when updateDevicesAndOutputs() is called.
Eric Laurent1f2f2232014-06-02 12:01:23 -0700823 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > mPreviousOutputs;
824 DefaultKeyedVector<audio_io_handle_t, sp<AudioInputDescriptor> > mInputs; // list of input descriptors
Eric Laurent1afeecb2014-05-14 08:52:28 -0700825 DeviceVector mAvailableOutputDevices; // all available output devices
826 DeviceVector mAvailableInputDevices; // all available input devices
Eric Laurente552edb2014-03-10 17:42:56 -0700827 int mPhoneState; // current phone state
Eric Laurent3b73df72014-03-11 09:06:29 -0700828 audio_policy_forced_cfg_t mForceUse[AUDIO_POLICY_FORCE_USE_CNT]; // current forced use configuration
Eric Laurente552edb2014-03-10 17:42:56 -0700829
Eric Laurent3b73df72014-03-11 09:06:29 -0700830 StreamDescriptor mStreams[AUDIO_STREAM_CNT]; // stream descriptors for volume control
Eric Laurente552edb2014-03-10 17:42:56 -0700831 bool mLimitRingtoneVolume; // limit ringtone volume to music volume if headset connected
832 audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
833 float mLastVoiceVolume; // last voice volume value sent to audio HAL
834
835 // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
836 static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
837 // Maximum memory allocated to audio effects in KB
838 static const uint32_t MAX_EFFECTS_MEMORY = 512;
839 uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
840 uint32_t mTotalEffectsMemory; // current memory used by effects
Eric Laurent1f2f2232014-06-02 12:01:23 -0700841 KeyedVector<int, sp<EffectDescriptor> > mEffects; // list of registered audio effects
Eric Laurente552edb2014-03-10 17:42:56 -0700842 bool mA2dpSuspended; // true if A2DP output is suspended
Eric Laurent3a4311c2014-03-17 12:00:47 -0700843 sp<DeviceDescriptor> mDefaultOutputDevice; // output device selected by default at boot time
Eric Laurente552edb2014-03-10 17:42:56 -0700844 bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
845 // to boost soft sounds, used to adjust volume curves accordingly
846
Eric Laurent1f2f2232014-06-02 12:01:23 -0700847 Vector < sp<HwModule> > mHwModules;
Paul McLeane743a472015-01-28 11:07:31 -0800848 static volatile int32_t mNextUniqueId;
Eric Laurent6a94d692014-05-20 11:18:06 -0700849 volatile int32_t mAudioPortGeneration;
850
851 DefaultKeyedVector<audio_patch_handle_t, sp<AudioPatch> > mAudioPatches;
Eric Laurente552edb2014-03-10 17:42:56 -0700852
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700853 DefaultKeyedVector<audio_session_t, audio_io_handle_t> mSoundTriggerSessions;
854
Eric Laurentc2730ba2014-07-20 15:47:07 -0700855 sp<AudioPatch> mCallTxPatch;
856 sp<AudioPatch> mCallRxPatch;
857
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700858 // for supporting "beacon" streams, i.e. streams that only play on speaker, and never
859 // when something other than STREAM_TTS (a.k.a. "Transmitted Through Speaker") is playing
860 enum {
861 STARTING_OUTPUT,
862 STARTING_BEACON,
863 STOPPING_OUTPUT,
864 STOPPING_BEACON
865 };
866 uint32_t mBeaconMuteRefCount; // ref count for stream that would mute beacon
867 uint32_t mBeaconPlayingRefCount;// ref count for the playing beacon streams
868 bool mBeaconMuted; // has STREAM_TTS been muted
869
Eric Laurent275e8e92014-11-30 15:14:47 -0800870 // custom mix entry in mPolicyMixes
871 class AudioPolicyMix : public RefBase {
872 public:
873 AudioPolicyMix() {}
874
875 AudioMix mMix; // Audio policy mix descriptor
876 sp<AudioOutputDescriptor> mOutput; // Corresponding output stream
877 };
878 DefaultKeyedVector<String8, sp<AudioPolicyMix> > mPolicyMixes; // list of registered mixes
879
880
Eric Laurente552edb2014-03-10 17:42:56 -0700881#ifdef AUDIO_POLICY_TEST
882 Mutex mLock;
883 Condition mWaitWorkCV;
884
885 int mCurOutput;
886 bool mDirectOutput;
887 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
888 int mTestInput;
889 uint32_t mTestDevice;
890 uint32_t mTestSamplingRate;
891 uint32_t mTestFormat;
892 uint32_t mTestChannels;
893 uint32_t mTestLatencyMs;
894#endif //AUDIO_POLICY_TEST
Eric Laurente552edb2014-03-10 17:42:56 -0700895 static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
896 int indexInUi);
Hochi Huange6b8b272015-01-13 20:27:13 +0800897 static bool isVirtualInputDevice(audio_devices_t device);
Paul McLeane743a472015-01-28 11:07:31 -0800898 static uint32_t nextUniqueId();
Hochi Huange6b8b272015-01-13 20:27:13 +0800899 uint32_t nextAudioPortGeneration();
Hochi Huang327cb702014-09-21 09:47:31 +0800900private:
Eric Laurente552edb2014-03-10 17:42:56 -0700901 // updates device caching and output for streams that can influence the
902 // routing of notifications
Eric Laurent3b73df72014-03-11 09:06:29 -0700903 void handleNotificationRoutingForStream(audio_stream_type_t stream);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700904 static bool deviceDistinguishesOnAddress(audio_devices_t device);
905 // find the outputs on a given output descriptor that have the given address.
906 // to be called on an AudioOutputDescriptor whose supported devices (as defined
907 // in mProfile->mSupportedDevices) matches the device whose address is to be matched.
908 // see deviceDistinguishesOnAddress(audio_devices_t) for whether the device type is one
909 // where addresses are used to distinguish between one connected device and another.
910 void findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
keunyoung3190e672014-12-30 13:00:52 -0800911 const audio_devices_t device /*in*/,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700912 const String8 address /*in*/,
913 SortedVector<audio_io_handle_t>& outputs /*out*/);
Eric Laurent6a94d692014-05-20 11:18:06 -0700914 uint32_t curAudioPortGeneration() const { return mAudioPortGeneration; }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700915 // internal method to return the output handle for the given device and format
916 audio_io_handle_t getOutputForDevice(
917 audio_devices_t device,
Eric Laurente83b55d2014-11-14 10:06:21 -0800918 audio_session_t session,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700919 audio_stream_type_t stream,
920 uint32_t samplingRate,
921 audio_format_t format,
922 audio_channel_mask_t channelMask,
923 audio_output_flags_t flags,
924 const audio_offload_info_t *offloadInfo);
925 // internal function to derive a stream type value from audio attributes
926 audio_stream_type_t streamTypefromAttributesInt(const audio_attributes_t *attr);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -0700927 // return true if any output is playing anything besides the stream to ignore
928 bool isAnyOutputActive(audio_stream_type_t streamToIgnore);
929 // event is one of STARTING_OUTPUT, STARTING_BEACON, STOPPING_OUTPUT, STOPPING_BEACON
930 // returns 0 if no mute/unmute event happened, the largest latency of the device where
931 // the mute/unmute happened
932 uint32_t handleEventForBeacon(int event);
933 uint32_t setBeaconMute(bool mute);
Eric Laurente83b55d2014-11-14 10:06:21 -0800934 bool isValidAttributes(const audio_attributes_t *paa);
Eric Laurentc73ca6e2014-12-12 14:34:22 -0800935
936 // select input device corresponding to requested audio source and return associated policy
937 // mix if any. Calls getDeviceForInputSource().
938 audio_devices_t getDeviceAndMixForInputSource(audio_source_t inputSource,
939 AudioMix **policyMix = NULL);
940
941 // Called by setDeviceConnectionState().
942 status_t setDeviceConnectionStateInt(audio_devices_t device,
943 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800944 const char *device_address,
945 const char *device_name);
Eric Laurenta1d525f2015-01-29 13:36:45 -0800946 sp<DeviceDescriptor> getDeviceDescriptor(const audio_devices_t device,
947 const char *device_address);
Eric Laurente552edb2014-03-10 17:42:56 -0700948};
949
950};