blob: 1b4796b142722aa6d1fbb9304b95eb076c24ccb8 [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
Eric Laurente0720872014-03-11 09:30:41 -070017#define LOG_TAG "AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27// A device mask for all audio input devices that are considered "virtual" when evaluating
28// active inputs in getActiveInput()
29#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL AUDIO_DEVICE_IN_REMOTE_SUBMIX
30// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
33
Eric Laurentd4692962014-05-05 18:13:44 -070034#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070035#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070036
Eric Laurente552edb2014-03-10 17:42:56 -070037#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070038#include <utils/Log.h>
39#include <hardware/audio.h>
40#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070041#include <media/AudioParameter.h>
Eric Laurentd4692962014-05-05 18:13:44 -070042#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070043#include "audio_policy_conf.h"
Eric Laurente552edb2014-03-10 17:42:56 -070044
Eric Laurent3b73df72014-03-11 09:06:29 -070045namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070046
47// ----------------------------------------------------------------------------
Eric Laurent3a4311c2014-03-17 12:00:47 -070048// Definitions for audio_policy.conf file parsing
49// ----------------------------------------------------------------------------
50
51struct StringToEnum {
52 const char *name;
53 uint32_t value;
54};
55
56#define STRING_TO_ENUM(string) { #string, string }
57#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
58
59const StringToEnum sDeviceNameToEnumTable[] = {
60 STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
61 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
62 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
63 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
64 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO),
65 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET),
66 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
67 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
68 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP),
69 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES),
70 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
71 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
72 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070073 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI),
Eric Laurent3a4311c2014-03-17 12:00:47 -070074 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
75 STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
76 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
77 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
78 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
79 STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
Eric Laurent1b776232014-05-19 17:26:41 -070080 STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX),
81 STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE),
82 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC),
83 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF),
84 STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM),
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
86 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
87 STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO),
88 STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
89 STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070090 STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI),
Eric Laurent3a4311c2014-03-17 12:00:47 -070091 STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
Eric Laurent1b776232014-05-19 17:26:41 -070092 STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
Eric Laurent3a4311c2014-03-17 12:00:47 -070093 STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
94 STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
95 STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
96 STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
97 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
Eric Laurentd4692962014-05-05 18:13:44 -070098 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
Eric Laurent1b776232014-05-19 17:26:41 -070099 STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
100 STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
101 STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE),
102 STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF),
Mike Lockwood41b0e242014-05-13 15:23:35 -0700103 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
Terry Heo7999a222014-06-27 15:23:36 +0900104 STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700105};
106
107const StringToEnum sFlagNameToEnumTable[] = {
108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
110 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
112 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
113 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
114};
115
116const StringToEnum sFormatNameToEnumTable[] = {
117 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
118 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
121 STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
122 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
123 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
124 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530125 STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN),
126 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
127 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR),
128 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP),
129 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
130 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE),
131 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC),
132 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD),
133 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
134 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700135 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Eric Laurentab5cdba2014-06-09 17:22:27 -0700136 STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1),
137 STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2),
138 STRING_TO_ENUM(AUDIO_FORMAT_OPUS),
139 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
140 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700141};
142
143const StringToEnum sOutChannelsNameToEnumTable[] = {
144 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
145 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
146 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
147 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
148};
149
150const StringToEnum sInChannelsNameToEnumTable[] = {
151 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
152 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
153 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
154};
155
Eric Laurent1afeecb2014-05-14 08:52:28 -0700156const StringToEnum sGainModeNameToEnumTable[] = {
157 STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT),
158 STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS),
159 STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP),
160};
161
Eric Laurent3a4311c2014-03-17 12:00:47 -0700162
163uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table,
164 size_t size,
165 const char *name)
166{
167 for (size_t i = 0; i < size; i++) {
168 if (strcmp(table[i].name, name) == 0) {
169 ALOGV("stringToEnum() found %s", table[i].name);
170 return table[i].value;
171 }
172 }
173 return 0;
174}
175
176const char *AudioPolicyManager::enumToString(const struct StringToEnum *table,
177 size_t size,
178 uint32_t value)
179{
180 for (size_t i = 0; i < size; i++) {
181 if (table[i].value == value) {
182 return table[i].name;
183 }
184 }
185 return "";
186}
187
188bool AudioPolicyManager::stringToBool(const char *value)
189{
190 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
191}
192
193
194// ----------------------------------------------------------------------------
Eric Laurente552edb2014-03-10 17:42:56 -0700195// AudioPolicyInterface implementation
196// ----------------------------------------------------------------------------
197
198
Eric Laurente0720872014-03-11 09:30:41 -0700199status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700200 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700201 const char *device_address)
202{
Eric Laurent3a4311c2014-03-17 12:00:47 -0700203 String8 address = String8(device_address);
Eric Laurente552edb2014-03-10 17:42:56 -0700204
205 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
206
207 // connect/disconnect only 1 device at a time
208 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
209
Eric Laurente552edb2014-03-10 17:42:56 -0700210 // handle output devices
211 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700212 SortedVector <audio_io_handle_t> outputs;
213
Eric Laurent1afeecb2014-05-14 08:52:28 -0700214 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
215 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700216 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
217
Eric Laurente552edb2014-03-10 17:42:56 -0700218 // save a copy of the opened output descriptors before any output is opened or closed
219 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
220 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700221 switch (state)
222 {
223 // handle output device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700224 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
Eric Laurent3a4311c2014-03-17 12:00:47 -0700225 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700226 ALOGW("setDeviceConnectionState() device already connected: %x", device);
227 return INVALID_OPERATION;
228 }
229 ALOGV("setDeviceConnectionState() connecting device %x", device);
230
Eric Laurent3a4311c2014-03-17 12:00:47 -0700231 if (checkOutputsForDevice(device, state, outputs, address) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -0700232 return INVALID_OPERATION;
233 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700234 // outputs should never be empty here
235 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
236 "checkOutputsForDevice() returned no outputs but status OK");
Eric Laurentd4692962014-05-05 18:13:44 -0700237 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
Eric Laurente552edb2014-03-10 17:42:56 -0700238 outputs.size());
239 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700240 index = mAvailableOutputDevices.add(devDesc);
241 if (index >= 0) {
242 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent1f2f2232014-06-02 12:01:23 -0700243 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700244 ALOG_ASSERT(module != NULL, "setDeviceConnectionState():"
245 "could not find HW module for device %08x", device);
246 mAvailableOutputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700247 } else {
248 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700249 }
250
251 break;
252 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700253 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700254 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700255 ALOGW("setDeviceConnectionState() device not connected: %x", device);
256 return INVALID_OPERATION;
257 }
258
259 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
260 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700261 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700262
Eric Laurent3a4311c2014-03-17 12:00:47 -0700263 checkOutputsForDevice(device, state, outputs, address);
Eric Laurente552edb2014-03-10 17:42:56 -0700264 // not currently handling multiple simultaneous submixes: ignoring remote submix
265 // case and address
266 } break;
267
268 default:
269 ALOGE("setDeviceConnectionState() invalid state: %x", state);
270 return BAD_VALUE;
271 }
272
Eric Laurent3a4311c2014-03-17 12:00:47 -0700273 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
274 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700275 checkA2dpSuspend();
276 checkOutputForAllStrategies();
277 // outputs must be closed after checkOutputForAllStrategies() is executed
278 if (!outputs.isEmpty()) {
279 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700280 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700281 // close unused outputs after device disconnection or direct outputs that have been
282 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700283 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700284 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
285 (desc->mDirectOpenCount == 0))) {
286 closeOutput(outputs[i]);
287 }
288 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700289 // check again after closing A2DP output to reset mA2dpSuspended if needed
290 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700291 }
292
293 updateDevicesAndOutputs();
294 for (size_t i = 0; i < mOutputs.size(); i++) {
295 // do not force device change on duplicated output because if device is 0, it will
296 // also force a device 0 for the two outputs it is duplicated to which may override
297 // a valid device selection on those outputs.
298 setOutputDevice(mOutputs.keyAt(i),
Eric Laurent1c333e22014-05-20 10:48:17 -0700299 getNewOutputDevice(mOutputs.keyAt(i), true /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700300 !mOutputs.valueAt(i)->isDuplicated(),
301 0);
302 }
303
Eric Laurent72aa32f2014-05-30 18:51:48 -0700304 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700305 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700306 } // end if is output device
307
Eric Laurente552edb2014-03-10 17:42:56 -0700308 // handle input devices
309 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700310 SortedVector <audio_io_handle_t> inputs;
311
Eric Laurent1afeecb2014-05-14 08:52:28 -0700312 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
313 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700314 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700315 switch (state)
316 {
317 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700318 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700319 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700320 ALOGW("setDeviceConnectionState() device already connected: %d", device);
321 return INVALID_OPERATION;
322 }
Eric Laurent1f2f2232014-06-02 12:01:23 -0700323 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700324 if (module == NULL) {
325 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
326 device);
327 return INVALID_OPERATION;
328 }
Eric Laurentd4692962014-05-05 18:13:44 -0700329 if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) {
330 return INVALID_OPERATION;
331 }
332
Eric Laurent3a4311c2014-03-17 12:00:47 -0700333 index = mAvailableInputDevices.add(devDesc);
334 if (index >= 0) {
335 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700336 mAvailableInputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700337 } else {
338 return NO_MEMORY;
339 }
Eric Laurentd4692962014-05-05 18:13:44 -0700340 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700341
342 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700343 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700344 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700345 ALOGW("setDeviceConnectionState() device not connected: %d", device);
346 return INVALID_OPERATION;
347 }
Eric Laurentd4692962014-05-05 18:13:44 -0700348 checkInputsForDevice(device, state, inputs, address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700349 mAvailableInputDevices.remove(devDesc);
Eric Laurentd4692962014-05-05 18:13:44 -0700350 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700351
352 default:
353 ALOGE("setDeviceConnectionState() invalid state: %x", state);
354 return BAD_VALUE;
355 }
356
Eric Laurentd4692962014-05-05 18:13:44 -0700357 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700358
Eric Laurentb52c1522014-05-20 11:27:36 -0700359 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700360 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700361 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700362
363 ALOGW("setDeviceConnectionState() invalid device: %x", device);
364 return BAD_VALUE;
365}
366
Eric Laurente0720872014-03-11 09:30:41 -0700367audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700368 const char *device_address)
369{
Eric Laurent3b73df72014-03-11 09:06:29 -0700370 audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700371 String8 address = String8(device_address);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700372 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
373 devDesc->mAddress = String8(device_address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700374 ssize_t index;
375 DeviceVector *deviceVector;
376
Eric Laurente552edb2014-03-10 17:42:56 -0700377 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700378 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700379 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700380 deviceVector = &mAvailableInputDevices;
381 } else {
382 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
383 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700384 }
385
Eric Laurent3a4311c2014-03-17 12:00:47 -0700386 index = deviceVector->indexOf(devDesc);
387 if (index >= 0) {
388 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
389 } else {
390 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
391 }
Eric Laurente552edb2014-03-10 17:42:56 -0700392}
393
Eric Laurente0720872014-03-11 09:30:41 -0700394void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700395{
396 ALOGV("setPhoneState() state %d", state);
397 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Eric Laurent3b73df72014-03-11 09:06:29 -0700398 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700399 ALOGW("setPhoneState() invalid state %d", state);
400 return;
401 }
402
403 if (state == mPhoneState ) {
404 ALOGW("setPhoneState() setting same state %d", state);
405 return;
406 }
407
408 // if leaving call state, handle special case of active streams
409 // pertaining to sonification strategy see handleIncallSonification()
410 if (isInCall()) {
411 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700412 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
413 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700414 }
415 }
416
417 // store previous phone state for management of sonification strategy below
418 int oldState = mPhoneState;
419 mPhoneState = state;
420 bool force = false;
421
422 // are we entering or starting a call
423 if (!isStateInCall(oldState) && isStateInCall(state)) {
424 ALOGV(" Entering call in setPhoneState()");
425 // force routing command to audio hardware when starting a call
426 // even if no device change is needed
427 force = true;
428 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
429 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
430 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
431 }
432 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
433 ALOGV(" Exiting call in setPhoneState()");
434 // force routing command to audio hardware when exiting a call
435 // even if no device change is needed
436 force = true;
437 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
438 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
439 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
440 }
441 } else if (isStateInCall(state) && (state != oldState)) {
442 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
443 // force routing command to audio hardware when switching between telephony and VoIP
444 // even if no device change is needed
445 force = true;
446 }
447
448 // check for device and output changes triggered by new phone state
Eric Laurent1c333e22014-05-20 10:48:17 -0700449 newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700450 checkA2dpSuspend();
451 checkOutputForAllStrategies();
452 updateDevicesAndOutputs();
453
Eric Laurent1f2f2232014-06-02 12:01:23 -0700454 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700455
456 // force routing command to audio hardware when ending call
457 // even if no device change is needed
458 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
459 newDevice = hwOutputDesc->device();
460 }
461
462 int delayMs = 0;
463 if (isStateInCall(state)) {
464 nsecs_t sysTime = systemTime();
465 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700466 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700467 // mute media and sonification strategies and delay device switch by the largest
468 // latency of any output where either strategy is active.
469 // This avoid sending the ring tone or music tail into the earpiece or headset.
470 if ((desc->isStrategyActive(STRATEGY_MEDIA,
471 SONIFICATION_HEADSET_MUSIC_DELAY,
472 sysTime) ||
473 desc->isStrategyActive(STRATEGY_SONIFICATION,
474 SONIFICATION_HEADSET_MUSIC_DELAY,
475 sysTime)) &&
476 (delayMs < (int)desc->mLatency*2)) {
477 delayMs = desc->mLatency*2;
478 }
479 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
480 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
481 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
482 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
483 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
484 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
485 }
486 }
487
488 // change routing is necessary
489 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
490
491 // if entering in call state, handle special case of active streams
492 // pertaining to sonification strategy see handleIncallSonification()
493 if (isStateInCall(state)) {
494 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700495 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
496 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700497 }
498 }
499
500 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700501 if (state == AUDIO_MODE_RINGTONE &&
502 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700503 mLimitRingtoneVolume = true;
504 } else {
505 mLimitRingtoneVolume = false;
506 }
507}
508
Eric Laurente0720872014-03-11 09:30:41 -0700509void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700510 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700511{
512 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
513
514 bool forceVolumeReeval = false;
515 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700516 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
517 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
518 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700519 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
520 return;
521 }
522 forceVolumeReeval = true;
523 mForceUse[usage] = config;
524 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700525 case AUDIO_POLICY_FORCE_FOR_MEDIA:
526 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
527 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
528 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
529 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
Jungshik Jang0c943092014-07-08 22:11:24 +0900530 config != AUDIO_POLICY_FORCE_NO_BT_A2DP) {
Eric Laurente552edb2014-03-10 17:42:56 -0700531 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
532 return;
533 }
534 mForceUse[usage] = config;
535 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700536 case AUDIO_POLICY_FORCE_FOR_RECORD:
537 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
538 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700539 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
540 return;
541 }
542 mForceUse[usage] = config;
543 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700544 case AUDIO_POLICY_FORCE_FOR_DOCK:
545 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
546 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
547 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
548 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
549 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700550 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
551 }
552 forceVolumeReeval = true;
553 mForceUse[usage] = config;
554 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700555 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
556 if (config != AUDIO_POLICY_FORCE_NONE &&
557 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700558 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
559 }
560 forceVolumeReeval = true;
561 mForceUse[usage] = config;
562 break;
563 default:
564 ALOGW("setForceUse() invalid usage %d", usage);
565 break;
566 }
567
568 // check for device and output changes triggered by new force usage
569 checkA2dpSuspend();
570 checkOutputForAllStrategies();
571 updateDevicesAndOutputs();
572 for (size_t i = 0; i < mOutputs.size(); i++) {
573 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700574 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700575 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
576 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
577 applyStreamVolumes(output, newDevice, 0, true);
578 }
579 }
580
581 audio_io_handle_t activeInput = getActiveInput();
582 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700583 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700584 }
585
586}
587
Eric Laurente0720872014-03-11 09:30:41 -0700588audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700589{
590 return mForceUse[usage];
591}
592
Eric Laurente0720872014-03-11 09:30:41 -0700593void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700594{
595 ALOGV("setSystemProperty() property %s, value %s", property, value);
596}
597
598// Find a direct output profile compatible with the parameters passed, even if the input flags do
599// not explicitly request a direct output
Eric Laurent1c333e22014-05-20 10:48:17 -0700600sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700601 audio_devices_t device,
602 uint32_t samplingRate,
603 audio_format_t format,
604 audio_channel_mask_t channelMask,
605 audio_output_flags_t flags)
606{
607 for (size_t i = 0; i < mHwModules.size(); i++) {
608 if (mHwModules[i]->mHandle == 0) {
609 continue;
610 }
611 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700612 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent3a4311c2014-03-17 12:00:47 -0700613 bool found = false;
Eric Laurente552edb2014-03-10 17:42:56 -0700614 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
615 if (profile->isCompatibleProfile(device, samplingRate, format,
616 channelMask,
617 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700618 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700619 }
620 } else {
621 if (profile->isCompatibleProfile(device, samplingRate, format,
622 channelMask,
623 AUDIO_OUTPUT_FLAG_DIRECT)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700624 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700625 }
626 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700627 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
628 return profile;
629 }
Eric Laurente552edb2014-03-10 17:42:56 -0700630 }
631 }
632 return 0;
633}
634
Eric Laurente0720872014-03-11 09:30:41 -0700635audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700636 uint32_t samplingRate,
637 audio_format_t format,
638 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700639 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700640 const audio_offload_info_t *offloadInfo)
641{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700642
Eric Laurent3b73df72014-03-11 09:06:29 -0700643 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700644 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
645 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
646 device, stream, samplingRate, format, channelMask, flags);
647
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700648 return getOutputForDevice(device, stream, samplingRate,format, channelMask, flags,
649 offloadInfo);
650}
651
652audio_io_handle_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
653 uint32_t samplingRate,
654 audio_format_t format,
655 audio_channel_mask_t channelMask,
656 audio_output_flags_t flags,
657 const audio_offload_info_t *offloadInfo)
658{
659 if (attr == NULL) {
660 ALOGE("getOutputForAttr() called with NULL audio attributes");
661 return 0;
662 }
663 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s",
664 attr->usage, attr->content_type, attr->tags);
665
666 // TODO this is where filtering for custom policies (rerouting, dynamic sources) will go
667 routing_strategy strategy = (routing_strategy) getStrategyForAttr(attr);
668 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
669 ALOGV("getOutputForAttr() device %d, samplingRate %d, format %x, channelMask %x, flags %x",
670 device, samplingRate, format, channelMask, flags);
671
672 audio_stream_type_t stream = streamTypefromAttributesInt(attr);
673 return getOutputForDevice(device, stream, samplingRate, format, channelMask, flags,
674 offloadInfo);
675}
676
677audio_io_handle_t AudioPolicyManager::getOutputForDevice(
678 audio_devices_t device,
679 audio_stream_type_t stream,
680 uint32_t samplingRate,
681 audio_format_t format,
682 audio_channel_mask_t channelMask,
683 audio_output_flags_t flags,
684 const audio_offload_info_t *offloadInfo)
685{
686 audio_io_handle_t output = 0;
687 uint32_t latency = 0;
688
Eric Laurente552edb2014-03-10 17:42:56 -0700689#ifdef AUDIO_POLICY_TEST
690 if (mCurOutput != 0) {
691 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
692 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
693
694 if (mTestOutputs[mCurOutput] == 0) {
695 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700696 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700697 outputDesc->mDevice = mTestDevice;
698 outputDesc->mSamplingRate = mTestSamplingRate;
699 outputDesc->mFormat = mTestFormat;
700 outputDesc->mChannelMask = mTestChannels;
701 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700702 outputDesc->mFlags =
703 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700704 outputDesc->mRefCount[stream] = 0;
705 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
706 &outputDesc->mSamplingRate,
707 &outputDesc->mFormat,
708 &outputDesc->mChannelMask,
709 &outputDesc->mLatency,
710 outputDesc->mFlags,
711 offloadInfo);
712 if (mTestOutputs[mCurOutput]) {
713 AudioParameter outputCmd = AudioParameter();
714 outputCmd.addInt(String8("set_id"),mCurOutput);
715 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
716 addOutput(mTestOutputs[mCurOutput], outputDesc);
717 }
718 }
719 return mTestOutputs[mCurOutput];
720 }
721#endif //AUDIO_POLICY_TEST
722
723 // open a direct output if required by specified parameters
724 //force direct flag if offload flag is set: offloading implies a direct output stream
725 // and all common behaviors are driven by checking only the direct flag
726 // this should normally be set appropriately in the policy configuration file
727 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700728 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700729 }
730
731 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
732 // creating an offloaded track and tearing it down immediately after start when audioflinger
733 // detects there is an active non offloadable effect.
734 // FIXME: We should check the audio session here but we do not have it in this context.
735 // This may prevent offloading in rare situations where effects are left active by apps
736 // in the background.
Eric Laurent1c333e22014-05-20 10:48:17 -0700737 sp<IOProfile> profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700738 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
739 !isNonOffloadableEffectEnabled()) {
740 profile = getProfileForDirectOutput(device,
741 samplingRate,
742 format,
743 channelMask,
744 (audio_output_flags_t)flags);
745 }
746
Eric Laurent1c333e22014-05-20 10:48:17 -0700747 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700748 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700749
750 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700751 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700752 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
753 outputDesc = desc;
754 // reuse direct output if currently open and configured with same parameters
755 if ((samplingRate == outputDesc->mSamplingRate) &&
756 (format == outputDesc->mFormat) &&
757 (channelMask == outputDesc->mChannelMask)) {
758 outputDesc->mDirectOpenCount++;
759 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
760 return mOutputs.keyAt(i);
761 }
762 }
763 }
764 // close direct output if currently open and configured with different parameters
765 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700766 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700767 }
768 outputDesc = new AudioOutputDescriptor(profile);
769 outputDesc->mDevice = device;
770 outputDesc->mSamplingRate = samplingRate;
771 outputDesc->mFormat = format;
772 outputDesc->mChannelMask = channelMask;
773 outputDesc->mLatency = 0;
774 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
775 outputDesc->mRefCount[stream] = 0;
776 outputDesc->mStopTime[stream] = 0;
777 outputDesc->mDirectOpenCount = 1;
778 output = mpClientInterface->openOutput(profile->mModule->mHandle,
779 &outputDesc->mDevice,
780 &outputDesc->mSamplingRate,
781 &outputDesc->mFormat,
782 &outputDesc->mChannelMask,
783 &outputDesc->mLatency,
784 outputDesc->mFlags,
785 offloadInfo);
786
787 // only accept an output with the requested parameters
788 if (output == 0 ||
789 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
790 (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
791 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
792 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
793 "format %d %d, channelMask %04x %04x", output, samplingRate,
794 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
795 outputDesc->mChannelMask);
796 if (output != 0) {
797 mpClientInterface->closeOutput(output);
798 }
Eric Laurente552edb2014-03-10 17:42:56 -0700799 return 0;
800 }
801 audio_io_handle_t srcOutput = getOutputForEffect();
802 addOutput(output, outputDesc);
803 audio_io_handle_t dstOutput = getOutputForEffect();
804 if (dstOutput == output) {
805 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
806 }
807 mPreviousOutputs = mOutputs;
808 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700809 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700810 return output;
811 }
812
813 // ignoring channel mask due to downmix capability in mixer
814
815 // open a non direct output
816
817 // for non direct outputs, only PCM is supported
818 if (audio_is_linear_pcm(format)) {
819 // get which output is suitable for the specified stream. The actual
820 // routing change will happen when startOutput() will be called
821 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
822
823 output = selectOutput(outputs, flags);
824 }
825 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
826 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
827
828 ALOGV("getOutput() returns output %d", output);
829
830 return output;
831}
832
Eric Laurente0720872014-03-11 09:30:41 -0700833audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3b73df72014-03-11 09:06:29 -0700834 audio_output_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -0700835{
836 // select one output among several that provide a path to a particular device or set of
837 // devices (the list was previously build by getOutputsForDevice()).
838 // The priority is as follows:
839 // 1: the output with the highest number of requested policy flags
840 // 2: the primary output
841 // 3: the first output in the list
842
843 if (outputs.size() == 0) {
844 return 0;
845 }
846 if (outputs.size() == 1) {
847 return outputs[0];
848 }
849
850 int maxCommonFlags = 0;
851 audio_io_handle_t outputFlags = 0;
852 audio_io_handle_t outputPrimary = 0;
853
854 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700855 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700856 if (!outputDesc->isDuplicated()) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700857 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700858 if (commonFlags > maxCommonFlags) {
859 outputFlags = outputs[i];
860 maxCommonFlags = commonFlags;
861 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
862 }
863 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
864 outputPrimary = outputs[i];
865 }
866 }
867 }
868
869 if (outputFlags != 0) {
870 return outputFlags;
871 }
872 if (outputPrimary != 0) {
873 return outputPrimary;
874 }
875
876 return outputs[0];
877}
878
Eric Laurente0720872014-03-11 09:30:41 -0700879status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700880 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700881 int session)
882{
883 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
884 ssize_t index = mOutputs.indexOfKey(output);
885 if (index < 0) {
886 ALOGW("startOutput() unknown output %d", output);
887 return BAD_VALUE;
888 }
889
Eric Laurent1f2f2232014-06-02 12:01:23 -0700890 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700891
892 // increment usage count for this stream on the requested output:
893 // NOTE that the usage count is the same for duplicated output and hardware output which is
894 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
895 outputDesc->changeRefCount(stream, 1);
896
897 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700898 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700899 routing_strategy strategy = getStrategy(stream);
900 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
901 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
902 uint32_t waitMs = 0;
903 bool force = false;
904 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700905 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700906 if (desc != outputDesc) {
907 // force a device change if any other output is managed by the same hw
908 // module and has a current device selection that differs from selected device.
909 // In this case, the audio HAL must receive the new device selection so that it can
910 // change the device currently selected by the other active output.
911 if (outputDesc->sharesHwModuleWith(desc) &&
912 desc->device() != newDevice) {
913 force = true;
914 }
915 // wait for audio on other active outputs to be presented when starting
916 // a notification so that audio focus effect can propagate.
917 uint32_t latency = desc->latency();
918 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
919 waitMs = latency;
920 }
921 }
922 }
923 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
924
925 // handle special case for sonification while in call
926 if (isInCall()) {
927 handleIncallSonification(stream, true, false);
928 }
929
930 // apply volume rules for current stream and device if necessary
931 checkAndSetVolume(stream,
932 mStreams[stream].getVolumeIndex(newDevice),
933 output,
934 newDevice);
935
936 // update the outputs if starting an output with a stream that can affect notification
937 // routing
938 handleNotificationRoutingForStream(stream);
939 if (waitMs > muteWaitMs) {
940 usleep((waitMs - muteWaitMs) * 2 * 1000);
941 }
942 }
943 return NO_ERROR;
944}
945
946
Eric Laurente0720872014-03-11 09:30:41 -0700947status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700948 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700949 int session)
950{
951 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
952 ssize_t index = mOutputs.indexOfKey(output);
953 if (index < 0) {
954 ALOGW("stopOutput() unknown output %d", output);
955 return BAD_VALUE;
956 }
957
Eric Laurent1f2f2232014-06-02 12:01:23 -0700958 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700959
960 // handle special case for sonification while in call
961 if (isInCall()) {
962 handleIncallSonification(stream, false, false);
963 }
964
965 if (outputDesc->mRefCount[stream] > 0) {
966 // decrement usage count of this stream on the output
967 outputDesc->changeRefCount(stream, -1);
968 // store time at which the stream was stopped - see isStreamActive()
969 if (outputDesc->mRefCount[stream] == 0) {
970 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -0700971 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700972 // delay the device switch by twice the latency because stopOutput() is executed when
973 // the track stop() command is received and at that time the audio track buffer can
974 // still contain data that needs to be drained. The latency only covers the audio HAL
975 // and kernel buffers. Also the latency does not always include additional delay in the
976 // audio path (audio DSP, CODEC ...)
977 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
978
979 // force restoring the device selection on other active outputs if it differs from the
980 // one being selected for this output
981 for (size_t i = 0; i < mOutputs.size(); i++) {
982 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700983 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700984 if (curOutput != output &&
985 desc->isActive() &&
986 outputDesc->sharesHwModuleWith(desc) &&
987 (newDevice != desc->device())) {
988 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -0700989 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700990 true,
991 outputDesc->mLatency*2);
992 }
993 }
994 // update the outputs if stopping one with a stream that can affect notification routing
995 handleNotificationRoutingForStream(stream);
996 }
997 return NO_ERROR;
998 } else {
999 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1000 return INVALID_OPERATION;
1001 }
1002}
1003
Eric Laurente0720872014-03-11 09:30:41 -07001004void AudioPolicyManager::releaseOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07001005{
1006 ALOGV("releaseOutput() %d", output);
1007 ssize_t index = mOutputs.indexOfKey(output);
1008 if (index < 0) {
1009 ALOGW("releaseOutput() releasing unknown output %d", output);
1010 return;
1011 }
1012
1013#ifdef AUDIO_POLICY_TEST
1014 int testIndex = testOutputIndex(output);
1015 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001016 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001017 if (outputDesc->isActive()) {
1018 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001019 mOutputs.removeItem(output);
1020 mTestOutputs[testIndex] = 0;
1021 }
1022 return;
1023 }
1024#endif //AUDIO_POLICY_TEST
1025
Eric Laurent1f2f2232014-06-02 12:01:23 -07001026 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001027 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001028 if (desc->mDirectOpenCount <= 0) {
1029 ALOGW("releaseOutput() invalid open count %d for output %d",
1030 desc->mDirectOpenCount, output);
1031 return;
1032 }
1033 if (--desc->mDirectOpenCount == 0) {
1034 closeOutput(output);
1035 // If effects where present on the output, audioflinger moved them to the primary
1036 // output by default: move them back to the appropriate output.
1037 audio_io_handle_t dstOutput = getOutputForEffect();
1038 if (dstOutput != mPrimaryOutput) {
1039 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1040 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001041 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001042 }
1043 }
1044}
1045
1046
Eric Laurente0720872014-03-11 09:30:41 -07001047audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource,
Eric Laurente552edb2014-03-10 17:42:56 -07001048 uint32_t samplingRate,
1049 audio_format_t format,
1050 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -07001051 audio_in_acoustics_t acoustics)
Eric Laurente552edb2014-03-10 17:42:56 -07001052{
1053 audio_io_handle_t input = 0;
1054 audio_devices_t device = getDeviceForInputSource(inputSource);
1055
1056 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
1057 inputSource, samplingRate, format, channelMask, acoustics);
1058
1059 if (device == AUDIO_DEVICE_NONE) {
1060 ALOGW("getInput() could not find device for inputSource %d", inputSource);
1061 return 0;
1062 }
1063
1064 // adapt channel selection to input source
1065 switch(inputSource) {
1066 case AUDIO_SOURCE_VOICE_UPLINK:
1067 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1068 break;
1069 case AUDIO_SOURCE_VOICE_DOWNLINK:
1070 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1071 break;
1072 case AUDIO_SOURCE_VOICE_CALL:
1073 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1074 break;
1075 default:
1076 break;
1077 }
1078
Eric Laurent1c333e22014-05-20 10:48:17 -07001079 sp<IOProfile> profile = getInputProfile(device,
Eric Laurente552edb2014-03-10 17:42:56 -07001080 samplingRate,
1081 format,
1082 channelMask);
Eric Laurent1c333e22014-05-20 10:48:17 -07001083 if (profile == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001084 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d, "
1085 "channelMask %04x",
1086 device, samplingRate, format, channelMask);
1087 return 0;
1088 }
1089
1090 if (profile->mModule->mHandle == 0) {
1091 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
1092 return 0;
1093 }
1094
Eric Laurent1f2f2232014-06-02 12:01:23 -07001095 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07001096
1097 inputDesc->mInputSource = inputSource;
1098 inputDesc->mDevice = device;
1099 inputDesc->mSamplingRate = samplingRate;
1100 inputDesc->mFormat = format;
1101 inputDesc->mChannelMask = channelMask;
1102 inputDesc->mRefCount = 0;
1103 input = mpClientInterface->openInput(profile->mModule->mHandle,
1104 &inputDesc->mDevice,
1105 &inputDesc->mSamplingRate,
1106 &inputDesc->mFormat,
1107 &inputDesc->mChannelMask);
1108
1109 // only accept input with the exact requested set of parameters
1110 if (input == 0 ||
1111 (samplingRate != inputDesc->mSamplingRate) ||
1112 (format != inputDesc->mFormat) ||
1113 (channelMask != inputDesc->mChannelMask)) {
1114 ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask %x",
1115 samplingRate, format, channelMask);
1116 if (input != 0) {
1117 mpClientInterface->closeInput(input);
1118 }
Eric Laurente552edb2014-03-10 17:42:56 -07001119 return 0;
1120 }
Eric Laurentd4692962014-05-05 18:13:44 -07001121 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001122 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001123 return input;
1124}
1125
Eric Laurente0720872014-03-11 09:30:41 -07001126status_t AudioPolicyManager::startInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001127{
1128 ALOGV("startInput() input %d", input);
1129 ssize_t index = mInputs.indexOfKey(input);
1130 if (index < 0) {
1131 ALOGW("startInput() unknown input %d", input);
1132 return BAD_VALUE;
1133 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001134 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001135
1136#ifdef AUDIO_POLICY_TEST
1137 if (mTestInput == 0)
1138#endif //AUDIO_POLICY_TEST
1139 {
1140 // refuse 2 active AudioRecord clients at the same time except if the active input
1141 // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
1142 audio_io_handle_t activeInput = getActiveInput();
1143 if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001144 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001145 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1146 ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1147 stopInput(activeInput);
1148 releaseInput(activeInput);
1149 } else {
1150 ALOGW("startInput() input %d failed: other input already started", input);
1151 return INVALID_OPERATION;
1152 }
1153 }
1154 }
1155
Eric Laurent1c333e22014-05-20 10:48:17 -07001156 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001157
1158 // automatically enable the remote submix output when input is started
1159 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1160 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001161 AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001162 }
1163
Eric Laurente552edb2014-03-10 17:42:56 -07001164 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1165
Eric Laurente552edb2014-03-10 17:42:56 -07001166 inputDesc->mRefCount = 1;
1167 return NO_ERROR;
1168}
1169
Eric Laurente0720872014-03-11 09:30:41 -07001170status_t AudioPolicyManager::stopInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001171{
1172 ALOGV("stopInput() input %d", input);
1173 ssize_t index = mInputs.indexOfKey(input);
1174 if (index < 0) {
1175 ALOGW("stopInput() unknown input %d", input);
1176 return BAD_VALUE;
1177 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001178 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001179
1180 if (inputDesc->mRefCount == 0) {
1181 ALOGW("stopInput() input %d already stopped", input);
1182 return INVALID_OPERATION;
1183 } else {
1184 // automatically disable the remote submix output when input is stopped
1185 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1186 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001187 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001188 }
1189
Eric Laurent1c333e22014-05-20 10:48:17 -07001190 resetInputDevice(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001191 inputDesc->mRefCount = 0;
1192 return NO_ERROR;
1193 }
1194}
1195
Eric Laurente0720872014-03-11 09:30:41 -07001196void AudioPolicyManager::releaseInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001197{
1198 ALOGV("releaseInput() %d", input);
1199 ssize_t index = mInputs.indexOfKey(input);
1200 if (index < 0) {
1201 ALOGW("releaseInput() releasing unknown input %d", input);
1202 return;
1203 }
1204 mpClientInterface->closeInput(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001205 mInputs.removeItem(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07001206 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001207 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001208 ALOGV("releaseInput() exit");
1209}
1210
Eric Laurentd4692962014-05-05 18:13:44 -07001211void AudioPolicyManager::closeAllInputs() {
1212 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1213 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1214 }
1215 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001216 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07001217}
1218
Eric Laurente0720872014-03-11 09:30:41 -07001219void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001220 int indexMin,
1221 int indexMax)
1222{
1223 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1224 if (indexMin < 0 || indexMin >= indexMax) {
1225 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1226 return;
1227 }
1228 mStreams[stream].mIndexMin = indexMin;
1229 mStreams[stream].mIndexMax = indexMax;
1230}
1231
Eric Laurente0720872014-03-11 09:30:41 -07001232status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001233 int index,
1234 audio_devices_t device)
1235{
1236
1237 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1238 return BAD_VALUE;
1239 }
1240 if (!audio_is_output_device(device)) {
1241 return BAD_VALUE;
1242 }
1243
1244 // Force max volume if stream cannot be muted
1245 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1246
1247 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1248 stream, device, index);
1249
1250 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1251 // clear all device specific values
1252 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1253 mStreams[stream].mIndexCur.clear();
1254 }
1255 mStreams[stream].mIndexCur.add(device, index);
1256
1257 // compute and apply stream volume on all outputs according to connected device
1258 status_t status = NO_ERROR;
1259 for (size_t i = 0; i < mOutputs.size(); i++) {
1260 audio_devices_t curDevice =
1261 getDeviceForVolume(mOutputs.valueAt(i)->device());
1262 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1263 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1264 if (volStatus != NO_ERROR) {
1265 status = volStatus;
1266 }
1267 }
1268 }
1269 return status;
1270}
1271
Eric Laurente0720872014-03-11 09:30:41 -07001272status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001273 int *index,
1274 audio_devices_t device)
1275{
1276 if (index == NULL) {
1277 return BAD_VALUE;
1278 }
1279 if (!audio_is_output_device(device)) {
1280 return BAD_VALUE;
1281 }
1282 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1283 // the strategy the stream belongs to.
1284 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1285 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1286 }
1287 device = getDeviceForVolume(device);
1288
1289 *index = mStreams[stream].getVolumeIndex(device);
1290 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1291 return NO_ERROR;
1292}
1293
Eric Laurente0720872014-03-11 09:30:41 -07001294audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001295 const SortedVector<audio_io_handle_t>& outputs)
1296{
1297 // select one output among several suitable for global effects.
1298 // The priority is as follows:
1299 // 1: An offloaded output. If the effect ends up not being offloadable,
1300 // AudioFlinger will invalidate the track and the offloaded output
1301 // will be closed causing the effect to be moved to a PCM output.
1302 // 2: A deep buffer output
1303 // 3: the first output in the list
1304
1305 if (outputs.size() == 0) {
1306 return 0;
1307 }
1308
1309 audio_io_handle_t outputOffloaded = 0;
1310 audio_io_handle_t outputDeepBuffer = 0;
1311
1312 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001313 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001314 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001315 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1316 outputOffloaded = outputs[i];
1317 }
1318 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1319 outputDeepBuffer = outputs[i];
1320 }
1321 }
1322
1323 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1324 outputOffloaded, outputDeepBuffer);
1325 if (outputOffloaded != 0) {
1326 return outputOffloaded;
1327 }
1328 if (outputDeepBuffer != 0) {
1329 return outputDeepBuffer;
1330 }
1331
1332 return outputs[0];
1333}
1334
Eric Laurente0720872014-03-11 09:30:41 -07001335audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001336{
1337 // apply simple rule where global effects are attached to the same output as MUSIC streams
1338
Eric Laurent3b73df72014-03-11 09:06:29 -07001339 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001340 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1341 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1342
1343 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1344 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1345 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1346
1347 return output;
1348}
1349
Eric Laurente0720872014-03-11 09:30:41 -07001350status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001351 audio_io_handle_t io,
1352 uint32_t strategy,
1353 int session,
1354 int id)
1355{
1356 ssize_t index = mOutputs.indexOfKey(io);
1357 if (index < 0) {
1358 index = mInputs.indexOfKey(io);
1359 if (index < 0) {
1360 ALOGW("registerEffect() unknown io %d", io);
1361 return INVALID_OPERATION;
1362 }
1363 }
1364
1365 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1366 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1367 desc->name, desc->memoryUsage);
1368 return INVALID_OPERATION;
1369 }
1370 mTotalEffectsMemory += desc->memoryUsage;
1371 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1372 desc->name, io, strategy, session, id);
1373 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1374
Eric Laurent1f2f2232014-06-02 12:01:23 -07001375 sp<EffectDescriptor> effectDesc = new EffectDescriptor();
1376 memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t));
1377 effectDesc->mIo = io;
1378 effectDesc->mStrategy = (routing_strategy)strategy;
1379 effectDesc->mSession = session;
1380 effectDesc->mEnabled = false;
Eric Laurente552edb2014-03-10 17:42:56 -07001381
Eric Laurent1f2f2232014-06-02 12:01:23 -07001382 mEffects.add(id, effectDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001383
1384 return NO_ERROR;
1385}
1386
Eric Laurente0720872014-03-11 09:30:41 -07001387status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001388{
1389 ssize_t index = mEffects.indexOfKey(id);
1390 if (index < 0) {
1391 ALOGW("unregisterEffect() unknown effect ID %d", id);
1392 return INVALID_OPERATION;
1393 }
1394
Eric Laurent1f2f2232014-06-02 12:01:23 -07001395 sp<EffectDescriptor> effectDesc = mEffects.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001396
Eric Laurent1f2f2232014-06-02 12:01:23 -07001397 setEffectEnabled(effectDesc, false);
Eric Laurente552edb2014-03-10 17:42:56 -07001398
Eric Laurent1f2f2232014-06-02 12:01:23 -07001399 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
Eric Laurente552edb2014-03-10 17:42:56 -07001400 ALOGW("unregisterEffect() memory %d too big for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001401 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1402 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
Eric Laurente552edb2014-03-10 17:42:56 -07001403 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001404 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
Eric Laurente552edb2014-03-10 17:42:56 -07001405 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001406 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
Eric Laurente552edb2014-03-10 17:42:56 -07001407
1408 mEffects.removeItem(id);
Eric Laurente552edb2014-03-10 17:42:56 -07001409
1410 return NO_ERROR;
1411}
1412
Eric Laurente0720872014-03-11 09:30:41 -07001413status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001414{
1415 ssize_t index = mEffects.indexOfKey(id);
1416 if (index < 0) {
1417 ALOGW("unregisterEffect() unknown effect ID %d", id);
1418 return INVALID_OPERATION;
1419 }
1420
1421 return setEffectEnabled(mEffects.valueAt(index), enabled);
1422}
1423
Eric Laurent1f2f2232014-06-02 12:01:23 -07001424status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001425{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001426 if (enabled == effectDesc->mEnabled) {
Eric Laurente552edb2014-03-10 17:42:56 -07001427 ALOGV("setEffectEnabled(%s) effect already %s",
1428 enabled?"true":"false", enabled?"enabled":"disabled");
1429 return INVALID_OPERATION;
1430 }
1431
1432 if (enabled) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001433 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001434 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001435 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
Eric Laurente552edb2014-03-10 17:42:56 -07001436 return INVALID_OPERATION;
1437 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001438 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001439 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1440 } else {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001441 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
Eric Laurente552edb2014-03-10 17:42:56 -07001442 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001443 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1444 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001445 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001446 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001447 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1448 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001449 effectDesc->mEnabled = enabled;
Eric Laurente552edb2014-03-10 17:42:56 -07001450 return NO_ERROR;
1451}
1452
Eric Laurente0720872014-03-11 09:30:41 -07001453bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001454{
1455 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001456 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
1457 if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) &&
1458 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001459 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001460 effectDesc->mDesc.name, effectDesc->mSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001461 return true;
1462 }
1463 }
1464 return false;
1465}
1466
Eric Laurente0720872014-03-11 09:30:41 -07001467bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001468{
1469 nsecs_t sysTime = systemTime();
1470 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001471 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001472 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001473 return true;
1474 }
1475 }
1476 return false;
1477}
1478
Eric Laurente0720872014-03-11 09:30:41 -07001479bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001480 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001481{
1482 nsecs_t sysTime = systemTime();
1483 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001484 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001485 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001486 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001487 return true;
1488 }
1489 }
1490 return false;
1491}
1492
Eric Laurente0720872014-03-11 09:30:41 -07001493bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001494{
1495 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001496 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001497 if ((inputDescriptor->mInputSource == (int)source ||
Eric Laurent3b73df72014-03-11 09:06:29 -07001498 (source == AUDIO_SOURCE_VOICE_RECOGNITION &&
Eric Laurente552edb2014-03-10 17:42:56 -07001499 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1500 && (inputDescriptor->mRefCount > 0)) {
1501 return true;
1502 }
1503 }
1504 return false;
1505}
1506
1507
Eric Laurente0720872014-03-11 09:30:41 -07001508status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001509{
1510 const size_t SIZE = 256;
1511 char buffer[SIZE];
1512 String8 result;
1513
1514 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1515 result.append(buffer);
1516
1517 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1518 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001519 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1520 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001521 snprintf(buffer, SIZE, " Force use for communications %d\n",
1522 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07001523 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001524 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07001525 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001526 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07001527 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001528 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07001529 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001530 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07001531 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001532
Eric Laurent3a4311c2014-03-17 12:00:47 -07001533 snprintf(buffer, SIZE, " Available output devices:\n");
1534 result.append(buffer);
1535 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07001536 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001537 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001538 }
1539 snprintf(buffer, SIZE, "\n Available input devices:\n");
1540 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07001541 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001542 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001543 }
Eric Laurente552edb2014-03-10 17:42:56 -07001544
1545 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
1546 write(fd, buffer, strlen(buffer));
1547 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001548 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07001549 write(fd, buffer, strlen(buffer));
1550 mHwModules[i]->dump(fd);
1551 }
1552
1553 snprintf(buffer, SIZE, "\nOutputs dump:\n");
1554 write(fd, buffer, strlen(buffer));
1555 for (size_t i = 0; i < mOutputs.size(); i++) {
1556 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1557 write(fd, buffer, strlen(buffer));
1558 mOutputs.valueAt(i)->dump(fd);
1559 }
1560
1561 snprintf(buffer, SIZE, "\nInputs dump:\n");
1562 write(fd, buffer, strlen(buffer));
1563 for (size_t i = 0; i < mInputs.size(); i++) {
1564 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1565 write(fd, buffer, strlen(buffer));
1566 mInputs.valueAt(i)->dump(fd);
1567 }
1568
1569 snprintf(buffer, SIZE, "\nStreams dump:\n");
1570 write(fd, buffer, strlen(buffer));
1571 snprintf(buffer, SIZE,
1572 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1573 write(fd, buffer, strlen(buffer));
Mark Salyzynbeb9e302014-06-18 16:33:15 -07001574 for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001575 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07001576 write(fd, buffer, strlen(buffer));
1577 mStreams[i].dump(fd);
1578 }
1579
1580 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1581 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1582 write(fd, buffer, strlen(buffer));
1583
1584 snprintf(buffer, SIZE, "Registered effects:\n");
1585 write(fd, buffer, strlen(buffer));
1586 for (size_t i = 0; i < mEffects.size(); i++) {
1587 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1588 write(fd, buffer, strlen(buffer));
1589 mEffects.valueAt(i)->dump(fd);
1590 }
1591
1592
1593 return NO_ERROR;
1594}
1595
1596// This function checks for the parameters which can be offloaded.
1597// This can be enhanced depending on the capability of the DSP and policy
1598// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001599bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001600{
1601 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001602 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001603 offloadInfo.sample_rate, offloadInfo.channel_mask,
1604 offloadInfo.format,
1605 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1606 offloadInfo.has_video);
1607
1608 // Check if offload has been disabled
1609 char propValue[PROPERTY_VALUE_MAX];
1610 if (property_get("audio.offload.disable", propValue, "0")) {
1611 if (atoi(propValue) != 0) {
1612 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1613 return false;
1614 }
1615 }
1616
1617 // Check if stream type is music, then only allow offload as of now.
1618 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1619 {
1620 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1621 return false;
1622 }
1623
1624 //TODO: enable audio offloading with video when ready
1625 if (offloadInfo.has_video)
1626 {
1627 ALOGV("isOffloadSupported: has_video == true, returning false");
1628 return false;
1629 }
1630
1631 //If duration is less than minimum value defined in property, return false
1632 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1633 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1634 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1635 return false;
1636 }
1637 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1638 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1639 return false;
1640 }
1641
1642 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1643 // creating an offloaded track and tearing it down immediately after start when audioflinger
1644 // detects there is an active non offloadable effect.
1645 // FIXME: We should check the audio session here but we do not have it in this context.
1646 // This may prevent offloading in rare situations where effects are left active by apps
1647 // in the background.
1648 if (isNonOffloadableEffectEnabled()) {
1649 return false;
1650 }
1651
1652 // See if there is a profile to support this.
1653 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001654 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001655 offloadInfo.sample_rate,
1656 offloadInfo.format,
1657 offloadInfo.channel_mask,
1658 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001659 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1660 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001661}
1662
Eric Laurent6a94d692014-05-20 11:18:06 -07001663status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1664 audio_port_type_t type,
1665 unsigned int *num_ports,
1666 struct audio_port *ports,
1667 unsigned int *generation)
1668{
1669 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1670 generation == NULL) {
1671 return BAD_VALUE;
1672 }
1673 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1674 if (ports == NULL) {
1675 *num_ports = 0;
1676 }
1677
1678 size_t portsWritten = 0;
1679 size_t portsMax = *num_ports;
1680 *num_ports = 0;
1681 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1682 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1683 for (size_t i = 0;
1684 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
1685 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
1686 }
1687 *num_ports += mAvailableOutputDevices.size();
1688 }
1689 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1690 for (size_t i = 0;
1691 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
1692 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
1693 }
1694 *num_ports += mAvailableInputDevices.size();
1695 }
1696 }
1697 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
1698 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1699 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
1700 mInputs[i]->toAudioPort(&ports[portsWritten++]);
1701 }
1702 *num_ports += mInputs.size();
1703 }
1704 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07001705 size_t numOutputs = 0;
1706 for (size_t i = 0; i < mOutputs.size(); i++) {
1707 if (!mOutputs[i]->isDuplicated()) {
1708 numOutputs++;
1709 if (portsWritten < portsMax) {
1710 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
1711 }
1712 }
Eric Laurent6a94d692014-05-20 11:18:06 -07001713 }
Eric Laurent84c70242014-06-23 08:46:27 -07001714 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07001715 }
1716 }
1717 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07001718 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07001719 return NO_ERROR;
1720}
1721
1722status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
1723{
1724 return NO_ERROR;
1725}
1726
Eric Laurent1f2f2232014-06-02 12:01:23 -07001727sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07001728 audio_port_handle_t id) const
1729{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001730 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07001731 for (size_t i = 0; i < mOutputs.size(); i++) {
1732 outputDesc = mOutputs.valueAt(i);
1733 if (outputDesc->mId == id) {
1734 break;
1735 }
1736 }
1737 return outputDesc;
1738}
1739
Eric Laurent1f2f2232014-06-02 12:01:23 -07001740sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07001741 audio_port_handle_t id) const
1742{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001743 sp<AudioInputDescriptor> inputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07001744 for (size_t i = 0; i < mInputs.size(); i++) {
1745 inputDesc = mInputs.valueAt(i);
1746 if (inputDesc->mId == id) {
1747 break;
1748 }
1749 }
1750 return inputDesc;
1751}
1752
Eric Laurent1f2f2232014-06-02 12:01:23 -07001753sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice(
1754 audio_devices_t device) const
Eric Laurent6a94d692014-05-20 11:18:06 -07001755{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001756 sp <HwModule> module;
1757
Eric Laurent6a94d692014-05-20 11:18:06 -07001758 for (size_t i = 0; i < mHwModules.size(); i++) {
1759 if (mHwModules[i]->mHandle == 0) {
1760 continue;
1761 }
1762 if (audio_is_output_device(device)) {
1763 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
1764 {
1765 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
1766 return mHwModules[i];
1767 }
1768 }
1769 } else {
1770 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1771 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
1772 device & ~AUDIO_DEVICE_BIT_IN) {
1773 return mHwModules[i];
1774 }
1775 }
1776 }
1777 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001778 return module;
Eric Laurent6a94d692014-05-20 11:18:06 -07001779}
1780
Eric Laurent1f2f2232014-06-02 12:01:23 -07001781sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const
Eric Laurent1afeecb2014-05-14 08:52:28 -07001782{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001783 sp <HwModule> module;
1784
Eric Laurent1afeecb2014-05-14 08:52:28 -07001785 for (size_t i = 0; i < mHwModules.size(); i++)
1786 {
1787 if (strcmp(mHwModules[i]->mName, name) == 0) {
1788 return mHwModules[i];
1789 }
1790 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001791 return module;
Eric Laurent1afeecb2014-05-14 08:52:28 -07001792}
1793
1794
Eric Laurent6a94d692014-05-20 11:18:06 -07001795status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
1796 audio_patch_handle_t *handle,
1797 uid_t uid)
1798{
1799 ALOGV("createAudioPatch()");
1800
1801 if (handle == NULL || patch == NULL) {
1802 return BAD_VALUE;
1803 }
1804 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
1805
1806 if (patch->num_sources > 1 || patch->num_sinks > 1) {
1807 return INVALID_OPERATION;
1808 }
1809 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE ||
1810 patch->sinks[0].role != AUDIO_PORT_ROLE_SINK) {
1811 return INVALID_OPERATION;
1812 }
1813
1814 sp<AudioPatch> patchDesc;
1815 ssize_t index = mAudioPatches.indexOfKey(*handle);
1816
1817 ALOGV("createAudioPatch sink id %d role %d type %d", patch->sinks[0].id, patch->sinks[0].role,
1818 patch->sinks[0].type);
1819 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
1820 patch->sources[0].role,
1821 patch->sources[0].type);
1822
1823 if (index >= 0) {
1824 patchDesc = mAudioPatches.valueAt(index);
1825 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
1826 mUidCached, patchDesc->mUid, uid);
1827 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
1828 return INVALID_OPERATION;
1829 }
1830 } else {
1831 *handle = 0;
1832 }
1833
1834 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
1835 // TODO add support for mix to mix connection
1836 if (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) {
1837 ALOGV("createAudioPatch() source mix sink not device");
1838 return BAD_VALUE;
1839 }
1840 // output mix to output device connection
Eric Laurent1f2f2232014-06-02 12:01:23 -07001841 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001842 if (outputDesc == NULL) {
1843 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
1844 return BAD_VALUE;
1845 }
Eric Laurent84c70242014-06-23 08:46:27 -07001846 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
1847 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07001848 if (patchDesc != 0) {
1849 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
1850 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
1851 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
1852 return BAD_VALUE;
1853 }
1854 }
1855 sp<DeviceDescriptor> devDesc =
1856 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1857 if (devDesc == 0) {
1858 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[0].id);
1859 return BAD_VALUE;
1860 }
1861
Eric Laurent84c70242014-06-23 08:46:27 -07001862 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent6a94d692014-05-20 11:18:06 -07001863 patch->sources[0].sample_rate,
1864 patch->sources[0].format,
1865 patch->sources[0].channel_mask,
1866 AUDIO_OUTPUT_FLAG_NONE)) {
1867 return INVALID_OPERATION;
1868 }
1869 // TODO: reconfigure output format and channels here
1870 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent84c70242014-06-23 08:46:27 -07001871 devDesc->mDeviceType, outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07001872 setOutputDevice(outputDesc->mIoHandle,
Eric Laurent84c70242014-06-23 08:46:27 -07001873 devDesc->mDeviceType,
Eric Laurent6a94d692014-05-20 11:18:06 -07001874 true,
1875 0,
1876 handle);
1877 index = mAudioPatches.indexOfKey(*handle);
1878 if (index >= 0) {
1879 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1880 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
1881 }
1882 patchDesc = mAudioPatches.valueAt(index);
1883 patchDesc->mUid = uid;
1884 ALOGV("createAudioPatch() success");
1885 } else {
1886 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
1887 return INVALID_OPERATION;
1888 }
1889 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1890 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
1891 // input device to input mix connection
Eric Laurent1f2f2232014-06-02 12:01:23 -07001892 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001893 if (inputDesc == NULL) {
1894 return BAD_VALUE;
1895 }
1896 if (patchDesc != 0) {
1897 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1898 return BAD_VALUE;
1899 }
1900 }
1901 sp<DeviceDescriptor> devDesc =
1902 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1903 if (devDesc == 0) {
1904 return BAD_VALUE;
1905 }
1906
Eric Laurent84c70242014-06-23 08:46:27 -07001907 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent6a94d692014-05-20 11:18:06 -07001908 patch->sinks[0].sample_rate,
1909 patch->sinks[0].format,
1910 patch->sinks[0].channel_mask,
1911 AUDIO_OUTPUT_FLAG_NONE)) {
1912 return INVALID_OPERATION;
1913 }
1914 // TODO: reconfigure output format and channels here
1915 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent84c70242014-06-23 08:46:27 -07001916 devDesc->mDeviceType, inputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07001917 setInputDevice(inputDesc->mIoHandle,
Eric Laurent84c70242014-06-23 08:46:27 -07001918 devDesc->mDeviceType,
Eric Laurent6a94d692014-05-20 11:18:06 -07001919 true,
1920 handle);
1921 index = mAudioPatches.indexOfKey(*handle);
1922 if (index >= 0) {
1923 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1924 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
1925 }
1926 patchDesc = mAudioPatches.valueAt(index);
1927 patchDesc->mUid = uid;
1928 ALOGV("createAudioPatch() success");
1929 } else {
1930 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
1931 return INVALID_OPERATION;
1932 }
1933 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1934 // device to device connection
1935 if (patchDesc != 0) {
1936 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id &&
1937 patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1938 return BAD_VALUE;
1939 }
1940 }
1941
1942 sp<DeviceDescriptor> srcDeviceDesc =
1943 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1944 sp<DeviceDescriptor> sinkDeviceDesc =
1945 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1946 if (srcDeviceDesc == 0 || sinkDeviceDesc == 0) {
1947 return BAD_VALUE;
1948 }
1949 //update source and sink with our own data as the data passed in the patch may
1950 // be incomplete.
1951 struct audio_patch newPatch = *patch;
1952 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
1953 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[0], &patch->sinks[0]);
1954
1955 // TODO: add support for devices on different HW modules
1956 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
1957 return INVALID_OPERATION;
1958 }
1959 // TODO: check from routing capabilities in config file and other conflicting patches
1960
1961 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
1962 if (index >= 0) {
1963 afPatchHandle = patchDesc->mAfPatchHandle;
1964 }
1965
1966 status_t status = mpClientInterface->createAudioPatch(&newPatch,
1967 &afPatchHandle,
1968 0);
1969 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
1970 status, afPatchHandle);
1971 if (status == NO_ERROR) {
1972 if (index < 0) {
1973 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
1974 &newPatch, uid);
1975 addAudioPatch(patchDesc->mHandle, patchDesc);
1976 } else {
1977 patchDesc->mPatch = newPatch;
1978 }
1979 patchDesc->mAfPatchHandle = afPatchHandle;
1980 *handle = patchDesc->mHandle;
1981 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001982 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07001983 } else {
1984 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
1985 status);
1986 return INVALID_OPERATION;
1987 }
1988 } else {
1989 return BAD_VALUE;
1990 }
1991 } else {
1992 return BAD_VALUE;
1993 }
1994 return NO_ERROR;
1995}
1996
1997status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
1998 uid_t uid)
1999{
2000 ALOGV("releaseAudioPatch() patch %d", handle);
2001
2002 ssize_t index = mAudioPatches.indexOfKey(handle);
2003
2004 if (index < 0) {
2005 return BAD_VALUE;
2006 }
2007 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2008 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2009 mUidCached, patchDesc->mUid, uid);
2010 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2011 return INVALID_OPERATION;
2012 }
2013
2014 struct audio_patch *patch = &patchDesc->mPatch;
2015 patchDesc->mUid = mUidCached;
2016 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002017 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002018 if (outputDesc == NULL) {
2019 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2020 return BAD_VALUE;
2021 }
2022
2023 setOutputDevice(outputDesc->mIoHandle,
2024 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2025 true,
2026 0,
2027 NULL);
2028 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2029 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002030 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002031 if (inputDesc == NULL) {
2032 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2033 return BAD_VALUE;
2034 }
2035 setInputDevice(inputDesc->mIoHandle,
2036 getNewInputDevice(inputDesc->mIoHandle),
2037 true,
2038 NULL);
2039 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2040 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2041 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2042 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2043 status, patchDesc->mAfPatchHandle);
2044 removeAudioPatch(patchDesc->mHandle);
2045 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002046 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002047 } else {
2048 return BAD_VALUE;
2049 }
2050 } else {
2051 return BAD_VALUE;
2052 }
2053 return NO_ERROR;
2054}
2055
2056status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2057 struct audio_patch *patches,
2058 unsigned int *generation)
2059{
2060 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
2061 generation == NULL) {
2062 return BAD_VALUE;
2063 }
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002064 ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
Eric Laurent6a94d692014-05-20 11:18:06 -07002065 *num_patches, patches, mAudioPatches.size());
2066 if (patches == NULL) {
2067 *num_patches = 0;
2068 }
2069
2070 size_t patchesWritten = 0;
2071 size_t patchesMax = *num_patches;
2072 for (size_t i = 0;
2073 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2074 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2075 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002076 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07002077 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2078 }
2079 *num_patches = mAudioPatches.size();
2080
2081 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002082 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002083 return NO_ERROR;
2084}
2085
Eric Laurente1715a42014-05-20 11:30:42 -07002086status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002087{
Eric Laurente1715a42014-05-20 11:30:42 -07002088 ALOGV("setAudioPortConfig()");
2089
2090 if (config == NULL) {
2091 return BAD_VALUE;
2092 }
2093 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2094 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002095 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2096 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002097 }
2098
Eric Laurenta121f902014-06-03 13:32:54 -07002099 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002100 if (config->type == AUDIO_PORT_TYPE_MIX) {
2101 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002102 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002103 if (outputDesc == NULL) {
2104 return BAD_VALUE;
2105 }
Eric Laurent84c70242014-06-23 08:46:27 -07002106 ALOG_ASSERT(!outputDesc->isDuplicated(),
2107 "setAudioPortConfig() called on duplicated output %d",
2108 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002109 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002110 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002111 sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002112 if (inputDesc == NULL) {
2113 return BAD_VALUE;
2114 }
Eric Laurenta121f902014-06-03 13:32:54 -07002115 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002116 } else {
2117 return BAD_VALUE;
2118 }
2119 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2120 sp<DeviceDescriptor> deviceDesc;
2121 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2122 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2123 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2124 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2125 } else {
2126 return BAD_VALUE;
2127 }
2128 if (deviceDesc == NULL) {
2129 return BAD_VALUE;
2130 }
Eric Laurenta121f902014-06-03 13:32:54 -07002131 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002132 } else {
2133 return BAD_VALUE;
2134 }
2135
Eric Laurenta121f902014-06-03 13:32:54 -07002136 struct audio_port_config backupConfig;
2137 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2138 if (status == NO_ERROR) {
2139 struct audio_port_config newConfig;
2140 audioPortConfig->toAudioPortConfig(&newConfig, config);
2141 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002142 }
Eric Laurenta121f902014-06-03 13:32:54 -07002143 if (status != NO_ERROR) {
2144 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002145 }
Eric Laurente1715a42014-05-20 11:30:42 -07002146
2147 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002148}
2149
2150void AudioPolicyManager::clearAudioPatches(uid_t uid)
2151{
2152 for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++) {
2153 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2154 if (patchDesc->mUid == uid) {
2155 // releaseAudioPatch() removes the patch from mAudioPatches
2156 if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) {
2157 i--;
2158 }
2159 }
2160 }
2161}
2162
2163status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2164 const sp<AudioPatch>& patch)
2165{
2166 ssize_t index = mAudioPatches.indexOfKey(handle);
2167
2168 if (index >= 0) {
2169 ALOGW("addAudioPatch() patch %d already in", handle);
2170 return ALREADY_EXISTS;
2171 }
2172 mAudioPatches.add(handle, patch);
2173 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2174 "sink handle %d",
2175 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2176 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2177 return NO_ERROR;
2178}
2179
2180status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2181{
2182 ssize_t index = mAudioPatches.indexOfKey(handle);
2183
2184 if (index < 0) {
2185 ALOGW("removeAudioPatch() patch %d not in", handle);
2186 return ALREADY_EXISTS;
2187 }
2188 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2189 mAudioPatches.valueAt(index)->mAfPatchHandle);
2190 mAudioPatches.removeItemsAt(index);
2191 return NO_ERROR;
2192}
2193
Eric Laurente552edb2014-03-10 17:42:56 -07002194// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002195// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002196// ----------------------------------------------------------------------------
2197
Eric Laurent3a4311c2014-03-17 12:00:47 -07002198uint32_t AudioPolicyManager::nextUniqueId()
2199{
2200 return android_atomic_inc(&mNextUniqueId);
2201}
2202
Eric Laurent6a94d692014-05-20 11:18:06 -07002203uint32_t AudioPolicyManager::nextAudioPortGeneration()
2204{
2205 return android_atomic_inc(&mAudioPortGeneration);
2206}
2207
Eric Laurente0720872014-03-11 09:30:41 -07002208AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002209 :
2210#ifdef AUDIO_POLICY_TEST
2211 Thread(false),
2212#endif //AUDIO_POLICY_TEST
2213 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002214 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002215 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2216 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002217 mA2dpSuspended(false),
Eric Laurent6a94d692014-05-20 11:18:06 -07002218 mSpeakerDrcEnabled(false), mNextUniqueId(1),
2219 mAudioPortGeneration(1)
Eric Laurente552edb2014-03-10 17:42:56 -07002220{
Eric Laurent6a94d692014-05-20 11:18:06 -07002221 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002222 mpClientInterface = clientInterface;
2223
Eric Laurent3b73df72014-03-11 09:06:29 -07002224 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2225 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002226 }
2227
Eric Laurent1afeecb2014-05-14 08:52:28 -07002228 mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002229 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2230 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2231 ALOGE("could not load audio policy configuration file, setting defaults");
2232 defaultAudioPolicyConfig();
2233 }
2234 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002235 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002236
2237 // must be done after reading the policy
2238 initializeVolumeCurves();
2239
2240 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002241 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2242 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002243 for (size_t i = 0; i < mHwModules.size(); i++) {
2244 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2245 if (mHwModules[i]->mHandle == 0) {
2246 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2247 continue;
2248 }
2249 // open all output streams needed to access attached devices
2250 // except for direct output streams that are only opened when they are actually
2251 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002252 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002253 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2254 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002255 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002256
Eric Laurent3a4311c2014-03-17 12:00:47 -07002257 if (outProfile->mSupportedDevices.isEmpty()) {
2258 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2259 continue;
2260 }
2261
2262 audio_devices_t profileTypes = outProfile->mSupportedDevices.types();
2263 if ((profileTypes & outputDeviceTypes) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002264 ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002265 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002266
Eric Laurent1c333e22014-05-20 10:48:17 -07002267 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice->mDeviceType & profileTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07002268 audio_io_handle_t output = mpClientInterface->openOutput(
2269 outProfile->mModule->mHandle,
2270 &outputDesc->mDevice,
2271 &outputDesc->mSamplingRate,
2272 &outputDesc->mFormat,
2273 &outputDesc->mChannelMask,
2274 &outputDesc->mLatency,
2275 outputDesc->mFlags);
2276 if (output == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002277 ALOGW("Cannot open output stream for device %08x on hw module %s",
2278 outputDesc->mDevice,
2279 mHwModules[i]->mName);
Eric Laurente552edb2014-03-10 17:42:56 -07002280 } else {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002281 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002282 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002283 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002284 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002285 // give a valid ID to an attached device once confirmed it is reachable
2286 if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) {
2287 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002288 mAvailableOutputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002289 }
2290 }
Eric Laurente552edb2014-03-10 17:42:56 -07002291 if (mPrimaryOutput == 0 &&
2292 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2293 mPrimaryOutput = output;
2294 }
2295 addOutput(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002296 ALOGI("CSTOR setOutputDevice %08x", outputDesc->mDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07002297 setOutputDevice(output,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002298 outputDesc->mDevice,
Eric Laurente552edb2014-03-10 17:42:56 -07002299 true);
2300 }
2301 }
2302 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002303 // open input streams needed to access attached devices to validate
2304 // mAvailableInputDevices list
2305 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2306 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002307 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002308
Eric Laurent3a4311c2014-03-17 12:00:47 -07002309 if (inProfile->mSupportedDevices.isEmpty()) {
2310 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2311 continue;
2312 }
2313
2314 audio_devices_t profileTypes = inProfile->mSupportedDevices.types();
2315 if (profileTypes & inputDeviceTypes) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002316 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002317
2318 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
Eric Laurent1c333e22014-05-20 10:48:17 -07002319 inputDesc->mDevice = inProfile->mSupportedDevices[0]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002320 audio_io_handle_t input = mpClientInterface->openInput(
2321 inProfile->mModule->mHandle,
2322 &inputDesc->mDevice,
2323 &inputDesc->mSamplingRate,
2324 &inputDesc->mFormat,
2325 &inputDesc->mChannelMask);
2326
2327 if (input != 0) {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002328 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002329 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002330 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002331 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002332 // give a valid ID to an attached device once confirmed it is reachable
2333 if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) {
2334 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002335 mAvailableInputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002336 }
2337 }
2338 mpClientInterface->closeInput(input);
2339 } else {
2340 ALOGW("Cannot open input stream for device %08x on hw module %s",
2341 inputDesc->mDevice,
2342 mHwModules[i]->mName);
2343 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002344 }
2345 }
2346 }
2347 // make sure all attached devices have been allocated a unique ID
2348 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
2349 if (mAvailableOutputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002350 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002351 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2352 continue;
2353 }
2354 i++;
2355 }
2356 for (size_t i = 0; i < mAvailableInputDevices.size();) {
2357 if (mAvailableInputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002358 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002359 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2360 continue;
2361 }
2362 i++;
2363 }
2364 // make sure default device is reachable
2365 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002366 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002367 }
Eric Laurente552edb2014-03-10 17:42:56 -07002368
2369 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2370
2371 updateDevicesAndOutputs();
2372
2373#ifdef AUDIO_POLICY_TEST
2374 if (mPrimaryOutput != 0) {
2375 AudioParameter outputCmd = AudioParameter();
2376 outputCmd.addInt(String8("set_id"), 0);
2377 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2378
2379 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2380 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002381 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2382 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002383 mTestLatencyMs = 0;
2384 mCurOutput = 0;
2385 mDirectOutput = false;
2386 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2387 mTestOutputs[i] = 0;
2388 }
2389
2390 const size_t SIZE = 256;
2391 char buffer[SIZE];
2392 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2393 run(buffer, ANDROID_PRIORITY_AUDIO);
2394 }
2395#endif //AUDIO_POLICY_TEST
2396}
2397
Eric Laurente0720872014-03-11 09:30:41 -07002398AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002399{
2400#ifdef AUDIO_POLICY_TEST
2401 exit();
2402#endif //AUDIO_POLICY_TEST
2403 for (size_t i = 0; i < mOutputs.size(); i++) {
2404 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002405 }
2406 for (size_t i = 0; i < mInputs.size(); i++) {
2407 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002408 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002409 mAvailableOutputDevices.clear();
2410 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002411 mOutputs.clear();
2412 mInputs.clear();
2413 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002414}
2415
Eric Laurente0720872014-03-11 09:30:41 -07002416status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002417{
2418 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2419}
2420
2421#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002422bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002423{
2424 ALOGV("entering threadLoop()");
2425 while (!exitPending())
2426 {
2427 String8 command;
2428 int valueInt;
2429 String8 value;
2430
2431 Mutex::Autolock _l(mLock);
2432 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2433
2434 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2435 AudioParameter param = AudioParameter(command);
2436
2437 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2438 valueInt != 0) {
2439 ALOGV("Test command %s received", command.string());
2440 String8 target;
2441 if (param.get(String8("target"), target) != NO_ERROR) {
2442 target = "Manager";
2443 }
2444 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2445 param.remove(String8("test_cmd_policy_output"));
2446 mCurOutput = valueInt;
2447 }
2448 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2449 param.remove(String8("test_cmd_policy_direct"));
2450 if (value == "false") {
2451 mDirectOutput = false;
2452 } else if (value == "true") {
2453 mDirectOutput = true;
2454 }
2455 }
2456 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2457 param.remove(String8("test_cmd_policy_input"));
2458 mTestInput = valueInt;
2459 }
2460
2461 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2462 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002463 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002464 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002465 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002466 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002467 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002468 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002469 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002470 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002471 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002472 if (target == "Manager") {
2473 mTestFormat = format;
2474 } else if (mTestOutputs[mCurOutput] != 0) {
2475 AudioParameter outputParam = AudioParameter();
2476 outputParam.addInt(String8("format"), format);
2477 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2478 }
2479 }
2480 }
2481 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2482 param.remove(String8("test_cmd_policy_channels"));
2483 int channels = 0;
2484
2485 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002486 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002487 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002488 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002489 }
2490 if (channels != 0) {
2491 if (target == "Manager") {
2492 mTestChannels = channels;
2493 } else if (mTestOutputs[mCurOutput] != 0) {
2494 AudioParameter outputParam = AudioParameter();
2495 outputParam.addInt(String8("channels"), channels);
2496 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2497 }
2498 }
2499 }
2500 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2501 param.remove(String8("test_cmd_policy_sampleRate"));
2502 if (valueInt >= 0 && valueInt <= 96000) {
2503 int samplingRate = valueInt;
2504 if (target == "Manager") {
2505 mTestSamplingRate = samplingRate;
2506 } else if (mTestOutputs[mCurOutput] != 0) {
2507 AudioParameter outputParam = AudioParameter();
2508 outputParam.addInt(String8("sampling_rate"), samplingRate);
2509 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2510 }
2511 }
2512 }
2513
2514 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2515 param.remove(String8("test_cmd_policy_reopen"));
2516
Eric Laurent1f2f2232014-06-02 12:01:23 -07002517 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07002518 mpClientInterface->closeOutput(mPrimaryOutput);
2519
2520 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
2521
Eric Laurente552edb2014-03-10 17:42:56 -07002522 mOutputs.removeItem(mPrimaryOutput);
2523
Eric Laurent1f2f2232014-06-02 12:01:23 -07002524 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07002525 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
2526 mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
2527 &outputDesc->mDevice,
2528 &outputDesc->mSamplingRate,
2529 &outputDesc->mFormat,
2530 &outputDesc->mChannelMask,
2531 &outputDesc->mLatency,
2532 outputDesc->mFlags);
2533 if (mPrimaryOutput == 0) {
2534 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
2535 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
2536 } else {
2537 AudioParameter outputCmd = AudioParameter();
2538 outputCmd.addInt(String8("set_id"), 0);
2539 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2540 addOutput(mPrimaryOutput, outputDesc);
2541 }
2542 }
2543
2544
2545 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2546 }
2547 }
2548 return false;
2549}
2550
Eric Laurente0720872014-03-11 09:30:41 -07002551void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002552{
2553 {
2554 AutoMutex _l(mLock);
2555 requestExit();
2556 mWaitWorkCV.signal();
2557 }
2558 requestExitAndWait();
2559}
2560
Eric Laurente0720872014-03-11 09:30:41 -07002561int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002562{
2563 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2564 if (output == mTestOutputs[i]) return i;
2565 }
2566 return 0;
2567}
2568#endif //AUDIO_POLICY_TEST
2569
2570// ---
2571
Eric Laurent1f2f2232014-06-02 12:01:23 -07002572void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002573{
Eric Laurent1c333e22014-05-20 10:48:17 -07002574 outputDesc->mIoHandle = output;
2575 outputDesc->mId = nextUniqueId();
2576 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002577 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002578}
2579
Eric Laurent1f2f2232014-06-02 12:01:23 -07002580void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002581{
Eric Laurent1c333e22014-05-20 10:48:17 -07002582 inputDesc->mIoHandle = input;
2583 inputDesc->mId = nextUniqueId();
2584 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002585 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002586}
Eric Laurente552edb2014-03-10 17:42:56 -07002587
Eric Laurent3a4311c2014-03-17 12:00:47 -07002588String8 AudioPolicyManager::addressToParameter(audio_devices_t device, const String8 address)
2589{
2590 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
2591 return String8("a2dp_sink_address=")+address;
2592 }
2593 return address;
2594}
2595
Eric Laurente0720872014-03-11 09:30:41 -07002596status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -07002597 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07002598 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002599 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002600{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002601 sp<AudioOutputDescriptor> desc;
Eric Laurente552edb2014-03-10 17:42:56 -07002602
Eric Laurent3b73df72014-03-11 09:06:29 -07002603 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002604 // first list already open outputs that can be routed to this device
2605 for (size_t i = 0; i < mOutputs.size(); i++) {
2606 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002607 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002608 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2609 outputs.add(mOutputs.keyAt(i));
2610 }
2611 }
2612 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002613 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002614 for (size_t i = 0; i < mHwModules.size(); i++)
2615 {
2616 if (mHwModules[i]->mHandle == 0) {
2617 continue;
2618 }
2619 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2620 {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002621 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
Eric Laurentd4692962014-05-05 18:13:44 -07002622 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002623 profiles.add(mHwModules[i]->mOutputProfiles[j]);
2624 }
2625 }
2626 }
2627
2628 if (profiles.isEmpty() && outputs.isEmpty()) {
2629 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2630 return BAD_VALUE;
2631 }
2632
2633 // open outputs for matching profiles if needed. Direct outputs are also opened to
2634 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2635 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002636 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002637
2638 // nothing to do if one output is already opened for this profile
2639 size_t j;
2640 for (j = 0; j < mOutputs.size(); j++) {
2641 desc = mOutputs.valueAt(j);
2642 if (!desc->isDuplicated() && desc->mProfile == profile) {
2643 break;
2644 }
2645 }
2646 if (j != mOutputs.size()) {
2647 continue;
2648 }
2649
Eric Laurent3a4311c2014-03-17 12:00:47 -07002650 ALOGV("opening output for device %08x with params %s", device, address.string());
Eric Laurente552edb2014-03-10 17:42:56 -07002651 desc = new AudioOutputDescriptor(profile);
2652 desc->mDevice = device;
2653 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
2654 offloadInfo.sample_rate = desc->mSamplingRate;
2655 offloadInfo.format = desc->mFormat;
2656 offloadInfo.channel_mask = desc->mChannelMask;
2657
2658 audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
2659 &desc->mDevice,
2660 &desc->mSamplingRate,
2661 &desc->mFormat,
2662 &desc->mChannelMask,
2663 &desc->mLatency,
2664 desc->mFlags,
2665 &offloadInfo);
2666 if (output != 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002667 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07002668 if (!address.isEmpty()) {
2669 mpClientInterface->setParameters(output, addressToParameter(device, address));
Eric Laurente552edb2014-03-10 17:42:56 -07002670 }
2671
Eric Laurentd4692962014-05-05 18:13:44 -07002672 // Here is where we step through and resolve any "dynamic" fields
2673 String8 reply;
2674 char *value;
2675 if (profile->mSamplingRates[0] == 0) {
2676 reply = mpClientInterface->getParameters(output,
2677 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2678 ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
2679 reply.string());
2680 value = strpbrk((char *)reply.string(), "=");
2681 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002682 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002683 }
Eric Laurentd4692962014-05-05 18:13:44 -07002684 }
2685 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2686 reply = mpClientInterface->getParameters(output,
2687 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2688 ALOGV("checkOutputsForDevice() direct output sup formats %s",
2689 reply.string());
2690 value = strpbrk((char *)reply.string(), "=");
2691 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002692 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002693 }
Eric Laurentd4692962014-05-05 18:13:44 -07002694 }
2695 if (profile->mChannelMasks[0] == 0) {
2696 reply = mpClientInterface->getParameters(output,
2697 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2698 ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
2699 reply.string());
2700 value = strpbrk((char *)reply.string(), "=");
2701 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002702 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002703 }
Eric Laurentd4692962014-05-05 18:13:44 -07002704 }
2705 if (((profile->mSamplingRates[0] == 0) &&
2706 (profile->mSamplingRates.size() < 2)) ||
2707 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
2708 (profile->mFormats.size() < 2)) ||
2709 ((profile->mChannelMasks[0] == 0) &&
2710 (profile->mChannelMasks.size() < 2))) {
2711 ALOGW("checkOutputsForDevice() direct output missing param");
2712 mpClientInterface->closeOutput(output);
2713 output = 0;
Eric Laurent1e693b52014-07-09 15:03:28 -07002714 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
2715 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002716 mpClientInterface->closeOutput(output);
Eric Laurent1e693b52014-07-09 15:03:28 -07002717 desc->mSamplingRate = profile->pickSamplingRate();
2718 desc->mFormat = profile->pickFormat();
2719 desc->mChannelMask = profile->pickChannelMask();
Eric Laurentd4692962014-05-05 18:13:44 -07002720 offloadInfo.sample_rate = desc->mSamplingRate;
Eric Laurent1e693b52014-07-09 15:03:28 -07002721 offloadInfo.format = desc->mFormat;
2722 offloadInfo.channel_mask = desc->mChannelMask;
Eric Laurentd4692962014-05-05 18:13:44 -07002723 output = mpClientInterface->openOutput(
2724 profile->mModule->mHandle,
2725 &desc->mDevice,
2726 &desc->mSamplingRate,
2727 &desc->mFormat,
2728 &desc->mChannelMask,
2729 &desc->mLatency,
2730 desc->mFlags,
2731 &offloadInfo);
2732 }
2733
2734 if (output != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002735 addOutput(output, desc);
Eric Laurentd4692962014-05-05 18:13:44 -07002736 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
2737 audio_io_handle_t duplicatedOutput = 0;
Eric Laurente552edb2014-03-10 17:42:56 -07002738
Eric Laurentd4692962014-05-05 18:13:44 -07002739 // set initial stream volume for device
2740 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07002741
Eric Laurentd4692962014-05-05 18:13:44 -07002742 //TODO: configure audio effect output stage here
2743
2744 // open a duplicating output thread for the new output and the primary output
2745 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
2746 mPrimaryOutput);
2747 if (duplicatedOutput != 0) {
2748 // add duplicated output descriptor
Eric Laurent1f2f2232014-06-02 12:01:23 -07002749 sp<AudioOutputDescriptor> dupOutputDesc = new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07002750 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
2751 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
2752 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
2753 dupOutputDesc->mFormat = desc->mFormat;
2754 dupOutputDesc->mChannelMask = desc->mChannelMask;
2755 dupOutputDesc->mLatency = desc->mLatency;
2756 addOutput(duplicatedOutput, dupOutputDesc);
2757 applyStreamVolumes(duplicatedOutput, device, 0, true);
2758 } else {
2759 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
2760 mPrimaryOutput, output);
2761 mpClientInterface->closeOutput(output);
2762 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07002763 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002764 output = 0;
2765 }
Eric Laurente552edb2014-03-10 17:42:56 -07002766 }
2767 }
2768 }
2769 if (output == 0) {
2770 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07002771 profiles.removeAt(profile_index);
2772 profile_index--;
2773 } else {
2774 outputs.add(output);
2775 ALOGV("checkOutputsForDevice(): adding output %d", output);
2776 }
2777 }
2778
2779 if (profiles.isEmpty()) {
2780 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2781 return BAD_VALUE;
2782 }
Eric Laurentd4692962014-05-05 18:13:44 -07002783 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07002784 // check if one opened output is not needed any more after disconnecting one device
2785 for (size_t i = 0; i < mOutputs.size(); i++) {
2786 desc = mOutputs.valueAt(i);
2787 if (!desc->isDuplicated() &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07002788 !(desc->mProfile->mSupportedDevices.types() &
2789 mAvailableOutputDevices.types())) {
Eric Laurente552edb2014-03-10 17:42:56 -07002790 ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
2791 outputs.add(mOutputs.keyAt(i));
2792 }
2793 }
Eric Laurentd4692962014-05-05 18:13:44 -07002794 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002795 for (size_t i = 0; i < mHwModules.size(); i++)
2796 {
2797 if (mHwModules[i]->mHandle == 0) {
2798 continue;
2799 }
2800 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2801 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002802 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07002803 if (profile->mSupportedDevices.types() & device) {
2804 ALOGV("checkOutputsForDevice(): "
2805 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002806 if (profile->mSamplingRates[0] == 0) {
2807 profile->mSamplingRates.clear();
2808 profile->mSamplingRates.add(0);
2809 }
2810 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2811 profile->mFormats.clear();
2812 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2813 }
2814 if (profile->mChannelMasks[0] == 0) {
2815 profile->mChannelMasks.clear();
2816 profile->mChannelMasks.add(0);
2817 }
2818 }
2819 }
2820 }
2821 }
2822 return NO_ERROR;
2823}
2824
Eric Laurentd4692962014-05-05 18:13:44 -07002825status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
2826 audio_policy_dev_state_t state,
2827 SortedVector<audio_io_handle_t>& inputs,
2828 const String8 address)
2829{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002830 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07002831 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2832 // first list already open inputs that can be routed to this device
2833 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2834 desc = mInputs.valueAt(input_index);
2835 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
2836 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2837 inputs.add(mInputs.keyAt(input_index));
2838 }
2839 }
2840
2841 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002842 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07002843 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
2844 {
2845 if (mHwModules[module_idx]->mHandle == 0) {
2846 continue;
2847 }
2848 for (size_t profile_index = 0;
2849 profile_index < mHwModules[module_idx]->mInputProfiles.size();
2850 profile_index++)
2851 {
2852 if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types()
2853 & (device & ~AUDIO_DEVICE_BIT_IN)) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002854 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07002855 profile_index, module_idx);
2856 profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]);
2857 }
2858 }
2859 }
2860
2861 if (profiles.isEmpty() && inputs.isEmpty()) {
2862 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2863 return BAD_VALUE;
2864 }
2865
2866 // open inputs for matching profiles if needed. Direct inputs are also opened to
2867 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2868 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2869
Eric Laurent1c333e22014-05-20 10:48:17 -07002870 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002871 // nothing to do if one input is already opened for this profile
2872 size_t input_index;
2873 for (input_index = 0; input_index < mInputs.size(); input_index++) {
2874 desc = mInputs.valueAt(input_index);
2875 if (desc->mProfile == profile) {
2876 break;
2877 }
2878 }
2879 if (input_index != mInputs.size()) {
2880 continue;
2881 }
2882
2883 ALOGV("opening input for device 0x%X with params %s", device, address.string());
2884 desc = new AudioInputDescriptor(profile);
2885 desc->mDevice = device;
2886
2887 audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2888 &desc->mDevice,
2889 &desc->mSamplingRate,
2890 &desc->mFormat,
2891 &desc->mChannelMask);
2892
2893 if (input != 0) {
2894 if (!address.isEmpty()) {
2895 mpClientInterface->setParameters(input, addressToParameter(device, address));
2896 }
2897
2898 // Here is where we step through and resolve any "dynamic" fields
2899 String8 reply;
2900 char *value;
2901 if (profile->mSamplingRates[0] == 0) {
2902 reply = mpClientInterface->getParameters(input,
2903 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2904 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2905 reply.string());
2906 value = strpbrk((char *)reply.string(), "=");
2907 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002908 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002909 }
2910 }
2911 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2912 reply = mpClientInterface->getParameters(input,
2913 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2914 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2915 value = strpbrk((char *)reply.string(), "=");
2916 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002917 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002918 }
2919 }
2920 if (profile->mChannelMasks[0] == 0) {
2921 reply = mpClientInterface->getParameters(input,
2922 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2923 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2924 reply.string());
2925 value = strpbrk((char *)reply.string(), "=");
2926 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002927 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002928 }
2929 }
2930 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2931 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2932 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2933 ALOGW("checkInputsForDevice() direct input missing param");
2934 mpClientInterface->closeInput(input);
2935 input = 0;
2936 }
2937
2938 if (input != 0) {
2939 addInput(input, desc);
2940 }
2941 } // endif input != 0
2942
2943 if (input == 0) {
2944 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07002945 profiles.removeAt(profile_index);
2946 profile_index--;
2947 } else {
2948 inputs.add(input);
2949 ALOGV("checkInputsForDevice(): adding input %d", input);
2950 }
2951 } // end scan profiles
2952
2953 if (profiles.isEmpty()) {
2954 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2955 return BAD_VALUE;
2956 }
2957 } else {
2958 // Disconnect
2959 // check if one opened input is not needed any more after disconnecting one device
2960 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2961 desc = mInputs.valueAt(input_index);
2962 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) {
2963 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2964 mInputs.keyAt(input_index));
2965 inputs.add(mInputs.keyAt(input_index));
2966 }
2967 }
2968 // Clear any profiles associated with the disconnected device.
2969 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
2970 if (mHwModules[module_index]->mHandle == 0) {
2971 continue;
2972 }
2973 for (size_t profile_index = 0;
2974 profile_index < mHwModules[module_index]->mInputProfiles.size();
2975 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002976 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002977 if (profile->mSupportedDevices.types() & device) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002978 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07002979 profile_index, module_index);
2980 if (profile->mSamplingRates[0] == 0) {
2981 profile->mSamplingRates.clear();
2982 profile->mSamplingRates.add(0);
2983 }
2984 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2985 profile->mFormats.clear();
2986 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2987 }
2988 if (profile->mChannelMasks[0] == 0) {
2989 profile->mChannelMasks.clear();
2990 profile->mChannelMasks.add(0);
2991 }
2992 }
2993 }
2994 }
2995 } // end disconnect
2996
2997 return NO_ERROR;
2998}
2999
3000
Eric Laurente0720872014-03-11 09:30:41 -07003001void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003002{
3003 ALOGV("closeOutput(%d)", output);
3004
Eric Laurent1f2f2232014-06-02 12:01:23 -07003005 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003006 if (outputDesc == NULL) {
3007 ALOGW("closeOutput() unknown output %d", output);
3008 return;
3009 }
3010
3011 // look for duplicated outputs connected to the output being removed.
3012 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003013 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003014 if (dupOutputDesc->isDuplicated() &&
3015 (dupOutputDesc->mOutput1 == outputDesc ||
3016 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003017 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003018 if (dupOutputDesc->mOutput1 == outputDesc) {
3019 outputDesc2 = dupOutputDesc->mOutput2;
3020 } else {
3021 outputDesc2 = dupOutputDesc->mOutput1;
3022 }
3023 // As all active tracks on duplicated output will be deleted,
3024 // and as they were also referenced on the other output, the reference
3025 // count for their stream type must be adjusted accordingly on
3026 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003027 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003028 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003029 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003030 }
3031 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3032 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3033
3034 mpClientInterface->closeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003035 mOutputs.removeItem(duplicatedOutput);
3036 }
3037 }
3038
3039 AudioParameter param;
3040 param.add(String8("closing"), String8("true"));
3041 mpClientInterface->setParameters(output, param.toString());
3042
3043 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003044 mOutputs.removeItem(output);
3045 mPreviousOutputs = mOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07003046 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003047}
3048
Eric Laurente0720872014-03-11 09:30:41 -07003049SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -07003050 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003051{
3052 SortedVector<audio_io_handle_t> outputs;
3053
3054 ALOGVV("getOutputsForDevice() device %04x", device);
3055 for (size_t i = 0; i < openOutputs.size(); i++) {
3056 ALOGVV("output %d isDuplicated=%d device=%04x",
3057 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3058 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3059 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3060 outputs.add(openOutputs.keyAt(i));
3061 }
3062 }
3063 return outputs;
3064}
3065
Eric Laurente0720872014-03-11 09:30:41 -07003066bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07003067 SortedVector<audio_io_handle_t>& outputs2)
3068{
3069 if (outputs1.size() != outputs2.size()) {
3070 return false;
3071 }
3072 for (size_t i = 0; i < outputs1.size(); i++) {
3073 if (outputs1[i] != outputs2[i]) {
3074 return false;
3075 }
3076 }
3077 return true;
3078}
3079
Eric Laurente0720872014-03-11 09:30:41 -07003080void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003081{
3082 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3083 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3084 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3085 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3086
3087 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3088 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3089 strategy, srcOutputs[0], dstOutputs[0]);
3090 // mute strategy while moving tracks from one output to another
3091 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003092 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07003093 if (desc->isStrategyActive(strategy)) {
3094 setStrategyMute(strategy, true, srcOutputs[i]);
3095 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3096 }
3097 }
3098
3099 // Move effects associated to this strategy from previous output to new output
3100 if (strategy == STRATEGY_MEDIA) {
3101 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3102 SortedVector<audio_io_handle_t> moved;
3103 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003104 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3105 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3106 effectDesc->mIo != fxOutput) {
3107 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003108 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3109 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003110 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003111 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003112 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003113 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003114 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003115 }
3116 }
3117 }
3118 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003119 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3120 if (getStrategy((audio_stream_type_t)i) == strategy) {
3121 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003122 }
3123 }
3124 }
3125}
3126
Eric Laurente0720872014-03-11 09:30:41 -07003127void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003128{
3129 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
3130 checkOutputForStrategy(STRATEGY_PHONE);
3131 checkOutputForStrategy(STRATEGY_SONIFICATION);
3132 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3133 checkOutputForStrategy(STRATEGY_MEDIA);
3134 checkOutputForStrategy(STRATEGY_DTMF);
3135}
3136
Eric Laurente0720872014-03-11 09:30:41 -07003137audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07003138{
Eric Laurente552edb2014-03-10 17:42:56 -07003139 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003140 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003141 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
3142 return mOutputs.keyAt(i);
3143 }
3144 }
3145
3146 return 0;
3147}
3148
Eric Laurente0720872014-03-11 09:30:41 -07003149void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003150{
Eric Laurente552edb2014-03-10 17:42:56 -07003151 audio_io_handle_t a2dpOutput = getA2dpOutput();
3152 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003153 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003154 return;
3155 }
3156
Eric Laurent3a4311c2014-03-17 12:00:47 -07003157 bool isScoConnected =
3158 (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0;
Eric Laurente552edb2014-03-10 17:42:56 -07003159 // suspend A2DP output if:
3160 // (NOT already suspended) &&
3161 // ((SCO device is connected &&
3162 // (forced usage for communication || for record is SCO))) ||
3163 // (phone state is ringing || in call)
3164 //
3165 // restore A2DP output if:
3166 // (Already suspended) &&
3167 // ((SCO device is NOT connected ||
3168 // (forced usage NOT for communication && NOT for record is SCO))) &&
3169 // (phone state is NOT ringing && NOT in call)
3170 //
3171 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003172 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003173 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
3174 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
3175 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
3176 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003177
3178 mpClientInterface->restoreOutput(a2dpOutput);
3179 mA2dpSuspended = false;
3180 }
3181 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003182 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003183 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
3184 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
3185 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
3186 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003187
3188 mpClientInterface->suspendOutput(a2dpOutput);
3189 mA2dpSuspended = true;
3190 }
3191 }
3192}
3193
Eric Laurent1c333e22014-05-20 10:48:17 -07003194audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003195{
3196 audio_devices_t device = AUDIO_DEVICE_NONE;
3197
Eric Laurent1f2f2232014-06-02 12:01:23 -07003198 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003199
3200 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3201 if (index >= 0) {
3202 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3203 if (patchDesc->mUid != mUidCached) {
3204 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3205 outputDesc->device(), outputDesc->mPatchHandle);
3206 return outputDesc->device();
3207 }
3208 }
3209
Eric Laurente552edb2014-03-10 17:42:56 -07003210 // check the following by order of priority to request a routing change if necessary:
3211 // 1: the strategy enforced audible is active on the output:
3212 // use device for strategy enforced audible
3213 // 2: we are in call or the strategy phone is active on the output:
3214 // use device for strategy phone
3215 // 3: the strategy sonification is active on the output:
3216 // use device for strategy sonification
3217 // 4: the strategy "respectful" sonification is active on the output:
3218 // use device for strategy "respectful" sonification
3219 // 5: the strategy media is active on the output:
3220 // use device for strategy media
3221 // 6: the strategy DTMF is active on the output:
3222 // use device for strategy DTMF
3223 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
3224 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3225 } else if (isInCall() ||
3226 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
3227 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
3228 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
3229 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
3230 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
3231 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
3232 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
3233 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
3234 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
3235 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
3236 }
3237
Eric Laurent1c333e22014-05-20 10:48:17 -07003238 ALOGV("getNewOutputDevice() selected device %x", device);
3239 return device;
3240}
3241
3242audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3243{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003244 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003245
3246 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3247 if (index >= 0) {
3248 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3249 if (patchDesc->mUid != mUidCached) {
3250 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3251 inputDesc->mDevice, inputDesc->mPatchHandle);
3252 return inputDesc->mDevice;
3253 }
3254 }
3255
Eric Laurent1c333e22014-05-20 10:48:17 -07003256 audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource);
3257
3258 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003259 return device;
3260}
3261
Eric Laurente0720872014-03-11 09:30:41 -07003262uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003263 return (uint32_t)getStrategy(stream);
3264}
3265
Eric Laurente0720872014-03-11 09:30:41 -07003266audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003267 // By checking the range of stream before calling getStrategy, we avoid
3268 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3269 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent3b73df72014-03-11 09:06:29 -07003270 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003271 return AUDIO_DEVICE_NONE;
3272 }
3273 audio_devices_t devices;
3274 AudioPolicyManager::routing_strategy strategy = getStrategy(stream);
3275 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3276 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3277 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003278 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent6a94d692014-05-20 11:18:06 -07003279 if (outputDesc->isStrategyActive(strategy)) {
3280 devices = outputDesc->device();
3281 break;
3282 }
Eric Laurente552edb2014-03-10 17:42:56 -07003283 }
3284 return devices;
3285}
3286
Eric Laurente0720872014-03-11 09:30:41 -07003287AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07003288 audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003289 // stream to strategy mapping
3290 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003291 case AUDIO_STREAM_VOICE_CALL:
3292 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003293 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07003294 case AUDIO_STREAM_RING:
3295 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07003296 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07003297 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07003298 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07003299 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07003300 return STRATEGY_DTMF;
3301 default:
3302 ALOGE("unknown stream type");
Eric Laurent3b73df72014-03-11 09:06:29 -07003303 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07003304 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
3305 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07003306 case AUDIO_STREAM_TTS:
3307 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003308 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07003309 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07003310 return STRATEGY_ENFORCED_AUDIBLE;
3311 }
3312}
3313
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07003314uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
3315 // flags to strategy mapping
3316 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
3317 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
3318 }
3319
3320 // usage to strategy mapping
3321 switch (attr->usage) {
3322 case AUDIO_USAGE_MEDIA:
3323 case AUDIO_USAGE_GAME:
3324 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
3325 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
3326 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
3327 return (uint32_t) STRATEGY_MEDIA;
3328
3329 case AUDIO_USAGE_VOICE_COMMUNICATION:
3330 return (uint32_t) STRATEGY_PHONE;
3331
3332 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
3333 return (uint32_t) STRATEGY_DTMF;
3334
3335 case AUDIO_USAGE_ALARM:
3336 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
3337 return (uint32_t) STRATEGY_SONIFICATION;
3338
3339 case AUDIO_USAGE_NOTIFICATION:
3340 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
3341 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
3342 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
3343 case AUDIO_USAGE_NOTIFICATION_EVENT:
3344 return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL;
3345
3346 case AUDIO_USAGE_UNKNOWN:
3347 default:
3348 return (uint32_t) STRATEGY_MEDIA;
3349 }
3350}
3351
Eric Laurente0720872014-03-11 09:30:41 -07003352void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003353 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003354 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003355 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3356 updateDevicesAndOutputs();
3357 break;
3358 default:
3359 break;
3360 }
3361}
3362
Eric Laurente0720872014-03-11 09:30:41 -07003363audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07003364 bool fromCache)
3365{
3366 uint32_t device = AUDIO_DEVICE_NONE;
3367
3368 if (fromCache) {
3369 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3370 strategy, mDeviceForStrategy[strategy]);
3371 return mDeviceForStrategy[strategy];
3372 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003373 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07003374 switch (strategy) {
3375
3376 case STRATEGY_SONIFICATION_RESPECTFUL:
3377 if (isInCall()) {
3378 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003379 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07003380 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
3381 // while media is playing on a remote device, use the the sonification behavior.
3382 // Note that we test this usecase before testing if media is playing because
3383 // the isStreamActive() method only informs about the activity of a stream, not
3384 // if it's for local playback. Note also that we use the same delay between both tests
3385 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003386 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003387 // while media is playing (or has recently played), use the same device
3388 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3389 } else {
3390 // when media is not playing anymore, fall back on the sonification behavior
3391 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
3392 }
3393
3394 break;
3395
3396 case STRATEGY_DTMF:
3397 if (!isInCall()) {
3398 // when off call, DTMF strategy follows the same rules as MEDIA strategy
3399 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3400 break;
3401 }
3402 // when in call, DTMF and PHONE strategies follow the same rules
3403 // FALL THROUGH
3404
3405 case STRATEGY_PHONE:
3406 // for phone strategy, we first consider the forced use and then the available devices by order
3407 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07003408 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
3409 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003410 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003411 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003412 if (device) break;
3413 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003414 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003415 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003416 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07003417 if (device) break;
3418 // if SCO device is requested but no SCO device is available, fall back to default case
3419 // FALL THROUGH
3420
3421 default: // FORCE_NONE
3422 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07003423 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003424 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003425 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003426 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003427 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003428 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003429 if (device) break;
3430 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003431 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003432 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003433 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003434 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07003435 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003436 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003437 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003438 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003439 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003440 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003441 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003442 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003443 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003444 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003445 if (device) break;
3446 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003447 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07003448 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003449 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003450 if (device == AUDIO_DEVICE_NONE) {
3451 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
3452 }
3453 break;
3454
Eric Laurent3b73df72014-03-11 09:06:29 -07003455 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07003456 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
3457 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07003458 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003459 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003460 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003461 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003462 if (device) break;
3463 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003464 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003465 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003466 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003467 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003468 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003469 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003470 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003471 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003472 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003473 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003474 if (device) break;
3475 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003476 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003477 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003478 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003479 if (device == AUDIO_DEVICE_NONE) {
3480 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
3481 }
3482 break;
3483 }
3484 break;
3485
3486 case STRATEGY_SONIFICATION:
3487
3488 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
3489 // handleIncallSonification().
3490 if (isInCall()) {
3491 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
3492 break;
3493 }
3494 // FALL THROUGH
3495
3496 case STRATEGY_ENFORCED_AUDIBLE:
3497 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
3498 // except:
3499 // - when in call where it doesn't default to STRATEGY_PHONE behavior
3500 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
3501
3502 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003503 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003504 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003505 if (device == AUDIO_DEVICE_NONE) {
3506 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
3507 }
3508 }
3509 // The second device used for sonification is the same as the device used by media strategy
3510 // FALL THROUGH
3511
3512 case STRATEGY_MEDIA: {
3513 uint32_t device2 = AUDIO_DEVICE_NONE;
3514 if (strategy != STRATEGY_SONIFICATION) {
3515 // no sonification on remote submix (e.g. WFD)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003516 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurente552edb2014-03-10 17:42:56 -07003517 }
3518 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003519 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003520 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003521 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003522 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003523 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003524 }
3525 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003526 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003527 }
3528 }
3529 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003530 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003531 }
3532 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003533 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003534 }
3535 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003536 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003537 }
3538 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003539 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003540 }
3541 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003542 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003543 }
3544 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
3545 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003546 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003547 }
3548 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003549 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003550 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003551 }
3552 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003553 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003554 }
Jungshik Jang839e4f32014-06-26 17:23:40 +09003555 int device3 = AUDIO_DEVICE_NONE;
3556 if (strategy == STRATEGY_MEDIA) {
Jungshik Jang0c943092014-07-08 22:11:24 +09003557 // ARC, SPDIF and LINE can co-exist with others.
3558 device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC;
3559 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF);
3560 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE);
Jungshik Jang839e4f32014-06-26 17:23:40 +09003561 }
Eric Laurente552edb2014-03-10 17:42:56 -07003562
Jungshik Jang839e4f32014-06-26 17:23:40 +09003563 device2 |= device3;
Eric Laurente552edb2014-03-10 17:42:56 -07003564 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
3565 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
3566 device |= device2;
Jungshik Jang839e4f32014-06-26 17:23:40 +09003567
Eric Laurente552edb2014-03-10 17:42:56 -07003568 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003569 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003570 if (device == AUDIO_DEVICE_NONE) {
3571 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
3572 }
3573 } break;
3574
3575 default:
3576 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
3577 break;
3578 }
3579
3580 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
3581 return device;
3582}
3583
Eric Laurente0720872014-03-11 09:30:41 -07003584void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003585{
3586 for (int i = 0; i < NUM_STRATEGIES; i++) {
3587 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3588 }
3589 mPreviousOutputs = mOutputs;
3590}
3591
Eric Laurent1f2f2232014-06-02 12:01:23 -07003592uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003593 audio_devices_t prevDevice,
3594 uint32_t delayMs)
3595{
3596 // mute/unmute strategies using an incompatible device combination
3597 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3598 // if unmuting, unmute only after the specified delay
3599 if (outputDesc->isDuplicated()) {
3600 return 0;
3601 }
3602
3603 uint32_t muteWaitMs = 0;
3604 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003605 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003606
3607 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3608 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3609 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3610 bool doMute = false;
3611
3612 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3613 doMute = true;
3614 outputDesc->mStrategyMutedByDevice[i] = true;
3615 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3616 doMute = true;
3617 outputDesc->mStrategyMutedByDevice[i] = false;
3618 }
Eric Laurent99401132014-05-07 19:48:15 -07003619 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003620 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003621 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003622 // skip output if it does not share any device with current output
3623 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3624 == AUDIO_DEVICE_NONE) {
3625 continue;
3626 }
3627 audio_io_handle_t curOutput = mOutputs.keyAt(j);
3628 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
3629 mute ? "muting" : "unmuting", i, curDevice, curOutput);
3630 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
3631 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003632 if (mute) {
3633 // FIXME: should not need to double latency if volume could be applied
3634 // immediately by the audioflinger mixer. We must account for the delay
3635 // between now and the next time the audioflinger thread for this output
3636 // will process a buffer (which corresponds to one buffer size,
3637 // usually 1/2 or 1/4 of the latency).
3638 if (muteWaitMs < desc->latency() * 2) {
3639 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003640 }
3641 }
3642 }
3643 }
3644 }
3645 }
3646
Eric Laurent99401132014-05-07 19:48:15 -07003647 // temporary mute output if device selection changes to avoid volume bursts due to
3648 // different per device volumes
3649 if (outputDesc->isActive() && (device != prevDevice)) {
3650 if (muteWaitMs < outputDesc->latency() * 2) {
3651 muteWaitMs = outputDesc->latency() * 2;
3652 }
3653 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3654 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003655 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07003656 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07003657 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07003658 muteWaitMs *2, device);
3659 }
3660 }
3661 }
3662
Eric Laurente552edb2014-03-10 17:42:56 -07003663 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3664 if (muteWaitMs > delayMs) {
3665 muteWaitMs -= delayMs;
3666 usleep(muteWaitMs * 1000);
3667 return muteWaitMs;
3668 }
3669 return 0;
3670}
3671
Eric Laurente0720872014-03-11 09:30:41 -07003672uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07003673 audio_devices_t device,
3674 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003675 int delayMs,
3676 audio_patch_handle_t *patchHandle)
Eric Laurente552edb2014-03-10 17:42:56 -07003677{
3678 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003679 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003680 AudioParameter param;
3681 uint32_t muteWaitMs;
3682
3683 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003684 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
3685 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003686 return muteWaitMs;
3687 }
3688 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
3689 // output profile
3690 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003691 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003692 return 0;
3693 }
3694
3695 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07003696 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07003697
3698 audio_devices_t prevDevice = outputDesc->mDevice;
3699
3700 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
3701
3702 if (device != AUDIO_DEVICE_NONE) {
3703 outputDesc->mDevice = device;
3704 }
3705 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
3706
3707 // Do not change the routing if:
3708 // - the requested device is AUDIO_DEVICE_NONE
3709 // - the requested device is the same as current device and force is not specified.
3710 // Doing this check here allows the caller to call setOutputDevice() without conditions
3711 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
3712 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
3713 return muteWaitMs;
3714 }
3715
3716 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07003717
Eric Laurente552edb2014-03-10 17:42:56 -07003718 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07003719 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003720 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07003721 } else {
3722 DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device);
3723 if (!deviceList.isEmpty()) {
3724 struct audio_patch patch;
3725 outputDesc->toAudioPortConfig(&patch.sources[0]);
3726 patch.num_sources = 1;
3727 patch.num_sinks = 0;
3728 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
3729 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07003730 patch.num_sinks++;
3731 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003732 ssize_t index;
3733 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3734 index = mAudioPatches.indexOfKey(*patchHandle);
3735 } else {
3736 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3737 }
3738 sp< AudioPatch> patchDesc;
3739 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3740 if (index >= 0) {
3741 patchDesc = mAudioPatches.valueAt(index);
3742 afPatchHandle = patchDesc->mAfPatchHandle;
3743 }
3744
Eric Laurent1c333e22014-05-20 10:48:17 -07003745 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003746 &afPatchHandle,
3747 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07003748 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
3749 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003750 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07003751 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003752 if (index < 0) {
3753 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3754 &patch, mUidCached);
3755 addAudioPatch(patchDesc->mHandle, patchDesc);
3756 } else {
3757 patchDesc->mPatch = patch;
3758 }
3759 patchDesc->mAfPatchHandle = afPatchHandle;
3760 patchDesc->mUid = mUidCached;
3761 if (patchHandle) {
3762 *patchHandle = patchDesc->mHandle;
3763 }
3764 outputDesc->mPatchHandle = patchDesc->mHandle;
3765 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003766 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003767 }
3768 }
3769 }
Eric Laurente552edb2014-03-10 17:42:56 -07003770
3771 // update stream volumes according to new device
3772 applyStreamVolumes(output, device, delayMs);
3773
3774 return muteWaitMs;
3775}
3776
Eric Laurent1c333e22014-05-20 10:48:17 -07003777status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07003778 int delayMs,
3779 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003780{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003781 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003782 ssize_t index;
3783 if (patchHandle) {
3784 index = mAudioPatches.indexOfKey(*patchHandle);
3785 } else {
3786 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3787 }
3788 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003789 return INVALID_OPERATION;
3790 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003791 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3792 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07003793 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
3794 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003795 removeAudioPatch(patchDesc->mHandle);
3796 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003797 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003798 return status;
3799}
3800
3801status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
3802 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07003803 bool force,
3804 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003805{
3806 status_t status = NO_ERROR;
3807
Eric Laurent1f2f2232014-06-02 12:01:23 -07003808 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003809 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
3810 inputDesc->mDevice = device;
3811
3812 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
3813 if (!deviceList.isEmpty()) {
3814 struct audio_patch patch;
3815 inputDesc->toAudioPortConfig(&patch.sinks[0]);
3816 patch.num_sinks = 1;
3817 //only one input device for now
3818 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07003819 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07003820 ssize_t index;
3821 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3822 index = mAudioPatches.indexOfKey(*patchHandle);
3823 } else {
3824 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3825 }
3826 sp< AudioPatch> patchDesc;
3827 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3828 if (index >= 0) {
3829 patchDesc = mAudioPatches.valueAt(index);
3830 afPatchHandle = patchDesc->mAfPatchHandle;
3831 }
3832
Eric Laurent1c333e22014-05-20 10:48:17 -07003833 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003834 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07003835 0);
3836 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003837 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07003838 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003839 if (index < 0) {
3840 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3841 &patch, mUidCached);
3842 addAudioPatch(patchDesc->mHandle, patchDesc);
3843 } else {
3844 patchDesc->mPatch = patch;
3845 }
3846 patchDesc->mAfPatchHandle = afPatchHandle;
3847 patchDesc->mUid = mUidCached;
3848 if (patchHandle) {
3849 *patchHandle = patchDesc->mHandle;
3850 }
3851 inputDesc->mPatchHandle = patchDesc->mHandle;
3852 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003853 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003854 }
3855 }
3856 }
3857 return status;
3858}
3859
Eric Laurent6a94d692014-05-20 11:18:06 -07003860status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
3861 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003862{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003863 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003864 ssize_t index;
3865 if (patchHandle) {
3866 index = mAudioPatches.indexOfKey(*patchHandle);
3867 } else {
3868 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3869 }
3870 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003871 return INVALID_OPERATION;
3872 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003873 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3874 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07003875 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
3876 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003877 removeAudioPatch(patchDesc->mHandle);
3878 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003879 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003880 return status;
3881}
3882
3883sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07003884 uint32_t samplingRate,
3885 audio_format_t format,
3886 audio_channel_mask_t channelMask)
3887{
3888 // Choose an input profile based on the requested capture parameters: select the first available
3889 // profile supporting all requested parameters.
3890
3891 for (size_t i = 0; i < mHwModules.size(); i++)
3892 {
3893 if (mHwModules[i]->mHandle == 0) {
3894 continue;
3895 }
3896 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3897 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003898 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003899 // profile->log();
Eric Laurente552edb2014-03-10 17:42:56 -07003900 if (profile->isCompatibleProfile(device, samplingRate, format,
3901 channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
3902 return profile;
3903 }
3904 }
3905 }
3906 return NULL;
3907}
3908
Eric Laurente0720872014-03-11 09:30:41 -07003909audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
Eric Laurente552edb2014-03-10 17:42:56 -07003910{
3911 uint32_t device = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003912 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
3913 ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003914 switch (inputSource) {
3915 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003916 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003917 device = AUDIO_DEVICE_IN_VOICE_CALL;
3918 break;
3919 }
3920 // FALL THROUGH
3921
3922 case AUDIO_SOURCE_DEFAULT:
3923 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07003924 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
3925 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
3926 break;
3927 }
3928 // FALL THROUGH
3929
Eric Laurente552edb2014-03-10 17:42:56 -07003930 case AUDIO_SOURCE_VOICE_RECOGNITION:
3931 case AUDIO_SOURCE_HOTWORD:
3932 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurent3b73df72014-03-11 09:06:29 -07003933 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003934 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003935 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003936 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003937 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07003938 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
3939 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003940 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003941 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3942 }
3943 break;
3944 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003945 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003946 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003947 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003948 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3949 }
3950 break;
3951 case AUDIO_SOURCE_VOICE_DOWNLINK:
3952 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003953 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003954 device = AUDIO_DEVICE_IN_VOICE_CALL;
3955 }
3956 break;
3957 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003958 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07003959 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
3960 }
3961 break;
3962 default:
3963 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
3964 break;
3965 }
3966 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
3967 return device;
3968}
3969
Eric Laurente0720872014-03-11 09:30:41 -07003970bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003971{
3972 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
3973 device &= ~AUDIO_DEVICE_BIT_IN;
3974 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
3975 return true;
3976 }
3977 return false;
3978}
3979
Eric Laurente0720872014-03-11 09:30:41 -07003980audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003981{
3982 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003983 const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003984 if ((input_descriptor->mRefCount > 0)
3985 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
3986 return mInputs.keyAt(i);
3987 }
3988 }
3989 return 0;
3990}
3991
3992
Eric Laurente0720872014-03-11 09:30:41 -07003993audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003994{
3995 if (device == AUDIO_DEVICE_NONE) {
3996 // this happens when forcing a route update and no track is active on an output.
3997 // In this case the returned category is not important.
3998 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent3b73df72014-03-11 09:06:29 -07003999 } else if (popcount(device) > 1) {
Eric Laurente552edb2014-03-10 17:42:56 -07004000 // Multiple device selection is either:
4001 // - speaker + one other device: give priority to speaker in this case.
4002 // - one A2DP device + another device: happens with duplicated output. In this case
4003 // retain the device on the A2DP output as the other must not correspond to an active
4004 // selection if not the speaker.
4005 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
4006 device = AUDIO_DEVICE_OUT_SPEAKER;
4007 } else {
4008 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
4009 }
4010 }
4011
Eric Laurent3b73df72014-03-11 09:06:29 -07004012 ALOGW_IF(popcount(device) != 1,
Eric Laurente552edb2014-03-10 17:42:56 -07004013 "getDeviceForVolume() invalid device combination: %08x",
4014 device);
4015
4016 return device;
4017}
4018
Eric Laurente0720872014-03-11 09:30:41 -07004019AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004020{
4021 switch(getDeviceForVolume(device)) {
4022 case AUDIO_DEVICE_OUT_EARPIECE:
4023 return DEVICE_CATEGORY_EARPIECE;
4024 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
4025 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
4026 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
4027 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
4028 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
4029 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
4030 return DEVICE_CATEGORY_HEADSET;
4031 case AUDIO_DEVICE_OUT_SPEAKER:
4032 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
4033 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
4034 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
4035 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
4036 case AUDIO_DEVICE_OUT_USB_DEVICE:
4037 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
4038 default:
4039 return DEVICE_CATEGORY_SPEAKER;
4040 }
4041}
4042
Eric Laurente0720872014-03-11 09:30:41 -07004043float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004044 int indexInUi)
4045{
4046 device_category deviceCategory = getDeviceCategory(device);
4047 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
4048
4049 // the volume index in the UI is relative to the min and max volume indices for this stream type
4050 int nbSteps = 1 + curve[VOLMAX].mIndex -
4051 curve[VOLMIN].mIndex;
4052 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
4053 (streamDesc.mIndexMax - streamDesc.mIndexMin);
4054
4055 // find what part of the curve this index volume belongs to, or if it's out of bounds
4056 int segment = 0;
4057 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
4058 return 0.0f;
4059 } else if (volIdx < curve[VOLKNEE1].mIndex) {
4060 segment = 0;
4061 } else if (volIdx < curve[VOLKNEE2].mIndex) {
4062 segment = 1;
4063 } else if (volIdx <= curve[VOLMAX].mIndex) {
4064 segment = 2;
4065 } else { // out of bounds
4066 return 1.0f;
4067 }
4068
4069 // linear interpolation in the attenuation table in dB
4070 float decibels = curve[segment].mDBAttenuation +
4071 ((float)(volIdx - curve[segment].mIndex)) *
4072 ( (curve[segment+1].mDBAttenuation -
4073 curve[segment].mDBAttenuation) /
4074 ((float)(curve[segment+1].mIndex -
4075 curve[segment].mIndex)) );
4076
4077 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
4078
4079 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
4080 curve[segment].mIndex, volIdx,
4081 curve[segment+1].mIndex,
4082 curve[segment].mDBAttenuation,
4083 decibels,
4084 curve[segment+1].mDBAttenuation,
4085 amplification);
4086
4087 return amplification;
4088}
4089
Eric Laurente0720872014-03-11 09:30:41 -07004090const AudioPolicyManager::VolumeCurvePoint
4091 AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004092 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
4093};
4094
Eric Laurente0720872014-03-11 09:30:41 -07004095const AudioPolicyManager::VolumeCurvePoint
4096 AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004097 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
4098};
4099
Eric Laurente0720872014-03-11 09:30:41 -07004100const AudioPolicyManager::VolumeCurvePoint
4101 AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004102 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
4103};
4104
Eric Laurente0720872014-03-11 09:30:41 -07004105const AudioPolicyManager::VolumeCurvePoint
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07004106 AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Jean-Michel Trivi98c60432014-07-09 08:51:34 -07004107 {1, -55.0f}, {20, -43.0f}, {86, -12.0f}, {100, 0.0f}
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07004108};
4109
4110const AudioPolicyManager::VolumeCurvePoint
Eric Laurente0720872014-03-11 09:30:41 -07004111 AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004112 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
4113};
4114
Eric Laurente0720872014-03-11 09:30:41 -07004115const AudioPolicyManager::VolumeCurvePoint
4116 AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004117 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
4118};
4119
4120// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
4121// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
4122// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
4123// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
4124
Eric Laurente0720872014-03-11 09:30:41 -07004125const AudioPolicyManager::VolumeCurvePoint
4126 AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004127 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
4128};
4129
Eric Laurente0720872014-03-11 09:30:41 -07004130const AudioPolicyManager::VolumeCurvePoint
4131 AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004132 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
4133};
4134
Eric Laurente0720872014-03-11 09:30:41 -07004135const AudioPolicyManager::VolumeCurvePoint
4136 AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004137 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
4138};
4139
Eric Laurente0720872014-03-11 09:30:41 -07004140const AudioPolicyManager::VolumeCurvePoint
4141 AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004142 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
4143};
4144
Eric Laurente0720872014-03-11 09:30:41 -07004145const AudioPolicyManager::VolumeCurvePoint
4146 AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004147 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
4148};
4149
Eric Laurente0720872014-03-11 09:30:41 -07004150const AudioPolicyManager::VolumeCurvePoint
4151 *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT]
4152 [AudioPolicyManager::DEVICE_CATEGORY_CNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004153 { // AUDIO_STREAM_VOICE_CALL
4154 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
4155 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4156 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
4157 },
4158 { // AUDIO_STREAM_SYSTEM
4159 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4160 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4161 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4162 },
4163 { // AUDIO_STREAM_RING
4164 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4165 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4166 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4167 },
4168 { // AUDIO_STREAM_MUSIC
4169 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4170 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4171 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4172 },
4173 { // AUDIO_STREAM_ALARM
4174 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4175 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4176 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4177 },
4178 { // AUDIO_STREAM_NOTIFICATION
4179 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4180 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4181 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4182 },
4183 { // AUDIO_STREAM_BLUETOOTH_SCO
4184 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
4185 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4186 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
4187 },
4188 { // AUDIO_STREAM_ENFORCED_AUDIBLE
4189 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4190 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4191 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4192 },
4193 { // AUDIO_STREAM_DTMF
4194 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4195 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4196 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4197 },
4198 { // AUDIO_STREAM_TTS
4199 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4200 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4201 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4202 },
4203};
4204
Eric Laurente0720872014-03-11 09:30:41 -07004205void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07004206{
4207 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
4208 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
4209 mStreams[i].mVolumeCurve[j] =
4210 sVolumeProfiles[i][j];
4211 }
4212 }
4213
4214 // Check availability of DRC on speaker path: if available, override some of the speaker curves
4215 if (mSpeakerDrcEnabled) {
4216 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4217 sDefaultSystemVolumeCurveDrc;
4218 mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4219 sSpeakerSonificationVolumeCurveDrc;
4220 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4221 sSpeakerSonificationVolumeCurveDrc;
4222 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4223 sSpeakerSonificationVolumeCurveDrc;
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07004224 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4225 sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07004226 }
4227}
4228
Eric Laurente0720872014-03-11 09:30:41 -07004229float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004230 int index,
4231 audio_io_handle_t output,
4232 audio_devices_t device)
4233{
4234 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004235 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004236 StreamDescriptor &streamDesc = mStreams[stream];
4237
4238 if (device == AUDIO_DEVICE_NONE) {
4239 device = outputDesc->device();
4240 }
4241
4242 // if volume is not 0 (not muted), force media volume to max on digital output
Eric Laurent3b73df72014-03-11 09:06:29 -07004243 if (stream == AUDIO_STREAM_MUSIC &&
Eric Laurente552edb2014-03-10 17:42:56 -07004244 index != mStreams[stream].mIndexMin &&
4245 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
Paul McLeanb919dd32014-06-26 13:16:44 -07004246 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004247 return 1.0;
4248 }
4249
4250 volume = volIndexToAmpl(device, streamDesc, index);
4251
4252 // if a headset is connected, apply the following rules to ring tones and notifications
4253 // to avoid sound level bursts in user's ears:
4254 // - always attenuate ring tones and notifications volume by 6dB
4255 // - if music is playing, always limit the volume to current music volume,
4256 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004257 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004258 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4259 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4260 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4261 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4262 ((stream_strategy == STRATEGY_SONIFICATION)
4263 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004264 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004265 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004266 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07004267 streamDesc.mCanBeMuted) {
4268 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4269 // when the phone is ringing we must consider that music could have been paused just before
4270 // by the music application and behave as if music was active if the last music track was
4271 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004272 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004273 mLimitRingtoneVolume) {
4274 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004275 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
4276 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004277 output,
4278 musicDevice);
4279 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4280 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4281 if (volume > minVol) {
4282 volume = minVol;
4283 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4284 }
4285 }
4286 }
4287
4288 return volume;
4289}
4290
Eric Laurente0720872014-03-11 09:30:41 -07004291status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004292 int index,
4293 audio_io_handle_t output,
4294 audio_devices_t device,
4295 int delayMs,
4296 bool force)
4297{
4298
4299 // do not change actual stream volume if the stream is muted
4300 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
4301 ALOGVV("checkAndSetVolume() stream %d muted count %d",
4302 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
4303 return NO_ERROR;
4304 }
4305
4306 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07004307 if ((stream == AUDIO_STREAM_VOICE_CALL &&
4308 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4309 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
4310 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004311 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07004312 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07004313 return INVALID_OPERATION;
4314 }
4315
4316 float volume = computeVolume(stream, index, output, device);
4317 // We actually change the volume if:
4318 // - the float value returned by computeVolume() changed
4319 // - the force flag is set
4320 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
4321 force) {
4322 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
4323 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
4324 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
4325 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07004326 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
4327 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004328 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004329 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004330 }
4331
Eric Laurent3b73df72014-03-11 09:06:29 -07004332 if (stream == AUDIO_STREAM_VOICE_CALL ||
4333 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004334 float voiceVolume;
4335 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004336 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004337 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
4338 } else {
4339 voiceVolume = 1.0;
4340 }
4341
4342 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
4343 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4344 mLastVoiceVolume = voiceVolume;
4345 }
4346 }
4347
4348 return NO_ERROR;
4349}
4350
Eric Laurente0720872014-03-11 09:30:41 -07004351void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004352 audio_devices_t device,
4353 int delayMs,
4354 bool force)
4355{
4356 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
4357
Eric Laurent3b73df72014-03-11 09:06:29 -07004358 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4359 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004360 mStreams[stream].getVolumeIndex(device),
4361 output,
4362 device,
4363 delayMs,
4364 force);
4365 }
4366}
4367
Eric Laurente0720872014-03-11 09:30:41 -07004368void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004369 bool on,
4370 audio_io_handle_t output,
4371 int delayMs,
4372 audio_devices_t device)
4373{
4374 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004375 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4376 if (getStrategy((audio_stream_type_t)stream) == strategy) {
4377 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004378 }
4379 }
4380}
4381
Eric Laurente0720872014-03-11 09:30:41 -07004382void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004383 bool on,
4384 audio_io_handle_t output,
4385 int delayMs,
4386 audio_devices_t device)
4387{
4388 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07004389 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004390 if (device == AUDIO_DEVICE_NONE) {
4391 device = outputDesc->device();
4392 }
4393
4394 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
4395 stream, on, output, outputDesc->mMuteCount[stream], device);
4396
4397 if (on) {
4398 if (outputDesc->mMuteCount[stream] == 0) {
4399 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004400 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
4401 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004402 checkAndSetVolume(stream, 0, output, device, delayMs);
4403 }
4404 }
4405 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4406 outputDesc->mMuteCount[stream]++;
4407 } else {
4408 if (outputDesc->mMuteCount[stream] == 0) {
4409 ALOGV("setStreamMute() unmuting non muted stream!");
4410 return;
4411 }
4412 if (--outputDesc->mMuteCount[stream] == 0) {
4413 checkAndSetVolume(stream,
4414 streamDesc.getVolumeIndex(device),
4415 output,
4416 device,
4417 delayMs);
4418 }
4419 }
4420}
4421
Eric Laurente0720872014-03-11 09:30:41 -07004422void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004423 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004424{
4425 // if the stream pertains to sonification strategy and we are in call we must
4426 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4427 // in the device used for phone strategy and play the tone if the selected device does not
4428 // interfere with the device used for phone strategy
4429 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4430 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004431 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004432 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4433 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004434 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004435 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4436 stream, starting, outputDesc->mDevice, stateChange);
4437 if (outputDesc->mRefCount[stream]) {
4438 int muteCount = 1;
4439 if (stateChange) {
4440 muteCount = outputDesc->mRefCount[stream];
4441 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004442 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004443 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4444 for (int i = 0; i < muteCount; i++) {
4445 setStreamMute(stream, starting, mPrimaryOutput);
4446 }
4447 } else {
4448 ALOGV("handleIncallSonification() high visibility");
4449 if (outputDesc->device() &
4450 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4451 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4452 for (int i = 0; i < muteCount; i++) {
4453 setStreamMute(stream, starting, mPrimaryOutput);
4454 }
4455 }
4456 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004457 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4458 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004459 } else {
4460 mpClientInterface->stopTone();
4461 }
4462 }
4463 }
4464 }
4465}
4466
Eric Laurente0720872014-03-11 09:30:41 -07004467bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07004468{
4469 return isStateInCall(mPhoneState);
4470}
4471
Eric Laurente0720872014-03-11 09:30:41 -07004472bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004473 return ((state == AUDIO_MODE_IN_CALL) ||
4474 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07004475}
4476
Eric Laurente0720872014-03-11 09:30:41 -07004477uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07004478{
4479 return MAX_EFFECTS_CPU_LOAD;
4480}
4481
Eric Laurente0720872014-03-11 09:30:41 -07004482uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07004483{
4484 return MAX_EFFECTS_MEMORY;
4485}
4486
Eric Laurent6a94d692014-05-20 11:18:06 -07004487
Eric Laurente552edb2014-03-10 17:42:56 -07004488// --- AudioOutputDescriptor class implementation
4489
Eric Laurente0720872014-03-11 09:30:41 -07004490AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor(
Eric Laurent1c333e22014-05-20 10:48:17 -07004491 const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07004492 : mId(0), mIoHandle(0), mLatency(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07004493 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0),
Eric Laurente552edb2014-03-10 17:42:56 -07004494 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
4495{
4496 // clear usage count for all stream types
Eric Laurent3b73df72014-03-11 09:06:29 -07004497 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004498 mRefCount[i] = 0;
4499 mCurVolume[i] = -1.0;
4500 mMuteCount[i] = 0;
4501 mStopTime[i] = 0;
4502 }
4503 for (int i = 0; i < NUM_STRATEGIES; i++) {
4504 mStrategyMutedByDevice[i] = false;
4505 }
4506 if (profile != NULL) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004507 mAudioPort = profile;
Eric Laurent1e693b52014-07-09 15:03:28 -07004508 mSamplingRate = profile->pickSamplingRate();
4509 mFormat = profile->pickFormat();
4510 mChannelMask = profile->pickChannelMask();
Eric Laurenta121f902014-06-03 13:32:54 -07004511 if (profile->mGains.size() > 0) {
4512 profile->mGains[0]->getDefaultConfig(&mGain);
4513 }
Eric Laurente552edb2014-03-10 17:42:56 -07004514 mFlags = profile->mFlags;
4515 }
4516}
4517
Eric Laurente0720872014-03-11 09:30:41 -07004518audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const
Eric Laurente552edb2014-03-10 17:42:56 -07004519{
4520 if (isDuplicated()) {
4521 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
4522 } else {
4523 return mDevice;
4524 }
4525}
4526
Eric Laurente0720872014-03-11 09:30:41 -07004527uint32_t AudioPolicyManager::AudioOutputDescriptor::latency()
Eric Laurente552edb2014-03-10 17:42:56 -07004528{
4529 if (isDuplicated()) {
4530 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
4531 } else {
4532 return mLatency;
4533 }
4534}
4535
Eric Laurente0720872014-03-11 09:30:41 -07004536bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith(
Eric Laurent1f2f2232014-06-02 12:01:23 -07004537 const sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07004538{
4539 if (isDuplicated()) {
4540 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
4541 } else if (outputDesc->isDuplicated()){
4542 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
4543 } else {
4544 return (mProfile->mModule == outputDesc->mProfile->mModule);
4545 }
4546}
4547
Eric Laurente0720872014-03-11 09:30:41 -07004548void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004549 int delta)
Eric Laurente552edb2014-03-10 17:42:56 -07004550{
4551 // forward usage count change to attached outputs
4552 if (isDuplicated()) {
4553 mOutput1->changeRefCount(stream, delta);
4554 mOutput2->changeRefCount(stream, delta);
4555 }
4556 if ((delta + (int)mRefCount[stream]) < 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004557 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d",
4558 delta, stream, mRefCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004559 mRefCount[stream] = 0;
4560 return;
4561 }
4562 mRefCount[stream] += delta;
4563 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
4564}
4565
Eric Laurente0720872014-03-11 09:30:41 -07004566audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices()
Eric Laurente552edb2014-03-10 17:42:56 -07004567{
4568 if (isDuplicated()) {
4569 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
4570 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004571 return mProfile->mSupportedDevices.types() ;
Eric Laurente552edb2014-03-10 17:42:56 -07004572 }
4573}
4574
Eric Laurente0720872014-03-11 09:30:41 -07004575bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07004576{
4577 return isStrategyActive(NUM_STRATEGIES, inPastMs);
4578}
4579
Eric Laurente0720872014-03-11 09:30:41 -07004580bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004581 uint32_t inPastMs,
4582 nsecs_t sysTime) const
4583{
4584 if ((sysTime == 0) && (inPastMs != 0)) {
4585 sysTime = systemTime();
4586 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004587 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4588 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004589 (NUM_STRATEGIES == strategy)) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004590 isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004591 return true;
4592 }
4593 }
4594 return false;
4595}
4596
Eric Laurente0720872014-03-11 09:30:41 -07004597bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004598 uint32_t inPastMs,
4599 nsecs_t sysTime) const
4600{
4601 if (mRefCount[stream] != 0) {
4602 return true;
4603 }
4604 if (inPastMs == 0) {
4605 return false;
4606 }
4607 if (sysTime == 0) {
4608 sysTime = systemTime();
4609 }
4610 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
4611 return true;
4612 }
4613 return false;
4614}
4615
Eric Laurent1c333e22014-05-20 10:48:17 -07004616void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004617 struct audio_port_config *dstConfig,
4618 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07004619{
Eric Laurent84c70242014-06-23 08:46:27 -07004620 ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle);
4621
Eric Laurent1f2f2232014-06-02 12:01:23 -07004622 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
4623 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
4624 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07004625 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004626 }
4627 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
4628
Eric Laurent6a94d692014-05-20 11:18:06 -07004629 dstConfig->id = mId;
4630 dstConfig->role = AUDIO_PORT_ROLE_SOURCE;
4631 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent6a94d692014-05-20 11:18:06 -07004632 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
4633 dstConfig->ext.mix.handle = mIoHandle;
4634 dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
Eric Laurent1c333e22014-05-20 10:48:17 -07004635}
4636
4637void AudioPolicyManager::AudioOutputDescriptor::toAudioPort(
4638 struct audio_port *port) const
4639{
Eric Laurent84c70242014-06-23 08:46:27 -07004640 ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004641 mProfile->toAudioPort(port);
4642 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004643 toAudioPortConfig(&port->active_config);
4644 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07004645 port->ext.mix.handle = mIoHandle;
4646 port->ext.mix.latency_class =
4647 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL;
4648}
Eric Laurente552edb2014-03-10 17:42:56 -07004649
Eric Laurente0720872014-03-11 09:30:41 -07004650status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004651{
4652 const size_t SIZE = 256;
4653 char buffer[SIZE];
4654 String8 result;
4655
4656 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4657 result.append(buffer);
4658 snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
4659 result.append(buffer);
4660 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4661 result.append(buffer);
4662 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
4663 result.append(buffer);
4664 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
4665 result.append(buffer);
4666 snprintf(buffer, SIZE, " Devices %08x\n", device());
4667 result.append(buffer);
4668 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
4669 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07004670 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4671 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n",
4672 i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07004673 result.append(buffer);
4674 }
4675 write(fd, result.string(), result.size());
4676
4677 return NO_ERROR;
4678}
4679
4680// --- AudioInputDescriptor class implementation
4681
Eric Laurent1c333e22014-05-20 10:48:17 -07004682AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07004683 : mId(0), mIoHandle(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07004684 mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0),
Eric Laurent3b73df72014-03-11 09:06:29 -07004685 mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile)
Eric Laurente552edb2014-03-10 17:42:56 -07004686{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004687 if (profile != NULL) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004688 mAudioPort = profile;
Eric Laurent1e693b52014-07-09 15:03:28 -07004689 mSamplingRate = profile->pickSamplingRate();
4690 mFormat = profile->pickFormat();
4691 mChannelMask = profile->pickChannelMask();
Eric Laurenta121f902014-06-03 13:32:54 -07004692 if (profile->mGains.size() > 0) {
4693 profile->mGains[0]->getDefaultConfig(&mGain);
4694 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004695 }
Eric Laurente552edb2014-03-10 17:42:56 -07004696}
4697
Eric Laurent1c333e22014-05-20 10:48:17 -07004698void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004699 struct audio_port_config *dstConfig,
4700 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07004701{
Eric Laurent84c70242014-06-23 08:46:27 -07004702 ALOG_ASSERT(mProfile != 0,
4703 "toAudioPortConfig() called on input with null profile %d", mIoHandle);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004704 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
4705 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
4706 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07004707 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004708 }
4709
4710 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
4711
Eric Laurent6a94d692014-05-20 11:18:06 -07004712 dstConfig->id = mId;
4713 dstConfig->role = AUDIO_PORT_ROLE_SINK;
4714 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent62aaabb2014-06-02 10:40:54 -07004715 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
4716 dstConfig->ext.mix.handle = mIoHandle;
4717 dstConfig->ext.mix.usecase.source = mInputSource;
Eric Laurent1c333e22014-05-20 10:48:17 -07004718}
4719
4720void AudioPolicyManager::AudioInputDescriptor::toAudioPort(
4721 struct audio_port *port) const
4722{
Eric Laurent84c70242014-06-23 08:46:27 -07004723 ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
4724
Eric Laurent1c333e22014-05-20 10:48:17 -07004725 mProfile->toAudioPort(port);
4726 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004727 toAudioPortConfig(&port->active_config);
4728 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07004729 port->ext.mix.handle = mIoHandle;
4730 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
4731}
4732
Eric Laurente0720872014-03-11 09:30:41 -07004733status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004734{
4735 const size_t SIZE = 256;
4736 char buffer[SIZE];
4737 String8 result;
4738
4739 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4740 result.append(buffer);
4741 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
4742 result.append(buffer);
4743 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4744 result.append(buffer);
4745 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
4746 result.append(buffer);
4747 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
4748 result.append(buffer);
4749 write(fd, result.string(), result.size());
4750
4751 return NO_ERROR;
4752}
4753
4754// --- StreamDescriptor class implementation
4755
Eric Laurente0720872014-03-11 09:30:41 -07004756AudioPolicyManager::StreamDescriptor::StreamDescriptor()
Eric Laurente552edb2014-03-10 17:42:56 -07004757 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
4758{
4759 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
4760}
4761
Eric Laurente0720872014-03-11 09:30:41 -07004762int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004763{
Eric Laurente0720872014-03-11 09:30:41 -07004764 device = AudioPolicyManager::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07004765 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
4766 if (mIndexCur.indexOfKey(device) < 0) {
4767 device = AUDIO_DEVICE_OUT_DEFAULT;
4768 }
4769 return mIndexCur.valueFor(device);
4770}
4771
Eric Laurente0720872014-03-11 09:30:41 -07004772void AudioPolicyManager::StreamDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004773{
4774 const size_t SIZE = 256;
4775 char buffer[SIZE];
4776 String8 result;
4777
4778 snprintf(buffer, SIZE, "%s %02d %02d ",
4779 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
4780 result.append(buffer);
4781 for (size_t i = 0; i < mIndexCur.size(); i++) {
4782 snprintf(buffer, SIZE, "%04x : %02d, ",
4783 mIndexCur.keyAt(i),
4784 mIndexCur.valueAt(i));
4785 result.append(buffer);
4786 }
4787 result.append("\n");
4788
4789 write(fd, result.string(), result.size());
4790}
4791
4792// --- EffectDescriptor class implementation
4793
Eric Laurente0720872014-03-11 09:30:41 -07004794status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004795{
4796 const size_t SIZE = 256;
4797 char buffer[SIZE];
4798 String8 result;
4799
4800 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
4801 result.append(buffer);
4802 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
4803 result.append(buffer);
4804 snprintf(buffer, SIZE, " Session: %d\n", mSession);
4805 result.append(buffer);
4806 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
4807 result.append(buffer);
4808 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
4809 result.append(buffer);
4810 write(fd, result.string(), result.size());
4811
4812 return NO_ERROR;
4813}
4814
Eric Laurent1c333e22014-05-20 10:48:17 -07004815// --- HwModule class implementation
Eric Laurente552edb2014-03-10 17:42:56 -07004816
Eric Laurente0720872014-03-11 09:30:41 -07004817AudioPolicyManager::HwModule::HwModule(const char *name)
Eric Laurenteb108a42014-06-06 14:56:52 -07004818 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)),
4819 mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0)
Eric Laurente552edb2014-03-10 17:42:56 -07004820{
4821}
4822
Eric Laurente0720872014-03-11 09:30:41 -07004823AudioPolicyManager::HwModule::~HwModule()
Eric Laurente552edb2014-03-10 17:42:56 -07004824{
4825 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004826 mOutputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004827 }
4828 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004829 mInputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004830 }
4831 free((void *)mName);
4832}
4833
Eric Laurent1afeecb2014-05-14 08:52:28 -07004834status_t AudioPolicyManager::HwModule::loadInput(cnode *root)
4835{
4836 cnode *node = root->first_child;
4837
4838 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this);
4839
4840 while (node) {
4841 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4842 profile->loadSamplingRates((char *)node->value);
4843 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4844 profile->loadFormats((char *)node->value);
4845 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4846 profile->loadInChannels((char *)node->value);
4847 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4848 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4849 mDeclaredDevices);
4850 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4851 profile->loadGains(node);
4852 }
4853 node = node->next;
4854 }
4855 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4856 "loadInput() invalid supported devices");
4857 ALOGW_IF(profile->mChannelMasks.size() == 0,
4858 "loadInput() invalid supported channel masks");
4859 ALOGW_IF(profile->mSamplingRates.size() == 0,
4860 "loadInput() invalid supported sampling rates");
4861 ALOGW_IF(profile->mFormats.size() == 0,
4862 "loadInput() invalid supported formats");
4863 if (!profile->mSupportedDevices.isEmpty() &&
4864 (profile->mChannelMasks.size() != 0) &&
4865 (profile->mSamplingRates.size() != 0) &&
4866 (profile->mFormats.size() != 0)) {
4867
4868 ALOGV("loadInput() adding input Supported Devices %04x",
4869 profile->mSupportedDevices.types());
4870
4871 mInputProfiles.add(profile);
4872 return NO_ERROR;
4873 } else {
4874 return BAD_VALUE;
4875 }
4876}
4877
4878status_t AudioPolicyManager::HwModule::loadOutput(cnode *root)
4879{
4880 cnode *node = root->first_child;
4881
4882 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this);
4883
4884 while (node) {
4885 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4886 profile->loadSamplingRates((char *)node->value);
4887 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4888 profile->loadFormats((char *)node->value);
4889 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4890 profile->loadOutChannels((char *)node->value);
4891 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4892 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4893 mDeclaredDevices);
4894 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
4895 profile->mFlags = parseFlagNames((char *)node->value);
4896 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4897 profile->loadGains(node);
4898 }
4899 node = node->next;
4900 }
4901 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4902 "loadOutput() invalid supported devices");
4903 ALOGW_IF(profile->mChannelMasks.size() == 0,
4904 "loadOutput() invalid supported channel masks");
4905 ALOGW_IF(profile->mSamplingRates.size() == 0,
4906 "loadOutput() invalid supported sampling rates");
4907 ALOGW_IF(profile->mFormats.size() == 0,
4908 "loadOutput() invalid supported formats");
4909 if (!profile->mSupportedDevices.isEmpty() &&
4910 (profile->mChannelMasks.size() != 0) &&
4911 (profile->mSamplingRates.size() != 0) &&
4912 (profile->mFormats.size() != 0)) {
4913
4914 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x",
4915 profile->mSupportedDevices.types(), profile->mFlags);
4916
4917 mOutputProfiles.add(profile);
4918 return NO_ERROR;
4919 } else {
4920 return BAD_VALUE;
4921 }
4922}
4923
4924status_t AudioPolicyManager::HwModule::loadDevice(cnode *root)
4925{
4926 cnode *node = root->first_child;
4927
4928 audio_devices_t type = AUDIO_DEVICE_NONE;
4929 while (node) {
4930 if (strcmp(node->name, DEVICE_TYPE) == 0) {
4931 type = parseDeviceNames((char *)node->value);
4932 break;
4933 }
4934 node = node->next;
4935 }
4936 if (type == AUDIO_DEVICE_NONE ||
4937 (!audio_is_input_device(type) && !audio_is_output_device(type))) {
4938 ALOGW("loadDevice() bad type %08x", type);
4939 return BAD_VALUE;
4940 }
4941 sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type);
4942 deviceDesc->mModule = this;
4943
4944 node = root->first_child;
4945 while (node) {
4946 if (strcmp(node->name, DEVICE_ADDRESS) == 0) {
4947 deviceDesc->mAddress = String8((char *)node->value);
4948 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4949 if (audio_is_input_device(type)) {
4950 deviceDesc->loadInChannels((char *)node->value);
4951 } else {
4952 deviceDesc->loadOutChannels((char *)node->value);
4953 }
4954 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4955 deviceDesc->loadGains(node);
4956 }
4957 node = node->next;
4958 }
4959
4960 ALOGV("loadDevice() adding device name %s type %08x address %s",
4961 deviceDesc->mName.string(), type, deviceDesc->mAddress.string());
4962
4963 mDeclaredDevices.add(deviceDesc);
4964
4965 return NO_ERROR;
4966}
4967
Eric Laurente0720872014-03-11 09:30:41 -07004968void AudioPolicyManager::HwModule::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004969{
4970 const size_t SIZE = 256;
4971 char buffer[SIZE];
4972 String8 result;
4973
4974 snprintf(buffer, SIZE, " - name: %s\n", mName);
4975 result.append(buffer);
4976 snprintf(buffer, SIZE, " - handle: %d\n", mHandle);
4977 result.append(buffer);
Eric Laurenteb108a42014-06-06 14:56:52 -07004978 snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF);
4979 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07004980 write(fd, result.string(), result.size());
4981 if (mOutputProfiles.size()) {
4982 write(fd, " - outputs:\n", strlen(" - outputs:\n"));
4983 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004984 snprintf(buffer, SIZE, " output %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004985 write(fd, buffer, strlen(buffer));
4986 mOutputProfiles[i]->dump(fd);
4987 }
4988 }
4989 if (mInputProfiles.size()) {
4990 write(fd, " - inputs:\n", strlen(" - inputs:\n"));
4991 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004992 snprintf(buffer, SIZE, " input %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004993 write(fd, buffer, strlen(buffer));
4994 mInputProfiles[i]->dump(fd);
4995 }
4996 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07004997 if (mDeclaredDevices.size()) {
4998 write(fd, " - devices:\n", strlen(" - devices:\n"));
4999 for (size_t i = 0; i < mDeclaredDevices.size(); i++) {
5000 mDeclaredDevices[i]->dump(fd, 4, i);
5001 }
5002 }
Eric Laurente552edb2014-03-10 17:42:56 -07005003}
5004
Eric Laurent1c333e22014-05-20 10:48:17 -07005005// --- AudioPort class implementation
5006
Eric Laurenta121f902014-06-03 13:32:54 -07005007
5008AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type,
5009 audio_port_role_t role, const sp<HwModule>& module) :
Eric Laurent1e693b52014-07-09 15:03:28 -07005010 mName(name), mType(type), mRole(role), mModule(module), mFlags((audio_output_flags_t)0)
Eric Laurenta121f902014-06-03 13:32:54 -07005011{
5012 mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) ||
5013 ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK));
5014}
5015
Eric Laurent1c333e22014-05-20 10:48:17 -07005016void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const
5017{
5018 port->role = mRole;
5019 port->type = mType;
5020 unsigned int i;
5021 for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) {
5022 port->sample_rates[i] = mSamplingRates[i];
5023 }
5024 port->num_sample_rates = i;
5025 for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) {
5026 port->channel_masks[i] = mChannelMasks[i];
5027 }
5028 port->num_channel_masks = i;
5029 for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) {
5030 port->formats[i] = mFormats[i];
5031 }
5032 port->num_formats = i;
Eric Laurente1715a42014-05-20 11:30:42 -07005033
Mark Salyzynbeb9e302014-06-18 16:33:15 -07005034 ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size());
Eric Laurente1715a42014-05-20 11:30:42 -07005035
5036 for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) {
5037 port->gains[i] = mGains[i]->mGain;
5038 }
5039 port->num_gains = i;
Eric Laurent1c333e22014-05-20 10:48:17 -07005040}
5041
5042
5043void AudioPolicyManager::AudioPort::loadSamplingRates(char *name)
5044{
5045 char *str = strtok(name, "|");
5046
5047 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
5048 // rates should be read from the output stream after it is opened for the first time
5049 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
5050 mSamplingRates.add(0);
5051 return;
5052 }
5053
5054 while (str != NULL) {
5055 uint32_t rate = atoi(str);
5056 if (rate != 0) {
5057 ALOGV("loadSamplingRates() adding rate %d", rate);
5058 mSamplingRates.add(rate);
5059 }
5060 str = strtok(NULL, "|");
5061 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005062}
5063
5064void AudioPolicyManager::AudioPort::loadFormats(char *name)
5065{
5066 char *str = strtok(name, "|");
5067
5068 // by convention, "0' in the first entry in mFormats indicates the supported formats
5069 // should be read from the output stream after it is opened for the first time
5070 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
5071 mFormats.add(AUDIO_FORMAT_DEFAULT);
5072 return;
5073 }
5074
5075 while (str != NULL) {
5076 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
5077 ARRAY_SIZE(sFormatNameToEnumTable),
5078 str);
5079 if (format != AUDIO_FORMAT_DEFAULT) {
5080 mFormats.add(format);
5081 }
5082 str = strtok(NULL, "|");
5083 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005084}
5085
5086void AudioPolicyManager::AudioPort::loadInChannels(char *name)
5087{
5088 const char *str = strtok(name, "|");
5089
5090 ALOGV("loadInChannels() %s", name);
5091
5092 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
5093 mChannelMasks.add(0);
5094 return;
5095 }
5096
5097 while (str != NULL) {
5098 audio_channel_mask_t channelMask =
5099 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
5100 ARRAY_SIZE(sInChannelsNameToEnumTable),
5101 str);
5102 if (channelMask != 0) {
5103 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
5104 mChannelMasks.add(channelMask);
5105 }
5106 str = strtok(NULL, "|");
5107 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005108}
5109
5110void AudioPolicyManager::AudioPort::loadOutChannels(char *name)
5111{
5112 const char *str = strtok(name, "|");
5113
5114 ALOGV("loadOutChannels() %s", name);
5115
5116 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
5117 // masks should be read from the output stream after it is opened for the first time
5118 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
5119 mChannelMasks.add(0);
5120 return;
5121 }
5122
5123 while (str != NULL) {
5124 audio_channel_mask_t channelMask =
5125 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
5126 ARRAY_SIZE(sOutChannelsNameToEnumTable),
5127 str);
5128 if (channelMask != 0) {
5129 mChannelMasks.add(channelMask);
5130 }
5131 str = strtok(NULL, "|");
5132 }
5133 return;
5134}
5135
Eric Laurent1afeecb2014-05-14 08:52:28 -07005136audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name)
5137{
5138 const char *str = strtok(name, "|");
5139
5140 ALOGV("loadGainMode() %s", name);
5141 audio_gain_mode_t mode = 0;
5142 while (str != NULL) {
5143 mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable,
5144 ARRAY_SIZE(sGainModeNameToEnumTable),
5145 str);
5146 str = strtok(NULL, "|");
5147 }
5148 return mode;
5149}
5150
Eric Laurenta121f902014-06-03 13:32:54 -07005151void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index)
Eric Laurent1afeecb2014-05-14 08:52:28 -07005152{
5153 cnode *node = root->first_child;
5154
Eric Laurenta121f902014-06-03 13:32:54 -07005155 sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005156
5157 while (node) {
5158 if (strcmp(node->name, GAIN_MODE) == 0) {
5159 gain->mGain.mode = loadGainMode((char *)node->value);
5160 } else if (strcmp(node->name, GAIN_CHANNELS) == 0) {
Eric Laurenta121f902014-06-03 13:32:54 -07005161 if (mUseInChannelMask) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005162 gain->mGain.channel_mask =
5163 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
5164 ARRAY_SIZE(sInChannelsNameToEnumTable),
5165 (char *)node->value);
5166 } else {
5167 gain->mGain.channel_mask =
5168 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
5169 ARRAY_SIZE(sOutChannelsNameToEnumTable),
5170 (char *)node->value);
5171 }
5172 } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) {
5173 gain->mGain.min_value = atoi((char *)node->value);
5174 } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) {
5175 gain->mGain.max_value = atoi((char *)node->value);
5176 } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) {
5177 gain->mGain.default_value = atoi((char *)node->value);
5178 } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) {
5179 gain->mGain.step_value = atoi((char *)node->value);
5180 } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) {
5181 gain->mGain.min_ramp_ms = atoi((char *)node->value);
5182 } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) {
5183 gain->mGain.max_ramp_ms = atoi((char *)node->value);
5184 }
5185 node = node->next;
5186 }
5187
5188 ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d",
5189 gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value);
5190
5191 if (gain->mGain.mode == 0) {
5192 return;
5193 }
5194 mGains.add(gain);
5195}
5196
5197void AudioPolicyManager::AudioPort::loadGains(cnode *root)
5198{
5199 cnode *node = root->first_child;
Eric Laurenta121f902014-06-03 13:32:54 -07005200 int index = 0;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005201 while (node) {
5202 ALOGV("loadGains() loading gain %s", node->name);
Eric Laurenta121f902014-06-03 13:32:54 -07005203 loadGain(node, index++);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005204 node = node->next;
5205 }
5206}
5207
Eric Laurenta121f902014-06-03 13:32:54 -07005208status_t AudioPolicyManager::AudioPort::checkSamplingRate(uint32_t samplingRate) const
5209{
5210 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
5211 if (mSamplingRates[i] == samplingRate) {
5212 return NO_ERROR;
5213 }
5214 }
5215 return BAD_VALUE;
5216}
5217
5218status_t AudioPolicyManager::AudioPort::checkChannelMask(audio_channel_mask_t channelMask) const
5219{
5220 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
5221 if (mChannelMasks[i] == channelMask) {
5222 return NO_ERROR;
5223 }
5224 }
5225 return BAD_VALUE;
5226}
5227
5228status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const
5229{
5230 for (size_t i = 0; i < mFormats.size(); i ++) {
5231 if (mFormats[i] == format) {
5232 return NO_ERROR;
5233 }
5234 }
5235 return BAD_VALUE;
5236}
5237
Eric Laurent1e693b52014-07-09 15:03:28 -07005238
5239uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const
5240{
5241 // special case for uninitialized dynamic profile
5242 if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) {
5243 return 0;
5244 }
5245
5246 uint32_t samplingRate = 0;
5247 uint32_t maxRate = MAX_MIXER_SAMPLING_RATE;
5248
5249 // For mixed output and inputs, use max mixer sampling rates. Do not
5250 // limit sampling rate otherwise
5251 if ((mType != AUDIO_PORT_TYPE_MIX) ||
5252 ((mRole == AUDIO_PORT_ROLE_SOURCE) &&
5253 (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) {
5254 maxRate = UINT_MAX;
5255 }
5256 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
5257 if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) {
5258 samplingRate = mSamplingRates[i];
5259 }
5260 }
5261 return samplingRate;
5262}
5263
5264audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const
5265{
5266 // special case for uninitialized dynamic profile
5267 if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) {
5268 return AUDIO_CHANNEL_NONE;
5269 }
5270
5271 audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE;
5272 uint32_t channelCount = 0;
5273 uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT;
5274
5275 // For mixed output and inputs, use max mixer channel count. Do not
5276 // limit channel count otherwise
5277 if ((mType != AUDIO_PORT_TYPE_MIX) ||
5278 ((mRole == AUDIO_PORT_ROLE_SOURCE) &&
5279 (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) {
5280 maxCount = UINT_MAX;
5281 }
5282 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
5283 uint32_t cnlCount;
5284 if (mUseInChannelMask) {
5285 cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]);
5286 } else {
5287 cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]);
5288 }
5289 if ((cnlCount > channelCount) && (cnlCount <= maxCount)) {
5290 channelMask = mChannelMasks[i];
5291 }
5292 }
5293 return channelMask;
5294}
5295
5296const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = {
5297 AUDIO_FORMAT_DEFAULT,
5298 AUDIO_FORMAT_PCM_16_BIT,
5299 AUDIO_FORMAT_PCM_24_BIT_PACKED,
5300};
5301
5302int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1,
5303 audio_format_t format2)
5304{
5305 // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any
5306 // compressed format and better than any PCM format. This is by design of pickFormat()
5307 if (!audio_is_linear_pcm(format1)) {
5308 if (!audio_is_linear_pcm(format2)) {
5309 return 0;
5310 }
5311 return 1;
5312 }
5313 if (!audio_is_linear_pcm(format2)) {
5314 return -1;
5315 }
5316
5317 int index1 = -1, index2 = -1;
5318 for (size_t i = 0;
5319 (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1));
5320 i ++) {
5321 if (sPcmFormatCompareTable[i] == format1) {
5322 index1 = i;
5323 }
5324 if (sPcmFormatCompareTable[i] == format2) {
5325 index2 = i;
5326 }
5327 }
5328 // format1 not found => index1 < 0 => format2 > format1
5329 // format2 not found => index2 < 0 => format2 < format1
5330 return index1 - index2;
5331}
5332
5333audio_format_t AudioPolicyManager::AudioPort::pickFormat() const
5334{
5335 // special case for uninitialized dynamic profile
5336 if (mFormats.size() == 1 && mFormats[0] == 0) {
5337 return AUDIO_FORMAT_DEFAULT;
5338 }
5339
5340 audio_format_t format = AUDIO_FORMAT_DEFAULT;
5341 audio_format_t bestFormat = BEST_MIXER_FORMAT;
5342 // For mixed output and inputs, use best mixer output format. Do not
5343 // limit format otherwise
5344 if ((mType != AUDIO_PORT_TYPE_MIX) ||
5345 ((mRole == AUDIO_PORT_ROLE_SOURCE) &&
5346 (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) == 0)))) {
5347 bestFormat = AUDIO_FORMAT_INVALID;
5348 }
5349
5350 for (size_t i = 0; i < mFormats.size(); i ++) {
5351 if ((compareFormats(mFormats[i], format) > 0) &&
5352 (compareFormats(mFormats[i], bestFormat) <= 0)) {
5353 format = mFormats[i];
5354 }
5355 }
5356 return format;
5357}
5358
Eric Laurenta121f902014-06-03 13:32:54 -07005359status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig,
5360 int index) const
5361{
5362 if (index < 0 || (size_t)index >= mGains.size()) {
5363 return BAD_VALUE;
5364 }
5365 return mGains[index]->checkConfig(gainConfig);
5366}
5367
Eric Laurent1afeecb2014-05-14 08:52:28 -07005368void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const
5369{
5370 const size_t SIZE = 256;
5371 char buffer[SIZE];
5372 String8 result;
5373
5374 if (mName.size() != 0) {
5375 snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string());
5376 result.append(buffer);
5377 }
5378
5379 if (mSamplingRates.size() != 0) {
5380 snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, "");
5381 result.append(buffer);
5382 for (size_t i = 0; i < mSamplingRates.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07005383 if (i == 0 && mSamplingRates[i] == 0) {
5384 snprintf(buffer, SIZE, "Dynamic");
5385 } else {
5386 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
5387 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005388 result.append(buffer);
5389 result.append(i == (mSamplingRates.size() - 1) ? "" : ", ");
5390 }
5391 result.append("\n");
5392 }
5393
5394 if (mChannelMasks.size() != 0) {
5395 snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, "");
5396 result.append(buffer);
5397 for (size_t i = 0; i < mChannelMasks.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07005398 ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]);
5399
5400 if (i == 0 && mChannelMasks[i] == 0) {
5401 snprintf(buffer, SIZE, "Dynamic");
5402 } else {
5403 snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
5404 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005405 result.append(buffer);
5406 result.append(i == (mChannelMasks.size() - 1) ? "" : ", ");
5407 }
5408 result.append("\n");
5409 }
5410
5411 if (mFormats.size() != 0) {
5412 snprintf(buffer, SIZE, "%*s- formats: ", spaces, "");
5413 result.append(buffer);
5414 for (size_t i = 0; i < mFormats.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07005415 const char *formatStr = enumToString(sFormatNameToEnumTable,
5416 ARRAY_SIZE(sFormatNameToEnumTable),
5417 mFormats[i]);
5418 if (i == 0 && strcmp(formatStr, "") == 0) {
5419 snprintf(buffer, SIZE, "Dynamic");
5420 } else {
5421 snprintf(buffer, SIZE, "%-48s", formatStr);
5422 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005423 result.append(buffer);
5424 result.append(i == (mFormats.size() - 1) ? "" : ", ");
5425 }
5426 result.append("\n");
5427 }
5428 write(fd, result.string(), result.size());
5429 if (mGains.size() != 0) {
5430 snprintf(buffer, SIZE, "%*s- gains:\n", spaces, "");
5431 write(fd, buffer, strlen(buffer) + 1);
5432 result.append(buffer);
5433 for (size_t i = 0; i < mGains.size(); i++) {
5434 mGains[i]->dump(fd, spaces + 2, i);
5435 }
5436 }
5437}
5438
5439// --- AudioGain class implementation
5440
Eric Laurenta121f902014-06-03 13:32:54 -07005441AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask)
Eric Laurent1afeecb2014-05-14 08:52:28 -07005442{
Eric Laurenta121f902014-06-03 13:32:54 -07005443 mIndex = index;
5444 mUseInChannelMask = useInChannelMask;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005445 memset(&mGain, 0, sizeof(struct audio_gain));
5446}
5447
Eric Laurenta121f902014-06-03 13:32:54 -07005448void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config)
5449{
5450 config->index = mIndex;
5451 config->mode = mGain.mode;
5452 config->channel_mask = mGain.channel_mask;
5453 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
5454 config->values[0] = mGain.default_value;
5455 } else {
5456 uint32_t numValues;
5457 if (mUseInChannelMask) {
5458 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
5459 } else {
5460 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
5461 }
5462 for (size_t i = 0; i < numValues; i++) {
5463 config->values[i] = mGain.default_value;
5464 }
5465 }
5466 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
5467 config->ramp_duration_ms = mGain.min_ramp_ms;
5468 }
5469}
5470
5471status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config)
5472{
5473 if ((config->mode & ~mGain.mode) != 0) {
5474 return BAD_VALUE;
5475 }
5476 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
5477 if ((config->values[0] < mGain.min_value) ||
5478 (config->values[0] > mGain.max_value)) {
5479 return BAD_VALUE;
5480 }
5481 } else {
5482 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
5483 return BAD_VALUE;
5484 }
5485 uint32_t numValues;
5486 if (mUseInChannelMask) {
5487 numValues = audio_channel_count_from_in_mask(config->channel_mask);
5488 } else {
5489 numValues = audio_channel_count_from_out_mask(config->channel_mask);
5490 }
5491 for (size_t i = 0; i < numValues; i++) {
5492 if ((config->values[i] < mGain.min_value) ||
5493 (config->values[i] > mGain.max_value)) {
5494 return BAD_VALUE;
5495 }
5496 }
5497 }
5498 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
5499 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
5500 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
5501 return BAD_VALUE;
5502 }
5503 }
5504 return NO_ERROR;
5505}
5506
Eric Laurent1afeecb2014-05-14 08:52:28 -07005507void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const
5508{
5509 const size_t SIZE = 256;
5510 char buffer[SIZE];
5511 String8 result;
5512
5513 snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
5514 result.append(buffer);
5515 snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
5516 result.append(buffer);
5517 snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
5518 result.append(buffer);
5519 snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
5520 result.append(buffer);
5521 snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
5522 result.append(buffer);
5523 snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
5524 result.append(buffer);
5525 snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
5526 result.append(buffer);
5527 snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
5528 result.append(buffer);
5529 snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
5530 result.append(buffer);
5531
5532 write(fd, result.string(), result.size());
5533}
5534
Eric Laurent1f2f2232014-06-02 12:01:23 -07005535// --- AudioPortConfig class implementation
5536
5537AudioPolicyManager::AudioPortConfig::AudioPortConfig()
5538{
5539 mSamplingRate = 0;
5540 mChannelMask = AUDIO_CHANNEL_NONE;
5541 mFormat = AUDIO_FORMAT_INVALID;
5542 mGain.index = -1;
5543}
5544
Eric Laurenta121f902014-06-03 13:32:54 -07005545status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig(
5546 const struct audio_port_config *config,
5547 struct audio_port_config *backupConfig)
5548{
5549 struct audio_port_config localBackupConfig;
5550 status_t status = NO_ERROR;
5551
5552 localBackupConfig.config_mask = config->config_mask;
5553 toAudioPortConfig(&localBackupConfig);
5554
5555 if (mAudioPort == 0) {
5556 status = NO_INIT;
5557 goto exit;
5558 }
5559 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
5560 status = mAudioPort->checkSamplingRate(config->sample_rate);
5561 if (status != NO_ERROR) {
5562 goto exit;
5563 }
5564 mSamplingRate = config->sample_rate;
5565 }
5566 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
5567 status = mAudioPort->checkChannelMask(config->channel_mask);
5568 if (status != NO_ERROR) {
5569 goto exit;
5570 }
5571 mChannelMask = config->channel_mask;
5572 }
5573 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
5574 status = mAudioPort->checkFormat(config->format);
5575 if (status != NO_ERROR) {
5576 goto exit;
5577 }
5578 mFormat = config->format;
5579 }
5580 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
5581 status = mAudioPort->checkGain(&config->gain, config->gain.index);
5582 if (status != NO_ERROR) {
5583 goto exit;
5584 }
5585 mGain = config->gain;
5586 }
5587
5588exit:
5589 if (status != NO_ERROR) {
5590 applyAudioPortConfig(&localBackupConfig);
5591 }
5592 if (backupConfig != NULL) {
5593 *backupConfig = localBackupConfig;
5594 }
5595 return status;
5596}
5597
Eric Laurent1f2f2232014-06-02 12:01:23 -07005598void AudioPolicyManager::AudioPortConfig::toAudioPortConfig(
5599 struct audio_port_config *dstConfig,
5600 const struct audio_port_config *srcConfig) const
5601{
5602 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
5603 dstConfig->sample_rate = mSamplingRate;
5604 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) {
5605 dstConfig->sample_rate = srcConfig->sample_rate;
5606 }
5607 } else {
5608 dstConfig->sample_rate = 0;
5609 }
5610 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
5611 dstConfig->channel_mask = mChannelMask;
5612 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) {
5613 dstConfig->channel_mask = srcConfig->channel_mask;
5614 }
5615 } else {
5616 dstConfig->channel_mask = AUDIO_CHANNEL_NONE;
5617 }
5618 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
5619 dstConfig->format = mFormat;
5620 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) {
5621 dstConfig->format = srcConfig->format;
5622 }
5623 } else {
5624 dstConfig->format = AUDIO_FORMAT_INVALID;
5625 }
5626 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
5627 dstConfig->gain = mGain;
5628 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) {
5629 dstConfig->gain = srcConfig->gain;
5630 }
5631 } else {
5632 dstConfig->gain.index = -1;
5633 }
5634 if (dstConfig->gain.index != -1) {
5635 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
5636 } else {
5637 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
5638 }
5639}
5640
Eric Laurent1c333e22014-05-20 10:48:17 -07005641// --- IOProfile class implementation
5642
Eric Laurent1afeecb2014-05-14 08:52:28 -07005643AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role,
Eric Laurent1f2f2232014-06-02 12:01:23 -07005644 const sp<HwModule>& module)
Eric Laurent1e693b52014-07-09 15:03:28 -07005645 : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module)
Eric Laurente552edb2014-03-10 17:42:56 -07005646{
5647}
5648
Eric Laurente0720872014-03-11 09:30:41 -07005649AudioPolicyManager::IOProfile::~IOProfile()
Eric Laurente552edb2014-03-10 17:42:56 -07005650{
5651}
5652
5653// checks if the IO profile is compatible with specified parameters.
5654// Sampling rate, format and channel mask must be specified in order to
5655// get a valid a match
Eric Laurente0720872014-03-11 09:30:41 -07005656bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07005657 uint32_t samplingRate,
5658 audio_format_t format,
5659 audio_channel_mask_t channelMask,
5660 audio_output_flags_t flags) const
5661{
5662 if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
5663 return false;
5664 }
5665
Eric Laurent3a4311c2014-03-17 12:00:47 -07005666 if ((mSupportedDevices.types() & device) != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07005667 return false;
5668 }
5669 if ((mFlags & flags) != flags) {
5670 return false;
5671 }
Eric Laurenta121f902014-06-03 13:32:54 -07005672 if (checkSamplingRate(samplingRate) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005673 return false;
5674 }
Eric Laurenta121f902014-06-03 13:32:54 -07005675 if (checkChannelMask(channelMask) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005676 return false;
5677 }
Eric Laurenta121f902014-06-03 13:32:54 -07005678 if (checkFormat(format) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005679 return false;
5680 }
5681 return true;
5682}
5683
Eric Laurente0720872014-03-11 09:30:41 -07005684void AudioPolicyManager::IOProfile::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005685{
5686 const size_t SIZE = 256;
5687 char buffer[SIZE];
5688 String8 result;
5689
Eric Laurent1afeecb2014-05-14 08:52:28 -07005690 AudioPort::dump(fd, 4);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005691
Eric Laurente552edb2014-03-10 17:42:56 -07005692 snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags);
5693 result.append(buffer);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005694 snprintf(buffer, SIZE, " - devices:\n");
5695 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07005696 write(fd, result.string(), result.size());
Eric Laurent1afeecb2014-05-14 08:52:28 -07005697 for (size_t i = 0; i < mSupportedDevices.size(); i++) {
5698 mSupportedDevices[i]->dump(fd, 6, i);
5699 }
Eric Laurente552edb2014-03-10 17:42:56 -07005700}
5701
Eric Laurentd4692962014-05-05 18:13:44 -07005702void AudioPolicyManager::IOProfile::log()
5703{
5704 const size_t SIZE = 256;
5705 char buffer[SIZE];
5706 String8 result;
5707
5708 ALOGV(" - sampling rates: ");
5709 for (size_t i = 0; i < mSamplingRates.size(); i++) {
5710 ALOGV(" %d", mSamplingRates[i]);
5711 }
5712
5713 ALOGV(" - channel masks: ");
5714 for (size_t i = 0; i < mChannelMasks.size(); i++) {
5715 ALOGV(" 0x%04x", mChannelMasks[i]);
5716 }
5717
5718 ALOGV(" - formats: ");
5719 for (size_t i = 0; i < mFormats.size(); i++) {
5720 ALOGV(" 0x%08x", mFormats[i]);
5721 }
5722
5723 ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types());
5724 ALOGV(" - flags: 0x%04x\n", mFlags);
5725}
5726
5727
Eric Laurent3a4311c2014-03-17 12:00:47 -07005728// --- DeviceDescriptor implementation
Eric Laurente552edb2014-03-10 17:42:56 -07005729
Eric Laurent1f2f2232014-06-02 12:01:23 -07005730
5731AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) :
5732 AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
5733 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
5734 AUDIO_PORT_ROLE_SOURCE,
5735 NULL),
Eric Laurent1e693b52014-07-09 15:03:28 -07005736 mDeviceType(type), mAddress(""), mId(0)
Eric Laurent1f2f2232014-06-02 12:01:23 -07005737{
5738 mAudioPort = this;
Eric Laurenta121f902014-06-03 13:32:54 -07005739 if (mGains.size() > 0) {
5740 mGains[0]->getDefaultConfig(&mGain);
5741 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07005742}
5743
Eric Laurent3a4311c2014-03-17 12:00:47 -07005744bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
Eric Laurente552edb2014-03-10 17:42:56 -07005745{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005746 // Devices are considered equal if they:
5747 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
5748 // - have the same address or one device does not specify the address
5749 // - have the same channel mask or one device does not specify the channel mask
Eric Laurent1c333e22014-05-20 10:48:17 -07005750 return (mDeviceType == other->mDeviceType) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005751 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
Eric Laurent2f8a36f2014-03-26 19:05:55 -07005752 (mChannelMask == 0 || other->mChannelMask == 0 ||
Eric Laurent3a4311c2014-03-17 12:00:47 -07005753 mChannelMask == other->mChannelMask);
5754}
5755
5756void AudioPolicyManager::DeviceVector::refreshTypes()
5757{
Eric Laurent1c333e22014-05-20 10:48:17 -07005758 mDeviceTypes = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005759 for(size_t i = 0; i < size(); i++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005760 mDeviceTypes |= itemAt(i)->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005761 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005762 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005763}
5764
5765ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
5766{
5767 for(size_t i = 0; i < size(); i++) {
5768 if (item->equals(itemAt(i))) {
5769 return i;
Eric Laurente552edb2014-03-10 17:42:56 -07005770 }
5771 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07005772 return -1;
Eric Laurente552edb2014-03-10 17:42:56 -07005773}
5774
Eric Laurent3a4311c2014-03-17 12:00:47 -07005775ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item)
Eric Laurente552edb2014-03-10 17:42:56 -07005776{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005777 ssize_t ret = indexOf(item);
5778
5779 if (ret < 0) {
5780 ret = SortedVector::add(item);
5781 if (ret >= 0) {
5782 refreshTypes();
5783 }
5784 } else {
Eric Laurent1c333e22014-05-20 10:48:17 -07005785 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005786 ret = -1;
5787 }
5788 return ret;
Eric Laurente552edb2014-03-10 17:42:56 -07005789}
5790
Eric Laurent3a4311c2014-03-17 12:00:47 -07005791ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item)
5792{
5793 size_t i;
5794 ssize_t ret = indexOf(item);
5795
5796 if (ret < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005797 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005798 } else {
5799 ret = SortedVector::removeAt(ret);
5800 if (ret >= 0) {
5801 refreshTypes();
5802 }
5803 }
5804 return ret;
5805}
5806
5807void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types)
5808{
5809 DeviceVector deviceList;
5810
5811 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
5812 types &= ~role_bit;
5813
5814 while (types) {
5815 uint32_t i = 31 - __builtin_clz(types);
5816 uint32_t type = 1 << i;
5817 types &= ~type;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005818 add(new DeviceDescriptor(String8(""), type | role_bit));
Eric Laurent3a4311c2014-03-17 12:00:47 -07005819 }
5820}
5821
Eric Laurent1afeecb2014-05-14 08:52:28 -07005822void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name,
5823 const DeviceVector& declaredDevices)
5824{
5825 char *devName = strtok(name, "|");
5826 while (devName != NULL) {
5827 if (strlen(devName) != 0) {
5828 audio_devices_t type = stringToEnum(sDeviceNameToEnumTable,
5829 ARRAY_SIZE(sDeviceNameToEnumTable),
5830 devName);
5831 if (type != AUDIO_DEVICE_NONE) {
5832 add(new DeviceDescriptor(String8(""), type));
5833 } else {
5834 sp<DeviceDescriptor> deviceDesc =
5835 declaredDevices.getDeviceFromName(String8(devName));
5836 if (deviceDesc != 0) {
5837 add(deviceDesc);
5838 }
5839 }
5840 }
5841 devName = strtok(NULL, "|");
5842 }
5843}
5844
Eric Laurent1c333e22014-05-20 10:48:17 -07005845sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice(
5846 audio_devices_t type, String8 address) const
5847{
5848 sp<DeviceDescriptor> device;
5849 for (size_t i = 0; i < size(); i++) {
5850 if (itemAt(i)->mDeviceType == type) {
5851 device = itemAt(i);
5852 if (itemAt(i)->mAddress = address) {
5853 break;
5854 }
5855 }
5856 }
5857 ALOGV("DeviceVector::getDevice() for type %d address %s found %p",
5858 type, address.string(), device.get());
5859 return device;
5860}
5861
Eric Laurent6a94d692014-05-20 11:18:06 -07005862sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId(
5863 audio_port_handle_t id) const
5864{
5865 sp<DeviceDescriptor> device;
5866 for (size_t i = 0; i < size(); i++) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07005867 ALOGV("DeviceVector::getDeviceFromId(%d) itemAt(%zu)->mId %d", id, i, itemAt(i)->mId);
Eric Laurent6a94d692014-05-20 11:18:06 -07005868 if (itemAt(i)->mId == id) {
5869 device = itemAt(i);
5870 break;
5871 }
5872 }
5873 return device;
5874}
5875
Eric Laurent1c333e22014-05-20 10:48:17 -07005876AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType(
5877 audio_devices_t type) const
5878{
5879 DeviceVector devices;
5880 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
5881 if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) {
5882 devices.add(itemAt(i));
5883 type &= ~itemAt(i)->mDeviceType;
5884 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
5885 itemAt(i)->mDeviceType, itemAt(i).get());
5886 }
5887 }
5888 return devices;
5889}
5890
Eric Laurent1afeecb2014-05-14 08:52:28 -07005891sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName(
5892 const String8& name) const
5893{
5894 sp<DeviceDescriptor> device;
5895 for (size_t i = 0; i < size(); i++) {
5896 if (itemAt(i)->mName == name) {
5897 device = itemAt(i);
5898 break;
5899 }
5900 }
5901 return device;
5902}
5903
Eric Laurent6a94d692014-05-20 11:18:06 -07005904void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig(
5905 struct audio_port_config *dstConfig,
5906 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07005907{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005908 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN;
5909 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07005910 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005911 }
5912
5913 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
5914
Eric Laurent6a94d692014-05-20 11:18:06 -07005915 dstConfig->id = mId;
5916 dstConfig->role = audio_is_output_device(mDeviceType) ?
Eric Laurent1c333e22014-05-20 10:48:17 -07005917 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
Eric Laurent6a94d692014-05-20 11:18:06 -07005918 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
Eric Laurent6a94d692014-05-20 11:18:06 -07005919 dstConfig->ext.device.type = mDeviceType;
5920 dstConfig->ext.device.hw_module = mModule->mHandle;
5921 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
Eric Laurent1c333e22014-05-20 10:48:17 -07005922}
5923
5924void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const
5925{
Eric Laurente1715a42014-05-20 11:30:42 -07005926 ALOGV("DeviceVector::toAudioPort() handle %d type %x", mId, mDeviceType);
Eric Laurent1c333e22014-05-20 10:48:17 -07005927 AudioPort::toAudioPort(port);
5928 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07005929 toAudioPortConfig(&port->active_config);
Eric Laurent1c333e22014-05-20 10:48:17 -07005930 port->ext.device.type = mDeviceType;
Eric Laurent6a94d692014-05-20 11:18:06 -07005931 port->ext.device.hw_module = mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07005932 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
5933}
5934
Eric Laurent1afeecb2014-05-14 08:52:28 -07005935status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const
Eric Laurent3a4311c2014-03-17 12:00:47 -07005936{
5937 const size_t SIZE = 256;
5938 char buffer[SIZE];
Eric Laurent1afeecb2014-05-14 08:52:28 -07005939 String8 result;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005940
Eric Laurent1afeecb2014-05-14 08:52:28 -07005941 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
5942 result.append(buffer);
5943 if (mId != 0) {
5944 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
5945 result.append(buffer);
5946 }
5947 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
5948 enumToString(sDeviceNameToEnumTable,
5949 ARRAY_SIZE(sDeviceNameToEnumTable),
5950 mDeviceType));
5951 result.append(buffer);
5952 if (mAddress.size() != 0) {
5953 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
5954 result.append(buffer);
5955 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005956 write(fd, result.string(), result.size());
5957 AudioPort::dump(fd, spaces);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005958
5959 return NO_ERROR;
5960}
5961
5962
5963// --- audio_policy.conf file parsing
5964
Eric Laurente0720872014-03-11 09:30:41 -07005965audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005966{
5967 uint32_t flag = 0;
5968
5969 // it is OK to cast name to non const here as we are not going to use it after
5970 // strtok() modifies it
5971 char *flagName = strtok(name, "|");
5972 while (flagName != NULL) {
5973 if (strlen(flagName) != 0) {
5974 flag |= stringToEnum(sFlagNameToEnumTable,
5975 ARRAY_SIZE(sFlagNameToEnumTable),
5976 flagName);
5977 }
5978 flagName = strtok(NULL, "|");
5979 }
5980 //force direct flag if offload flag is set: offloading implies a direct output stream
5981 // and all common behaviors are driven by checking only the direct flag
5982 // this should normally be set appropriately in the policy configuration file
5983 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
5984 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
5985 }
5986
5987 return (audio_output_flags_t)flag;
5988}
5989
Eric Laurente0720872014-03-11 09:30:41 -07005990audio_devices_t AudioPolicyManager::parseDeviceNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005991{
5992 uint32_t device = 0;
5993
5994 char *devName = strtok(name, "|");
5995 while (devName != NULL) {
5996 if (strlen(devName) != 0) {
5997 device |= stringToEnum(sDeviceNameToEnumTable,
5998 ARRAY_SIZE(sDeviceNameToEnumTable),
5999 devName);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006000 }
Eric Laurente552edb2014-03-10 17:42:56 -07006001 devName = strtok(NULL, "|");
Eric Laurent3a4311c2014-03-17 12:00:47 -07006002 }
Eric Laurente552edb2014-03-10 17:42:56 -07006003 return device;
6004}
6005
Eric Laurente0720872014-03-11 09:30:41 -07006006void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07006007{
Eric Laurente552edb2014-03-10 17:42:56 -07006008 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07006009 cnode *node;
Eric Laurent1f2f2232014-06-02 12:01:23 -07006010 sp<HwModule> module = new HwModule(root->name);
Eric Laurente552edb2014-03-10 17:42:56 -07006011
Eric Laurent1afeecb2014-05-14 08:52:28 -07006012 node = config_find(root, DEVICES_TAG);
6013 if (node != NULL) {
6014 node = node->first_child;
6015 while (node) {
6016 ALOGV("loadHwModule() loading device %s", node->name);
6017 status_t tmpStatus = module->loadDevice(node);
6018 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
6019 status = tmpStatus;
6020 }
6021 node = node->next;
6022 }
6023 }
6024 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07006025 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07006026 node = node->first_child;
6027 while (node) {
6028 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006029 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07006030 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
6031 status = tmpStatus;
6032 }
6033 node = node->next;
6034 }
6035 }
6036 node = config_find(root, INPUTS_TAG);
6037 if (node != NULL) {
6038 node = node->first_child;
6039 while (node) {
6040 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006041 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07006042 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
6043 status = tmpStatus;
6044 }
6045 node = node->next;
6046 }
6047 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006048 loadGlobalConfig(root, module);
6049
Eric Laurente552edb2014-03-10 17:42:56 -07006050 if (status == NO_ERROR) {
6051 mHwModules.add(module);
Eric Laurente552edb2014-03-10 17:42:56 -07006052 }
6053}
6054
Eric Laurente0720872014-03-11 09:30:41 -07006055void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07006056{
6057 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
6058 if (node == NULL) {
6059 return;
6060 }
6061
6062 node = node->first_child;
6063 while (node) {
6064 ALOGV("loadHwModules() loading module %s", node->name);
6065 loadHwModule(node);
6066 node = node->next;
6067 }
6068}
6069
Eric Laurent1f2f2232014-06-02 12:01:23 -07006070void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module)
Eric Laurente552edb2014-03-10 17:42:56 -07006071{
6072 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
Eric Laurenteb108a42014-06-06 14:56:52 -07006073
Eric Laurente552edb2014-03-10 17:42:56 -07006074 if (node == NULL) {
6075 return;
6076 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006077 DeviceVector declaredDevices;
6078 if (module != NULL) {
6079 declaredDevices = module->mDeclaredDevices;
6080 }
6081
Eric Laurente552edb2014-03-10 17:42:56 -07006082 node = node->first_child;
6083 while (node) {
6084 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07006085 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
6086 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006087 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
6088 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07006089 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07006090 audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
Eric Laurente552edb2014-03-10 17:42:56 -07006091 ARRAY_SIZE(sDeviceNameToEnumTable),
6092 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006093 if (device != AUDIO_DEVICE_NONE) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07006094 mDefaultOutputDevice = new DeviceDescriptor(String8(""), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006095 } else {
6096 ALOGW("loadGlobalConfig() default device not specified");
6097 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006098 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07006099 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07006100 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
6101 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006102 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07006103 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
6104 mSpeakerDrcEnabled = stringToBool((char *)node->value);
6105 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
Eric Laurenteb108a42014-06-06 14:56:52 -07006106 } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) {
6107 uint32_t major, minor;
6108 sscanf((char *)node->value, "%u.%u", &major, &minor);
6109 module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor);
6110 ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u",
6111 module->mHalVersion, major, minor);
Eric Laurente552edb2014-03-10 17:42:56 -07006112 }
6113 node = node->next;
6114 }
6115}
6116
Eric Laurente0720872014-03-11 09:30:41 -07006117status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07006118{
6119 cnode *root;
6120 char *data;
6121
6122 data = (char *)load_file(path, NULL);
6123 if (data == NULL) {
6124 return -ENODEV;
6125 }
6126 root = config_node("", "");
6127 config_load(root, data);
6128
Eric Laurente552edb2014-03-10 17:42:56 -07006129 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006130 // legacy audio_policy.conf files have one global_configuration section
6131 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07006132 config_free(root);
6133 free(root);
6134 free(data);
6135
6136 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
6137
6138 return NO_ERROR;
6139}
6140
Eric Laurente0720872014-03-11 09:30:41 -07006141void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07006142{
Eric Laurent1f2f2232014-06-02 12:01:23 -07006143 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07006144 sp<IOProfile> profile;
Eric Laurent1f2f2232014-06-02 12:01:23 -07006145 sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""),
6146 AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006147 mAvailableOutputDevices.add(mDefaultOutputDevice);
6148 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07006149
6150 module = new HwModule("primary");
6151
Eric Laurent1afeecb2014-05-14 08:52:28 -07006152 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07006153 profile->mSamplingRates.add(44100);
6154 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
6155 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006156 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07006157 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
6158 module->mOutputProfiles.add(profile);
6159
Eric Laurent1afeecb2014-05-14 08:52:28 -07006160 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07006161 profile->mSamplingRates.add(8000);
6162 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
6163 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07006164 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07006165 module->mInputProfiles.add(profile);
6166
6167 mHwModules.add(module);
6168}
6169
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07006170audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
6171{
6172 // flags to stream type mapping
6173 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
6174 return AUDIO_STREAM_ENFORCED_AUDIBLE;
6175 }
6176 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
6177 return AUDIO_STREAM_BLUETOOTH_SCO;
6178 }
6179
6180 // usage to stream type mapping
6181 switch (attr->usage) {
6182 case AUDIO_USAGE_MEDIA:
6183 case AUDIO_USAGE_GAME:
6184 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
6185 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
6186 return AUDIO_STREAM_MUSIC;
6187 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
6188 return AUDIO_STREAM_SYSTEM;
6189 case AUDIO_USAGE_VOICE_COMMUNICATION:
6190 return AUDIO_STREAM_VOICE_CALL;
6191
6192 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
6193 return AUDIO_STREAM_DTMF;
6194
6195 case AUDIO_USAGE_ALARM:
6196 return AUDIO_STREAM_ALARM;
6197 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
6198 return AUDIO_STREAM_RING;
6199
6200 case AUDIO_USAGE_NOTIFICATION:
6201 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
6202 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
6203 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
6204 case AUDIO_USAGE_NOTIFICATION_EVENT:
6205 return AUDIO_STREAM_NOTIFICATION;
6206
6207 case AUDIO_USAGE_UNKNOWN:
6208 default:
6209 return AUDIO_STREAM_MUSIC;
6210 }
6211}
Eric Laurente552edb2014-03-10 17:42:56 -07006212}; // namespace android