blob: 87d7771e7bc5ca9d0ced580813c6150a64684ebe [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),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700103};
104
105const StringToEnum sFlagNameToEnumTable[] = {
106 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
110 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
112};
113
114const StringToEnum sFormatNameToEnumTable[] = {
115 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
116 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
117 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
118 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
121 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
122 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
123 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
124};
125
126const StringToEnum sOutChannelsNameToEnumTable[] = {
127 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
128 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
129 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
131};
132
133const StringToEnum sInChannelsNameToEnumTable[] = {
134 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
135 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
136 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
137};
138
Eric Laurent1afeecb2014-05-14 08:52:28 -0700139const StringToEnum sGainModeNameToEnumTable[] = {
140 STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT),
141 STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS),
142 STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP),
143};
144
Eric Laurent3a4311c2014-03-17 12:00:47 -0700145
146uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table,
147 size_t size,
148 const char *name)
149{
150 for (size_t i = 0; i < size; i++) {
151 if (strcmp(table[i].name, name) == 0) {
152 ALOGV("stringToEnum() found %s", table[i].name);
153 return table[i].value;
154 }
155 }
156 return 0;
157}
158
159const char *AudioPolicyManager::enumToString(const struct StringToEnum *table,
160 size_t size,
161 uint32_t value)
162{
163 for (size_t i = 0; i < size; i++) {
164 if (table[i].value == value) {
165 return table[i].name;
166 }
167 }
168 return "";
169}
170
171bool AudioPolicyManager::stringToBool(const char *value)
172{
173 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
174}
175
176
177// ----------------------------------------------------------------------------
Eric Laurente552edb2014-03-10 17:42:56 -0700178// AudioPolicyInterface implementation
179// ----------------------------------------------------------------------------
180
181
Eric Laurente0720872014-03-11 09:30:41 -0700182status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700183 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700184 const char *device_address)
185{
Eric Laurent3a4311c2014-03-17 12:00:47 -0700186 String8 address = String8(device_address);
Eric Laurente552edb2014-03-10 17:42:56 -0700187
188 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
189
190 // connect/disconnect only 1 device at a time
191 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
192
Eric Laurente552edb2014-03-10 17:42:56 -0700193 // handle output devices
194 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700195 SortedVector <audio_io_handle_t> outputs;
196
Eric Laurent1afeecb2014-05-14 08:52:28 -0700197 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
198 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700199 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
200
Eric Laurente552edb2014-03-10 17:42:56 -0700201 // save a copy of the opened output descriptors before any output is opened or closed
202 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
203 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700204 switch (state)
205 {
206 // handle output device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700207 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
Eric Laurent3a4311c2014-03-17 12:00:47 -0700208 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700209 ALOGW("setDeviceConnectionState() device already connected: %x", device);
210 return INVALID_OPERATION;
211 }
212 ALOGV("setDeviceConnectionState() connecting device %x", device);
213
Eric Laurent3a4311c2014-03-17 12:00:47 -0700214 if (checkOutputsForDevice(device, state, outputs, address) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -0700215 return INVALID_OPERATION;
216 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700217 // outputs should never be empty here
218 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
219 "checkOutputsForDevice() returned no outputs but status OK");
Eric Laurentd4692962014-05-05 18:13:44 -0700220 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
Eric Laurente552edb2014-03-10 17:42:56 -0700221 outputs.size());
222 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700223 index = mAvailableOutputDevices.add(devDesc);
224 if (index >= 0) {
225 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700226 HwModule *module = getModuleForDevice(device);
227 ALOG_ASSERT(module != NULL, "setDeviceConnectionState():"
228 "could not find HW module for device %08x", device);
229 mAvailableOutputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700230 } else {
231 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700232 }
233
234 break;
235 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700236 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700237 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700238 ALOGW("setDeviceConnectionState() device not connected: %x", device);
239 return INVALID_OPERATION;
240 }
241
242 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
243 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700244 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700245
Eric Laurent3a4311c2014-03-17 12:00:47 -0700246 checkOutputsForDevice(device, state, outputs, address);
Eric Laurente552edb2014-03-10 17:42:56 -0700247 // not currently handling multiple simultaneous submixes: ignoring remote submix
248 // case and address
249 } break;
250
251 default:
252 ALOGE("setDeviceConnectionState() invalid state: %x", state);
253 return BAD_VALUE;
254 }
255
Eric Laurent3a4311c2014-03-17 12:00:47 -0700256 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
257 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700258 checkA2dpSuspend();
259 checkOutputForAllStrategies();
260 // outputs must be closed after checkOutputForAllStrategies() is executed
261 if (!outputs.isEmpty()) {
262 for (size_t i = 0; i < outputs.size(); i++) {
263 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
264 // close unused outputs after device disconnection or direct outputs that have been
265 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700266 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700267 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
268 (desc->mDirectOpenCount == 0))) {
269 closeOutput(outputs[i]);
270 }
271 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700272 // check again after closing A2DP output to reset mA2dpSuspended if needed
273 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700274 }
275
276 updateDevicesAndOutputs();
277 for (size_t i = 0; i < mOutputs.size(); i++) {
278 // do not force device change on duplicated output because if device is 0, it will
279 // also force a device 0 for the two outputs it is duplicated to which may override
280 // a valid device selection on those outputs.
281 setOutputDevice(mOutputs.keyAt(i),
Eric Laurent951f4552014-05-20 10:48:17 -0700282 getNewOutputDevice(mOutputs.keyAt(i), true /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700283 !mOutputs.valueAt(i)->isDuplicated(),
284 0);
285 }
286
287 if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
288 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
289 } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
290 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
291 device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
292 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
293 } else {
Eric Laurentb52c1522014-05-20 11:27:36 -0700294 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700295 return NO_ERROR;
296 }
Eric Laurentd4692962014-05-05 18:13:44 -0700297 } // end if is output device
298
Eric Laurente552edb2014-03-10 17:42:56 -0700299 // handle input devices
300 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700301 SortedVector <audio_io_handle_t> inputs;
302
Eric Laurent1afeecb2014-05-14 08:52:28 -0700303 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
304 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700305 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700306 switch (state)
307 {
308 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700309 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700310 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700311 ALOGW("setDeviceConnectionState() device already connected: %d", device);
312 return INVALID_OPERATION;
313 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700314 HwModule *module = getModuleForDevice(device);
315 if (module == NULL) {
316 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
317 device);
318 return INVALID_OPERATION;
319 }
Eric Laurentd4692962014-05-05 18:13:44 -0700320 if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) {
321 return INVALID_OPERATION;
322 }
323
Eric Laurent3a4311c2014-03-17 12:00:47 -0700324 index = mAvailableInputDevices.add(devDesc);
325 if (index >= 0) {
326 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700327 mAvailableInputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 } else {
329 return NO_MEMORY;
330 }
Eric Laurentd4692962014-05-05 18:13:44 -0700331 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700332
333 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700334 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700335 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700336 ALOGW("setDeviceConnectionState() device not connected: %d", device);
337 return INVALID_OPERATION;
338 }
Eric Laurentd4692962014-05-05 18:13:44 -0700339 checkInputsForDevice(device, state, inputs, address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700340 mAvailableInputDevices.remove(devDesc);
Eric Laurentd4692962014-05-05 18:13:44 -0700341 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700342
343 default:
344 ALOGE("setDeviceConnectionState() invalid state: %x", state);
345 return BAD_VALUE;
346 }
347
Eric Laurentd4692962014-05-05 18:13:44 -0700348 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700349
Eric Laurentb52c1522014-05-20 11:27:36 -0700350 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700351 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700352 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700353
354 ALOGW("setDeviceConnectionState() invalid device: %x", device);
355 return BAD_VALUE;
356}
357
Eric Laurente0720872014-03-11 09:30:41 -0700358audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700359 const char *device_address)
360{
Eric Laurent3b73df72014-03-11 09:06:29 -0700361 audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700362 String8 address = String8(device_address);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700363 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
364 devDesc->mAddress = String8(device_address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700365 ssize_t index;
366 DeviceVector *deviceVector;
367
Eric Laurente552edb2014-03-10 17:42:56 -0700368 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700369 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700370 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700371 deviceVector = &mAvailableInputDevices;
372 } else {
373 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
374 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700375 }
376
Eric Laurent3a4311c2014-03-17 12:00:47 -0700377 index = deviceVector->indexOf(devDesc);
378 if (index >= 0) {
379 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
380 } else {
381 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
382 }
Eric Laurente552edb2014-03-10 17:42:56 -0700383}
384
Eric Laurente0720872014-03-11 09:30:41 -0700385void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700386{
387 ALOGV("setPhoneState() state %d", state);
388 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Eric Laurent3b73df72014-03-11 09:06:29 -0700389 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700390 ALOGW("setPhoneState() invalid state %d", state);
391 return;
392 }
393
394 if (state == mPhoneState ) {
395 ALOGW("setPhoneState() setting same state %d", state);
396 return;
397 }
398
399 // if leaving call state, handle special case of active streams
400 // pertaining to sonification strategy see handleIncallSonification()
401 if (isInCall()) {
402 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700403 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
404 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700405 }
406 }
407
408 // store previous phone state for management of sonification strategy below
409 int oldState = mPhoneState;
410 mPhoneState = state;
411 bool force = false;
412
413 // are we entering or starting a call
414 if (!isStateInCall(oldState) && isStateInCall(state)) {
415 ALOGV(" Entering call in setPhoneState()");
416 // force routing command to audio hardware when starting a call
417 // even if no device change is needed
418 force = true;
419 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
420 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
421 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
422 }
423 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
424 ALOGV(" Exiting call in setPhoneState()");
425 // force routing command to audio hardware when exiting 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_DTMF][j];
431 }
432 } else if (isStateInCall(state) && (state != oldState)) {
433 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
434 // force routing command to audio hardware when switching between telephony and VoIP
435 // even if no device change is needed
436 force = true;
437 }
438
439 // check for device and output changes triggered by new phone state
Eric Laurent951f4552014-05-20 10:48:17 -0700440 newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700441 checkA2dpSuspend();
442 checkOutputForAllStrategies();
443 updateDevicesAndOutputs();
444
445 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
446
447 // force routing command to audio hardware when ending call
448 // even if no device change is needed
449 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
450 newDevice = hwOutputDesc->device();
451 }
452
453 int delayMs = 0;
454 if (isStateInCall(state)) {
455 nsecs_t sysTime = systemTime();
456 for (size_t i = 0; i < mOutputs.size(); i++) {
457 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
458 // mute media and sonification strategies and delay device switch by the largest
459 // latency of any output where either strategy is active.
460 // This avoid sending the ring tone or music tail into the earpiece or headset.
461 if ((desc->isStrategyActive(STRATEGY_MEDIA,
462 SONIFICATION_HEADSET_MUSIC_DELAY,
463 sysTime) ||
464 desc->isStrategyActive(STRATEGY_SONIFICATION,
465 SONIFICATION_HEADSET_MUSIC_DELAY,
466 sysTime)) &&
467 (delayMs < (int)desc->mLatency*2)) {
468 delayMs = desc->mLatency*2;
469 }
470 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
471 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
472 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
473 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
474 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
475 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
476 }
477 }
478
479 // change routing is necessary
480 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
481
482 // if entering in call state, handle special case of active streams
483 // pertaining to sonification strategy see handleIncallSonification()
484 if (isStateInCall(state)) {
485 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700486 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
487 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700488 }
489 }
490
491 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700492 if (state == AUDIO_MODE_RINGTONE &&
493 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700494 mLimitRingtoneVolume = true;
495 } else {
496 mLimitRingtoneVolume = false;
497 }
498}
499
Eric Laurente0720872014-03-11 09:30:41 -0700500void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700501 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700502{
503 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
504
505 bool forceVolumeReeval = false;
506 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700507 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
508 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
509 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700510 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
511 return;
512 }
513 forceVolumeReeval = true;
514 mForceUse[usage] = config;
515 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700516 case AUDIO_POLICY_FORCE_FOR_MEDIA:
517 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
518 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
519 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
520 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
521 config != AUDIO_POLICY_FORCE_NO_BT_A2DP) {
Eric Laurente552edb2014-03-10 17:42:56 -0700522 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
523 return;
524 }
525 mForceUse[usage] = config;
526 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700527 case AUDIO_POLICY_FORCE_FOR_RECORD:
528 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
529 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700530 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
531 return;
532 }
533 mForceUse[usage] = config;
534 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700535 case AUDIO_POLICY_FORCE_FOR_DOCK:
536 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
537 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
538 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
539 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
540 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700541 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
542 }
543 forceVolumeReeval = true;
544 mForceUse[usage] = config;
545 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700546 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
547 if (config != AUDIO_POLICY_FORCE_NONE &&
548 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700549 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
550 }
551 forceVolumeReeval = true;
552 mForceUse[usage] = config;
553 break;
554 default:
555 ALOGW("setForceUse() invalid usage %d", usage);
556 break;
557 }
558
559 // check for device and output changes triggered by new force usage
560 checkA2dpSuspend();
561 checkOutputForAllStrategies();
562 updateDevicesAndOutputs();
563 for (size_t i = 0; i < mOutputs.size(); i++) {
564 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent951f4552014-05-20 10:48:17 -0700565 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700566 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
567 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
568 applyStreamVolumes(output, newDevice, 0, true);
569 }
570 }
571
572 audio_io_handle_t activeInput = getActiveInput();
573 if (activeInput != 0) {
Eric Laurent951f4552014-05-20 10:48:17 -0700574 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700575 }
576
577}
578
Eric Laurente0720872014-03-11 09:30:41 -0700579audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700580{
581 return mForceUse[usage];
582}
583
Eric Laurente0720872014-03-11 09:30:41 -0700584void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700585{
586 ALOGV("setSystemProperty() property %s, value %s", property, value);
587}
588
589// Find a direct output profile compatible with the parameters passed, even if the input flags do
590// not explicitly request a direct output
Eric Laurent951f4552014-05-20 10:48:17 -0700591sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700592 audio_devices_t device,
593 uint32_t samplingRate,
594 audio_format_t format,
595 audio_channel_mask_t channelMask,
596 audio_output_flags_t flags)
597{
598 for (size_t i = 0; i < mHwModules.size(); i++) {
599 if (mHwModules[i]->mHandle == 0) {
600 continue;
601 }
602 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent951f4552014-05-20 10:48:17 -0700603 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent3a4311c2014-03-17 12:00:47 -0700604 bool found = false;
Eric Laurente552edb2014-03-10 17:42:56 -0700605 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
606 if (profile->isCompatibleProfile(device, samplingRate, format,
607 channelMask,
608 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700609 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700610 }
611 } else {
612 if (profile->isCompatibleProfile(device, samplingRate, format,
613 channelMask,
614 AUDIO_OUTPUT_FLAG_DIRECT)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700615 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700616 }
617 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700618 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
619 return profile;
620 }
Eric Laurente552edb2014-03-10 17:42:56 -0700621 }
622 }
623 return 0;
624}
625
Eric Laurente0720872014-03-11 09:30:41 -0700626audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700627 uint32_t samplingRate,
628 audio_format_t format,
629 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700630 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700631 const audio_offload_info_t *offloadInfo)
632{
633 audio_io_handle_t output = 0;
634 uint32_t latency = 0;
Eric Laurent3b73df72014-03-11 09:06:29 -0700635 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700636 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
637 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
638 device, stream, samplingRate, format, channelMask, flags);
639
640#ifdef AUDIO_POLICY_TEST
641 if (mCurOutput != 0) {
642 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
643 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
644
645 if (mTestOutputs[mCurOutput] == 0) {
646 ALOGV("getOutput() opening test output");
647 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
648 outputDesc->mDevice = mTestDevice;
649 outputDesc->mSamplingRate = mTestSamplingRate;
650 outputDesc->mFormat = mTestFormat;
651 outputDesc->mChannelMask = mTestChannels;
652 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700653 outputDesc->mFlags =
654 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700655 outputDesc->mRefCount[stream] = 0;
656 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
657 &outputDesc->mSamplingRate,
658 &outputDesc->mFormat,
659 &outputDesc->mChannelMask,
660 &outputDesc->mLatency,
661 outputDesc->mFlags,
662 offloadInfo);
663 if (mTestOutputs[mCurOutput]) {
664 AudioParameter outputCmd = AudioParameter();
665 outputCmd.addInt(String8("set_id"),mCurOutput);
666 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
667 addOutput(mTestOutputs[mCurOutput], outputDesc);
668 }
669 }
670 return mTestOutputs[mCurOutput];
671 }
672#endif //AUDIO_POLICY_TEST
673
674 // open a direct output if required by specified parameters
675 //force direct flag if offload flag is set: offloading implies a direct output stream
676 // and all common behaviors are driven by checking only the direct flag
677 // this should normally be set appropriately in the policy configuration file
678 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700679 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700680 }
681
682 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
683 // creating an offloaded track and tearing it down immediately after start when audioflinger
684 // detects there is an active non offloadable effect.
685 // FIXME: We should check the audio session here but we do not have it in this context.
686 // This may prevent offloading in rare situations where effects are left active by apps
687 // in the background.
Eric Laurent951f4552014-05-20 10:48:17 -0700688 sp<IOProfile> profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700689 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
690 !isNonOffloadableEffectEnabled()) {
691 profile = getProfileForDirectOutput(device,
692 samplingRate,
693 format,
694 channelMask,
695 (audio_output_flags_t)flags);
696 }
697
Eric Laurent951f4552014-05-20 10:48:17 -0700698 if (profile != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700699 AudioOutputDescriptor *outputDesc = NULL;
700
701 for (size_t i = 0; i < mOutputs.size(); i++) {
702 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
703 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
704 outputDesc = desc;
705 // reuse direct output if currently open and configured with same parameters
706 if ((samplingRate == outputDesc->mSamplingRate) &&
707 (format == outputDesc->mFormat) &&
708 (channelMask == outputDesc->mChannelMask)) {
709 outputDesc->mDirectOpenCount++;
710 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
711 return mOutputs.keyAt(i);
712 }
713 }
714 }
715 // close direct output if currently open and configured with different parameters
716 if (outputDesc != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -0700717 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700718 }
719 outputDesc = new AudioOutputDescriptor(profile);
720 outputDesc->mDevice = device;
721 outputDesc->mSamplingRate = samplingRate;
722 outputDesc->mFormat = format;
723 outputDesc->mChannelMask = channelMask;
724 outputDesc->mLatency = 0;
725 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
726 outputDesc->mRefCount[stream] = 0;
727 outputDesc->mStopTime[stream] = 0;
728 outputDesc->mDirectOpenCount = 1;
729 output = mpClientInterface->openOutput(profile->mModule->mHandle,
730 &outputDesc->mDevice,
731 &outputDesc->mSamplingRate,
732 &outputDesc->mFormat,
733 &outputDesc->mChannelMask,
734 &outputDesc->mLatency,
735 outputDesc->mFlags,
736 offloadInfo);
737
738 // only accept an output with the requested parameters
739 if (output == 0 ||
740 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
741 (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
742 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
743 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
744 "format %d %d, channelMask %04x %04x", output, samplingRate,
745 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
746 outputDesc->mChannelMask);
747 if (output != 0) {
748 mpClientInterface->closeOutput(output);
749 }
750 delete outputDesc;
751 return 0;
752 }
753 audio_io_handle_t srcOutput = getOutputForEffect();
754 addOutput(output, outputDesc);
755 audio_io_handle_t dstOutput = getOutputForEffect();
756 if (dstOutput == output) {
757 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
758 }
759 mPreviousOutputs = mOutputs;
760 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700761 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700762 return output;
763 }
764
765 // ignoring channel mask due to downmix capability in mixer
766
767 // open a non direct output
768
769 // for non direct outputs, only PCM is supported
770 if (audio_is_linear_pcm(format)) {
771 // get which output is suitable for the specified stream. The actual
772 // routing change will happen when startOutput() will be called
773 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
774
775 output = selectOutput(outputs, flags);
776 }
777 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
778 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
779
780 ALOGV("getOutput() returns output %d", output);
781
782 return output;
783}
784
Eric Laurente0720872014-03-11 09:30:41 -0700785audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3b73df72014-03-11 09:06:29 -0700786 audio_output_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -0700787{
788 // select one output among several that provide a path to a particular device or set of
789 // devices (the list was previously build by getOutputsForDevice()).
790 // The priority is as follows:
791 // 1: the output with the highest number of requested policy flags
792 // 2: the primary output
793 // 3: the first output in the list
794
795 if (outputs.size() == 0) {
796 return 0;
797 }
798 if (outputs.size() == 1) {
799 return outputs[0];
800 }
801
802 int maxCommonFlags = 0;
803 audio_io_handle_t outputFlags = 0;
804 audio_io_handle_t outputPrimary = 0;
805
806 for (size_t i = 0; i < outputs.size(); i++) {
807 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
808 if (!outputDesc->isDuplicated()) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700809 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700810 if (commonFlags > maxCommonFlags) {
811 outputFlags = outputs[i];
812 maxCommonFlags = commonFlags;
813 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
814 }
815 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
816 outputPrimary = outputs[i];
817 }
818 }
819 }
820
821 if (outputFlags != 0) {
822 return outputFlags;
823 }
824 if (outputPrimary != 0) {
825 return outputPrimary;
826 }
827
828 return outputs[0];
829}
830
Eric Laurente0720872014-03-11 09:30:41 -0700831status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700832 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700833 int session)
834{
835 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
836 ssize_t index = mOutputs.indexOfKey(output);
837 if (index < 0) {
838 ALOGW("startOutput() unknown output %d", output);
839 return BAD_VALUE;
840 }
841
842 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
843
844 // increment usage count for this stream on the requested output:
845 // NOTE that the usage count is the same for duplicated output and hardware output which is
846 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
847 outputDesc->changeRefCount(stream, 1);
848
849 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent951f4552014-05-20 10:48:17 -0700850 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700851 routing_strategy strategy = getStrategy(stream);
852 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
853 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
854 uint32_t waitMs = 0;
855 bool force = false;
856 for (size_t i = 0; i < mOutputs.size(); i++) {
857 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
858 if (desc != outputDesc) {
859 // force a device change if any other output is managed by the same hw
860 // module and has a current device selection that differs from selected device.
861 // In this case, the audio HAL must receive the new device selection so that it can
862 // change the device currently selected by the other active output.
863 if (outputDesc->sharesHwModuleWith(desc) &&
864 desc->device() != newDevice) {
865 force = true;
866 }
867 // wait for audio on other active outputs to be presented when starting
868 // a notification so that audio focus effect can propagate.
869 uint32_t latency = desc->latency();
870 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
871 waitMs = latency;
872 }
873 }
874 }
875 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
876
877 // handle special case for sonification while in call
878 if (isInCall()) {
879 handleIncallSonification(stream, true, false);
880 }
881
882 // apply volume rules for current stream and device if necessary
883 checkAndSetVolume(stream,
884 mStreams[stream].getVolumeIndex(newDevice),
885 output,
886 newDevice);
887
888 // update the outputs if starting an output with a stream that can affect notification
889 // routing
890 handleNotificationRoutingForStream(stream);
891 if (waitMs > muteWaitMs) {
892 usleep((waitMs - muteWaitMs) * 2 * 1000);
893 }
894 }
895 return NO_ERROR;
896}
897
898
Eric Laurente0720872014-03-11 09:30:41 -0700899status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700900 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700901 int session)
902{
903 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
904 ssize_t index = mOutputs.indexOfKey(output);
905 if (index < 0) {
906 ALOGW("stopOutput() unknown output %d", output);
907 return BAD_VALUE;
908 }
909
910 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
911
912 // handle special case for sonification while in call
913 if (isInCall()) {
914 handleIncallSonification(stream, false, false);
915 }
916
917 if (outputDesc->mRefCount[stream] > 0) {
918 // decrement usage count of this stream on the output
919 outputDesc->changeRefCount(stream, -1);
920 // store time at which the stream was stopped - see isStreamActive()
921 if (outputDesc->mRefCount[stream] == 0) {
922 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent951f4552014-05-20 10:48:17 -0700923 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700924 // delay the device switch by twice the latency because stopOutput() is executed when
925 // the track stop() command is received and at that time the audio track buffer can
926 // still contain data that needs to be drained. The latency only covers the audio HAL
927 // and kernel buffers. Also the latency does not always include additional delay in the
928 // audio path (audio DSP, CODEC ...)
929 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
930
931 // force restoring the device selection on other active outputs if it differs from the
932 // one being selected for this output
933 for (size_t i = 0; i < mOutputs.size(); i++) {
934 audio_io_handle_t curOutput = mOutputs.keyAt(i);
935 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
936 if (curOutput != output &&
937 desc->isActive() &&
938 outputDesc->sharesHwModuleWith(desc) &&
939 (newDevice != desc->device())) {
940 setOutputDevice(curOutput,
Eric Laurent951f4552014-05-20 10:48:17 -0700941 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700942 true,
943 outputDesc->mLatency*2);
944 }
945 }
946 // update the outputs if stopping one with a stream that can affect notification routing
947 handleNotificationRoutingForStream(stream);
948 }
949 return NO_ERROR;
950 } else {
951 ALOGW("stopOutput() refcount is already 0 for output %d", output);
952 return INVALID_OPERATION;
953 }
954}
955
Eric Laurente0720872014-03-11 09:30:41 -0700956void AudioPolicyManager::releaseOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -0700957{
958 ALOGV("releaseOutput() %d", output);
959 ssize_t index = mOutputs.indexOfKey(output);
960 if (index < 0) {
961 ALOGW("releaseOutput() releasing unknown output %d", output);
962 return;
963 }
964
965#ifdef AUDIO_POLICY_TEST
966 int testIndex = testOutputIndex(output);
967 if (testIndex != 0) {
968 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
969 if (outputDesc->isActive()) {
970 mpClientInterface->closeOutput(output);
971 delete mOutputs.valueAt(index);
972 mOutputs.removeItem(output);
973 mTestOutputs[testIndex] = 0;
974 }
975 return;
976 }
977#endif //AUDIO_POLICY_TEST
978
979 AudioOutputDescriptor *desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -0700980 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700981 if (desc->mDirectOpenCount <= 0) {
982 ALOGW("releaseOutput() invalid open count %d for output %d",
983 desc->mDirectOpenCount, output);
984 return;
985 }
986 if (--desc->mDirectOpenCount == 0) {
987 closeOutput(output);
988 // If effects where present on the output, audioflinger moved them to the primary
989 // output by default: move them back to the appropriate output.
990 audio_io_handle_t dstOutput = getOutputForEffect();
991 if (dstOutput != mPrimaryOutput) {
992 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
993 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700994 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700995 }
996 }
997}
998
999
Eric Laurente0720872014-03-11 09:30:41 -07001000audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource,
Eric Laurente552edb2014-03-10 17:42:56 -07001001 uint32_t samplingRate,
1002 audio_format_t format,
1003 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -07001004 audio_in_acoustics_t acoustics)
Eric Laurente552edb2014-03-10 17:42:56 -07001005{
1006 audio_io_handle_t input = 0;
1007 audio_devices_t device = getDeviceForInputSource(inputSource);
1008
1009 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
1010 inputSource, samplingRate, format, channelMask, acoustics);
1011
1012 if (device == AUDIO_DEVICE_NONE) {
1013 ALOGW("getInput() could not find device for inputSource %d", inputSource);
1014 return 0;
1015 }
1016
1017 // adapt channel selection to input source
1018 switch(inputSource) {
1019 case AUDIO_SOURCE_VOICE_UPLINK:
1020 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1021 break;
1022 case AUDIO_SOURCE_VOICE_DOWNLINK:
1023 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1024 break;
1025 case AUDIO_SOURCE_VOICE_CALL:
1026 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1027 break;
1028 default:
1029 break;
1030 }
1031
Eric Laurent951f4552014-05-20 10:48:17 -07001032 sp<IOProfile> profile = getInputProfile(device,
Eric Laurente552edb2014-03-10 17:42:56 -07001033 samplingRate,
1034 format,
1035 channelMask);
Eric Laurent951f4552014-05-20 10:48:17 -07001036 if (profile == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001037 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d, "
1038 "channelMask %04x",
1039 device, samplingRate, format, channelMask);
1040 return 0;
1041 }
1042
1043 if (profile->mModule->mHandle == 0) {
1044 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
1045 return 0;
1046 }
1047
1048 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
1049
1050 inputDesc->mInputSource = inputSource;
1051 inputDesc->mDevice = device;
1052 inputDesc->mSamplingRate = samplingRate;
1053 inputDesc->mFormat = format;
1054 inputDesc->mChannelMask = channelMask;
1055 inputDesc->mRefCount = 0;
1056 input = mpClientInterface->openInput(profile->mModule->mHandle,
1057 &inputDesc->mDevice,
1058 &inputDesc->mSamplingRate,
1059 &inputDesc->mFormat,
1060 &inputDesc->mChannelMask);
1061
1062 // only accept input with the exact requested set of parameters
1063 if (input == 0 ||
1064 (samplingRate != inputDesc->mSamplingRate) ||
1065 (format != inputDesc->mFormat) ||
1066 (channelMask != inputDesc->mChannelMask)) {
1067 ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask %x",
1068 samplingRate, format, channelMask);
1069 if (input != 0) {
1070 mpClientInterface->closeInput(input);
1071 }
1072 delete inputDesc;
1073 return 0;
1074 }
Eric Laurentd4692962014-05-05 18:13:44 -07001075 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001076 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001077 return input;
1078}
1079
Eric Laurente0720872014-03-11 09:30:41 -07001080status_t AudioPolicyManager::startInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001081{
1082 ALOGV("startInput() input %d", input);
1083 ssize_t index = mInputs.indexOfKey(input);
1084 if (index < 0) {
1085 ALOGW("startInput() unknown input %d", input);
1086 return BAD_VALUE;
1087 }
1088 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1089
1090#ifdef AUDIO_POLICY_TEST
1091 if (mTestInput == 0)
1092#endif //AUDIO_POLICY_TEST
1093 {
1094 // refuse 2 active AudioRecord clients at the same time except if the active input
1095 // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
1096 audio_io_handle_t activeInput = getActiveInput();
1097 if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
1098 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1099 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1100 ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1101 stopInput(activeInput);
1102 releaseInput(activeInput);
1103 } else {
1104 ALOGW("startInput() input %d failed: other input already started", input);
1105 return INVALID_OPERATION;
1106 }
1107 }
1108 }
1109
Eric Laurent951f4552014-05-20 10:48:17 -07001110 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001111
1112 // automatically enable the remote submix output when input is started
1113 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1114 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001115 AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001116 }
1117
Eric Laurente552edb2014-03-10 17:42:56 -07001118 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1119
Eric Laurente552edb2014-03-10 17:42:56 -07001120 inputDesc->mRefCount = 1;
1121 return NO_ERROR;
1122}
1123
Eric Laurente0720872014-03-11 09:30:41 -07001124status_t AudioPolicyManager::stopInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001125{
1126 ALOGV("stopInput() input %d", input);
1127 ssize_t index = mInputs.indexOfKey(input);
1128 if (index < 0) {
1129 ALOGW("stopInput() unknown input %d", input);
1130 return BAD_VALUE;
1131 }
1132 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1133
1134 if (inputDesc->mRefCount == 0) {
1135 ALOGW("stopInput() input %d already stopped", input);
1136 return INVALID_OPERATION;
1137 } else {
1138 // automatically disable the remote submix output when input is stopped
1139 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1140 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001141 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001142 }
1143
Eric Laurent951f4552014-05-20 10:48:17 -07001144 resetInputDevice(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001145 inputDesc->mRefCount = 0;
1146 return NO_ERROR;
1147 }
1148}
1149
Eric Laurente0720872014-03-11 09:30:41 -07001150void AudioPolicyManager::releaseInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001151{
1152 ALOGV("releaseInput() %d", input);
1153 ssize_t index = mInputs.indexOfKey(input);
1154 if (index < 0) {
1155 ALOGW("releaseInput() releasing unknown input %d", input);
1156 return;
1157 }
1158 mpClientInterface->closeInput(input);
1159 delete mInputs.valueAt(index);
1160 mInputs.removeItem(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07001161 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001162 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001163 ALOGV("releaseInput() exit");
1164}
1165
Eric Laurentd4692962014-05-05 18:13:44 -07001166void AudioPolicyManager::closeAllInputs() {
1167 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1168 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1169 }
1170 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001171 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07001172}
1173
Eric Laurente0720872014-03-11 09:30:41 -07001174void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001175 int indexMin,
1176 int indexMax)
1177{
1178 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1179 if (indexMin < 0 || indexMin >= indexMax) {
1180 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1181 return;
1182 }
1183 mStreams[stream].mIndexMin = indexMin;
1184 mStreams[stream].mIndexMax = indexMax;
1185}
1186
Eric Laurente0720872014-03-11 09:30:41 -07001187status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001188 int index,
1189 audio_devices_t device)
1190{
1191
1192 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1193 return BAD_VALUE;
1194 }
1195 if (!audio_is_output_device(device)) {
1196 return BAD_VALUE;
1197 }
1198
1199 // Force max volume if stream cannot be muted
1200 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1201
1202 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1203 stream, device, index);
1204
1205 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1206 // clear all device specific values
1207 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1208 mStreams[stream].mIndexCur.clear();
1209 }
1210 mStreams[stream].mIndexCur.add(device, index);
1211
1212 // compute and apply stream volume on all outputs according to connected device
1213 status_t status = NO_ERROR;
1214 for (size_t i = 0; i < mOutputs.size(); i++) {
1215 audio_devices_t curDevice =
1216 getDeviceForVolume(mOutputs.valueAt(i)->device());
1217 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1218 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1219 if (volStatus != NO_ERROR) {
1220 status = volStatus;
1221 }
1222 }
1223 }
1224 return status;
1225}
1226
Eric Laurente0720872014-03-11 09:30:41 -07001227status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001228 int *index,
1229 audio_devices_t device)
1230{
1231 if (index == NULL) {
1232 return BAD_VALUE;
1233 }
1234 if (!audio_is_output_device(device)) {
1235 return BAD_VALUE;
1236 }
1237 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1238 // the strategy the stream belongs to.
1239 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1240 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1241 }
1242 device = getDeviceForVolume(device);
1243
1244 *index = mStreams[stream].getVolumeIndex(device);
1245 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1246 return NO_ERROR;
1247}
1248
Eric Laurente0720872014-03-11 09:30:41 -07001249audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001250 const SortedVector<audio_io_handle_t>& outputs)
1251{
1252 // select one output among several suitable for global effects.
1253 // The priority is as follows:
1254 // 1: An offloaded output. If the effect ends up not being offloadable,
1255 // AudioFlinger will invalidate the track and the offloaded output
1256 // will be closed causing the effect to be moved to a PCM output.
1257 // 2: A deep buffer output
1258 // 3: the first output in the list
1259
1260 if (outputs.size() == 0) {
1261 return 0;
1262 }
1263
1264 audio_io_handle_t outputOffloaded = 0;
1265 audio_io_handle_t outputDeepBuffer = 0;
1266
1267 for (size_t i = 0; i < outputs.size(); i++) {
1268 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001269 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001270 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1271 outputOffloaded = outputs[i];
1272 }
1273 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1274 outputDeepBuffer = outputs[i];
1275 }
1276 }
1277
1278 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1279 outputOffloaded, outputDeepBuffer);
1280 if (outputOffloaded != 0) {
1281 return outputOffloaded;
1282 }
1283 if (outputDeepBuffer != 0) {
1284 return outputDeepBuffer;
1285 }
1286
1287 return outputs[0];
1288}
1289
Eric Laurente0720872014-03-11 09:30:41 -07001290audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001291{
1292 // apply simple rule where global effects are attached to the same output as MUSIC streams
1293
Eric Laurent3b73df72014-03-11 09:06:29 -07001294 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001295 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1296 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1297
1298 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1299 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1300 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1301
1302 return output;
1303}
1304
Eric Laurente0720872014-03-11 09:30:41 -07001305status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001306 audio_io_handle_t io,
1307 uint32_t strategy,
1308 int session,
1309 int id)
1310{
1311 ssize_t index = mOutputs.indexOfKey(io);
1312 if (index < 0) {
1313 index = mInputs.indexOfKey(io);
1314 if (index < 0) {
1315 ALOGW("registerEffect() unknown io %d", io);
1316 return INVALID_OPERATION;
1317 }
1318 }
1319
1320 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1321 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1322 desc->name, desc->memoryUsage);
1323 return INVALID_OPERATION;
1324 }
1325 mTotalEffectsMemory += desc->memoryUsage;
1326 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1327 desc->name, io, strategy, session, id);
1328 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1329
1330 EffectDescriptor *pDesc = new EffectDescriptor();
1331 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
1332 pDesc->mIo = io;
1333 pDesc->mStrategy = (routing_strategy)strategy;
1334 pDesc->mSession = session;
1335 pDesc->mEnabled = false;
1336
1337 mEffects.add(id, pDesc);
1338
1339 return NO_ERROR;
1340}
1341
Eric Laurente0720872014-03-11 09:30:41 -07001342status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001343{
1344 ssize_t index = mEffects.indexOfKey(id);
1345 if (index < 0) {
1346 ALOGW("unregisterEffect() unknown effect ID %d", id);
1347 return INVALID_OPERATION;
1348 }
1349
1350 EffectDescriptor *pDesc = mEffects.valueAt(index);
1351
1352 setEffectEnabled(pDesc, false);
1353
1354 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
1355 ALOGW("unregisterEffect() memory %d too big for total %d",
1356 pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1357 pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1358 }
1359 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
1360 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1361 pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1362
1363 mEffects.removeItem(id);
1364 delete pDesc;
1365
1366 return NO_ERROR;
1367}
1368
Eric Laurente0720872014-03-11 09:30:41 -07001369status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001370{
1371 ssize_t index = mEffects.indexOfKey(id);
1372 if (index < 0) {
1373 ALOGW("unregisterEffect() unknown effect ID %d", id);
1374 return INVALID_OPERATION;
1375 }
1376
1377 return setEffectEnabled(mEffects.valueAt(index), enabled);
1378}
1379
Eric Laurente0720872014-03-11 09:30:41 -07001380status_t AudioPolicyManager::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001381{
1382 if (enabled == pDesc->mEnabled) {
1383 ALOGV("setEffectEnabled(%s) effect already %s",
1384 enabled?"true":"false", enabled?"enabled":"disabled");
1385 return INVALID_OPERATION;
1386 }
1387
1388 if (enabled) {
1389 if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
1390 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1391 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1392 return INVALID_OPERATION;
1393 }
1394 mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
1395 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1396 } else {
1397 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
1398 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1399 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1400 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1401 }
1402 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
1403 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1404 }
1405 pDesc->mEnabled = enabled;
1406 return NO_ERROR;
1407}
1408
Eric Laurente0720872014-03-11 09:30:41 -07001409bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001410{
1411 for (size_t i = 0; i < mEffects.size(); i++) {
1412 const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1413 if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1414 ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1415 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1416 pDesc->mDesc.name, pDesc->mSession);
1417 return true;
1418 }
1419 }
1420 return false;
1421}
1422
Eric Laurente0720872014-03-11 09:30:41 -07001423bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001424{
1425 nsecs_t sysTime = systemTime();
1426 for (size_t i = 0; i < mOutputs.size(); i++) {
1427 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001428 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001429 return true;
1430 }
1431 }
1432 return false;
1433}
1434
Eric Laurente0720872014-03-11 09:30:41 -07001435bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001436 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001437{
1438 nsecs_t sysTime = systemTime();
1439 for (size_t i = 0; i < mOutputs.size(); i++) {
1440 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1441 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001442 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001443 return true;
1444 }
1445 }
1446 return false;
1447}
1448
Eric Laurente0720872014-03-11 09:30:41 -07001449bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001450{
1451 for (size_t i = 0; i < mInputs.size(); i++) {
1452 const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1453 if ((inputDescriptor->mInputSource == (int)source ||
Eric Laurent3b73df72014-03-11 09:06:29 -07001454 (source == AUDIO_SOURCE_VOICE_RECOGNITION &&
Eric Laurente552edb2014-03-10 17:42:56 -07001455 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1456 && (inputDescriptor->mRefCount > 0)) {
1457 return true;
1458 }
1459 }
1460 return false;
1461}
1462
1463
Eric Laurente0720872014-03-11 09:30:41 -07001464status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001465{
1466 const size_t SIZE = 256;
1467 char buffer[SIZE];
1468 String8 result;
1469
1470 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1471 result.append(buffer);
1472
1473 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1474 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001475 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1476 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001477 snprintf(buffer, SIZE, " Force use for communications %d\n",
1478 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07001479 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001480 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07001481 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001482 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07001483 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001484 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07001485 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001486 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07001487 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001488
Eric Laurent3a4311c2014-03-17 12:00:47 -07001489 snprintf(buffer, SIZE, " Available output devices:\n");
1490 result.append(buffer);
1491 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07001492 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001493 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001494 }
1495 snprintf(buffer, SIZE, "\n Available input devices:\n");
1496 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07001497 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001498 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001499 }
Eric Laurente552edb2014-03-10 17:42:56 -07001500
1501 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
1502 write(fd, buffer, strlen(buffer));
1503 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001504 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07001505 write(fd, buffer, strlen(buffer));
1506 mHwModules[i]->dump(fd);
1507 }
1508
1509 snprintf(buffer, SIZE, "\nOutputs dump:\n");
1510 write(fd, buffer, strlen(buffer));
1511 for (size_t i = 0; i < mOutputs.size(); i++) {
1512 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1513 write(fd, buffer, strlen(buffer));
1514 mOutputs.valueAt(i)->dump(fd);
1515 }
1516
1517 snprintf(buffer, SIZE, "\nInputs dump:\n");
1518 write(fd, buffer, strlen(buffer));
1519 for (size_t i = 0; i < mInputs.size(); i++) {
1520 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1521 write(fd, buffer, strlen(buffer));
1522 mInputs.valueAt(i)->dump(fd);
1523 }
1524
1525 snprintf(buffer, SIZE, "\nStreams dump:\n");
1526 write(fd, buffer, strlen(buffer));
1527 snprintf(buffer, SIZE,
1528 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1529 write(fd, buffer, strlen(buffer));
Eric Laurent3b73df72014-03-11 09:06:29 -07001530 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001531 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07001532 write(fd, buffer, strlen(buffer));
1533 mStreams[i].dump(fd);
1534 }
1535
1536 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1537 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1538 write(fd, buffer, strlen(buffer));
1539
1540 snprintf(buffer, SIZE, "Registered effects:\n");
1541 write(fd, buffer, strlen(buffer));
1542 for (size_t i = 0; i < mEffects.size(); i++) {
1543 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1544 write(fd, buffer, strlen(buffer));
1545 mEffects.valueAt(i)->dump(fd);
1546 }
1547
1548
1549 return NO_ERROR;
1550}
1551
1552// This function checks for the parameters which can be offloaded.
1553// This can be enhanced depending on the capability of the DSP and policy
1554// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001555bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001556{
1557 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001558 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001559 offloadInfo.sample_rate, offloadInfo.channel_mask,
1560 offloadInfo.format,
1561 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1562 offloadInfo.has_video);
1563
1564 // Check if offload has been disabled
1565 char propValue[PROPERTY_VALUE_MAX];
1566 if (property_get("audio.offload.disable", propValue, "0")) {
1567 if (atoi(propValue) != 0) {
1568 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1569 return false;
1570 }
1571 }
1572
1573 // Check if stream type is music, then only allow offload as of now.
1574 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1575 {
1576 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1577 return false;
1578 }
1579
1580 //TODO: enable audio offloading with video when ready
1581 if (offloadInfo.has_video)
1582 {
1583 ALOGV("isOffloadSupported: has_video == true, returning false");
1584 return false;
1585 }
1586
1587 //If duration is less than minimum value defined in property, return false
1588 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1589 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1590 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1591 return false;
1592 }
1593 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1594 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1595 return false;
1596 }
1597
1598 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1599 // creating an offloaded track and tearing it down immediately after start when audioflinger
1600 // detects there is an active non offloadable effect.
1601 // FIXME: We should check the audio session here but we do not have it in this context.
1602 // This may prevent offloading in rare situations where effects are left active by apps
1603 // in the background.
1604 if (isNonOffloadableEffectEnabled()) {
1605 return false;
1606 }
1607
1608 // See if there is a profile to support this.
1609 // AUDIO_DEVICE_NONE
Eric Laurent951f4552014-05-20 10:48:17 -07001610 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001611 offloadInfo.sample_rate,
1612 offloadInfo.format,
1613 offloadInfo.channel_mask,
1614 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent951f4552014-05-20 10:48:17 -07001615 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1616 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001617}
1618
Eric Laurent6a94d692014-05-20 11:18:06 -07001619status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1620 audio_port_type_t type,
1621 unsigned int *num_ports,
1622 struct audio_port *ports,
1623 unsigned int *generation)
1624{
1625 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1626 generation == NULL) {
1627 return BAD_VALUE;
1628 }
1629 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1630 if (ports == NULL) {
1631 *num_ports = 0;
1632 }
1633
1634 size_t portsWritten = 0;
1635 size_t portsMax = *num_ports;
1636 *num_ports = 0;
1637 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1638 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1639 for (size_t i = 0;
1640 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
1641 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
1642 }
1643 *num_ports += mAvailableOutputDevices.size();
1644 }
1645 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1646 for (size_t i = 0;
1647 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
1648 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
1649 }
1650 *num_ports += mAvailableInputDevices.size();
1651 }
1652 }
1653 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
1654 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1655 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
1656 mInputs[i]->toAudioPort(&ports[portsWritten++]);
1657 }
1658 *num_ports += mInputs.size();
1659 }
1660 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1661 for (size_t i = 0; i < mOutputs.size() && portsWritten < portsMax; i++) {
1662 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
1663 }
1664 *num_ports += mOutputs.size();
1665 }
1666 }
1667 *generation = curAudioPortGeneration();
1668 ALOGV("listAudioPorts() got %d ports needed %d", portsWritten, *num_ports);
1669 return NO_ERROR;
1670}
1671
1672status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
1673{
1674 return NO_ERROR;
1675}
1676
1677AudioPolicyManager::AudioOutputDescriptor *AudioPolicyManager::getOutputFromId(
1678 audio_port_handle_t id) const
1679{
1680 AudioOutputDescriptor *outputDesc = NULL;
1681 for (size_t i = 0; i < mOutputs.size(); i++) {
1682 outputDesc = mOutputs.valueAt(i);
1683 if (outputDesc->mId == id) {
1684 break;
1685 }
1686 }
1687 return outputDesc;
1688}
1689
1690AudioPolicyManager::AudioInputDescriptor *AudioPolicyManager::getInputFromId(
1691 audio_port_handle_t id) const
1692{
1693 AudioInputDescriptor *inputDesc = NULL;
1694 for (size_t i = 0; i < mInputs.size(); i++) {
1695 inputDesc = mInputs.valueAt(i);
1696 if (inputDesc->mId == id) {
1697 break;
1698 }
1699 }
1700 return inputDesc;
1701}
1702
1703AudioPolicyManager::HwModule *AudioPolicyManager::getModuleForDevice(audio_devices_t device) const
1704{
1705 for (size_t i = 0; i < mHwModules.size(); i++) {
1706 if (mHwModules[i]->mHandle == 0) {
1707 continue;
1708 }
1709 if (audio_is_output_device(device)) {
1710 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
1711 {
1712 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
1713 return mHwModules[i];
1714 }
1715 }
1716 } else {
1717 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1718 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
1719 device & ~AUDIO_DEVICE_BIT_IN) {
1720 return mHwModules[i];
1721 }
1722 }
1723 }
1724 }
1725 return NULL;
1726}
1727
Eric Laurent1afeecb2014-05-14 08:52:28 -07001728AudioPolicyManager::HwModule *AudioPolicyManager::getModuleFromName(const char *name) const
1729{
1730 for (size_t i = 0; i < mHwModules.size(); i++)
1731 {
1732 if (strcmp(mHwModules[i]->mName, name) == 0) {
1733 return mHwModules[i];
1734 }
1735 }
1736 return NULL;
1737}
1738
1739
Eric Laurent6a94d692014-05-20 11:18:06 -07001740status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
1741 audio_patch_handle_t *handle,
1742 uid_t uid)
1743{
1744 ALOGV("createAudioPatch()");
1745
1746 if (handle == NULL || patch == NULL) {
1747 return BAD_VALUE;
1748 }
1749 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
1750
1751 if (patch->num_sources > 1 || patch->num_sinks > 1) {
1752 return INVALID_OPERATION;
1753 }
1754 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE ||
1755 patch->sinks[0].role != AUDIO_PORT_ROLE_SINK) {
1756 return INVALID_OPERATION;
1757 }
1758
1759 sp<AudioPatch> patchDesc;
1760 ssize_t index = mAudioPatches.indexOfKey(*handle);
1761
1762 ALOGV("createAudioPatch sink id %d role %d type %d", patch->sinks[0].id, patch->sinks[0].role,
1763 patch->sinks[0].type);
1764 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
1765 patch->sources[0].role,
1766 patch->sources[0].type);
1767
1768 if (index >= 0) {
1769 patchDesc = mAudioPatches.valueAt(index);
1770 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
1771 mUidCached, patchDesc->mUid, uid);
1772 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
1773 return INVALID_OPERATION;
1774 }
1775 } else {
1776 *handle = 0;
1777 }
1778
1779 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
1780 // TODO add support for mix to mix connection
1781 if (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) {
1782 ALOGV("createAudioPatch() source mix sink not device");
1783 return BAD_VALUE;
1784 }
1785 // output mix to output device connection
1786 AudioOutputDescriptor *outputDesc = getOutputFromId(patch->sources[0].id);
1787 if (outputDesc == NULL) {
1788 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
1789 return BAD_VALUE;
1790 }
1791 if (patchDesc != 0) {
1792 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
1793 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
1794 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
1795 return BAD_VALUE;
1796 }
1797 }
1798 sp<DeviceDescriptor> devDesc =
1799 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1800 if (devDesc == 0) {
1801 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[0].id);
1802 return BAD_VALUE;
1803 }
1804
1805 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mType,
1806 patch->sources[0].sample_rate,
1807 patch->sources[0].format,
1808 patch->sources[0].channel_mask,
1809 AUDIO_OUTPUT_FLAG_NONE)) {
1810 return INVALID_OPERATION;
1811 }
1812 // TODO: reconfigure output format and channels here
1813 ALOGV("createAudioPatch() setting device %08x on output %d",
1814 devDesc->mType, outputDesc->mIoHandle);
1815 setOutputDevice(outputDesc->mIoHandle,
1816 devDesc->mType,
1817 true,
1818 0,
1819 handle);
1820 index = mAudioPatches.indexOfKey(*handle);
1821 if (index >= 0) {
1822 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1823 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
1824 }
1825 patchDesc = mAudioPatches.valueAt(index);
1826 patchDesc->mUid = uid;
1827 ALOGV("createAudioPatch() success");
1828 } else {
1829 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
1830 return INVALID_OPERATION;
1831 }
1832 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1833 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
1834 // input device to input mix connection
1835 AudioInputDescriptor *inputDesc = getInputFromId(patch->sinks[0].id);
1836 if (inputDesc == NULL) {
1837 return BAD_VALUE;
1838 }
1839 if (patchDesc != 0) {
1840 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1841 return BAD_VALUE;
1842 }
1843 }
1844 sp<DeviceDescriptor> devDesc =
1845 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1846 if (devDesc == 0) {
1847 return BAD_VALUE;
1848 }
1849
1850 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mType,
1851 patch->sinks[0].sample_rate,
1852 patch->sinks[0].format,
1853 patch->sinks[0].channel_mask,
1854 AUDIO_OUTPUT_FLAG_NONE)) {
1855 return INVALID_OPERATION;
1856 }
1857 // TODO: reconfigure output format and channels here
1858 ALOGV("createAudioPatch() setting device %08x on output %d",
1859 devDesc->mType, inputDesc->mIoHandle);
1860 setInputDevice(inputDesc->mIoHandle,
1861 devDesc->mType,
1862 true,
1863 handle);
1864 index = mAudioPatches.indexOfKey(*handle);
1865 if (index >= 0) {
1866 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1867 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
1868 }
1869 patchDesc = mAudioPatches.valueAt(index);
1870 patchDesc->mUid = uid;
1871 ALOGV("createAudioPatch() success");
1872 } else {
1873 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
1874 return INVALID_OPERATION;
1875 }
1876 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1877 // device to device connection
1878 if (patchDesc != 0) {
1879 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id &&
1880 patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1881 return BAD_VALUE;
1882 }
1883 }
1884
1885 sp<DeviceDescriptor> srcDeviceDesc =
1886 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1887 sp<DeviceDescriptor> sinkDeviceDesc =
1888 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1889 if (srcDeviceDesc == 0 || sinkDeviceDesc == 0) {
1890 return BAD_VALUE;
1891 }
1892 //update source and sink with our own data as the data passed in the patch may
1893 // be incomplete.
1894 struct audio_patch newPatch = *patch;
1895 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
1896 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[0], &patch->sinks[0]);
1897
1898 // TODO: add support for devices on different HW modules
1899 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
1900 return INVALID_OPERATION;
1901 }
1902 // TODO: check from routing capabilities in config file and other conflicting patches
1903
1904 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
1905 if (index >= 0) {
1906 afPatchHandle = patchDesc->mAfPatchHandle;
1907 }
1908
1909 status_t status = mpClientInterface->createAudioPatch(&newPatch,
1910 &afPatchHandle,
1911 0);
1912 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
1913 status, afPatchHandle);
1914 if (status == NO_ERROR) {
1915 if (index < 0) {
1916 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
1917 &newPatch, uid);
1918 addAudioPatch(patchDesc->mHandle, patchDesc);
1919 } else {
1920 patchDesc->mPatch = newPatch;
1921 }
1922 patchDesc->mAfPatchHandle = afPatchHandle;
1923 *handle = patchDesc->mHandle;
1924 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001925 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07001926 } else {
1927 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
1928 status);
1929 return INVALID_OPERATION;
1930 }
1931 } else {
1932 return BAD_VALUE;
1933 }
1934 } else {
1935 return BAD_VALUE;
1936 }
1937 return NO_ERROR;
1938}
1939
1940status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
1941 uid_t uid)
1942{
1943 ALOGV("releaseAudioPatch() patch %d", handle);
1944
1945 ssize_t index = mAudioPatches.indexOfKey(handle);
1946
1947 if (index < 0) {
1948 return BAD_VALUE;
1949 }
1950 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
1951 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
1952 mUidCached, patchDesc->mUid, uid);
1953 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
1954 return INVALID_OPERATION;
1955 }
1956
1957 struct audio_patch *patch = &patchDesc->mPatch;
1958 patchDesc->mUid = mUidCached;
1959 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
1960 AudioOutputDescriptor *outputDesc = getOutputFromId(patch->sources[0].id);
1961 if (outputDesc == NULL) {
1962 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
1963 return BAD_VALUE;
1964 }
1965
1966 setOutputDevice(outputDesc->mIoHandle,
1967 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
1968 true,
1969 0,
1970 NULL);
1971 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1972 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
1973 AudioInputDescriptor *inputDesc = getInputFromId(patch->sinks[0].id);
1974 if (inputDesc == NULL) {
1975 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
1976 return BAD_VALUE;
1977 }
1978 setInputDevice(inputDesc->mIoHandle,
1979 getNewInputDevice(inputDesc->mIoHandle),
1980 true,
1981 NULL);
1982 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1983 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
1984 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1985 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
1986 status, patchDesc->mAfPatchHandle);
1987 removeAudioPatch(patchDesc->mHandle);
1988 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001989 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07001990 } else {
1991 return BAD_VALUE;
1992 }
1993 } else {
1994 return BAD_VALUE;
1995 }
1996 return NO_ERROR;
1997}
1998
1999status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2000 struct audio_patch *patches,
2001 unsigned int *generation)
2002{
2003 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
2004 generation == NULL) {
2005 return BAD_VALUE;
2006 }
2007 ALOGV("listAudioPatches() num_patches %d patches %p available patches %d",
2008 *num_patches, patches, mAudioPatches.size());
2009 if (patches == NULL) {
2010 *num_patches = 0;
2011 }
2012
2013 size_t patchesWritten = 0;
2014 size_t patchesMax = *num_patches;
2015 for (size_t i = 0;
2016 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2017 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2018 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
2019 ALOGV("listAudioPatches() patch %d num_sources %d num_sinks %d",
2020 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2021 }
2022 *num_patches = mAudioPatches.size();
2023
2024 *generation = curAudioPortGeneration();
2025 ALOGV("listAudioPatches() got %d patches needed %d", patchesWritten, *num_patches);
2026 return NO_ERROR;
2027}
2028
2029status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config __unused)
2030{
2031 return NO_ERROR;
2032}
2033
2034void AudioPolicyManager::clearAudioPatches(uid_t uid)
2035{
2036 for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++) {
2037 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2038 if (patchDesc->mUid == uid) {
2039 // releaseAudioPatch() removes the patch from mAudioPatches
2040 if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) {
2041 i--;
2042 }
2043 }
2044 }
2045}
2046
2047status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2048 const sp<AudioPatch>& patch)
2049{
2050 ssize_t index = mAudioPatches.indexOfKey(handle);
2051
2052 if (index >= 0) {
2053 ALOGW("addAudioPatch() patch %d already in", handle);
2054 return ALREADY_EXISTS;
2055 }
2056 mAudioPatches.add(handle, patch);
2057 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2058 "sink handle %d",
2059 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2060 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2061 return NO_ERROR;
2062}
2063
2064status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2065{
2066 ssize_t index = mAudioPatches.indexOfKey(handle);
2067
2068 if (index < 0) {
2069 ALOGW("removeAudioPatch() patch %d not in", handle);
2070 return ALREADY_EXISTS;
2071 }
2072 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2073 mAudioPatches.valueAt(index)->mAfPatchHandle);
2074 mAudioPatches.removeItemsAt(index);
2075 return NO_ERROR;
2076}
2077
Eric Laurente552edb2014-03-10 17:42:56 -07002078// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002079// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002080// ----------------------------------------------------------------------------
2081
Eric Laurent3a4311c2014-03-17 12:00:47 -07002082uint32_t AudioPolicyManager::nextUniqueId()
2083{
2084 return android_atomic_inc(&mNextUniqueId);
2085}
2086
Eric Laurent6a94d692014-05-20 11:18:06 -07002087uint32_t AudioPolicyManager::nextAudioPortGeneration()
2088{
2089 return android_atomic_inc(&mAudioPortGeneration);
2090}
2091
Eric Laurente0720872014-03-11 09:30:41 -07002092AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002093 :
2094#ifdef AUDIO_POLICY_TEST
2095 Thread(false),
2096#endif //AUDIO_POLICY_TEST
2097 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002098 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002099 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2100 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002101 mA2dpSuspended(false),
Eric Laurent6a94d692014-05-20 11:18:06 -07002102 mSpeakerDrcEnabled(false), mNextUniqueId(1),
2103 mAudioPortGeneration(1)
Eric Laurente552edb2014-03-10 17:42:56 -07002104{
Eric Laurent6a94d692014-05-20 11:18:06 -07002105 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002106 mpClientInterface = clientInterface;
2107
Eric Laurent3b73df72014-03-11 09:06:29 -07002108 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2109 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002110 }
2111
Eric Laurent1afeecb2014-05-14 08:52:28 -07002112 mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002113 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2114 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2115 ALOGE("could not load audio policy configuration file, setting defaults");
2116 defaultAudioPolicyConfig();
2117 }
2118 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002119 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002120
2121 // must be done after reading the policy
2122 initializeVolumeCurves();
2123
2124 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002125 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2126 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002127 for (size_t i = 0; i < mHwModules.size(); i++) {
2128 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2129 if (mHwModules[i]->mHandle == 0) {
2130 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2131 continue;
2132 }
2133 // open all output streams needed to access attached devices
2134 // except for direct output streams that are only opened when they are actually
2135 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002136 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002137 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2138 {
Eric Laurent951f4552014-05-20 10:48:17 -07002139 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002140
Eric Laurent3a4311c2014-03-17 12:00:47 -07002141 if (outProfile->mSupportedDevices.isEmpty()) {
2142 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2143 continue;
2144 }
2145
2146 audio_devices_t profileTypes = outProfile->mSupportedDevices.types();
2147 if ((profileTypes & outputDeviceTypes) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002148 ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
2149 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002150
Eric Laurent951f4552014-05-20 10:48:17 -07002151 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice->mDeviceType & profileTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07002152 audio_io_handle_t output = mpClientInterface->openOutput(
2153 outProfile->mModule->mHandle,
2154 &outputDesc->mDevice,
2155 &outputDesc->mSamplingRate,
2156 &outputDesc->mFormat,
2157 &outputDesc->mChannelMask,
2158 &outputDesc->mLatency,
2159 outputDesc->mFlags);
2160 if (output == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002161 ALOGW("Cannot open output stream for device %08x on hw module %s",
2162 outputDesc->mDevice,
2163 mHwModules[i]->mName);
Eric Laurente552edb2014-03-10 17:42:56 -07002164 delete outputDesc;
2165 } else {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002166 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
Eric Laurent951f4552014-05-20 10:48:17 -07002167 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002168 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002169 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002170 // give a valid ID to an attached device once confirmed it is reachable
2171 if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) {
2172 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002173 mAvailableOutputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002174 }
2175 }
Eric Laurente552edb2014-03-10 17:42:56 -07002176 if (mPrimaryOutput == 0 &&
2177 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2178 mPrimaryOutput = output;
2179 }
2180 addOutput(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002181 ALOGI("CSTOR setOutputDevice %08x", outputDesc->mDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07002182 setOutputDevice(output,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002183 outputDesc->mDevice,
Eric Laurente552edb2014-03-10 17:42:56 -07002184 true);
2185 }
2186 }
2187 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002188 // open input streams needed to access attached devices to validate
2189 // mAvailableInputDevices list
2190 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2191 {
Eric Laurent951f4552014-05-20 10:48:17 -07002192 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002193
Eric Laurent3a4311c2014-03-17 12:00:47 -07002194 if (inProfile->mSupportedDevices.isEmpty()) {
2195 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2196 continue;
2197 }
2198
2199 audio_devices_t profileTypes = inProfile->mSupportedDevices.types();
2200 if (profileTypes & inputDeviceTypes) {
2201 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(inProfile);
2202
2203 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
Eric Laurent951f4552014-05-20 10:48:17 -07002204 inputDesc->mDevice = inProfile->mSupportedDevices[0]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002205 audio_io_handle_t input = mpClientInterface->openInput(
2206 inProfile->mModule->mHandle,
2207 &inputDesc->mDevice,
2208 &inputDesc->mSamplingRate,
2209 &inputDesc->mFormat,
2210 &inputDesc->mChannelMask);
2211
2212 if (input != 0) {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002213 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
Eric Laurent951f4552014-05-20 10:48:17 -07002214 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002215 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002216 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002217 // give a valid ID to an attached device once confirmed it is reachable
2218 if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) {
2219 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002220 mAvailableInputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002221 }
2222 }
2223 mpClientInterface->closeInput(input);
2224 } else {
2225 ALOGW("Cannot open input stream for device %08x on hw module %s",
2226 inputDesc->mDevice,
2227 mHwModules[i]->mName);
2228 }
2229 delete inputDesc;
2230 }
2231 }
2232 }
2233 // make sure all attached devices have been allocated a unique ID
2234 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
2235 if (mAvailableOutputDevices[i]->mId == 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07002236 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002237 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2238 continue;
2239 }
2240 i++;
2241 }
2242 for (size_t i = 0; i < mAvailableInputDevices.size();) {
2243 if (mAvailableInputDevices[i]->mId == 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07002244 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002245 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2246 continue;
2247 }
2248 i++;
2249 }
2250 // make sure default device is reachable
2251 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07002252 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002253 }
Eric Laurente552edb2014-03-10 17:42:56 -07002254
2255 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2256
2257 updateDevicesAndOutputs();
2258
2259#ifdef AUDIO_POLICY_TEST
2260 if (mPrimaryOutput != 0) {
2261 AudioParameter outputCmd = AudioParameter();
2262 outputCmd.addInt(String8("set_id"), 0);
2263 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2264
2265 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2266 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002267 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2268 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002269 mTestLatencyMs = 0;
2270 mCurOutput = 0;
2271 mDirectOutput = false;
2272 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2273 mTestOutputs[i] = 0;
2274 }
2275
2276 const size_t SIZE = 256;
2277 char buffer[SIZE];
2278 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2279 run(buffer, ANDROID_PRIORITY_AUDIO);
2280 }
2281#endif //AUDIO_POLICY_TEST
2282}
2283
Eric Laurente0720872014-03-11 09:30:41 -07002284AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002285{
2286#ifdef AUDIO_POLICY_TEST
2287 exit();
2288#endif //AUDIO_POLICY_TEST
2289 for (size_t i = 0; i < mOutputs.size(); i++) {
2290 mpClientInterface->closeOutput(mOutputs.keyAt(i));
2291 delete mOutputs.valueAt(i);
2292 }
2293 for (size_t i = 0; i < mInputs.size(); i++) {
2294 mpClientInterface->closeInput(mInputs.keyAt(i));
2295 delete mInputs.valueAt(i);
2296 }
2297 for (size_t i = 0; i < mHwModules.size(); i++) {
2298 delete mHwModules[i];
2299 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002300 mAvailableOutputDevices.clear();
2301 mAvailableInputDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002302}
2303
Eric Laurente0720872014-03-11 09:30:41 -07002304status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002305{
2306 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2307}
2308
2309#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002310bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002311{
2312 ALOGV("entering threadLoop()");
2313 while (!exitPending())
2314 {
2315 String8 command;
2316 int valueInt;
2317 String8 value;
2318
2319 Mutex::Autolock _l(mLock);
2320 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2321
2322 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2323 AudioParameter param = AudioParameter(command);
2324
2325 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2326 valueInt != 0) {
2327 ALOGV("Test command %s received", command.string());
2328 String8 target;
2329 if (param.get(String8("target"), target) != NO_ERROR) {
2330 target = "Manager";
2331 }
2332 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2333 param.remove(String8("test_cmd_policy_output"));
2334 mCurOutput = valueInt;
2335 }
2336 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2337 param.remove(String8("test_cmd_policy_direct"));
2338 if (value == "false") {
2339 mDirectOutput = false;
2340 } else if (value == "true") {
2341 mDirectOutput = true;
2342 }
2343 }
2344 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2345 param.remove(String8("test_cmd_policy_input"));
2346 mTestInput = valueInt;
2347 }
2348
2349 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2350 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002351 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002352 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002353 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002354 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002355 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002356 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002357 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002358 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002359 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002360 if (target == "Manager") {
2361 mTestFormat = format;
2362 } else if (mTestOutputs[mCurOutput] != 0) {
2363 AudioParameter outputParam = AudioParameter();
2364 outputParam.addInt(String8("format"), format);
2365 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2366 }
2367 }
2368 }
2369 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2370 param.remove(String8("test_cmd_policy_channels"));
2371 int channels = 0;
2372
2373 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002374 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002375 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002376 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002377 }
2378 if (channels != 0) {
2379 if (target == "Manager") {
2380 mTestChannels = channels;
2381 } else if (mTestOutputs[mCurOutput] != 0) {
2382 AudioParameter outputParam = AudioParameter();
2383 outputParam.addInt(String8("channels"), channels);
2384 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2385 }
2386 }
2387 }
2388 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2389 param.remove(String8("test_cmd_policy_sampleRate"));
2390 if (valueInt >= 0 && valueInt <= 96000) {
2391 int samplingRate = valueInt;
2392 if (target == "Manager") {
2393 mTestSamplingRate = samplingRate;
2394 } else if (mTestOutputs[mCurOutput] != 0) {
2395 AudioParameter outputParam = AudioParameter();
2396 outputParam.addInt(String8("sampling_rate"), samplingRate);
2397 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2398 }
2399 }
2400 }
2401
2402 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2403 param.remove(String8("test_cmd_policy_reopen"));
2404
2405 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
2406 mpClientInterface->closeOutput(mPrimaryOutput);
2407
2408 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
2409
2410 delete mOutputs.valueFor(mPrimaryOutput);
2411 mOutputs.removeItem(mPrimaryOutput);
2412
2413 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
2414 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
2415 mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
2416 &outputDesc->mDevice,
2417 &outputDesc->mSamplingRate,
2418 &outputDesc->mFormat,
2419 &outputDesc->mChannelMask,
2420 &outputDesc->mLatency,
2421 outputDesc->mFlags);
2422 if (mPrimaryOutput == 0) {
2423 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
2424 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
2425 } else {
2426 AudioParameter outputCmd = AudioParameter();
2427 outputCmd.addInt(String8("set_id"), 0);
2428 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2429 addOutput(mPrimaryOutput, outputDesc);
2430 }
2431 }
2432
2433
2434 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2435 }
2436 }
2437 return false;
2438}
2439
Eric Laurente0720872014-03-11 09:30:41 -07002440void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002441{
2442 {
2443 AutoMutex _l(mLock);
2444 requestExit();
2445 mWaitWorkCV.signal();
2446 }
2447 requestExitAndWait();
2448}
2449
Eric Laurente0720872014-03-11 09:30:41 -07002450int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002451{
2452 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2453 if (output == mTestOutputs[i]) return i;
2454 }
2455 return 0;
2456}
2457#endif //AUDIO_POLICY_TEST
2458
2459// ---
2460
Eric Laurent951f4552014-05-20 10:48:17 -07002461void AudioPolicyManager::addOutput(audio_io_handle_t output, AudioOutputDescriptor *outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002462{
Eric Laurent951f4552014-05-20 10:48:17 -07002463 outputDesc->mIoHandle = output;
2464 outputDesc->mId = nextUniqueId();
2465 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002466 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002467}
2468
Eric Laurent951f4552014-05-20 10:48:17 -07002469void AudioPolicyManager::addInput(audio_io_handle_t input, AudioInputDescriptor *inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002470{
Eric Laurent951f4552014-05-20 10:48:17 -07002471 inputDesc->mIoHandle = input;
2472 inputDesc->mId = nextUniqueId();
2473 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002474 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002475}
Eric Laurente552edb2014-03-10 17:42:56 -07002476
Eric Laurent3a4311c2014-03-17 12:00:47 -07002477String8 AudioPolicyManager::addressToParameter(audio_devices_t device, const String8 address)
2478{
2479 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
2480 return String8("a2dp_sink_address=")+address;
2481 }
2482 return address;
2483}
2484
Eric Laurente0720872014-03-11 09:30:41 -07002485status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -07002486 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07002487 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002488 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002489{
2490 AudioOutputDescriptor *desc;
2491
Eric Laurent3b73df72014-03-11 09:06:29 -07002492 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002493 // first list already open outputs that can be routed to this device
2494 for (size_t i = 0; i < mOutputs.size(); i++) {
2495 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002496 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002497 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2498 outputs.add(mOutputs.keyAt(i));
2499 }
2500 }
2501 // then look for output profiles that can be routed to this device
Eric Laurent951f4552014-05-20 10:48:17 -07002502 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002503 for (size_t i = 0; i < mHwModules.size(); i++)
2504 {
2505 if (mHwModules[i]->mHandle == 0) {
2506 continue;
2507 }
2508 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2509 {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002510 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
Eric Laurentd4692962014-05-05 18:13:44 -07002511 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002512 profiles.add(mHwModules[i]->mOutputProfiles[j]);
2513 }
2514 }
2515 }
2516
2517 if (profiles.isEmpty() && outputs.isEmpty()) {
2518 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2519 return BAD_VALUE;
2520 }
2521
2522 // open outputs for matching profiles if needed. Direct outputs are also opened to
2523 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2524 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent951f4552014-05-20 10:48:17 -07002525 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002526
2527 // nothing to do if one output is already opened for this profile
2528 size_t j;
2529 for (j = 0; j < mOutputs.size(); j++) {
2530 desc = mOutputs.valueAt(j);
2531 if (!desc->isDuplicated() && desc->mProfile == profile) {
2532 break;
2533 }
2534 }
2535 if (j != mOutputs.size()) {
2536 continue;
2537 }
2538
Eric Laurent3a4311c2014-03-17 12:00:47 -07002539 ALOGV("opening output for device %08x with params %s", device, address.string());
Eric Laurente552edb2014-03-10 17:42:56 -07002540 desc = new AudioOutputDescriptor(profile);
2541 desc->mDevice = device;
2542 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
2543 offloadInfo.sample_rate = desc->mSamplingRate;
2544 offloadInfo.format = desc->mFormat;
2545 offloadInfo.channel_mask = desc->mChannelMask;
2546
2547 audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
2548 &desc->mDevice,
2549 &desc->mSamplingRate,
2550 &desc->mFormat,
2551 &desc->mChannelMask,
2552 &desc->mLatency,
2553 desc->mFlags,
2554 &offloadInfo);
2555 if (output != 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002556 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07002557 if (!address.isEmpty()) {
2558 mpClientInterface->setParameters(output, addressToParameter(device, address));
Eric Laurente552edb2014-03-10 17:42:56 -07002559 }
2560
Eric Laurentd4692962014-05-05 18:13:44 -07002561 // Here is where we step through and resolve any "dynamic" fields
2562 String8 reply;
2563 char *value;
2564 if (profile->mSamplingRates[0] == 0) {
2565 reply = mpClientInterface->getParameters(output,
2566 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2567 ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
2568 reply.string());
2569 value = strpbrk((char *)reply.string(), "=");
2570 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002571 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002572 }
Eric Laurentd4692962014-05-05 18:13:44 -07002573 }
2574 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2575 reply = mpClientInterface->getParameters(output,
2576 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2577 ALOGV("checkOutputsForDevice() direct output sup formats %s",
2578 reply.string());
2579 value = strpbrk((char *)reply.string(), "=");
2580 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002581 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002582 }
Eric Laurentd4692962014-05-05 18:13:44 -07002583 }
2584 if (profile->mChannelMasks[0] == 0) {
2585 reply = mpClientInterface->getParameters(output,
2586 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2587 ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
2588 reply.string());
2589 value = strpbrk((char *)reply.string(), "=");
2590 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002591 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002592 }
Eric Laurentd4692962014-05-05 18:13:44 -07002593 }
2594 if (((profile->mSamplingRates[0] == 0) &&
2595 (profile->mSamplingRates.size() < 2)) ||
2596 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
2597 (profile->mFormats.size() < 2)) ||
2598 ((profile->mChannelMasks[0] == 0) &&
2599 (profile->mChannelMasks.size() < 2))) {
2600 ALOGW("checkOutputsForDevice() direct output missing param");
2601 mpClientInterface->closeOutput(output);
2602 output = 0;
2603 } else if (profile->mSamplingRates[0] == 0) {
2604 mpClientInterface->closeOutput(output);
2605 desc->mSamplingRate = profile->mSamplingRates[1];
2606 offloadInfo.sample_rate = desc->mSamplingRate;
2607 output = mpClientInterface->openOutput(
2608 profile->mModule->mHandle,
2609 &desc->mDevice,
2610 &desc->mSamplingRate,
2611 &desc->mFormat,
2612 &desc->mChannelMask,
2613 &desc->mLatency,
2614 desc->mFlags,
2615 &offloadInfo);
2616 }
2617
2618 if (output != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002619 addOutput(output, desc);
Eric Laurentd4692962014-05-05 18:13:44 -07002620 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
2621 audio_io_handle_t duplicatedOutput = 0;
Eric Laurente552edb2014-03-10 17:42:56 -07002622
Eric Laurentd4692962014-05-05 18:13:44 -07002623 // set initial stream volume for device
2624 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07002625
Eric Laurentd4692962014-05-05 18:13:44 -07002626 //TODO: configure audio effect output stage here
2627
2628 // open a duplicating output thread for the new output and the primary output
2629 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
2630 mPrimaryOutput);
2631 if (duplicatedOutput != 0) {
2632 // add duplicated output descriptor
2633 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
2634 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
2635 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
2636 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
2637 dupOutputDesc->mFormat = desc->mFormat;
2638 dupOutputDesc->mChannelMask = desc->mChannelMask;
2639 dupOutputDesc->mLatency = desc->mLatency;
2640 addOutput(duplicatedOutput, dupOutputDesc);
2641 applyStreamVolumes(duplicatedOutput, device, 0, true);
2642 } else {
2643 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
2644 mPrimaryOutput, output);
2645 mpClientInterface->closeOutput(output);
2646 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07002647 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002648 output = 0;
2649 }
Eric Laurente552edb2014-03-10 17:42:56 -07002650 }
2651 }
2652 }
2653 if (output == 0) {
2654 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
2655 delete desc;
2656 profiles.removeAt(profile_index);
2657 profile_index--;
2658 } else {
2659 outputs.add(output);
2660 ALOGV("checkOutputsForDevice(): adding output %d", output);
2661 }
2662 }
2663
2664 if (profiles.isEmpty()) {
2665 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2666 return BAD_VALUE;
2667 }
Eric Laurentd4692962014-05-05 18:13:44 -07002668 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07002669 // check if one opened output is not needed any more after disconnecting one device
2670 for (size_t i = 0; i < mOutputs.size(); i++) {
2671 desc = mOutputs.valueAt(i);
2672 if (!desc->isDuplicated() &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07002673 !(desc->mProfile->mSupportedDevices.types() &
2674 mAvailableOutputDevices.types())) {
Eric Laurente552edb2014-03-10 17:42:56 -07002675 ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
2676 outputs.add(mOutputs.keyAt(i));
2677 }
2678 }
Eric Laurentd4692962014-05-05 18:13:44 -07002679 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002680 for (size_t i = 0; i < mHwModules.size(); i++)
2681 {
2682 if (mHwModules[i]->mHandle == 0) {
2683 continue;
2684 }
2685 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2686 {
Eric Laurent951f4552014-05-20 10:48:17 -07002687 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07002688 if (profile->mSupportedDevices.types() & device) {
2689 ALOGV("checkOutputsForDevice(): "
2690 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002691 if (profile->mSamplingRates[0] == 0) {
2692 profile->mSamplingRates.clear();
2693 profile->mSamplingRates.add(0);
2694 }
2695 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2696 profile->mFormats.clear();
2697 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2698 }
2699 if (profile->mChannelMasks[0] == 0) {
2700 profile->mChannelMasks.clear();
2701 profile->mChannelMasks.add(0);
2702 }
2703 }
2704 }
2705 }
2706 }
2707 return NO_ERROR;
2708}
2709
Eric Laurentd4692962014-05-05 18:13:44 -07002710status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
2711 audio_policy_dev_state_t state,
2712 SortedVector<audio_io_handle_t>& inputs,
2713 const String8 address)
2714{
2715 AudioInputDescriptor *desc;
2716 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2717 // first list already open inputs that can be routed to this device
2718 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2719 desc = mInputs.valueAt(input_index);
2720 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
2721 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2722 inputs.add(mInputs.keyAt(input_index));
2723 }
2724 }
2725
2726 // then look for input profiles that can be routed to this device
Eric Laurent951f4552014-05-20 10:48:17 -07002727 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07002728 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
2729 {
2730 if (mHwModules[module_idx]->mHandle == 0) {
2731 continue;
2732 }
2733 for (size_t profile_index = 0;
2734 profile_index < mHwModules[module_idx]->mInputProfiles.size();
2735 profile_index++)
2736 {
2737 if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types()
2738 & (device & ~AUDIO_DEVICE_BIT_IN)) {
2739 ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2740 profile_index, module_idx);
2741 profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]);
2742 }
2743 }
2744 }
2745
2746 if (profiles.isEmpty() && inputs.isEmpty()) {
2747 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2748 return BAD_VALUE;
2749 }
2750
2751 // open inputs for matching profiles if needed. Direct inputs are also opened to
2752 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2753 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2754
Eric Laurent951f4552014-05-20 10:48:17 -07002755 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002756 // nothing to do if one input is already opened for this profile
2757 size_t input_index;
2758 for (input_index = 0; input_index < mInputs.size(); input_index++) {
2759 desc = mInputs.valueAt(input_index);
2760 if (desc->mProfile == profile) {
2761 break;
2762 }
2763 }
2764 if (input_index != mInputs.size()) {
2765 continue;
2766 }
2767
2768 ALOGV("opening input for device 0x%X with params %s", device, address.string());
2769 desc = new AudioInputDescriptor(profile);
2770 desc->mDevice = device;
2771
2772 audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2773 &desc->mDevice,
2774 &desc->mSamplingRate,
2775 &desc->mFormat,
2776 &desc->mChannelMask);
2777
2778 if (input != 0) {
2779 if (!address.isEmpty()) {
2780 mpClientInterface->setParameters(input, addressToParameter(device, address));
2781 }
2782
2783 // Here is where we step through and resolve any "dynamic" fields
2784 String8 reply;
2785 char *value;
2786 if (profile->mSamplingRates[0] == 0) {
2787 reply = mpClientInterface->getParameters(input,
2788 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2789 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2790 reply.string());
2791 value = strpbrk((char *)reply.string(), "=");
2792 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002793 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002794 }
2795 }
2796 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2797 reply = mpClientInterface->getParameters(input,
2798 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2799 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2800 value = strpbrk((char *)reply.string(), "=");
2801 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002802 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002803 }
2804 }
2805 if (profile->mChannelMasks[0] == 0) {
2806 reply = mpClientInterface->getParameters(input,
2807 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2808 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2809 reply.string());
2810 value = strpbrk((char *)reply.string(), "=");
2811 if (value != NULL) {
Eric Laurent951f4552014-05-20 10:48:17 -07002812 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002813 }
2814 }
2815 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2816 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2817 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2818 ALOGW("checkInputsForDevice() direct input missing param");
2819 mpClientInterface->closeInput(input);
2820 input = 0;
2821 }
2822
2823 if (input != 0) {
2824 addInput(input, desc);
2825 }
2826 } // endif input != 0
2827
2828 if (input == 0) {
2829 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
2830 delete desc;
2831 profiles.removeAt(profile_index);
2832 profile_index--;
2833 } else {
2834 inputs.add(input);
2835 ALOGV("checkInputsForDevice(): adding input %d", input);
2836 }
2837 } // end scan profiles
2838
2839 if (profiles.isEmpty()) {
2840 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2841 return BAD_VALUE;
2842 }
2843 } else {
2844 // Disconnect
2845 // check if one opened input is not needed any more after disconnecting one device
2846 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2847 desc = mInputs.valueAt(input_index);
2848 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) {
2849 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2850 mInputs.keyAt(input_index));
2851 inputs.add(mInputs.keyAt(input_index));
2852 }
2853 }
2854 // Clear any profiles associated with the disconnected device.
2855 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
2856 if (mHwModules[module_index]->mHandle == 0) {
2857 continue;
2858 }
2859 for (size_t profile_index = 0;
2860 profile_index < mHwModules[module_index]->mInputProfiles.size();
2861 profile_index++) {
Eric Laurent951f4552014-05-20 10:48:17 -07002862 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002863 if (profile->mSupportedDevices.types() & device) {
2864 ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2865 profile_index, module_index);
2866 if (profile->mSamplingRates[0] == 0) {
2867 profile->mSamplingRates.clear();
2868 profile->mSamplingRates.add(0);
2869 }
2870 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2871 profile->mFormats.clear();
2872 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2873 }
2874 if (profile->mChannelMasks[0] == 0) {
2875 profile->mChannelMasks.clear();
2876 profile->mChannelMasks.add(0);
2877 }
2878 }
2879 }
2880 }
2881 } // end disconnect
2882
2883 return NO_ERROR;
2884}
2885
2886
Eric Laurente0720872014-03-11 09:30:41 -07002887void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002888{
2889 ALOGV("closeOutput(%d)", output);
2890
2891 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2892 if (outputDesc == NULL) {
2893 ALOGW("closeOutput() unknown output %d", output);
2894 return;
2895 }
2896
2897 // look for duplicated outputs connected to the output being removed.
2898 for (size_t i = 0; i < mOutputs.size(); i++) {
2899 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2900 if (dupOutputDesc->isDuplicated() &&
2901 (dupOutputDesc->mOutput1 == outputDesc ||
2902 dupOutputDesc->mOutput2 == outputDesc)) {
2903 AudioOutputDescriptor *outputDesc2;
2904 if (dupOutputDesc->mOutput1 == outputDesc) {
2905 outputDesc2 = dupOutputDesc->mOutput2;
2906 } else {
2907 outputDesc2 = dupOutputDesc->mOutput1;
2908 }
2909 // As all active tracks on duplicated output will be deleted,
2910 // and as they were also referenced on the other output, the reference
2911 // count for their stream type must be adjusted accordingly on
2912 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07002913 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07002914 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07002915 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07002916 }
2917 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2918 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2919
2920 mpClientInterface->closeOutput(duplicatedOutput);
2921 delete mOutputs.valueFor(duplicatedOutput);
2922 mOutputs.removeItem(duplicatedOutput);
2923 }
2924 }
2925
2926 AudioParameter param;
2927 param.add(String8("closing"), String8("true"));
2928 mpClientInterface->setParameters(output, param.toString());
2929
2930 mpClientInterface->closeOutput(output);
2931 delete outputDesc;
2932 mOutputs.removeItem(output);
2933 mPreviousOutputs = mOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002934 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002935}
2936
Eric Laurente0720872014-03-11 09:30:41 -07002937SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07002938 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2939{
2940 SortedVector<audio_io_handle_t> outputs;
2941
2942 ALOGVV("getOutputsForDevice() device %04x", device);
2943 for (size_t i = 0; i < openOutputs.size(); i++) {
2944 ALOGVV("output %d isDuplicated=%d device=%04x",
2945 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2946 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2947 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2948 outputs.add(openOutputs.keyAt(i));
2949 }
2950 }
2951 return outputs;
2952}
2953
Eric Laurente0720872014-03-11 09:30:41 -07002954bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07002955 SortedVector<audio_io_handle_t>& outputs2)
2956{
2957 if (outputs1.size() != outputs2.size()) {
2958 return false;
2959 }
2960 for (size_t i = 0; i < outputs1.size(); i++) {
2961 if (outputs1[i] != outputs2[i]) {
2962 return false;
2963 }
2964 }
2965 return true;
2966}
2967
Eric Laurente0720872014-03-11 09:30:41 -07002968void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07002969{
2970 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
2971 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2972 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2973 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2974
2975 if (!vectorsEqual(srcOutputs,dstOutputs)) {
2976 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2977 strategy, srcOutputs[0], dstOutputs[0]);
2978 // mute strategy while moving tracks from one output to another
2979 for (size_t i = 0; i < srcOutputs.size(); i++) {
2980 AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
2981 if (desc->isStrategyActive(strategy)) {
2982 setStrategyMute(strategy, true, srcOutputs[i]);
2983 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2984 }
2985 }
2986
2987 // Move effects associated to this strategy from previous output to new output
2988 if (strategy == STRATEGY_MEDIA) {
2989 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
2990 SortedVector<audio_io_handle_t> moved;
2991 for (size_t i = 0; i < mEffects.size(); i++) {
2992 EffectDescriptor *desc = mEffects.valueAt(i);
2993 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
2994 desc->mIo != fxOutput) {
2995 if (moved.indexOf(desc->mIo) < 0) {
2996 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
2997 mEffects.keyAt(i), fxOutput);
2998 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
2999 fxOutput);
3000 moved.add(desc->mIo);
3001 }
3002 desc->mIo = fxOutput;
3003 }
3004 }
3005 }
3006 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003007 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3008 if (getStrategy((audio_stream_type_t)i) == strategy) {
3009 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003010 }
3011 }
3012 }
3013}
3014
Eric Laurente0720872014-03-11 09:30:41 -07003015void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003016{
3017 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
3018 checkOutputForStrategy(STRATEGY_PHONE);
3019 checkOutputForStrategy(STRATEGY_SONIFICATION);
3020 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3021 checkOutputForStrategy(STRATEGY_MEDIA);
3022 checkOutputForStrategy(STRATEGY_DTMF);
3023}
3024
Eric Laurente0720872014-03-11 09:30:41 -07003025audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07003026{
Eric Laurente552edb2014-03-10 17:42:56 -07003027 for (size_t i = 0; i < mOutputs.size(); i++) {
3028 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
3029 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
3030 return mOutputs.keyAt(i);
3031 }
3032 }
3033
3034 return 0;
3035}
3036
Eric Laurente0720872014-03-11 09:30:41 -07003037void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003038{
Eric Laurente552edb2014-03-10 17:42:56 -07003039 audio_io_handle_t a2dpOutput = getA2dpOutput();
3040 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003041 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003042 return;
3043 }
3044
Eric Laurent3a4311c2014-03-17 12:00:47 -07003045 bool isScoConnected =
3046 (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0;
Eric Laurente552edb2014-03-10 17:42:56 -07003047 // suspend A2DP output if:
3048 // (NOT already suspended) &&
3049 // ((SCO device is connected &&
3050 // (forced usage for communication || for record is SCO))) ||
3051 // (phone state is ringing || in call)
3052 //
3053 // restore A2DP output if:
3054 // (Already suspended) &&
3055 // ((SCO device is NOT connected ||
3056 // (forced usage NOT for communication && NOT for record is SCO))) &&
3057 // (phone state is NOT ringing && NOT in call)
3058 //
3059 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003060 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003061 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
3062 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
3063 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
3064 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003065
3066 mpClientInterface->restoreOutput(a2dpOutput);
3067 mA2dpSuspended = false;
3068 }
3069 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003070 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003071 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
3072 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
3073 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
3074 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003075
3076 mpClientInterface->suspendOutput(a2dpOutput);
3077 mA2dpSuspended = true;
3078 }
3079 }
3080}
3081
Eric Laurent951f4552014-05-20 10:48:17 -07003082audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003083{
3084 audio_devices_t device = AUDIO_DEVICE_NONE;
3085
3086 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003087
3088 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3089 if (index >= 0) {
3090 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3091 if (patchDesc->mUid != mUidCached) {
3092 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3093 outputDesc->device(), outputDesc->mPatchHandle);
3094 return outputDesc->device();
3095 }
3096 }
3097
Eric Laurente552edb2014-03-10 17:42:56 -07003098 // check the following by order of priority to request a routing change if necessary:
3099 // 1: the strategy enforced audible is active on the output:
3100 // use device for strategy enforced audible
3101 // 2: we are in call or the strategy phone is active on the output:
3102 // use device for strategy phone
3103 // 3: the strategy sonification is active on the output:
3104 // use device for strategy sonification
3105 // 4: the strategy "respectful" sonification is active on the output:
3106 // use device for strategy "respectful" sonification
3107 // 5: the strategy media is active on the output:
3108 // use device for strategy media
3109 // 6: the strategy DTMF is active on the output:
3110 // use device for strategy DTMF
3111 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
3112 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3113 } else if (isInCall() ||
3114 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
3115 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
3116 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
3117 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
3118 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
3119 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
3120 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
3121 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
3122 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
3123 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
3124 }
3125
Eric Laurent951f4552014-05-20 10:48:17 -07003126 ALOGV("getNewOutputDevice() selected device %x", device);
3127 return device;
3128}
3129
3130audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3131{
3132 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003133
3134 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3135 if (index >= 0) {
3136 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3137 if (patchDesc->mUid != mUidCached) {
3138 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3139 inputDesc->mDevice, inputDesc->mPatchHandle);
3140 return inputDesc->mDevice;
3141 }
3142 }
3143
Eric Laurent951f4552014-05-20 10:48:17 -07003144 audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource);
3145
3146 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003147 return device;
3148}
3149
Eric Laurente0720872014-03-11 09:30:41 -07003150uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003151 return (uint32_t)getStrategy(stream);
3152}
3153
Eric Laurente0720872014-03-11 09:30:41 -07003154audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003155 // By checking the range of stream before calling getStrategy, we avoid
3156 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3157 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent3b73df72014-03-11 09:06:29 -07003158 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003159 return AUDIO_DEVICE_NONE;
3160 }
3161 audio_devices_t devices;
3162 AudioPolicyManager::routing_strategy strategy = getStrategy(stream);
3163 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3164 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3165 for (size_t i = 0; i < outputs.size(); i++) {
3166 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
3167 if (outputDesc->isStrategyActive(strategy)) {
3168 devices = outputDesc->device();
3169 break;
3170 }
Eric Laurente552edb2014-03-10 17:42:56 -07003171 }
3172 return devices;
3173}
3174
Eric Laurente0720872014-03-11 09:30:41 -07003175AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07003176 audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003177 // stream to strategy mapping
3178 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003179 case AUDIO_STREAM_VOICE_CALL:
3180 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003181 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07003182 case AUDIO_STREAM_RING:
3183 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07003184 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07003185 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07003186 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07003187 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07003188 return STRATEGY_DTMF;
3189 default:
3190 ALOGE("unknown stream type");
Eric Laurent3b73df72014-03-11 09:06:29 -07003191 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07003192 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
3193 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07003194 case AUDIO_STREAM_TTS:
3195 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003196 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07003197 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07003198 return STRATEGY_ENFORCED_AUDIBLE;
3199 }
3200}
3201
Eric Laurente0720872014-03-11 09:30:41 -07003202void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003203 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003204 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003205 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3206 updateDevicesAndOutputs();
3207 break;
3208 default:
3209 break;
3210 }
3211}
3212
Eric Laurente0720872014-03-11 09:30:41 -07003213audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07003214 bool fromCache)
3215{
3216 uint32_t device = AUDIO_DEVICE_NONE;
3217
3218 if (fromCache) {
3219 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3220 strategy, mDeviceForStrategy[strategy]);
3221 return mDeviceForStrategy[strategy];
3222 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003223 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07003224 switch (strategy) {
3225
3226 case STRATEGY_SONIFICATION_RESPECTFUL:
3227 if (isInCall()) {
3228 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003229 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07003230 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
3231 // while media is playing on a remote device, use the the sonification behavior.
3232 // Note that we test this usecase before testing if media is playing because
3233 // the isStreamActive() method only informs about the activity of a stream, not
3234 // if it's for local playback. Note also that we use the same delay between both tests
3235 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003236 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003237 // while media is playing (or has recently played), use the same device
3238 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3239 } else {
3240 // when media is not playing anymore, fall back on the sonification behavior
3241 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
3242 }
3243
3244 break;
3245
3246 case STRATEGY_DTMF:
3247 if (!isInCall()) {
3248 // when off call, DTMF strategy follows the same rules as MEDIA strategy
3249 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3250 break;
3251 }
3252 // when in call, DTMF and PHONE strategies follow the same rules
3253 // FALL THROUGH
3254
3255 case STRATEGY_PHONE:
3256 // for phone strategy, we first consider the forced use and then the available devices by order
3257 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07003258 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
3259 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003260 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003261 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003262 if (device) break;
3263 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003264 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003265 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003266 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07003267 if (device) break;
3268 // if SCO device is requested but no SCO device is available, fall back to default case
3269 // FALL THROUGH
3270
3271 default: // FORCE_NONE
3272 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07003273 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003274 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003275 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003276 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003277 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003278 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003279 if (device) break;
3280 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003281 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003282 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003283 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003284 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07003285 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003286 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003287 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003288 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003289 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003290 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003291 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003292 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003293 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003294 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003295 if (device) break;
3296 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003297 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07003298 if (device) break;
Eric Laurent951f4552014-05-20 10:48:17 -07003299 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003300 if (device == AUDIO_DEVICE_NONE) {
3301 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
3302 }
3303 break;
3304
Eric Laurent3b73df72014-03-11 09:06:29 -07003305 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07003306 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
3307 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07003308 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003309 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003310 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003311 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003312 if (device) break;
3313 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003314 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003315 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003316 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003317 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003318 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003319 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003320 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003321 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003322 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003323 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003324 if (device) break;
3325 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003326 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003327 if (device) break;
Eric Laurent951f4552014-05-20 10:48:17 -07003328 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003329 if (device == AUDIO_DEVICE_NONE) {
3330 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
3331 }
3332 break;
3333 }
3334 break;
3335
3336 case STRATEGY_SONIFICATION:
3337
3338 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
3339 // handleIncallSonification().
3340 if (isInCall()) {
3341 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
3342 break;
3343 }
3344 // FALL THROUGH
3345
3346 case STRATEGY_ENFORCED_AUDIBLE:
3347 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
3348 // except:
3349 // - when in call where it doesn't default to STRATEGY_PHONE behavior
3350 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
3351
3352 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003353 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003354 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003355 if (device == AUDIO_DEVICE_NONE) {
3356 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
3357 }
3358 }
3359 // The second device used for sonification is the same as the device used by media strategy
3360 // FALL THROUGH
3361
3362 case STRATEGY_MEDIA: {
3363 uint32_t device2 = AUDIO_DEVICE_NONE;
3364 if (strategy != STRATEGY_SONIFICATION) {
3365 // no sonification on remote submix (e.g. WFD)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003366 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurente552edb2014-03-10 17:42:56 -07003367 }
3368 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003369 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003370 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003371 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003372 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003373 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003374 }
3375 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003376 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003377 }
3378 }
3379 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003380 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003381 }
3382 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003383 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003384 }
3385 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003386 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003387 }
3388 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003389 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003390 }
3391 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003392 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003393 }
3394 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
3395 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003396 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003397 }
3398 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003399 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003400 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003401 }
3402 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003403 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003404 }
3405
3406 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
3407 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
3408 device |= device2;
3409 if (device) break;
Eric Laurent951f4552014-05-20 10:48:17 -07003410 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003411 if (device == AUDIO_DEVICE_NONE) {
3412 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
3413 }
3414 } break;
3415
3416 default:
3417 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
3418 break;
3419 }
3420
3421 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
3422 return device;
3423}
3424
Eric Laurente0720872014-03-11 09:30:41 -07003425void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003426{
3427 for (int i = 0; i < NUM_STRATEGIES; i++) {
3428 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3429 }
3430 mPreviousOutputs = mOutputs;
3431}
3432
Eric Laurente0720872014-03-11 09:30:41 -07003433uint32_t AudioPolicyManager::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003434 audio_devices_t prevDevice,
3435 uint32_t delayMs)
3436{
3437 // mute/unmute strategies using an incompatible device combination
3438 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3439 // if unmuting, unmute only after the specified delay
3440 if (outputDesc->isDuplicated()) {
3441 return 0;
3442 }
3443
3444 uint32_t muteWaitMs = 0;
3445 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003446 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003447
3448 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3449 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3450 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3451 bool doMute = false;
3452
3453 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3454 doMute = true;
3455 outputDesc->mStrategyMutedByDevice[i] = true;
3456 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3457 doMute = true;
3458 outputDesc->mStrategyMutedByDevice[i] = false;
3459 }
Eric Laurent99401132014-05-07 19:48:15 -07003460 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003461 for (size_t j = 0; j < mOutputs.size(); j++) {
3462 AudioOutputDescriptor *desc = mOutputs.valueAt(j);
3463 // skip output if it does not share any device with current output
3464 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3465 == AUDIO_DEVICE_NONE) {
3466 continue;
3467 }
3468 audio_io_handle_t curOutput = mOutputs.keyAt(j);
3469 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
3470 mute ? "muting" : "unmuting", i, curDevice, curOutput);
3471 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
3472 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003473 if (mute) {
3474 // FIXME: should not need to double latency if volume could be applied
3475 // immediately by the audioflinger mixer. We must account for the delay
3476 // between now and the next time the audioflinger thread for this output
3477 // will process a buffer (which corresponds to one buffer size,
3478 // usually 1/2 or 1/4 of the latency).
3479 if (muteWaitMs < desc->latency() * 2) {
3480 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003481 }
3482 }
3483 }
3484 }
3485 }
3486 }
3487
Eric Laurent99401132014-05-07 19:48:15 -07003488 // temporary mute output if device selection changes to avoid volume bursts due to
3489 // different per device volumes
3490 if (outputDesc->isActive() && (device != prevDevice)) {
3491 if (muteWaitMs < outputDesc->latency() * 2) {
3492 muteWaitMs = outputDesc->latency() * 2;
3493 }
3494 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3495 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent951f4552014-05-20 10:48:17 -07003496 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07003497 // do tempMute unmute after twice the mute wait time
Eric Laurent951f4552014-05-20 10:48:17 -07003498 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07003499 muteWaitMs *2, device);
3500 }
3501 }
3502 }
3503
Eric Laurente552edb2014-03-10 17:42:56 -07003504 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3505 if (muteWaitMs > delayMs) {
3506 muteWaitMs -= delayMs;
3507 usleep(muteWaitMs * 1000);
3508 return muteWaitMs;
3509 }
3510 return 0;
3511}
3512
Eric Laurente0720872014-03-11 09:30:41 -07003513uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07003514 audio_devices_t device,
3515 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003516 int delayMs,
3517 audio_patch_handle_t *patchHandle)
Eric Laurente552edb2014-03-10 17:42:56 -07003518{
3519 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
3520 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3521 AudioParameter param;
3522 uint32_t muteWaitMs;
3523
3524 if (outputDesc->isDuplicated()) {
Eric Laurent951f4552014-05-20 10:48:17 -07003525 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
3526 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003527 return muteWaitMs;
3528 }
3529 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
3530 // output profile
3531 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003532 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003533 return 0;
3534 }
3535
3536 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07003537 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07003538
3539 audio_devices_t prevDevice = outputDesc->mDevice;
3540
3541 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
3542
3543 if (device != AUDIO_DEVICE_NONE) {
3544 outputDesc->mDevice = device;
3545 }
3546 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
3547
3548 // Do not change the routing if:
3549 // - the requested device is AUDIO_DEVICE_NONE
3550 // - the requested device is the same as current device and force is not specified.
3551 // Doing this check here allows the caller to call setOutputDevice() without conditions
3552 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
3553 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
3554 return muteWaitMs;
3555 }
3556
3557 ALOGV("setOutputDevice() changing device");
Eric Laurent951f4552014-05-20 10:48:17 -07003558
Eric Laurente552edb2014-03-10 17:42:56 -07003559 // do the routing
Eric Laurent951f4552014-05-20 10:48:17 -07003560 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003561 resetOutputDevice(output, delayMs, NULL);
Eric Laurent951f4552014-05-20 10:48:17 -07003562 } else {
3563 DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device);
3564 if (!deviceList.isEmpty()) {
3565 struct audio_patch patch;
3566 outputDesc->toAudioPortConfig(&patch.sources[0]);
3567 patch.num_sources = 1;
3568 patch.num_sinks = 0;
3569 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
3570 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent951f4552014-05-20 10:48:17 -07003571 patch.num_sinks++;
3572 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003573 ssize_t index;
3574 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3575 index = mAudioPatches.indexOfKey(*patchHandle);
3576 } else {
3577 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3578 }
3579 sp< AudioPatch> patchDesc;
3580 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3581 if (index >= 0) {
3582 patchDesc = mAudioPatches.valueAt(index);
3583 afPatchHandle = patchDesc->mAfPatchHandle;
3584 }
3585
Eric Laurent951f4552014-05-20 10:48:17 -07003586 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003587 &afPatchHandle,
3588 delayMs);
Eric Laurent951f4552014-05-20 10:48:17 -07003589 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
3590 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003591 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent951f4552014-05-20 10:48:17 -07003592 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003593 if (index < 0) {
3594 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3595 &patch, mUidCached);
3596 addAudioPatch(patchDesc->mHandle, patchDesc);
3597 } else {
3598 patchDesc->mPatch = patch;
3599 }
3600 patchDesc->mAfPatchHandle = afPatchHandle;
3601 patchDesc->mUid = mUidCached;
3602 if (patchHandle) {
3603 *patchHandle = patchDesc->mHandle;
3604 }
3605 outputDesc->mPatchHandle = patchDesc->mHandle;
3606 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003607 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -07003608 }
3609 }
3610 }
Eric Laurente552edb2014-03-10 17:42:56 -07003611
3612 // update stream volumes according to new device
3613 applyStreamVolumes(output, device, delayMs);
3614
3615 return muteWaitMs;
3616}
3617
Eric Laurent951f4552014-05-20 10:48:17 -07003618status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07003619 int delayMs,
3620 audio_patch_handle_t *patchHandle)
Eric Laurent951f4552014-05-20 10:48:17 -07003621{
3622 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003623 ssize_t index;
3624 if (patchHandle) {
3625 index = mAudioPatches.indexOfKey(*patchHandle);
3626 } else {
3627 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3628 }
3629 if (index < 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07003630 return INVALID_OPERATION;
3631 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003632 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3633 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent951f4552014-05-20 10:48:17 -07003634 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
3635 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003636 removeAudioPatch(patchDesc->mHandle);
3637 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003638 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -07003639 return status;
3640}
3641
3642status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
3643 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07003644 bool force,
3645 audio_patch_handle_t *patchHandle)
Eric Laurent951f4552014-05-20 10:48:17 -07003646{
3647 status_t status = NO_ERROR;
3648
3649 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
3650 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
3651 inputDesc->mDevice = device;
3652
3653 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
3654 if (!deviceList.isEmpty()) {
3655 struct audio_patch patch;
3656 inputDesc->toAudioPortConfig(&patch.sinks[0]);
3657 patch.num_sinks = 1;
3658 //only one input device for now
3659 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent951f4552014-05-20 10:48:17 -07003660 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07003661 ssize_t index;
3662 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3663 index = mAudioPatches.indexOfKey(*patchHandle);
3664 } else {
3665 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3666 }
3667 sp< AudioPatch> patchDesc;
3668 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3669 if (index >= 0) {
3670 patchDesc = mAudioPatches.valueAt(index);
3671 afPatchHandle = patchDesc->mAfPatchHandle;
3672 }
3673
Eric Laurent951f4552014-05-20 10:48:17 -07003674 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003675 &afPatchHandle,
Eric Laurent951f4552014-05-20 10:48:17 -07003676 0);
3677 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003678 status, afPatchHandle);
Eric Laurent951f4552014-05-20 10:48:17 -07003679 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003680 if (index < 0) {
3681 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3682 &patch, mUidCached);
3683 addAudioPatch(patchDesc->mHandle, patchDesc);
3684 } else {
3685 patchDesc->mPatch = patch;
3686 }
3687 patchDesc->mAfPatchHandle = afPatchHandle;
3688 patchDesc->mUid = mUidCached;
3689 if (patchHandle) {
3690 *patchHandle = patchDesc->mHandle;
3691 }
3692 inputDesc->mPatchHandle = patchDesc->mHandle;
3693 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003694 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -07003695 }
3696 }
3697 }
3698 return status;
3699}
3700
Eric Laurent6a94d692014-05-20 11:18:06 -07003701status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
3702 audio_patch_handle_t *patchHandle)
Eric Laurent951f4552014-05-20 10:48:17 -07003703{
3704 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003705 ssize_t index;
3706 if (patchHandle) {
3707 index = mAudioPatches.indexOfKey(*patchHandle);
3708 } else {
3709 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3710 }
3711 if (index < 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07003712 return INVALID_OPERATION;
3713 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003714 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3715 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent951f4552014-05-20 10:48:17 -07003716 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
3717 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003718 removeAudioPatch(patchDesc->mHandle);
3719 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003720 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -07003721 return status;
3722}
3723
3724sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07003725 uint32_t samplingRate,
3726 audio_format_t format,
3727 audio_channel_mask_t channelMask)
3728{
3729 // Choose an input profile based on the requested capture parameters: select the first available
3730 // profile supporting all requested parameters.
3731
3732 for (size_t i = 0; i < mHwModules.size(); i++)
3733 {
3734 if (mHwModules[i]->mHandle == 0) {
3735 continue;
3736 }
3737 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3738 {
Eric Laurent951f4552014-05-20 10:48:17 -07003739 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003740 // profile->log();
Eric Laurente552edb2014-03-10 17:42:56 -07003741 if (profile->isCompatibleProfile(device, samplingRate, format,
3742 channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
3743 return profile;
3744 }
3745 }
3746 }
3747 return NULL;
3748}
3749
Eric Laurente0720872014-03-11 09:30:41 -07003750audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
Eric Laurente552edb2014-03-10 17:42:56 -07003751{
3752 uint32_t device = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003753 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
3754 ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003755 switch (inputSource) {
3756 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003757 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003758 device = AUDIO_DEVICE_IN_VOICE_CALL;
3759 break;
3760 }
3761 // FALL THROUGH
3762
3763 case AUDIO_SOURCE_DEFAULT:
3764 case AUDIO_SOURCE_MIC:
3765 case AUDIO_SOURCE_VOICE_RECOGNITION:
3766 case AUDIO_SOURCE_HOTWORD:
3767 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurent3b73df72014-03-11 09:06:29 -07003768 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003769 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003770 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003771 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003772 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07003773 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
3774 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003775 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003776 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3777 }
3778 break;
3779 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003780 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003781 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003782 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003783 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3784 }
3785 break;
3786 case AUDIO_SOURCE_VOICE_DOWNLINK:
3787 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003788 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003789 device = AUDIO_DEVICE_IN_VOICE_CALL;
3790 }
3791 break;
3792 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003793 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07003794 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
3795 }
3796 break;
3797 default:
3798 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
3799 break;
3800 }
3801 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
3802 return device;
3803}
3804
Eric Laurente0720872014-03-11 09:30:41 -07003805bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003806{
3807 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
3808 device &= ~AUDIO_DEVICE_BIT_IN;
3809 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
3810 return true;
3811 }
3812 return false;
3813}
3814
Eric Laurente0720872014-03-11 09:30:41 -07003815audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003816{
3817 for (size_t i = 0; i < mInputs.size(); i++) {
3818 const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
3819 if ((input_descriptor->mRefCount > 0)
3820 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
3821 return mInputs.keyAt(i);
3822 }
3823 }
3824 return 0;
3825}
3826
3827
Eric Laurente0720872014-03-11 09:30:41 -07003828audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003829{
3830 if (device == AUDIO_DEVICE_NONE) {
3831 // this happens when forcing a route update and no track is active on an output.
3832 // In this case the returned category is not important.
3833 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent3b73df72014-03-11 09:06:29 -07003834 } else if (popcount(device) > 1) {
Eric Laurente552edb2014-03-10 17:42:56 -07003835 // Multiple device selection is either:
3836 // - speaker + one other device: give priority to speaker in this case.
3837 // - one A2DP device + another device: happens with duplicated output. In this case
3838 // retain the device on the A2DP output as the other must not correspond to an active
3839 // selection if not the speaker.
3840 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
3841 device = AUDIO_DEVICE_OUT_SPEAKER;
3842 } else {
3843 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
3844 }
3845 }
3846
Eric Laurent3b73df72014-03-11 09:06:29 -07003847 ALOGW_IF(popcount(device) != 1,
Eric Laurente552edb2014-03-10 17:42:56 -07003848 "getDeviceForVolume() invalid device combination: %08x",
3849 device);
3850
3851 return device;
3852}
3853
Eric Laurente0720872014-03-11 09:30:41 -07003854AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003855{
3856 switch(getDeviceForVolume(device)) {
3857 case AUDIO_DEVICE_OUT_EARPIECE:
3858 return DEVICE_CATEGORY_EARPIECE;
3859 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3860 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3861 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3862 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3863 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3864 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3865 return DEVICE_CATEGORY_HEADSET;
3866 case AUDIO_DEVICE_OUT_SPEAKER:
3867 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3868 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3869 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3870 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3871 case AUDIO_DEVICE_OUT_USB_DEVICE:
3872 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3873 default:
3874 return DEVICE_CATEGORY_SPEAKER;
3875 }
3876}
3877
Eric Laurente0720872014-03-11 09:30:41 -07003878float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003879 int indexInUi)
3880{
3881 device_category deviceCategory = getDeviceCategory(device);
3882 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3883
3884 // the volume index in the UI is relative to the min and max volume indices for this stream type
3885 int nbSteps = 1 + curve[VOLMAX].mIndex -
3886 curve[VOLMIN].mIndex;
3887 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3888 (streamDesc.mIndexMax - streamDesc.mIndexMin);
3889
3890 // find what part of the curve this index volume belongs to, or if it's out of bounds
3891 int segment = 0;
3892 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
3893 return 0.0f;
3894 } else if (volIdx < curve[VOLKNEE1].mIndex) {
3895 segment = 0;
3896 } else if (volIdx < curve[VOLKNEE2].mIndex) {
3897 segment = 1;
3898 } else if (volIdx <= curve[VOLMAX].mIndex) {
3899 segment = 2;
3900 } else { // out of bounds
3901 return 1.0f;
3902 }
3903
3904 // linear interpolation in the attenuation table in dB
3905 float decibels = curve[segment].mDBAttenuation +
3906 ((float)(volIdx - curve[segment].mIndex)) *
3907 ( (curve[segment+1].mDBAttenuation -
3908 curve[segment].mDBAttenuation) /
3909 ((float)(curve[segment+1].mIndex -
3910 curve[segment].mIndex)) );
3911
3912 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3913
3914 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3915 curve[segment].mIndex, volIdx,
3916 curve[segment+1].mIndex,
3917 curve[segment].mDBAttenuation,
3918 decibels,
3919 curve[segment+1].mDBAttenuation,
3920 amplification);
3921
3922 return amplification;
3923}
3924
Eric Laurente0720872014-03-11 09:30:41 -07003925const AudioPolicyManager::VolumeCurvePoint
3926 AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003927 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3928};
3929
Eric Laurente0720872014-03-11 09:30:41 -07003930const AudioPolicyManager::VolumeCurvePoint
3931 AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003932 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3933};
3934
Eric Laurente0720872014-03-11 09:30:41 -07003935const AudioPolicyManager::VolumeCurvePoint
3936 AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003937 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3938};
3939
Eric Laurente0720872014-03-11 09:30:41 -07003940const AudioPolicyManager::VolumeCurvePoint
3941 AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003942 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3943};
3944
Eric Laurente0720872014-03-11 09:30:41 -07003945const AudioPolicyManager::VolumeCurvePoint
3946 AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003947 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
3948};
3949
3950// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
3951// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
3952// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
3953// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
3954
Eric Laurente0720872014-03-11 09:30:41 -07003955const AudioPolicyManager::VolumeCurvePoint
3956 AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003957 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
3958};
3959
Eric Laurente0720872014-03-11 09:30:41 -07003960const AudioPolicyManager::VolumeCurvePoint
3961 AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003962 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
3963};
3964
Eric Laurente0720872014-03-11 09:30:41 -07003965const AudioPolicyManager::VolumeCurvePoint
3966 AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003967 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
3968};
3969
Eric Laurente0720872014-03-11 09:30:41 -07003970const AudioPolicyManager::VolumeCurvePoint
3971 AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003972 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
3973};
3974
Eric Laurente0720872014-03-11 09:30:41 -07003975const AudioPolicyManager::VolumeCurvePoint
3976 AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003977 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
3978};
3979
Eric Laurente0720872014-03-11 09:30:41 -07003980const AudioPolicyManager::VolumeCurvePoint
3981 *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT]
3982 [AudioPolicyManager::DEVICE_CATEGORY_CNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003983 { // AUDIO_STREAM_VOICE_CALL
3984 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
3985 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3986 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
3987 },
3988 { // AUDIO_STREAM_SYSTEM
3989 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3990 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3991 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
3992 },
3993 { // AUDIO_STREAM_RING
3994 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3995 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3996 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
3997 },
3998 { // AUDIO_STREAM_MUSIC
3999 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4000 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4001 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4002 },
4003 { // AUDIO_STREAM_ALARM
4004 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4005 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4006 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4007 },
4008 { // AUDIO_STREAM_NOTIFICATION
4009 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4010 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4011 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4012 },
4013 { // AUDIO_STREAM_BLUETOOTH_SCO
4014 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
4015 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4016 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
4017 },
4018 { // AUDIO_STREAM_ENFORCED_AUDIBLE
4019 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4020 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4021 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4022 },
4023 { // AUDIO_STREAM_DTMF
4024 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4025 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4026 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4027 },
4028 { // AUDIO_STREAM_TTS
4029 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4030 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4031 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4032 },
4033};
4034
Eric Laurente0720872014-03-11 09:30:41 -07004035void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07004036{
4037 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
4038 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
4039 mStreams[i].mVolumeCurve[j] =
4040 sVolumeProfiles[i][j];
4041 }
4042 }
4043
4044 // Check availability of DRC on speaker path: if available, override some of the speaker curves
4045 if (mSpeakerDrcEnabled) {
4046 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4047 sDefaultSystemVolumeCurveDrc;
4048 mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4049 sSpeakerSonificationVolumeCurveDrc;
4050 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4051 sSpeakerSonificationVolumeCurveDrc;
4052 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4053 sSpeakerSonificationVolumeCurveDrc;
4054 }
4055}
4056
Eric Laurente0720872014-03-11 09:30:41 -07004057float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004058 int index,
4059 audio_io_handle_t output,
4060 audio_devices_t device)
4061{
4062 float volume = 1.0;
4063 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
4064 StreamDescriptor &streamDesc = mStreams[stream];
4065
4066 if (device == AUDIO_DEVICE_NONE) {
4067 device = outputDesc->device();
4068 }
4069
4070 // if volume is not 0 (not muted), force media volume to max on digital output
Eric Laurent3b73df72014-03-11 09:06:29 -07004071 if (stream == AUDIO_STREAM_MUSIC &&
Eric Laurente552edb2014-03-10 17:42:56 -07004072 index != mStreams[stream].mIndexMin &&
4073 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
4074 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
4075 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
4076 device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
4077 return 1.0;
4078 }
4079
4080 volume = volIndexToAmpl(device, streamDesc, index);
4081
4082 // if a headset is connected, apply the following rules to ring tones and notifications
4083 // to avoid sound level bursts in user's ears:
4084 // - always attenuate ring tones and notifications volume by 6dB
4085 // - if music is playing, always limit the volume to current music volume,
4086 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004087 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004088 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4089 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4090 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4091 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4092 ((stream_strategy == STRATEGY_SONIFICATION)
4093 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004094 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004095 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004096 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07004097 streamDesc.mCanBeMuted) {
4098 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4099 // when the phone is ringing we must consider that music could have been paused just before
4100 // by the music application and behave as if music was active if the last music track was
4101 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004102 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004103 mLimitRingtoneVolume) {
4104 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004105 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
4106 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004107 output,
4108 musicDevice);
4109 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4110 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4111 if (volume > minVol) {
4112 volume = minVol;
4113 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4114 }
4115 }
4116 }
4117
4118 return volume;
4119}
4120
Eric Laurente0720872014-03-11 09:30:41 -07004121status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004122 int index,
4123 audio_io_handle_t output,
4124 audio_devices_t device,
4125 int delayMs,
4126 bool force)
4127{
4128
4129 // do not change actual stream volume if the stream is muted
4130 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
4131 ALOGVV("checkAndSetVolume() stream %d muted count %d",
4132 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
4133 return NO_ERROR;
4134 }
4135
4136 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07004137 if ((stream == AUDIO_STREAM_VOICE_CALL &&
4138 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4139 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
4140 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004141 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07004142 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07004143 return INVALID_OPERATION;
4144 }
4145
4146 float volume = computeVolume(stream, index, output, device);
4147 // We actually change the volume if:
4148 // - the float value returned by computeVolume() changed
4149 // - the force flag is set
4150 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
4151 force) {
4152 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
4153 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
4154 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
4155 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07004156 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
4157 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004158 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004159 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004160 }
4161
Eric Laurent3b73df72014-03-11 09:06:29 -07004162 if (stream == AUDIO_STREAM_VOICE_CALL ||
4163 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004164 float voiceVolume;
4165 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004166 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004167 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
4168 } else {
4169 voiceVolume = 1.0;
4170 }
4171
4172 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
4173 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4174 mLastVoiceVolume = voiceVolume;
4175 }
4176 }
4177
4178 return NO_ERROR;
4179}
4180
Eric Laurente0720872014-03-11 09:30:41 -07004181void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004182 audio_devices_t device,
4183 int delayMs,
4184 bool force)
4185{
4186 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
4187
Eric Laurent3b73df72014-03-11 09:06:29 -07004188 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4189 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004190 mStreams[stream].getVolumeIndex(device),
4191 output,
4192 device,
4193 delayMs,
4194 force);
4195 }
4196}
4197
Eric Laurente0720872014-03-11 09:30:41 -07004198void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004199 bool on,
4200 audio_io_handle_t output,
4201 int delayMs,
4202 audio_devices_t device)
4203{
4204 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004205 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4206 if (getStrategy((audio_stream_type_t)stream) == strategy) {
4207 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004208 }
4209 }
4210}
4211
Eric Laurente0720872014-03-11 09:30:41 -07004212void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004213 bool on,
4214 audio_io_handle_t output,
4215 int delayMs,
4216 audio_devices_t device)
4217{
4218 StreamDescriptor &streamDesc = mStreams[stream];
4219 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
4220 if (device == AUDIO_DEVICE_NONE) {
4221 device = outputDesc->device();
4222 }
4223
4224 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
4225 stream, on, output, outputDesc->mMuteCount[stream], device);
4226
4227 if (on) {
4228 if (outputDesc->mMuteCount[stream] == 0) {
4229 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004230 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
4231 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004232 checkAndSetVolume(stream, 0, output, device, delayMs);
4233 }
4234 }
4235 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4236 outputDesc->mMuteCount[stream]++;
4237 } else {
4238 if (outputDesc->mMuteCount[stream] == 0) {
4239 ALOGV("setStreamMute() unmuting non muted stream!");
4240 return;
4241 }
4242 if (--outputDesc->mMuteCount[stream] == 0) {
4243 checkAndSetVolume(stream,
4244 streamDesc.getVolumeIndex(device),
4245 output,
4246 device,
4247 delayMs);
4248 }
4249 }
4250}
4251
Eric Laurente0720872014-03-11 09:30:41 -07004252void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004253 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004254{
4255 // if the stream pertains to sonification strategy and we are in call we must
4256 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4257 // in the device used for phone strategy and play the tone if the selected device does not
4258 // interfere with the device used for phone strategy
4259 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4260 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004261 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004262 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4263 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
4264 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
4265 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4266 stream, starting, outputDesc->mDevice, stateChange);
4267 if (outputDesc->mRefCount[stream]) {
4268 int muteCount = 1;
4269 if (stateChange) {
4270 muteCount = outputDesc->mRefCount[stream];
4271 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004272 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004273 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4274 for (int i = 0; i < muteCount; i++) {
4275 setStreamMute(stream, starting, mPrimaryOutput);
4276 }
4277 } else {
4278 ALOGV("handleIncallSonification() high visibility");
4279 if (outputDesc->device() &
4280 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4281 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4282 for (int i = 0; i < muteCount; i++) {
4283 setStreamMute(stream, starting, mPrimaryOutput);
4284 }
4285 }
4286 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004287 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4288 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004289 } else {
4290 mpClientInterface->stopTone();
4291 }
4292 }
4293 }
4294 }
4295}
4296
Eric Laurente0720872014-03-11 09:30:41 -07004297bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07004298{
4299 return isStateInCall(mPhoneState);
4300}
4301
Eric Laurente0720872014-03-11 09:30:41 -07004302bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004303 return ((state == AUDIO_MODE_IN_CALL) ||
4304 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07004305}
4306
Eric Laurente0720872014-03-11 09:30:41 -07004307uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07004308{
4309 return MAX_EFFECTS_CPU_LOAD;
4310}
4311
Eric Laurente0720872014-03-11 09:30:41 -07004312uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07004313{
4314 return MAX_EFFECTS_MEMORY;
4315}
4316
Eric Laurent6a94d692014-05-20 11:18:06 -07004317
Eric Laurente552edb2014-03-10 17:42:56 -07004318// --- AudioOutputDescriptor class implementation
4319
Eric Laurente0720872014-03-11 09:30:41 -07004320AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor(
Eric Laurent951f4552014-05-20 10:48:17 -07004321 const sp<IOProfile>& profile)
4322 : mId(0), mIoHandle(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT),
Eric Laurente552edb2014-03-10 17:42:56 -07004323 mChannelMask(0), mLatency(0),
Eric Laurent951f4552014-05-20 10:48:17 -07004324 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0),
Eric Laurente552edb2014-03-10 17:42:56 -07004325 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
4326{
4327 // clear usage count for all stream types
Eric Laurent3b73df72014-03-11 09:06:29 -07004328 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004329 mRefCount[i] = 0;
4330 mCurVolume[i] = -1.0;
4331 mMuteCount[i] = 0;
4332 mStopTime[i] = 0;
4333 }
4334 for (int i = 0; i < NUM_STRATEGIES; i++) {
4335 mStrategyMutedByDevice[i] = false;
4336 }
4337 if (profile != NULL) {
4338 mSamplingRate = profile->mSamplingRates[0];
4339 mFormat = profile->mFormats[0];
4340 mChannelMask = profile->mChannelMasks[0];
4341 mFlags = profile->mFlags;
4342 }
4343}
4344
Eric Laurente0720872014-03-11 09:30:41 -07004345audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const
Eric Laurente552edb2014-03-10 17:42:56 -07004346{
4347 if (isDuplicated()) {
4348 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
4349 } else {
4350 return mDevice;
4351 }
4352}
4353
Eric Laurente0720872014-03-11 09:30:41 -07004354uint32_t AudioPolicyManager::AudioOutputDescriptor::latency()
Eric Laurente552edb2014-03-10 17:42:56 -07004355{
4356 if (isDuplicated()) {
4357 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
4358 } else {
4359 return mLatency;
4360 }
4361}
4362
Eric Laurente0720872014-03-11 09:30:41 -07004363bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith(
Eric Laurente552edb2014-03-10 17:42:56 -07004364 const AudioOutputDescriptor *outputDesc)
4365{
4366 if (isDuplicated()) {
4367 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
4368 } else if (outputDesc->isDuplicated()){
4369 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
4370 } else {
4371 return (mProfile->mModule == outputDesc->mProfile->mModule);
4372 }
4373}
4374
Eric Laurente0720872014-03-11 09:30:41 -07004375void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004376 int delta)
Eric Laurente552edb2014-03-10 17:42:56 -07004377{
4378 // forward usage count change to attached outputs
4379 if (isDuplicated()) {
4380 mOutput1->changeRefCount(stream, delta);
4381 mOutput2->changeRefCount(stream, delta);
4382 }
4383 if ((delta + (int)mRefCount[stream]) < 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004384 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d",
4385 delta, stream, mRefCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004386 mRefCount[stream] = 0;
4387 return;
4388 }
4389 mRefCount[stream] += delta;
4390 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
4391}
4392
Eric Laurente0720872014-03-11 09:30:41 -07004393audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices()
Eric Laurente552edb2014-03-10 17:42:56 -07004394{
4395 if (isDuplicated()) {
4396 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
4397 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004398 return mProfile->mSupportedDevices.types() ;
Eric Laurente552edb2014-03-10 17:42:56 -07004399 }
4400}
4401
Eric Laurente0720872014-03-11 09:30:41 -07004402bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07004403{
4404 return isStrategyActive(NUM_STRATEGIES, inPastMs);
4405}
4406
Eric Laurente0720872014-03-11 09:30:41 -07004407bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004408 uint32_t inPastMs,
4409 nsecs_t sysTime) const
4410{
4411 if ((sysTime == 0) && (inPastMs != 0)) {
4412 sysTime = systemTime();
4413 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004414 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4415 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004416 (NUM_STRATEGIES == strategy)) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004417 isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004418 return true;
4419 }
4420 }
4421 return false;
4422}
4423
Eric Laurente0720872014-03-11 09:30:41 -07004424bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004425 uint32_t inPastMs,
4426 nsecs_t sysTime) const
4427{
4428 if (mRefCount[stream] != 0) {
4429 return true;
4430 }
4431 if (inPastMs == 0) {
4432 return false;
4433 }
4434 if (sysTime == 0) {
4435 sysTime = systemTime();
4436 }
4437 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
4438 return true;
4439 }
4440 return false;
4441}
4442
Eric Laurent951f4552014-05-20 10:48:17 -07004443void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004444 struct audio_port_config *dstConfig,
4445 const struct audio_port_config *srcConfig) const
Eric Laurent951f4552014-05-20 10:48:17 -07004446{
Eric Laurent6a94d692014-05-20 11:18:06 -07004447 dstConfig->id = mId;
4448 dstConfig->role = AUDIO_PORT_ROLE_SOURCE;
4449 dstConfig->type = AUDIO_PORT_TYPE_MIX;
4450 dstConfig->sample_rate = mSamplingRate;
4451 dstConfig->channel_mask = mChannelMask;
4452 dstConfig->format = mFormat;
4453 dstConfig->gain.index = -1;
4454 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
Eric Laurent951f4552014-05-20 10:48:17 -07004455 AUDIO_PORT_CONFIG_FORMAT;
Eric Laurent6a94d692014-05-20 11:18:06 -07004456 // use supplied variable configuration parameters if any
4457 if (srcConfig != NULL) {
4458 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
4459 dstConfig->sample_rate = srcConfig->sample_rate;
4460 }
4461 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
4462 dstConfig->channel_mask = srcConfig->channel_mask;
4463 }
4464 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
4465 dstConfig->format = srcConfig->format;
4466 }
4467 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
4468 dstConfig->gain = srcConfig->gain;
4469 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
4470 }
4471 }
4472 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
4473 dstConfig->ext.mix.handle = mIoHandle;
4474 dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
Eric Laurent951f4552014-05-20 10:48:17 -07004475}
4476
4477void AudioPolicyManager::AudioOutputDescriptor::toAudioPort(
4478 struct audio_port *port) const
4479{
4480 mProfile->toAudioPort(port);
4481 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004482 toAudioPortConfig(&port->active_config);
4483 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent951f4552014-05-20 10:48:17 -07004484 port->ext.mix.handle = mIoHandle;
4485 port->ext.mix.latency_class =
4486 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL;
4487}
Eric Laurente552edb2014-03-10 17:42:56 -07004488
Eric Laurente0720872014-03-11 09:30:41 -07004489status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004490{
4491 const size_t SIZE = 256;
4492 char buffer[SIZE];
4493 String8 result;
4494
4495 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4496 result.append(buffer);
4497 snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
4498 result.append(buffer);
4499 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4500 result.append(buffer);
4501 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
4502 result.append(buffer);
4503 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
4504 result.append(buffer);
4505 snprintf(buffer, SIZE, " Devices %08x\n", device());
4506 result.append(buffer);
4507 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
4508 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07004509 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4510 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n",
4511 i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07004512 result.append(buffer);
4513 }
4514 write(fd, result.string(), result.size());
4515
4516 return NO_ERROR;
4517}
4518
4519// --- AudioInputDescriptor class implementation
4520
Eric Laurent951f4552014-05-20 10:48:17 -07004521AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
4522 : mId(0), mIoHandle(0), mSamplingRate(0),
4523 mFormat(AUDIO_FORMAT_DEFAULT), mChannelMask(0),
4524 mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0),
Eric Laurent3b73df72014-03-11 09:06:29 -07004525 mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile)
Eric Laurente552edb2014-03-10 17:42:56 -07004526{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004527 if (profile != NULL) {
4528 mSamplingRate = profile->mSamplingRates[0];
4529 mFormat = profile->mFormats[0];
4530 mChannelMask = profile->mChannelMasks[0];
4531 }
Eric Laurente552edb2014-03-10 17:42:56 -07004532}
4533
Eric Laurent951f4552014-05-20 10:48:17 -07004534void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004535 struct audio_port_config *dstConfig,
4536 const struct audio_port_config *srcConfig) const
Eric Laurent951f4552014-05-20 10:48:17 -07004537{
Eric Laurent6a94d692014-05-20 11:18:06 -07004538 dstConfig->id = mId;
4539 dstConfig->role = AUDIO_PORT_ROLE_SINK;
4540 dstConfig->type = AUDIO_PORT_TYPE_MIX;
4541 dstConfig->sample_rate = mSamplingRate;
4542 dstConfig->channel_mask = mChannelMask;
4543 dstConfig->format = mFormat;
4544 dstConfig->gain.index = -1;
4545 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
4546 AUDIO_PORT_CONFIG_FORMAT;
4547 // use supplied variable configuration parameters if any
4548 if (srcConfig != NULL) {
4549 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
4550 dstConfig->sample_rate = srcConfig->sample_rate;
4551 }
4552 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
4553 dstConfig->channel_mask = srcConfig->channel_mask;
4554 }
4555 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
4556 dstConfig->format = srcConfig->format;
4557 }
4558 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
4559 dstConfig->gain = srcConfig->gain;
4560 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
4561 }
4562 }
Eric Laurent951f4552014-05-20 10:48:17 -07004563}
4564
4565void AudioPolicyManager::AudioInputDescriptor::toAudioPort(
4566 struct audio_port *port) const
4567{
4568 mProfile->toAudioPort(port);
4569 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004570 toAudioPortConfig(&port->active_config);
4571 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent951f4552014-05-20 10:48:17 -07004572 port->ext.mix.handle = mIoHandle;
4573 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
4574}
4575
Eric Laurente0720872014-03-11 09:30:41 -07004576status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004577{
4578 const size_t SIZE = 256;
4579 char buffer[SIZE];
4580 String8 result;
4581
4582 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4583 result.append(buffer);
4584 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
4585 result.append(buffer);
4586 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4587 result.append(buffer);
4588 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
4589 result.append(buffer);
4590 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
4591 result.append(buffer);
4592 write(fd, result.string(), result.size());
4593
4594 return NO_ERROR;
4595}
4596
4597// --- StreamDescriptor class implementation
4598
Eric Laurente0720872014-03-11 09:30:41 -07004599AudioPolicyManager::StreamDescriptor::StreamDescriptor()
Eric Laurente552edb2014-03-10 17:42:56 -07004600 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
4601{
4602 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
4603}
4604
Eric Laurente0720872014-03-11 09:30:41 -07004605int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004606{
Eric Laurente0720872014-03-11 09:30:41 -07004607 device = AudioPolicyManager::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07004608 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
4609 if (mIndexCur.indexOfKey(device) < 0) {
4610 device = AUDIO_DEVICE_OUT_DEFAULT;
4611 }
4612 return mIndexCur.valueFor(device);
4613}
4614
Eric Laurente0720872014-03-11 09:30:41 -07004615void AudioPolicyManager::StreamDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004616{
4617 const size_t SIZE = 256;
4618 char buffer[SIZE];
4619 String8 result;
4620
4621 snprintf(buffer, SIZE, "%s %02d %02d ",
4622 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
4623 result.append(buffer);
4624 for (size_t i = 0; i < mIndexCur.size(); i++) {
4625 snprintf(buffer, SIZE, "%04x : %02d, ",
4626 mIndexCur.keyAt(i),
4627 mIndexCur.valueAt(i));
4628 result.append(buffer);
4629 }
4630 result.append("\n");
4631
4632 write(fd, result.string(), result.size());
4633}
4634
4635// --- EffectDescriptor class implementation
4636
Eric Laurente0720872014-03-11 09:30:41 -07004637status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004638{
4639 const size_t SIZE = 256;
4640 char buffer[SIZE];
4641 String8 result;
4642
4643 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
4644 result.append(buffer);
4645 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
4646 result.append(buffer);
4647 snprintf(buffer, SIZE, " Session: %d\n", mSession);
4648 result.append(buffer);
4649 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
4650 result.append(buffer);
4651 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
4652 result.append(buffer);
4653 write(fd, result.string(), result.size());
4654
4655 return NO_ERROR;
4656}
4657
Eric Laurent951f4552014-05-20 10:48:17 -07004658// --- HwModule class implementation
Eric Laurente552edb2014-03-10 17:42:56 -07004659
Eric Laurente0720872014-03-11 09:30:41 -07004660AudioPolicyManager::HwModule::HwModule(const char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07004661 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
4662{
4663}
4664
Eric Laurente0720872014-03-11 09:30:41 -07004665AudioPolicyManager::HwModule::~HwModule()
Eric Laurente552edb2014-03-10 17:42:56 -07004666{
4667 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004668 mOutputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004669 }
4670 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004671 mInputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004672 }
4673 free((void *)mName);
4674}
4675
Eric Laurent1afeecb2014-05-14 08:52:28 -07004676status_t AudioPolicyManager::HwModule::loadInput(cnode *root)
4677{
4678 cnode *node = root->first_child;
4679
4680 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this);
4681
4682 while (node) {
4683 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4684 profile->loadSamplingRates((char *)node->value);
4685 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4686 profile->loadFormats((char *)node->value);
4687 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4688 profile->loadInChannels((char *)node->value);
4689 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4690 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4691 mDeclaredDevices);
4692 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4693 profile->loadGains(node);
4694 }
4695 node = node->next;
4696 }
4697 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4698 "loadInput() invalid supported devices");
4699 ALOGW_IF(profile->mChannelMasks.size() == 0,
4700 "loadInput() invalid supported channel masks");
4701 ALOGW_IF(profile->mSamplingRates.size() == 0,
4702 "loadInput() invalid supported sampling rates");
4703 ALOGW_IF(profile->mFormats.size() == 0,
4704 "loadInput() invalid supported formats");
4705 if (!profile->mSupportedDevices.isEmpty() &&
4706 (profile->mChannelMasks.size() != 0) &&
4707 (profile->mSamplingRates.size() != 0) &&
4708 (profile->mFormats.size() != 0)) {
4709
4710 ALOGV("loadInput() adding input Supported Devices %04x",
4711 profile->mSupportedDevices.types());
4712
4713 mInputProfiles.add(profile);
4714 return NO_ERROR;
4715 } else {
4716 return BAD_VALUE;
4717 }
4718}
4719
4720status_t AudioPolicyManager::HwModule::loadOutput(cnode *root)
4721{
4722 cnode *node = root->first_child;
4723
4724 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this);
4725
4726 while (node) {
4727 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4728 profile->loadSamplingRates((char *)node->value);
4729 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4730 profile->loadFormats((char *)node->value);
4731 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4732 profile->loadOutChannels((char *)node->value);
4733 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4734 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4735 mDeclaredDevices);
4736 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
4737 profile->mFlags = parseFlagNames((char *)node->value);
4738 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4739 profile->loadGains(node);
4740 }
4741 node = node->next;
4742 }
4743 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4744 "loadOutput() invalid supported devices");
4745 ALOGW_IF(profile->mChannelMasks.size() == 0,
4746 "loadOutput() invalid supported channel masks");
4747 ALOGW_IF(profile->mSamplingRates.size() == 0,
4748 "loadOutput() invalid supported sampling rates");
4749 ALOGW_IF(profile->mFormats.size() == 0,
4750 "loadOutput() invalid supported formats");
4751 if (!profile->mSupportedDevices.isEmpty() &&
4752 (profile->mChannelMasks.size() != 0) &&
4753 (profile->mSamplingRates.size() != 0) &&
4754 (profile->mFormats.size() != 0)) {
4755
4756 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x",
4757 profile->mSupportedDevices.types(), profile->mFlags);
4758
4759 mOutputProfiles.add(profile);
4760 return NO_ERROR;
4761 } else {
4762 return BAD_VALUE;
4763 }
4764}
4765
4766status_t AudioPolicyManager::HwModule::loadDevice(cnode *root)
4767{
4768 cnode *node = root->first_child;
4769
4770 audio_devices_t type = AUDIO_DEVICE_NONE;
4771 while (node) {
4772 if (strcmp(node->name, DEVICE_TYPE) == 0) {
4773 type = parseDeviceNames((char *)node->value);
4774 break;
4775 }
4776 node = node->next;
4777 }
4778 if (type == AUDIO_DEVICE_NONE ||
4779 (!audio_is_input_device(type) && !audio_is_output_device(type))) {
4780 ALOGW("loadDevice() bad type %08x", type);
4781 return BAD_VALUE;
4782 }
4783 sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type);
4784 deviceDesc->mModule = this;
4785
4786 node = root->first_child;
4787 while (node) {
4788 if (strcmp(node->name, DEVICE_ADDRESS) == 0) {
4789 deviceDesc->mAddress = String8((char *)node->value);
4790 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4791 if (audio_is_input_device(type)) {
4792 deviceDesc->loadInChannels((char *)node->value);
4793 } else {
4794 deviceDesc->loadOutChannels((char *)node->value);
4795 }
4796 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4797 deviceDesc->loadGains(node);
4798 }
4799 node = node->next;
4800 }
4801
4802 ALOGV("loadDevice() adding device name %s type %08x address %s",
4803 deviceDesc->mName.string(), type, deviceDesc->mAddress.string());
4804
4805 mDeclaredDevices.add(deviceDesc);
4806
4807 return NO_ERROR;
4808}
4809
Eric Laurente0720872014-03-11 09:30:41 -07004810void AudioPolicyManager::HwModule::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004811{
4812 const size_t SIZE = 256;
4813 char buffer[SIZE];
4814 String8 result;
4815
4816 snprintf(buffer, SIZE, " - name: %s\n", mName);
4817 result.append(buffer);
4818 snprintf(buffer, SIZE, " - handle: %d\n", mHandle);
4819 result.append(buffer);
4820 write(fd, result.string(), result.size());
4821 if (mOutputProfiles.size()) {
4822 write(fd, " - outputs:\n", strlen(" - outputs:\n"));
4823 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004824 snprintf(buffer, SIZE, " output %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004825 write(fd, buffer, strlen(buffer));
4826 mOutputProfiles[i]->dump(fd);
4827 }
4828 }
4829 if (mInputProfiles.size()) {
4830 write(fd, " - inputs:\n", strlen(" - inputs:\n"));
4831 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004832 snprintf(buffer, SIZE, " input %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004833 write(fd, buffer, strlen(buffer));
4834 mInputProfiles[i]->dump(fd);
4835 }
4836 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07004837 if (mDeclaredDevices.size()) {
4838 write(fd, " - devices:\n", strlen(" - devices:\n"));
4839 for (size_t i = 0; i < mDeclaredDevices.size(); i++) {
4840 mDeclaredDevices[i]->dump(fd, 4, i);
4841 }
4842 }
Eric Laurente552edb2014-03-10 17:42:56 -07004843}
4844
Eric Laurent951f4552014-05-20 10:48:17 -07004845// --- AudioPort class implementation
4846
4847void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const
4848{
4849 port->role = mRole;
4850 port->type = mType;
4851 unsigned int i;
4852 for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) {
4853 port->sample_rates[i] = mSamplingRates[i];
4854 }
4855 port->num_sample_rates = i;
4856 for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) {
4857 port->channel_masks[i] = mChannelMasks[i];
4858 }
4859 port->num_channel_masks = i;
4860 for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) {
4861 port->formats[i] = mFormats[i];
4862 }
4863 port->num_formats = i;
4864 port->num_gains = 0;
4865}
4866
4867
4868void AudioPolicyManager::AudioPort::loadSamplingRates(char *name)
4869{
4870 char *str = strtok(name, "|");
4871
4872 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
4873 // rates should be read from the output stream after it is opened for the first time
4874 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4875 mSamplingRates.add(0);
4876 return;
4877 }
4878
4879 while (str != NULL) {
4880 uint32_t rate = atoi(str);
4881 if (rate != 0) {
4882 ALOGV("loadSamplingRates() adding rate %d", rate);
4883 mSamplingRates.add(rate);
4884 }
4885 str = strtok(NULL, "|");
4886 }
Eric Laurent951f4552014-05-20 10:48:17 -07004887}
4888
4889void AudioPolicyManager::AudioPort::loadFormats(char *name)
4890{
4891 char *str = strtok(name, "|");
4892
4893 // by convention, "0' in the first entry in mFormats indicates the supported formats
4894 // should be read from the output stream after it is opened for the first time
4895 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4896 mFormats.add(AUDIO_FORMAT_DEFAULT);
4897 return;
4898 }
4899
4900 while (str != NULL) {
4901 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
4902 ARRAY_SIZE(sFormatNameToEnumTable),
4903 str);
4904 if (format != AUDIO_FORMAT_DEFAULT) {
4905 mFormats.add(format);
4906 }
4907 str = strtok(NULL, "|");
4908 }
Eric Laurent951f4552014-05-20 10:48:17 -07004909}
4910
4911void AudioPolicyManager::AudioPort::loadInChannels(char *name)
4912{
4913 const char *str = strtok(name, "|");
4914
4915 ALOGV("loadInChannels() %s", name);
4916
4917 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4918 mChannelMasks.add(0);
4919 return;
4920 }
4921
4922 while (str != NULL) {
4923 audio_channel_mask_t channelMask =
4924 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
4925 ARRAY_SIZE(sInChannelsNameToEnumTable),
4926 str);
4927 if (channelMask != 0) {
4928 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
4929 mChannelMasks.add(channelMask);
4930 }
4931 str = strtok(NULL, "|");
4932 }
Eric Laurent951f4552014-05-20 10:48:17 -07004933}
4934
4935void AudioPolicyManager::AudioPort::loadOutChannels(char *name)
4936{
4937 const char *str = strtok(name, "|");
4938
4939 ALOGV("loadOutChannels() %s", name);
4940
4941 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
4942 // masks should be read from the output stream after it is opened for the first time
4943 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4944 mChannelMasks.add(0);
4945 return;
4946 }
4947
4948 while (str != NULL) {
4949 audio_channel_mask_t channelMask =
4950 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
4951 ARRAY_SIZE(sOutChannelsNameToEnumTable),
4952 str);
4953 if (channelMask != 0) {
4954 mChannelMasks.add(channelMask);
4955 }
4956 str = strtok(NULL, "|");
4957 }
4958 return;
4959}
4960
Eric Laurent1afeecb2014-05-14 08:52:28 -07004961audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name)
4962{
4963 const char *str = strtok(name, "|");
4964
4965 ALOGV("loadGainMode() %s", name);
4966 audio_gain_mode_t mode = 0;
4967 while (str != NULL) {
4968 mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable,
4969 ARRAY_SIZE(sGainModeNameToEnumTable),
4970 str);
4971 str = strtok(NULL, "|");
4972 }
4973 return mode;
4974}
4975
4976void AudioPolicyManager::AudioPort::loadGain(cnode *root)
4977{
4978 cnode *node = root->first_child;
4979
4980 sp<AudioGain> gain = new AudioGain();
4981
4982 while (node) {
4983 if (strcmp(node->name, GAIN_MODE) == 0) {
4984 gain->mGain.mode = loadGainMode((char *)node->value);
4985 } else if (strcmp(node->name, GAIN_CHANNELS) == 0) {
4986 if ((mType == AUDIO_PORT_TYPE_DEVICE && mRole == AUDIO_PORT_ROLE_SOURCE) ||
4987 (mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK)) {
4988 gain->mGain.channel_mask =
4989 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
4990 ARRAY_SIZE(sInChannelsNameToEnumTable),
4991 (char *)node->value);
4992 } else {
4993 gain->mGain.channel_mask =
4994 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
4995 ARRAY_SIZE(sOutChannelsNameToEnumTable),
4996 (char *)node->value);
4997 }
4998 } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) {
4999 gain->mGain.min_value = atoi((char *)node->value);
5000 } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) {
5001 gain->mGain.max_value = atoi((char *)node->value);
5002 } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) {
5003 gain->mGain.default_value = atoi((char *)node->value);
5004 } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) {
5005 gain->mGain.step_value = atoi((char *)node->value);
5006 } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) {
5007 gain->mGain.min_ramp_ms = atoi((char *)node->value);
5008 } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) {
5009 gain->mGain.max_ramp_ms = atoi((char *)node->value);
5010 }
5011 node = node->next;
5012 }
5013
5014 ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d",
5015 gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value);
5016
5017 if (gain->mGain.mode == 0) {
5018 return;
5019 }
5020 mGains.add(gain);
5021}
5022
5023void AudioPolicyManager::AudioPort::loadGains(cnode *root)
5024{
5025 cnode *node = root->first_child;
5026 while (node) {
5027 ALOGV("loadGains() loading gain %s", node->name);
5028 loadGain(node);
5029 node = node->next;
5030 }
5031}
5032
5033void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const
5034{
5035 const size_t SIZE = 256;
5036 char buffer[SIZE];
5037 String8 result;
5038
5039 if (mName.size() != 0) {
5040 snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string());
5041 result.append(buffer);
5042 }
5043
5044 if (mSamplingRates.size() != 0) {
5045 snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, "");
5046 result.append(buffer);
5047 for (size_t i = 0; i < mSamplingRates.size(); i++) {
5048 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
5049 result.append(buffer);
5050 result.append(i == (mSamplingRates.size() - 1) ? "" : ", ");
5051 }
5052 result.append("\n");
5053 }
5054
5055 if (mChannelMasks.size() != 0) {
5056 snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, "");
5057 result.append(buffer);
5058 for (size_t i = 0; i < mChannelMasks.size(); i++) {
5059 snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
5060 result.append(buffer);
5061 result.append(i == (mChannelMasks.size() - 1) ? "" : ", ");
5062 }
5063 result.append("\n");
5064 }
5065
5066 if (mFormats.size() != 0) {
5067 snprintf(buffer, SIZE, "%*s- formats: ", spaces, "");
5068 result.append(buffer);
5069 for (size_t i = 0; i < mFormats.size(); i++) {
5070 snprintf(buffer, SIZE, "%-48s", enumToString(sFormatNameToEnumTable,
5071 ARRAY_SIZE(sFormatNameToEnumTable),
5072 mFormats[i]));
5073 result.append(buffer);
5074 result.append(i == (mFormats.size() - 1) ? "" : ", ");
5075 }
5076 result.append("\n");
5077 }
5078 write(fd, result.string(), result.size());
5079 if (mGains.size() != 0) {
5080 snprintf(buffer, SIZE, "%*s- gains:\n", spaces, "");
5081 write(fd, buffer, strlen(buffer) + 1);
5082 result.append(buffer);
5083 for (size_t i = 0; i < mGains.size(); i++) {
5084 mGains[i]->dump(fd, spaces + 2, i);
5085 }
5086 }
5087}
5088
5089// --- AudioGain class implementation
5090
5091AudioPolicyManager::AudioGain::AudioGain()
5092{
5093 memset(&mGain, 0, sizeof(struct audio_gain));
5094}
5095
5096void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const
5097{
5098 const size_t SIZE = 256;
5099 char buffer[SIZE];
5100 String8 result;
5101
5102 snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
5103 result.append(buffer);
5104 snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
5105 result.append(buffer);
5106 snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
5107 result.append(buffer);
5108 snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
5109 result.append(buffer);
5110 snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
5111 result.append(buffer);
5112 snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
5113 result.append(buffer);
5114 snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
5115 result.append(buffer);
5116 snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
5117 result.append(buffer);
5118 snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
5119 result.append(buffer);
5120
5121 write(fd, result.string(), result.size());
5122}
5123
Eric Laurent951f4552014-05-20 10:48:17 -07005124// --- IOProfile class implementation
5125
Eric Laurent1afeecb2014-05-14 08:52:28 -07005126AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role,
5127 HwModule *module)
5128 : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module), mFlags((audio_output_flags_t)0)
Eric Laurente552edb2014-03-10 17:42:56 -07005129{
5130}
5131
Eric Laurente0720872014-03-11 09:30:41 -07005132AudioPolicyManager::IOProfile::~IOProfile()
Eric Laurente552edb2014-03-10 17:42:56 -07005133{
5134}
5135
5136// checks if the IO profile is compatible with specified parameters.
5137// Sampling rate, format and channel mask must be specified in order to
5138// get a valid a match
Eric Laurente0720872014-03-11 09:30:41 -07005139bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07005140 uint32_t samplingRate,
5141 audio_format_t format,
5142 audio_channel_mask_t channelMask,
5143 audio_output_flags_t flags) const
5144{
5145 if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
5146 return false;
5147 }
5148
Eric Laurent3a4311c2014-03-17 12:00:47 -07005149 if ((mSupportedDevices.types() & device) != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07005150 return false;
5151 }
5152 if ((mFlags & flags) != flags) {
5153 return false;
5154 }
5155 size_t i;
5156 for (i = 0; i < mSamplingRates.size(); i++)
5157 {
5158 if (mSamplingRates[i] == samplingRate) {
5159 break;
5160 }
5161 }
5162 if (i == mSamplingRates.size()) {
5163 return false;
5164 }
5165 for (i = 0; i < mFormats.size(); i++)
5166 {
5167 if (mFormats[i] == format) {
5168 break;
5169 }
5170 }
5171 if (i == mFormats.size()) {
5172 return false;
5173 }
5174 for (i = 0; i < mChannelMasks.size(); i++)
5175 {
5176 if (mChannelMasks[i] == channelMask) {
5177 break;
5178 }
5179 }
5180 if (i == mChannelMasks.size()) {
5181 return false;
5182 }
5183 return true;
5184}
5185
Eric Laurente0720872014-03-11 09:30:41 -07005186void AudioPolicyManager::IOProfile::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005187{
5188 const size_t SIZE = 256;
5189 char buffer[SIZE];
5190 String8 result;
5191
Eric Laurent1afeecb2014-05-14 08:52:28 -07005192 AudioPort::dump(fd, 4);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005193
Eric Laurente552edb2014-03-10 17:42:56 -07005194 snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags);
5195 result.append(buffer);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005196 snprintf(buffer, SIZE, " - devices:\n");
5197 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07005198 write(fd, result.string(), result.size());
Eric Laurent1afeecb2014-05-14 08:52:28 -07005199 for (size_t i = 0; i < mSupportedDevices.size(); i++) {
5200 mSupportedDevices[i]->dump(fd, 6, i);
5201 }
Eric Laurente552edb2014-03-10 17:42:56 -07005202}
5203
Eric Laurentd4692962014-05-05 18:13:44 -07005204void AudioPolicyManager::IOProfile::log()
5205{
5206 const size_t SIZE = 256;
5207 char buffer[SIZE];
5208 String8 result;
5209
5210 ALOGV(" - sampling rates: ");
5211 for (size_t i = 0; i < mSamplingRates.size(); i++) {
5212 ALOGV(" %d", mSamplingRates[i]);
5213 }
5214
5215 ALOGV(" - channel masks: ");
5216 for (size_t i = 0; i < mChannelMasks.size(); i++) {
5217 ALOGV(" 0x%04x", mChannelMasks[i]);
5218 }
5219
5220 ALOGV(" - formats: ");
5221 for (size_t i = 0; i < mFormats.size(); i++) {
5222 ALOGV(" 0x%08x", mFormats[i]);
5223 }
5224
5225 ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types());
5226 ALOGV(" - flags: 0x%04x\n", mFlags);
5227}
5228
5229
Eric Laurent3a4311c2014-03-17 12:00:47 -07005230// --- DeviceDescriptor implementation
Eric Laurente552edb2014-03-10 17:42:56 -07005231
Eric Laurent3a4311c2014-03-17 12:00:47 -07005232bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
Eric Laurente552edb2014-03-10 17:42:56 -07005233{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005234 // Devices are considered equal if they:
5235 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
5236 // - have the same address or one device does not specify the address
5237 // - have the same channel mask or one device does not specify the channel mask
Eric Laurent951f4552014-05-20 10:48:17 -07005238 return (mDeviceType == other->mDeviceType) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005239 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
Eric Laurent2f8a36f2014-03-26 19:05:55 -07005240 (mChannelMask == 0 || other->mChannelMask == 0 ||
Eric Laurent3a4311c2014-03-17 12:00:47 -07005241 mChannelMask == other->mChannelMask);
5242}
5243
5244void AudioPolicyManager::DeviceVector::refreshTypes()
5245{
Eric Laurent951f4552014-05-20 10:48:17 -07005246 mDeviceTypes = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005247 for(size_t i = 0; i < size(); i++) {
Eric Laurent951f4552014-05-20 10:48:17 -07005248 mDeviceTypes |= itemAt(i)->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005249 }
Eric Laurent951f4552014-05-20 10:48:17 -07005250 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005251}
5252
5253ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
5254{
5255 for(size_t i = 0; i < size(); i++) {
5256 if (item->equals(itemAt(i))) {
5257 return i;
Eric Laurente552edb2014-03-10 17:42:56 -07005258 }
5259 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07005260 return -1;
Eric Laurente552edb2014-03-10 17:42:56 -07005261}
5262
Eric Laurent3a4311c2014-03-17 12:00:47 -07005263ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item)
Eric Laurente552edb2014-03-10 17:42:56 -07005264{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005265 ssize_t ret = indexOf(item);
5266
5267 if (ret < 0) {
5268 ret = SortedVector::add(item);
5269 if (ret >= 0) {
5270 refreshTypes();
5271 }
5272 } else {
Eric Laurent951f4552014-05-20 10:48:17 -07005273 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005274 ret = -1;
5275 }
5276 return ret;
Eric Laurente552edb2014-03-10 17:42:56 -07005277}
5278
Eric Laurent3a4311c2014-03-17 12:00:47 -07005279ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item)
5280{
5281 size_t i;
5282 ssize_t ret = indexOf(item);
5283
5284 if (ret < 0) {
Eric Laurent951f4552014-05-20 10:48:17 -07005285 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005286 } else {
5287 ret = SortedVector::removeAt(ret);
5288 if (ret >= 0) {
5289 refreshTypes();
5290 }
5291 }
5292 return ret;
5293}
5294
5295void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types)
5296{
5297 DeviceVector deviceList;
5298
5299 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
5300 types &= ~role_bit;
5301
5302 while (types) {
5303 uint32_t i = 31 - __builtin_clz(types);
5304 uint32_t type = 1 << i;
5305 types &= ~type;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005306 add(new DeviceDescriptor(String8(""), type | role_bit));
Eric Laurent3a4311c2014-03-17 12:00:47 -07005307 }
5308}
5309
Eric Laurent1afeecb2014-05-14 08:52:28 -07005310void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name,
5311 const DeviceVector& declaredDevices)
5312{
5313 char *devName = strtok(name, "|");
5314 while (devName != NULL) {
5315 if (strlen(devName) != 0) {
5316 audio_devices_t type = stringToEnum(sDeviceNameToEnumTable,
5317 ARRAY_SIZE(sDeviceNameToEnumTable),
5318 devName);
5319 if (type != AUDIO_DEVICE_NONE) {
5320 add(new DeviceDescriptor(String8(""), type));
5321 } else {
5322 sp<DeviceDescriptor> deviceDesc =
5323 declaredDevices.getDeviceFromName(String8(devName));
5324 if (deviceDesc != 0) {
5325 add(deviceDesc);
5326 }
5327 }
5328 }
5329 devName = strtok(NULL, "|");
5330 }
5331}
5332
Eric Laurent951f4552014-05-20 10:48:17 -07005333sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice(
5334 audio_devices_t type, String8 address) const
5335{
5336 sp<DeviceDescriptor> device;
5337 for (size_t i = 0; i < size(); i++) {
5338 if (itemAt(i)->mDeviceType == type) {
5339 device = itemAt(i);
5340 if (itemAt(i)->mAddress = address) {
5341 break;
5342 }
5343 }
5344 }
5345 ALOGV("DeviceVector::getDevice() for type %d address %s found %p",
5346 type, address.string(), device.get());
5347 return device;
5348}
5349
Eric Laurent6a94d692014-05-20 11:18:06 -07005350sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId(
5351 audio_port_handle_t id) const
5352{
5353 sp<DeviceDescriptor> device;
5354 for (size_t i = 0; i < size(); i++) {
5355 ALOGV("DeviceVector::getDeviceFromId(%d) itemAt(%d)->mId %d", id, i, itemAt(i)->mId);
5356 if (itemAt(i)->mId == id) {
5357 device = itemAt(i);
5358 break;
5359 }
5360 }
5361 return device;
5362}
5363
Eric Laurent951f4552014-05-20 10:48:17 -07005364AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType(
5365 audio_devices_t type) const
5366{
5367 DeviceVector devices;
5368 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
5369 if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) {
5370 devices.add(itemAt(i));
5371 type &= ~itemAt(i)->mDeviceType;
5372 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
5373 itemAt(i)->mDeviceType, itemAt(i).get());
5374 }
5375 }
5376 return devices;
5377}
5378
Eric Laurent1afeecb2014-05-14 08:52:28 -07005379sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName(
5380 const String8& name) const
5381{
5382 sp<DeviceDescriptor> device;
5383 for (size_t i = 0; i < size(); i++) {
5384 if (itemAt(i)->mName == name) {
5385 device = itemAt(i);
5386 break;
5387 }
5388 }
5389 return device;
5390}
5391
Eric Laurent6a94d692014-05-20 11:18:06 -07005392void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig(
5393 struct audio_port_config *dstConfig,
5394 const struct audio_port_config *srcConfig) const
Eric Laurent951f4552014-05-20 10:48:17 -07005395{
Eric Laurent6a94d692014-05-20 11:18:06 -07005396 dstConfig->id = mId;
5397 dstConfig->role = audio_is_output_device(mDeviceType) ?
Eric Laurent951f4552014-05-20 10:48:17 -07005398 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
Eric Laurent6a94d692014-05-20 11:18:06 -07005399 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
5400 dstConfig->channel_mask = mChannelMask;
5401 dstConfig->gain.index = -1;
5402 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK;
5403 // use supplied variable configuration parameters if any
5404 if (srcConfig != NULL) {
5405 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
5406 dstConfig->channel_mask = srcConfig->channel_mask;
5407 }
5408 if (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
5409 dstConfig->gain = srcConfig->gain;
5410 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
5411 }
5412 }
5413 dstConfig->ext.device.type = mDeviceType;
5414 dstConfig->ext.device.hw_module = mModule->mHandle;
5415 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
Eric Laurent951f4552014-05-20 10:48:17 -07005416}
5417
5418void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const
5419{
5420 AudioPort::toAudioPort(port);
5421 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07005422 toAudioPortConfig(&port->active_config);
Eric Laurent951f4552014-05-20 10:48:17 -07005423 port->ext.device.type = mDeviceType;
Eric Laurent6a94d692014-05-20 11:18:06 -07005424 port->ext.device.hw_module = mModule->mHandle;
Eric Laurent951f4552014-05-20 10:48:17 -07005425 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
5426}
5427
Eric Laurent1afeecb2014-05-14 08:52:28 -07005428status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const
Eric Laurent3a4311c2014-03-17 12:00:47 -07005429{
5430 const size_t SIZE = 256;
5431 char buffer[SIZE];
Eric Laurent1afeecb2014-05-14 08:52:28 -07005432 String8 result;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005433
Eric Laurent1afeecb2014-05-14 08:52:28 -07005434 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
5435 result.append(buffer);
5436 if (mId != 0) {
5437 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
5438 result.append(buffer);
5439 }
5440 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
5441 enumToString(sDeviceNameToEnumTable,
5442 ARRAY_SIZE(sDeviceNameToEnumTable),
5443 mDeviceType));
5444 result.append(buffer);
5445 if (mAddress.size() != 0) {
5446 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
5447 result.append(buffer);
5448 }
5449 if (mChannelMask != AUDIO_CHANNEL_NONE) {
5450 snprintf(buffer, SIZE, "%*s- channel mask: %08x\n", spaces, "", mChannelMask);
5451 result.append(buffer);
5452 }
5453 write(fd, result.string(), result.size());
5454 AudioPort::dump(fd, spaces);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005455
5456 return NO_ERROR;
5457}
5458
5459
5460// --- audio_policy.conf file parsing
5461
Eric Laurente0720872014-03-11 09:30:41 -07005462audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005463{
5464 uint32_t flag = 0;
5465
5466 // it is OK to cast name to non const here as we are not going to use it after
5467 // strtok() modifies it
5468 char *flagName = strtok(name, "|");
5469 while (flagName != NULL) {
5470 if (strlen(flagName) != 0) {
5471 flag |= stringToEnum(sFlagNameToEnumTable,
5472 ARRAY_SIZE(sFlagNameToEnumTable),
5473 flagName);
5474 }
5475 flagName = strtok(NULL, "|");
5476 }
5477 //force direct flag if offload flag is set: offloading implies a direct output stream
5478 // and all common behaviors are driven by checking only the direct flag
5479 // this should normally be set appropriately in the policy configuration file
5480 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
5481 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
5482 }
5483
5484 return (audio_output_flags_t)flag;
5485}
5486
Eric Laurente0720872014-03-11 09:30:41 -07005487audio_devices_t AudioPolicyManager::parseDeviceNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005488{
5489 uint32_t device = 0;
5490
5491 char *devName = strtok(name, "|");
5492 while (devName != NULL) {
5493 if (strlen(devName) != 0) {
5494 device |= stringToEnum(sDeviceNameToEnumTable,
5495 ARRAY_SIZE(sDeviceNameToEnumTable),
5496 devName);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005497 }
Eric Laurente552edb2014-03-10 17:42:56 -07005498 devName = strtok(NULL, "|");
Eric Laurent3a4311c2014-03-17 12:00:47 -07005499 }
Eric Laurente552edb2014-03-10 17:42:56 -07005500 return device;
5501}
5502
Eric Laurente0720872014-03-11 09:30:41 -07005503void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005504{
Eric Laurente552edb2014-03-10 17:42:56 -07005505 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005506 cnode *node;
Eric Laurente552edb2014-03-10 17:42:56 -07005507 HwModule *module = new HwModule(root->name);
5508
Eric Laurent1afeecb2014-05-14 08:52:28 -07005509 node = config_find(root, DEVICES_TAG);
5510 if (node != NULL) {
5511 node = node->first_child;
5512 while (node) {
5513 ALOGV("loadHwModule() loading device %s", node->name);
5514 status_t tmpStatus = module->loadDevice(node);
5515 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5516 status = tmpStatus;
5517 }
5518 node = node->next;
5519 }
5520 }
5521 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07005522 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005523 node = node->first_child;
5524 while (node) {
5525 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005526 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005527 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5528 status = tmpStatus;
5529 }
5530 node = node->next;
5531 }
5532 }
5533 node = config_find(root, INPUTS_TAG);
5534 if (node != NULL) {
5535 node = node->first_child;
5536 while (node) {
5537 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005538 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005539 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5540 status = tmpStatus;
5541 }
5542 node = node->next;
5543 }
5544 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005545 loadGlobalConfig(root, module);
5546
Eric Laurente552edb2014-03-10 17:42:56 -07005547 if (status == NO_ERROR) {
5548 mHwModules.add(module);
5549 } else {
5550 delete module;
5551 }
5552}
5553
Eric Laurente0720872014-03-11 09:30:41 -07005554void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005555{
5556 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
5557 if (node == NULL) {
5558 return;
5559 }
5560
5561 node = node->first_child;
5562 while (node) {
5563 ALOGV("loadHwModules() loading module %s", node->name);
5564 loadHwModule(node);
5565 node = node->next;
5566 }
5567}
5568
Eric Laurent1afeecb2014-05-14 08:52:28 -07005569void AudioPolicyManager::loadGlobalConfig(cnode *root, HwModule *module)
Eric Laurente552edb2014-03-10 17:42:56 -07005570{
5571 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
5572 if (node == NULL) {
5573 return;
5574 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005575 DeviceVector declaredDevices;
5576 if (module != NULL) {
5577 declaredDevices = module->mDeclaredDevices;
5578 }
5579
Eric Laurente552edb2014-03-10 17:42:56 -07005580 node = node->first_child;
5581 while (node) {
5582 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005583 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
5584 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005585 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
5586 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005587 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07005588 audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
Eric Laurente552edb2014-03-10 17:42:56 -07005589 ARRAY_SIZE(sDeviceNameToEnumTable),
5590 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005591 if (device != AUDIO_DEVICE_NONE) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005592 mDefaultOutputDevice = new DeviceDescriptor(String8(""), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005593 } else {
5594 ALOGW("loadGlobalConfig() default device not specified");
5595 }
Eric Laurent951f4552014-05-20 10:48:17 -07005596 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07005597 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005598 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
5599 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005600 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005601 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
5602 mSpeakerDrcEnabled = stringToBool((char *)node->value);
5603 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
5604 }
5605 node = node->next;
5606 }
5607}
5608
Eric Laurente0720872014-03-11 09:30:41 -07005609status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07005610{
5611 cnode *root;
5612 char *data;
5613
5614 data = (char *)load_file(path, NULL);
5615 if (data == NULL) {
5616 return -ENODEV;
5617 }
5618 root = config_node("", "");
5619 config_load(root, data);
5620
Eric Laurente552edb2014-03-10 17:42:56 -07005621 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005622 // legacy audio_policy.conf files have one global_configuration section
5623 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07005624 config_free(root);
5625 free(root);
5626 free(data);
5627
5628 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
5629
5630 return NO_ERROR;
5631}
5632
Eric Laurente0720872014-03-11 09:30:41 -07005633void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07005634{
5635 HwModule *module;
Eric Laurent951f4552014-05-20 10:48:17 -07005636 sp<IOProfile> profile;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005637 sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005638 mAvailableOutputDevices.add(mDefaultOutputDevice);
5639 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005640
5641 module = new HwModule("primary");
5642
Eric Laurent1afeecb2014-05-14 08:52:28 -07005643 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005644 profile->mSamplingRates.add(44100);
5645 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5646 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005647 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005648 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
5649 module->mOutputProfiles.add(profile);
5650
Eric Laurent1afeecb2014-05-14 08:52:28 -07005651 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005652 profile->mSamplingRates.add(8000);
5653 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5654 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005655 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005656 module->mInputProfiles.add(profile);
5657
5658 mHwModules.add(module);
5659}
5660
5661}; // namespace android