blob: d63ad53b332c06b57b03afa5fbdd65d62e48330f [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>
41#include <hardware_legacy/audio_policy_conf.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070042#include <media/AudioParameter.h>
Eric Laurentd4692962014-05-05 18:13:44 -070043#include "AudioPolicyManager.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
139
140uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table,
141 size_t size,
142 const char *name)
143{
144 for (size_t i = 0; i < size; i++) {
145 if (strcmp(table[i].name, name) == 0) {
146 ALOGV("stringToEnum() found %s", table[i].name);
147 return table[i].value;
148 }
149 }
150 return 0;
151}
152
153const char *AudioPolicyManager::enumToString(const struct StringToEnum *table,
154 size_t size,
155 uint32_t value)
156{
157 for (size_t i = 0; i < size; i++) {
158 if (table[i].value == value) {
159 return table[i].name;
160 }
161 }
162 return "";
163}
164
165bool AudioPolicyManager::stringToBool(const char *value)
166{
167 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
168}
169
170
171// ----------------------------------------------------------------------------
Eric Laurente552edb2014-03-10 17:42:56 -0700172// AudioPolicyInterface implementation
173// ----------------------------------------------------------------------------
174
175
Eric Laurente0720872014-03-11 09:30:41 -0700176status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700177 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700178 const char *device_address)
179{
Eric Laurent3a4311c2014-03-17 12:00:47 -0700180 String8 address = String8(device_address);
Eric Laurente552edb2014-03-10 17:42:56 -0700181
182 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
183
184 // connect/disconnect only 1 device at a time
185 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
186
Eric Laurente552edb2014-03-10 17:42:56 -0700187 // handle output devices
188 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700189 SortedVector <audio_io_handle_t> outputs;
190
Eric Laurent3a4311c2014-03-17 12:00:47 -0700191 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(device,
192 address,
Eric Laurent2f8a36f2014-03-26 19:05:55 -0700193 0);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700194 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
195
Eric Laurente552edb2014-03-10 17:42:56 -0700196 // save a copy of the opened output descriptors before any output is opened or closed
197 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
198 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700199 switch (state)
200 {
201 // handle output device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700202 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
Eric Laurent3a4311c2014-03-17 12:00:47 -0700203 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700204 ALOGW("setDeviceConnectionState() device already connected: %x", device);
205 return INVALID_OPERATION;
206 }
207 ALOGV("setDeviceConnectionState() connecting device %x", device);
208
Eric Laurent3a4311c2014-03-17 12:00:47 -0700209 if (checkOutputsForDevice(device, state, outputs, address) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -0700210 return INVALID_OPERATION;
211 }
Eric Laurentd4692962014-05-05 18:13:44 -0700212 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
Eric Laurente552edb2014-03-10 17:42:56 -0700213 outputs.size());
214 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700215 index = mAvailableOutputDevices.add(devDesc);
216 if (index >= 0) {
217 mAvailableOutputDevices[index]->mId = nextUniqueId();
218 } else {
219 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700220 }
221
222 break;
223 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700224 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700225 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700226 ALOGW("setDeviceConnectionState() device not connected: %x", device);
227 return INVALID_OPERATION;
228 }
229
230 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
231 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700232 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700233
Eric Laurent3a4311c2014-03-17 12:00:47 -0700234 checkOutputsForDevice(device, state, outputs, address);
Eric Laurente552edb2014-03-10 17:42:56 -0700235 // not currently handling multiple simultaneous submixes: ignoring remote submix
236 // case and address
237 } break;
238
239 default:
240 ALOGE("setDeviceConnectionState() invalid state: %x", state);
241 return BAD_VALUE;
242 }
243
Eric Laurent3a4311c2014-03-17 12:00:47 -0700244 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
245 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700246 checkA2dpSuspend();
247 checkOutputForAllStrategies();
248 // outputs must be closed after checkOutputForAllStrategies() is executed
249 if (!outputs.isEmpty()) {
250 for (size_t i = 0; i < outputs.size(); i++) {
251 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
252 // close unused outputs after device disconnection or direct outputs that have been
253 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700254 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700255 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
256 (desc->mDirectOpenCount == 0))) {
257 closeOutput(outputs[i]);
258 }
259 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700260 // check again after closing A2DP output to reset mA2dpSuspended if needed
261 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700262 }
263
264 updateDevicesAndOutputs();
265 for (size_t i = 0; i < mOutputs.size(); i++) {
266 // do not force device change on duplicated output because if device is 0, it will
267 // also force a device 0 for the two outputs it is duplicated to which may override
268 // a valid device selection on those outputs.
269 setOutputDevice(mOutputs.keyAt(i),
Eric Laurent1c333e22014-05-20 10:48:17 -0700270 getNewOutputDevice(mOutputs.keyAt(i), true /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700271 !mOutputs.valueAt(i)->isDuplicated(),
272 0);
273 }
274
Eric Laurentb71e58b2014-05-29 16:08:11 -0700275 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700276 } // end if is output device
277
Eric Laurente552edb2014-03-10 17:42:56 -0700278 // handle input devices
279 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700280 SortedVector <audio_io_handle_t> inputs;
281
Eric Laurent3a4311c2014-03-17 12:00:47 -0700282 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(device,
283 address,
Eric Laurent2f8a36f2014-03-26 19:05:55 -0700284 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700285
Eric Laurent3a4311c2014-03-17 12:00:47 -0700286 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700287 switch (state)
288 {
289 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700290 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700291 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700292 ALOGW("setDeviceConnectionState() device already connected: %d", device);
293 return INVALID_OPERATION;
294 }
Eric Laurentd4692962014-05-05 18:13:44 -0700295 if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) {
296 return INVALID_OPERATION;
297 }
298
Eric Laurent3a4311c2014-03-17 12:00:47 -0700299 index = mAvailableInputDevices.add(devDesc);
300 if (index >= 0) {
301 mAvailableInputDevices[index]->mId = nextUniqueId();
302 } else {
303 return NO_MEMORY;
304 }
Eric Laurentd4692962014-05-05 18:13:44 -0700305 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700306
307 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700308 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700309 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700310 ALOGW("setDeviceConnectionState() device not connected: %d", device);
311 return INVALID_OPERATION;
312 }
Eric Laurentd4692962014-05-05 18:13:44 -0700313 checkInputsForDevice(device, state, inputs, address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700314 mAvailableInputDevices.remove(devDesc);
Eric Laurentd4692962014-05-05 18:13:44 -0700315 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700316
317 default:
318 ALOGE("setDeviceConnectionState() invalid state: %x", state);
319 return BAD_VALUE;
320 }
321
Eric Laurentd4692962014-05-05 18:13:44 -0700322 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700323
324 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700325 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700326
327 ALOGW("setDeviceConnectionState() invalid device: %x", device);
328 return BAD_VALUE;
329}
330
Eric Laurente0720872014-03-11 09:30:41 -0700331audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700332 const char *device_address)
333{
Eric Laurent3b73df72014-03-11 09:06:29 -0700334 audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700335 String8 address = String8(device_address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700336 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(device,
337 String8(device_address),
Eric Laurent2f8a36f2014-03-26 19:05:55 -0700338 0);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700339 ssize_t index;
340 DeviceVector *deviceVector;
341
Eric Laurente552edb2014-03-10 17:42:56 -0700342 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700343 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700344 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700345 deviceVector = &mAvailableInputDevices;
346 } else {
347 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
348 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700349 }
350
Eric Laurent3a4311c2014-03-17 12:00:47 -0700351 index = deviceVector->indexOf(devDesc);
352 if (index >= 0) {
353 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
354 } else {
355 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
356 }
Eric Laurente552edb2014-03-10 17:42:56 -0700357}
358
Eric Laurente0720872014-03-11 09:30:41 -0700359void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700360{
361 ALOGV("setPhoneState() state %d", state);
362 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Eric Laurent3b73df72014-03-11 09:06:29 -0700363 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700364 ALOGW("setPhoneState() invalid state %d", state);
365 return;
366 }
367
368 if (state == mPhoneState ) {
369 ALOGW("setPhoneState() setting same state %d", state);
370 return;
371 }
372
373 // if leaving call state, handle special case of active streams
374 // pertaining to sonification strategy see handleIncallSonification()
375 if (isInCall()) {
376 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700377 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
378 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700379 }
380 }
381
382 // store previous phone state for management of sonification strategy below
383 int oldState = mPhoneState;
384 mPhoneState = state;
385 bool force = false;
386
387 // are we entering or starting a call
388 if (!isStateInCall(oldState) && isStateInCall(state)) {
389 ALOGV(" Entering call in setPhoneState()");
390 // force routing command to audio hardware when starting a call
391 // even if no device change is needed
392 force = true;
393 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
394 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
395 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
396 }
397 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
398 ALOGV(" Exiting call in setPhoneState()");
399 // force routing command to audio hardware when exiting a call
400 // even if no device change is needed
401 force = true;
402 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
403 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
404 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
405 }
406 } else if (isStateInCall(state) && (state != oldState)) {
407 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
408 // force routing command to audio hardware when switching between telephony and VoIP
409 // even if no device change is needed
410 force = true;
411 }
412
413 // check for device and output changes triggered by new phone state
Eric Laurent1c333e22014-05-20 10:48:17 -0700414 newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700415 checkA2dpSuspend();
416 checkOutputForAllStrategies();
417 updateDevicesAndOutputs();
418
419 AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
420
421 // force routing command to audio hardware when ending call
422 // even if no device change is needed
423 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
424 newDevice = hwOutputDesc->device();
425 }
426
427 int delayMs = 0;
428 if (isStateInCall(state)) {
429 nsecs_t sysTime = systemTime();
430 for (size_t i = 0; i < mOutputs.size(); i++) {
431 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
432 // mute media and sonification strategies and delay device switch by the largest
433 // latency of any output where either strategy is active.
434 // This avoid sending the ring tone or music tail into the earpiece or headset.
435 if ((desc->isStrategyActive(STRATEGY_MEDIA,
436 SONIFICATION_HEADSET_MUSIC_DELAY,
437 sysTime) ||
438 desc->isStrategyActive(STRATEGY_SONIFICATION,
439 SONIFICATION_HEADSET_MUSIC_DELAY,
440 sysTime)) &&
441 (delayMs < (int)desc->mLatency*2)) {
442 delayMs = desc->mLatency*2;
443 }
444 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
445 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
446 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
447 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
448 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
449 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
450 }
451 }
452
453 // change routing is necessary
454 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
455
456 // if entering in call state, handle special case of active streams
457 // pertaining to sonification strategy see handleIncallSonification()
458 if (isStateInCall(state)) {
459 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700460 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
461 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700462 }
463 }
464
465 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700466 if (state == AUDIO_MODE_RINGTONE &&
467 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700468 mLimitRingtoneVolume = true;
469 } else {
470 mLimitRingtoneVolume = false;
471 }
472}
473
Eric Laurente0720872014-03-11 09:30:41 -0700474void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700475 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700476{
477 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
478
479 bool forceVolumeReeval = false;
480 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700481 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
482 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
483 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700484 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
485 return;
486 }
487 forceVolumeReeval = true;
488 mForceUse[usage] = config;
489 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700490 case AUDIO_POLICY_FORCE_FOR_MEDIA:
491 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
492 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
493 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
494 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
495 config != AUDIO_POLICY_FORCE_NO_BT_A2DP) {
Eric Laurente552edb2014-03-10 17:42:56 -0700496 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
497 return;
498 }
499 mForceUse[usage] = config;
500 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700501 case AUDIO_POLICY_FORCE_FOR_RECORD:
502 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
503 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700504 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
505 return;
506 }
507 mForceUse[usage] = config;
508 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700509 case AUDIO_POLICY_FORCE_FOR_DOCK:
510 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
511 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
512 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
513 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
514 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700515 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
516 }
517 forceVolumeReeval = true;
518 mForceUse[usage] = config;
519 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
521 if (config != AUDIO_POLICY_FORCE_NONE &&
522 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700523 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
524 }
525 forceVolumeReeval = true;
526 mForceUse[usage] = config;
527 break;
528 default:
529 ALOGW("setForceUse() invalid usage %d", usage);
530 break;
531 }
532
533 // check for device and output changes triggered by new force usage
534 checkA2dpSuspend();
535 checkOutputForAllStrategies();
536 updateDevicesAndOutputs();
537 for (size_t i = 0; i < mOutputs.size(); i++) {
538 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700539 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700540 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
541 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
542 applyStreamVolumes(output, newDevice, 0, true);
543 }
544 }
545
546 audio_io_handle_t activeInput = getActiveInput();
547 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700548 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700549 }
550
551}
552
Eric Laurente0720872014-03-11 09:30:41 -0700553audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700554{
555 return mForceUse[usage];
556}
557
Eric Laurente0720872014-03-11 09:30:41 -0700558void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700559{
560 ALOGV("setSystemProperty() property %s, value %s", property, value);
561}
562
563// Find a direct output profile compatible with the parameters passed, even if the input flags do
564// not explicitly request a direct output
Eric Laurent1c333e22014-05-20 10:48:17 -0700565sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700566 audio_devices_t device,
567 uint32_t samplingRate,
568 audio_format_t format,
569 audio_channel_mask_t channelMask,
570 audio_output_flags_t flags)
571{
572 for (size_t i = 0; i < mHwModules.size(); i++) {
573 if (mHwModules[i]->mHandle == 0) {
574 continue;
575 }
576 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700577 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent3a4311c2014-03-17 12:00:47 -0700578 bool found = false;
Eric Laurente552edb2014-03-10 17:42:56 -0700579 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
580 if (profile->isCompatibleProfile(device, samplingRate, format,
581 channelMask,
582 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700583 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700584 }
585 } else {
586 if (profile->isCompatibleProfile(device, samplingRate, format,
587 channelMask,
588 AUDIO_OUTPUT_FLAG_DIRECT)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700589 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700590 }
591 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700592 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
593 return profile;
594 }
Eric Laurente552edb2014-03-10 17:42:56 -0700595 }
596 }
597 return 0;
598}
599
Eric Laurente0720872014-03-11 09:30:41 -0700600audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700601 uint32_t samplingRate,
602 audio_format_t format,
603 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700604 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700605 const audio_offload_info_t *offloadInfo)
606{
607 audio_io_handle_t output = 0;
608 uint32_t latency = 0;
Eric Laurent3b73df72014-03-11 09:06:29 -0700609 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700610 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
611 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
612 device, stream, samplingRate, format, channelMask, flags);
613
614#ifdef AUDIO_POLICY_TEST
615 if (mCurOutput != 0) {
616 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
617 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
618
619 if (mTestOutputs[mCurOutput] == 0) {
620 ALOGV("getOutput() opening test output");
621 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
622 outputDesc->mDevice = mTestDevice;
623 outputDesc->mSamplingRate = mTestSamplingRate;
624 outputDesc->mFormat = mTestFormat;
625 outputDesc->mChannelMask = mTestChannels;
626 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700627 outputDesc->mFlags =
628 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700629 outputDesc->mRefCount[stream] = 0;
630 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
631 &outputDesc->mSamplingRate,
632 &outputDesc->mFormat,
633 &outputDesc->mChannelMask,
634 &outputDesc->mLatency,
635 outputDesc->mFlags,
636 offloadInfo);
637 if (mTestOutputs[mCurOutput]) {
638 AudioParameter outputCmd = AudioParameter();
639 outputCmd.addInt(String8("set_id"),mCurOutput);
640 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
641 addOutput(mTestOutputs[mCurOutput], outputDesc);
642 }
643 }
644 return mTestOutputs[mCurOutput];
645 }
646#endif //AUDIO_POLICY_TEST
647
648 // open a direct output if required by specified parameters
649 //force direct flag if offload flag is set: offloading implies a direct output stream
650 // and all common behaviors are driven by checking only the direct flag
651 // this should normally be set appropriately in the policy configuration file
652 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700653 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700654 }
655
656 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
657 // creating an offloaded track and tearing it down immediately after start when audioflinger
658 // detects there is an active non offloadable effect.
659 // FIXME: We should check the audio session here but we do not have it in this context.
660 // This may prevent offloading in rare situations where effects are left active by apps
661 // in the background.
Eric Laurent1c333e22014-05-20 10:48:17 -0700662 sp<IOProfile> profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700663 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
664 !isNonOffloadableEffectEnabled()) {
665 profile = getProfileForDirectOutput(device,
666 samplingRate,
667 format,
668 channelMask,
669 (audio_output_flags_t)flags);
670 }
671
Eric Laurent1c333e22014-05-20 10:48:17 -0700672 if (profile != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700673 AudioOutputDescriptor *outputDesc = NULL;
674
675 for (size_t i = 0; i < mOutputs.size(); i++) {
676 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
677 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
678 outputDesc = desc;
679 // reuse direct output if currently open and configured with same parameters
680 if ((samplingRate == outputDesc->mSamplingRate) &&
681 (format == outputDesc->mFormat) &&
682 (channelMask == outputDesc->mChannelMask)) {
683 outputDesc->mDirectOpenCount++;
684 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
685 return mOutputs.keyAt(i);
686 }
687 }
688 }
689 // close direct output if currently open and configured with different parameters
690 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700691 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700692 }
693 outputDesc = new AudioOutputDescriptor(profile);
694 outputDesc->mDevice = device;
695 outputDesc->mSamplingRate = samplingRate;
696 outputDesc->mFormat = format;
697 outputDesc->mChannelMask = channelMask;
698 outputDesc->mLatency = 0;
699 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
700 outputDesc->mRefCount[stream] = 0;
701 outputDesc->mStopTime[stream] = 0;
702 outputDesc->mDirectOpenCount = 1;
703 output = mpClientInterface->openOutput(profile->mModule->mHandle,
704 &outputDesc->mDevice,
705 &outputDesc->mSamplingRate,
706 &outputDesc->mFormat,
707 &outputDesc->mChannelMask,
708 &outputDesc->mLatency,
709 outputDesc->mFlags,
710 offloadInfo);
711
712 // only accept an output with the requested parameters
713 if (output == 0 ||
714 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
715 (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
716 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
717 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
718 "format %d %d, channelMask %04x %04x", output, samplingRate,
719 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
720 outputDesc->mChannelMask);
721 if (output != 0) {
722 mpClientInterface->closeOutput(output);
723 }
724 delete outputDesc;
725 return 0;
726 }
727 audio_io_handle_t srcOutput = getOutputForEffect();
728 addOutput(output, outputDesc);
729 audio_io_handle_t dstOutput = getOutputForEffect();
730 if (dstOutput == output) {
731 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
732 }
733 mPreviousOutputs = mOutputs;
734 ALOGV("getOutput() returns new direct output %d", output);
735 return output;
736 }
737
738 // ignoring channel mask due to downmix capability in mixer
739
740 // open a non direct output
741
742 // for non direct outputs, only PCM is supported
743 if (audio_is_linear_pcm(format)) {
744 // get which output is suitable for the specified stream. The actual
745 // routing change will happen when startOutput() will be called
746 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
747
748 output = selectOutput(outputs, flags);
749 }
750 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
751 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
752
753 ALOGV("getOutput() returns output %d", output);
754
755 return output;
756}
757
Eric Laurente0720872014-03-11 09:30:41 -0700758audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3b73df72014-03-11 09:06:29 -0700759 audio_output_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -0700760{
761 // select one output among several that provide a path to a particular device or set of
762 // devices (the list was previously build by getOutputsForDevice()).
763 // The priority is as follows:
764 // 1: the output with the highest number of requested policy flags
765 // 2: the primary output
766 // 3: the first output in the list
767
768 if (outputs.size() == 0) {
769 return 0;
770 }
771 if (outputs.size() == 1) {
772 return outputs[0];
773 }
774
775 int maxCommonFlags = 0;
776 audio_io_handle_t outputFlags = 0;
777 audio_io_handle_t outputPrimary = 0;
778
779 for (size_t i = 0; i < outputs.size(); i++) {
780 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
781 if (!outputDesc->isDuplicated()) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700782 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700783 if (commonFlags > maxCommonFlags) {
784 outputFlags = outputs[i];
785 maxCommonFlags = commonFlags;
786 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
787 }
788 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
789 outputPrimary = outputs[i];
790 }
791 }
792 }
793
794 if (outputFlags != 0) {
795 return outputFlags;
796 }
797 if (outputPrimary != 0) {
798 return outputPrimary;
799 }
800
801 return outputs[0];
802}
803
Eric Laurente0720872014-03-11 09:30:41 -0700804status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700805 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700806 int session)
807{
808 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
809 ssize_t index = mOutputs.indexOfKey(output);
810 if (index < 0) {
811 ALOGW("startOutput() unknown output %d", output);
812 return BAD_VALUE;
813 }
814
815 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
816
817 // increment usage count for this stream on the requested output:
818 // NOTE that the usage count is the same for duplicated output and hardware output which is
819 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
820 outputDesc->changeRefCount(stream, 1);
821
822 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700823 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700824 routing_strategy strategy = getStrategy(stream);
825 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
826 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
827 uint32_t waitMs = 0;
828 bool force = false;
829 for (size_t i = 0; i < mOutputs.size(); i++) {
830 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
831 if (desc != outputDesc) {
832 // force a device change if any other output is managed by the same hw
833 // module and has a current device selection that differs from selected device.
834 // In this case, the audio HAL must receive the new device selection so that it can
835 // change the device currently selected by the other active output.
836 if (outputDesc->sharesHwModuleWith(desc) &&
837 desc->device() != newDevice) {
838 force = true;
839 }
840 // wait for audio on other active outputs to be presented when starting
841 // a notification so that audio focus effect can propagate.
842 uint32_t latency = desc->latency();
843 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
844 waitMs = latency;
845 }
846 }
847 }
848 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
849
850 // handle special case for sonification while in call
851 if (isInCall()) {
852 handleIncallSonification(stream, true, false);
853 }
854
855 // apply volume rules for current stream and device if necessary
856 checkAndSetVolume(stream,
857 mStreams[stream].getVolumeIndex(newDevice),
858 output,
859 newDevice);
860
861 // update the outputs if starting an output with a stream that can affect notification
862 // routing
863 handleNotificationRoutingForStream(stream);
864 if (waitMs > muteWaitMs) {
865 usleep((waitMs - muteWaitMs) * 2 * 1000);
866 }
867 }
868 return NO_ERROR;
869}
870
871
Eric Laurente0720872014-03-11 09:30:41 -0700872status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700873 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700874 int session)
875{
876 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
877 ssize_t index = mOutputs.indexOfKey(output);
878 if (index < 0) {
879 ALOGW("stopOutput() unknown output %d", output);
880 return BAD_VALUE;
881 }
882
883 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
884
885 // handle special case for sonification while in call
886 if (isInCall()) {
887 handleIncallSonification(stream, false, false);
888 }
889
890 if (outputDesc->mRefCount[stream] > 0) {
891 // decrement usage count of this stream on the output
892 outputDesc->changeRefCount(stream, -1);
893 // store time at which the stream was stopped - see isStreamActive()
894 if (outputDesc->mRefCount[stream] == 0) {
895 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -0700896 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700897 // delay the device switch by twice the latency because stopOutput() is executed when
898 // the track stop() command is received and at that time the audio track buffer can
899 // still contain data that needs to be drained. The latency only covers the audio HAL
900 // and kernel buffers. Also the latency does not always include additional delay in the
901 // audio path (audio DSP, CODEC ...)
902 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
903
904 // force restoring the device selection on other active outputs if it differs from the
905 // one being selected for this output
906 for (size_t i = 0; i < mOutputs.size(); i++) {
907 audio_io_handle_t curOutput = mOutputs.keyAt(i);
908 AudioOutputDescriptor *desc = mOutputs.valueAt(i);
909 if (curOutput != output &&
910 desc->isActive() &&
911 outputDesc->sharesHwModuleWith(desc) &&
912 (newDevice != desc->device())) {
913 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -0700914 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700915 true,
916 outputDesc->mLatency*2);
917 }
918 }
919 // update the outputs if stopping one with a stream that can affect notification routing
920 handleNotificationRoutingForStream(stream);
921 }
922 return NO_ERROR;
923 } else {
924 ALOGW("stopOutput() refcount is already 0 for output %d", output);
925 return INVALID_OPERATION;
926 }
927}
928
Eric Laurente0720872014-03-11 09:30:41 -0700929void AudioPolicyManager::releaseOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -0700930{
931 ALOGV("releaseOutput() %d", output);
932 ssize_t index = mOutputs.indexOfKey(output);
933 if (index < 0) {
934 ALOGW("releaseOutput() releasing unknown output %d", output);
935 return;
936 }
937
938#ifdef AUDIO_POLICY_TEST
939 int testIndex = testOutputIndex(output);
940 if (testIndex != 0) {
941 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
942 if (outputDesc->isActive()) {
943 mpClientInterface->closeOutput(output);
944 delete mOutputs.valueAt(index);
945 mOutputs.removeItem(output);
946 mTestOutputs[testIndex] = 0;
947 }
948 return;
949 }
950#endif //AUDIO_POLICY_TEST
951
952 AudioOutputDescriptor *desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -0700953 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700954 if (desc->mDirectOpenCount <= 0) {
955 ALOGW("releaseOutput() invalid open count %d for output %d",
956 desc->mDirectOpenCount, output);
957 return;
958 }
959 if (--desc->mDirectOpenCount == 0) {
960 closeOutput(output);
961 // If effects where present on the output, audioflinger moved them to the primary
962 // output by default: move them back to the appropriate output.
963 audio_io_handle_t dstOutput = getOutputForEffect();
964 if (dstOutput != mPrimaryOutput) {
965 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
966 }
967 }
968 }
969}
970
971
Eric Laurente0720872014-03-11 09:30:41 -0700972audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource,
Eric Laurente552edb2014-03-10 17:42:56 -0700973 uint32_t samplingRate,
974 audio_format_t format,
975 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700976 audio_in_acoustics_t acoustics)
Eric Laurente552edb2014-03-10 17:42:56 -0700977{
978 audio_io_handle_t input = 0;
979 audio_devices_t device = getDeviceForInputSource(inputSource);
980
981 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
982 inputSource, samplingRate, format, channelMask, acoustics);
983
984 if (device == AUDIO_DEVICE_NONE) {
985 ALOGW("getInput() could not find device for inputSource %d", inputSource);
986 return 0;
987 }
988
989 // adapt channel selection to input source
990 switch(inputSource) {
991 case AUDIO_SOURCE_VOICE_UPLINK:
992 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
993 break;
994 case AUDIO_SOURCE_VOICE_DOWNLINK:
995 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
996 break;
997 case AUDIO_SOURCE_VOICE_CALL:
998 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
999 break;
1000 default:
1001 break;
1002 }
1003
Eric Laurent1c333e22014-05-20 10:48:17 -07001004 sp<IOProfile> profile = getInputProfile(device,
Eric Laurente552edb2014-03-10 17:42:56 -07001005 samplingRate,
1006 format,
1007 channelMask);
Eric Laurent1c333e22014-05-20 10:48:17 -07001008 if (profile == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001009 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d, "
1010 "channelMask %04x",
1011 device, samplingRate, format, channelMask);
1012 return 0;
1013 }
1014
1015 if (profile->mModule->mHandle == 0) {
1016 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
1017 return 0;
1018 }
1019
1020 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
1021
1022 inputDesc->mInputSource = inputSource;
1023 inputDesc->mDevice = device;
1024 inputDesc->mSamplingRate = samplingRate;
1025 inputDesc->mFormat = format;
1026 inputDesc->mChannelMask = channelMask;
1027 inputDesc->mRefCount = 0;
1028 input = mpClientInterface->openInput(profile->mModule->mHandle,
1029 &inputDesc->mDevice,
1030 &inputDesc->mSamplingRate,
1031 &inputDesc->mFormat,
1032 &inputDesc->mChannelMask);
1033
1034 // only accept input with the exact requested set of parameters
1035 if (input == 0 ||
1036 (samplingRate != inputDesc->mSamplingRate) ||
1037 (format != inputDesc->mFormat) ||
1038 (channelMask != inputDesc->mChannelMask)) {
1039 ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask %x",
1040 samplingRate, format, channelMask);
1041 if (input != 0) {
1042 mpClientInterface->closeInput(input);
1043 }
1044 delete inputDesc;
1045 return 0;
1046 }
Eric Laurentd4692962014-05-05 18:13:44 -07001047 addInput(input, inputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001048 return input;
1049}
1050
Eric Laurente0720872014-03-11 09:30:41 -07001051status_t AudioPolicyManager::startInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001052{
1053 ALOGV("startInput() input %d", input);
1054 ssize_t index = mInputs.indexOfKey(input);
1055 if (index < 0) {
1056 ALOGW("startInput() unknown input %d", input);
1057 return BAD_VALUE;
1058 }
1059 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1060
1061#ifdef AUDIO_POLICY_TEST
1062 if (mTestInput == 0)
1063#endif //AUDIO_POLICY_TEST
1064 {
1065 // refuse 2 active AudioRecord clients at the same time except if the active input
1066 // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
1067 audio_io_handle_t activeInput = getActiveInput();
1068 if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
1069 AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1070 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1071 ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1072 stopInput(activeInput);
1073 releaseInput(activeInput);
1074 } else {
1075 ALOGW("startInput() input %d failed: other input already started", input);
1076 return INVALID_OPERATION;
1077 }
1078 }
1079 }
1080
Eric Laurent1c333e22014-05-20 10:48:17 -07001081 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001082
1083 // automatically enable the remote submix output when input is started
1084 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1085 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001086 AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001087 }
1088
Eric Laurente552edb2014-03-10 17:42:56 -07001089 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1090
Eric Laurente552edb2014-03-10 17:42:56 -07001091 inputDesc->mRefCount = 1;
1092 return NO_ERROR;
1093}
1094
Eric Laurente0720872014-03-11 09:30:41 -07001095status_t AudioPolicyManager::stopInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001096{
1097 ALOGV("stopInput() input %d", input);
1098 ssize_t index = mInputs.indexOfKey(input);
1099 if (index < 0) {
1100 ALOGW("stopInput() unknown input %d", input);
1101 return BAD_VALUE;
1102 }
1103 AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1104
1105 if (inputDesc->mRefCount == 0) {
1106 ALOGW("stopInput() input %d already stopped", input);
1107 return INVALID_OPERATION;
1108 } else {
1109 // automatically disable the remote submix output when input is stopped
1110 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1111 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001112 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001113 }
1114
Eric Laurent1c333e22014-05-20 10:48:17 -07001115 resetInputDevice(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001116 inputDesc->mRefCount = 0;
1117 return NO_ERROR;
1118 }
1119}
1120
Eric Laurente0720872014-03-11 09:30:41 -07001121void AudioPolicyManager::releaseInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001122{
1123 ALOGV("releaseInput() %d", input);
1124 ssize_t index = mInputs.indexOfKey(input);
1125 if (index < 0) {
1126 ALOGW("releaseInput() releasing unknown input %d", input);
1127 return;
1128 }
1129 mpClientInterface->closeInput(input);
1130 delete mInputs.valueAt(index);
1131 mInputs.removeItem(input);
1132 ALOGV("releaseInput() exit");
1133}
1134
Eric Laurentd4692962014-05-05 18:13:44 -07001135void AudioPolicyManager::closeAllInputs() {
1136 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1137 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1138 }
1139 mInputs.clear();
1140}
1141
Eric Laurente0720872014-03-11 09:30:41 -07001142void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001143 int indexMin,
1144 int indexMax)
1145{
1146 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1147 if (indexMin < 0 || indexMin >= indexMax) {
1148 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1149 return;
1150 }
1151 mStreams[stream].mIndexMin = indexMin;
1152 mStreams[stream].mIndexMax = indexMax;
1153}
1154
Eric Laurente0720872014-03-11 09:30:41 -07001155status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001156 int index,
1157 audio_devices_t device)
1158{
1159
1160 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1161 return BAD_VALUE;
1162 }
1163 if (!audio_is_output_device(device)) {
1164 return BAD_VALUE;
1165 }
1166
1167 // Force max volume if stream cannot be muted
1168 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1169
1170 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1171 stream, device, index);
1172
1173 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1174 // clear all device specific values
1175 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1176 mStreams[stream].mIndexCur.clear();
1177 }
1178 mStreams[stream].mIndexCur.add(device, index);
1179
1180 // compute and apply stream volume on all outputs according to connected device
1181 status_t status = NO_ERROR;
1182 for (size_t i = 0; i < mOutputs.size(); i++) {
1183 audio_devices_t curDevice =
1184 getDeviceForVolume(mOutputs.valueAt(i)->device());
1185 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1186 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1187 if (volStatus != NO_ERROR) {
1188 status = volStatus;
1189 }
1190 }
1191 }
1192 return status;
1193}
1194
Eric Laurente0720872014-03-11 09:30:41 -07001195status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001196 int *index,
1197 audio_devices_t device)
1198{
1199 if (index == NULL) {
1200 return BAD_VALUE;
1201 }
1202 if (!audio_is_output_device(device)) {
1203 return BAD_VALUE;
1204 }
1205 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1206 // the strategy the stream belongs to.
1207 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1208 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1209 }
1210 device = getDeviceForVolume(device);
1211
1212 *index = mStreams[stream].getVolumeIndex(device);
1213 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1214 return NO_ERROR;
1215}
1216
Eric Laurente0720872014-03-11 09:30:41 -07001217audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001218 const SortedVector<audio_io_handle_t>& outputs)
1219{
1220 // select one output among several suitable for global effects.
1221 // The priority is as follows:
1222 // 1: An offloaded output. If the effect ends up not being offloadable,
1223 // AudioFlinger will invalidate the track and the offloaded output
1224 // will be closed causing the effect to be moved to a PCM output.
1225 // 2: A deep buffer output
1226 // 3: the first output in the list
1227
1228 if (outputs.size() == 0) {
1229 return 0;
1230 }
1231
1232 audio_io_handle_t outputOffloaded = 0;
1233 audio_io_handle_t outputDeepBuffer = 0;
1234
1235 for (size_t i = 0; i < outputs.size(); i++) {
1236 AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001237 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001238 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1239 outputOffloaded = outputs[i];
1240 }
1241 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1242 outputDeepBuffer = outputs[i];
1243 }
1244 }
1245
1246 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1247 outputOffloaded, outputDeepBuffer);
1248 if (outputOffloaded != 0) {
1249 return outputOffloaded;
1250 }
1251 if (outputDeepBuffer != 0) {
1252 return outputDeepBuffer;
1253 }
1254
1255 return outputs[0];
1256}
1257
Eric Laurente0720872014-03-11 09:30:41 -07001258audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001259{
1260 // apply simple rule where global effects are attached to the same output as MUSIC streams
1261
Eric Laurent3b73df72014-03-11 09:06:29 -07001262 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001263 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1264 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1265
1266 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1267 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1268 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1269
1270 return output;
1271}
1272
Eric Laurente0720872014-03-11 09:30:41 -07001273status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001274 audio_io_handle_t io,
1275 uint32_t strategy,
1276 int session,
1277 int id)
1278{
1279 ssize_t index = mOutputs.indexOfKey(io);
1280 if (index < 0) {
1281 index = mInputs.indexOfKey(io);
1282 if (index < 0) {
1283 ALOGW("registerEffect() unknown io %d", io);
1284 return INVALID_OPERATION;
1285 }
1286 }
1287
1288 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1289 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1290 desc->name, desc->memoryUsage);
1291 return INVALID_OPERATION;
1292 }
1293 mTotalEffectsMemory += desc->memoryUsage;
1294 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1295 desc->name, io, strategy, session, id);
1296 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1297
1298 EffectDescriptor *pDesc = new EffectDescriptor();
1299 memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
1300 pDesc->mIo = io;
1301 pDesc->mStrategy = (routing_strategy)strategy;
1302 pDesc->mSession = session;
1303 pDesc->mEnabled = false;
1304
1305 mEffects.add(id, pDesc);
1306
1307 return NO_ERROR;
1308}
1309
Eric Laurente0720872014-03-11 09:30:41 -07001310status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001311{
1312 ssize_t index = mEffects.indexOfKey(id);
1313 if (index < 0) {
1314 ALOGW("unregisterEffect() unknown effect ID %d", id);
1315 return INVALID_OPERATION;
1316 }
1317
1318 EffectDescriptor *pDesc = mEffects.valueAt(index);
1319
1320 setEffectEnabled(pDesc, false);
1321
1322 if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
1323 ALOGW("unregisterEffect() memory %d too big for total %d",
1324 pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1325 pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1326 }
1327 mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
1328 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1329 pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1330
1331 mEffects.removeItem(id);
1332 delete pDesc;
1333
1334 return NO_ERROR;
1335}
1336
Eric Laurente0720872014-03-11 09:30:41 -07001337status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001338{
1339 ssize_t index = mEffects.indexOfKey(id);
1340 if (index < 0) {
1341 ALOGW("unregisterEffect() unknown effect ID %d", id);
1342 return INVALID_OPERATION;
1343 }
1344
1345 return setEffectEnabled(mEffects.valueAt(index), enabled);
1346}
1347
Eric Laurente0720872014-03-11 09:30:41 -07001348status_t AudioPolicyManager::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001349{
1350 if (enabled == pDesc->mEnabled) {
1351 ALOGV("setEffectEnabled(%s) effect already %s",
1352 enabled?"true":"false", enabled?"enabled":"disabled");
1353 return INVALID_OPERATION;
1354 }
1355
1356 if (enabled) {
1357 if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
1358 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1359 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1360 return INVALID_OPERATION;
1361 }
1362 mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
1363 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1364 } else {
1365 if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
1366 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1367 pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1368 pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1369 }
1370 mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
1371 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1372 }
1373 pDesc->mEnabled = enabled;
1374 return NO_ERROR;
1375}
1376
Eric Laurente0720872014-03-11 09:30:41 -07001377bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001378{
1379 for (size_t i = 0; i < mEffects.size(); i++) {
1380 const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1381 if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1382 ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1383 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1384 pDesc->mDesc.name, pDesc->mSession);
1385 return true;
1386 }
1387 }
1388 return false;
1389}
1390
Eric Laurente0720872014-03-11 09:30:41 -07001391bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001392{
1393 nsecs_t sysTime = systemTime();
1394 for (size_t i = 0; i < mOutputs.size(); i++) {
1395 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001396 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001397 return true;
1398 }
1399 }
1400 return false;
1401}
1402
Eric Laurente0720872014-03-11 09:30:41 -07001403bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001404 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001405{
1406 nsecs_t sysTime = systemTime();
1407 for (size_t i = 0; i < mOutputs.size(); i++) {
1408 const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1409 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001410 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001411 return true;
1412 }
1413 }
1414 return false;
1415}
1416
Eric Laurente0720872014-03-11 09:30:41 -07001417bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001418{
1419 for (size_t i = 0; i < mInputs.size(); i++) {
1420 const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1421 if ((inputDescriptor->mInputSource == (int)source ||
Eric Laurent3b73df72014-03-11 09:06:29 -07001422 (source == AUDIO_SOURCE_VOICE_RECOGNITION &&
Eric Laurente552edb2014-03-10 17:42:56 -07001423 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1424 && (inputDescriptor->mRefCount > 0)) {
1425 return true;
1426 }
1427 }
1428 return false;
1429}
1430
1431
Eric Laurente0720872014-03-11 09:30:41 -07001432status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001433{
1434 const size_t SIZE = 256;
1435 char buffer[SIZE];
1436 String8 result;
1437
1438 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1439 result.append(buffer);
1440
1441 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1442 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001443 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1444 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001445 snprintf(buffer, SIZE, " Force use for communications %d\n",
1446 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07001447 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001448 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07001449 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001450 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07001451 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001452 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07001453 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001454 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07001455 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001456
Eric Laurent3a4311c2014-03-17 12:00:47 -07001457 snprintf(buffer, SIZE, " Available output devices:\n");
1458 result.append(buffer);
1459 write(fd, result.string(), result.size());
1460 DeviceDescriptor::dumpHeader(fd, 2);
1461 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
1462 mAvailableOutputDevices[i]->dump(fd, 2);
1463 }
1464 snprintf(buffer, SIZE, "\n Available input devices:\n");
1465 write(fd, buffer, strlen(buffer));
1466 DeviceDescriptor::dumpHeader(fd, 2);
1467 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
1468 mAvailableInputDevices[i]->dump(fd, 2);
1469 }
Eric Laurente552edb2014-03-10 17:42:56 -07001470
1471 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
1472 write(fd, buffer, strlen(buffer));
1473 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001474 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07001475 write(fd, buffer, strlen(buffer));
1476 mHwModules[i]->dump(fd);
1477 }
1478
1479 snprintf(buffer, SIZE, "\nOutputs dump:\n");
1480 write(fd, buffer, strlen(buffer));
1481 for (size_t i = 0; i < mOutputs.size(); i++) {
1482 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1483 write(fd, buffer, strlen(buffer));
1484 mOutputs.valueAt(i)->dump(fd);
1485 }
1486
1487 snprintf(buffer, SIZE, "\nInputs dump:\n");
1488 write(fd, buffer, strlen(buffer));
1489 for (size_t i = 0; i < mInputs.size(); i++) {
1490 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1491 write(fd, buffer, strlen(buffer));
1492 mInputs.valueAt(i)->dump(fd);
1493 }
1494
1495 snprintf(buffer, SIZE, "\nStreams dump:\n");
1496 write(fd, buffer, strlen(buffer));
1497 snprintf(buffer, SIZE,
1498 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1499 write(fd, buffer, strlen(buffer));
Eric Laurent3b73df72014-03-11 09:06:29 -07001500 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001501 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07001502 write(fd, buffer, strlen(buffer));
1503 mStreams[i].dump(fd);
1504 }
1505
1506 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1507 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1508 write(fd, buffer, strlen(buffer));
1509
1510 snprintf(buffer, SIZE, "Registered effects:\n");
1511 write(fd, buffer, strlen(buffer));
1512 for (size_t i = 0; i < mEffects.size(); i++) {
1513 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1514 write(fd, buffer, strlen(buffer));
1515 mEffects.valueAt(i)->dump(fd);
1516 }
1517
1518
1519 return NO_ERROR;
1520}
1521
1522// This function checks for the parameters which can be offloaded.
1523// This can be enhanced depending on the capability of the DSP and policy
1524// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001525bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001526{
1527 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001528 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001529 offloadInfo.sample_rate, offloadInfo.channel_mask,
1530 offloadInfo.format,
1531 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1532 offloadInfo.has_video);
1533
1534 // Check if offload has been disabled
1535 char propValue[PROPERTY_VALUE_MAX];
1536 if (property_get("audio.offload.disable", propValue, "0")) {
1537 if (atoi(propValue) != 0) {
1538 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1539 return false;
1540 }
1541 }
1542
1543 // Check if stream type is music, then only allow offload as of now.
1544 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1545 {
1546 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1547 return false;
1548 }
1549
1550 //TODO: enable audio offloading with video when ready
1551 if (offloadInfo.has_video)
1552 {
1553 ALOGV("isOffloadSupported: has_video == true, returning false");
1554 return false;
1555 }
1556
1557 //If duration is less than minimum value defined in property, return false
1558 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1559 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1560 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1561 return false;
1562 }
1563 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1564 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1565 return false;
1566 }
1567
1568 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1569 // creating an offloaded track and tearing it down immediately after start when audioflinger
1570 // detects there is an active non offloadable effect.
1571 // FIXME: We should check the audio session here but we do not have it in this context.
1572 // This may prevent offloading in rare situations where effects are left active by apps
1573 // in the background.
1574 if (isNonOffloadableEffectEnabled()) {
1575 return false;
1576 }
1577
1578 // See if there is a profile to support this.
1579 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001580 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001581 offloadInfo.sample_rate,
1582 offloadInfo.format,
1583 offloadInfo.channel_mask,
1584 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001585 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1586 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001587}
1588
1589// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07001590// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07001591// ----------------------------------------------------------------------------
1592
Eric Laurent3a4311c2014-03-17 12:00:47 -07001593uint32_t AudioPolicyManager::nextUniqueId()
1594{
1595 return android_atomic_inc(&mNextUniqueId);
1596}
1597
Eric Laurente0720872014-03-11 09:30:41 -07001598AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07001599 :
1600#ifdef AUDIO_POLICY_TEST
1601 Thread(false),
1602#endif //AUDIO_POLICY_TEST
1603 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07001604 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07001605 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1606 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07001607 mA2dpSuspended(false),
1608 mSpeakerDrcEnabled(false), mNextUniqueId(0)
Eric Laurente552edb2014-03-10 17:42:56 -07001609{
1610 mpClientInterface = clientInterface;
1611
Eric Laurent3b73df72014-03-11 09:06:29 -07001612 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
1613 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001614 }
1615
Eric Laurent3a4311c2014-03-17 12:00:47 -07001616 mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07001617 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
1618 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1619 ALOGE("could not load audio policy configuration file, setting defaults");
1620 defaultAudioPolicyConfig();
1621 }
1622 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07001623 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07001624
1625 // must be done after reading the policy
1626 initializeVolumeCurves();
1627
1628 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07001629 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
1630 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07001631 for (size_t i = 0; i < mHwModules.size(); i++) {
1632 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
1633 if (mHwModules[i]->mHandle == 0) {
1634 ALOGW("could not open HW module %s", mHwModules[i]->mName);
1635 continue;
1636 }
1637 // open all output streams needed to access attached devices
1638 // except for direct output streams that are only opened when they are actually
1639 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07001640 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07001641 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
1642 {
Eric Laurent1c333e22014-05-20 10:48:17 -07001643 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07001644
Eric Laurent3a4311c2014-03-17 12:00:47 -07001645 if (outProfile->mSupportedDevices.isEmpty()) {
1646 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
1647 continue;
1648 }
1649
1650 audio_devices_t profileTypes = outProfile->mSupportedDevices.types();
1651 if ((profileTypes & outputDeviceTypes) &&
Eric Laurente552edb2014-03-10 17:42:56 -07001652 ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
1653 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001654
Eric Laurent1c333e22014-05-20 10:48:17 -07001655 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice->mDeviceType & profileTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07001656 audio_io_handle_t output = mpClientInterface->openOutput(
1657 outProfile->mModule->mHandle,
1658 &outputDesc->mDevice,
1659 &outputDesc->mSamplingRate,
1660 &outputDesc->mFormat,
1661 &outputDesc->mChannelMask,
1662 &outputDesc->mLatency,
1663 outputDesc->mFlags);
1664 if (output == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07001665 ALOGW("Cannot open output stream for device %08x on hw module %s",
1666 outputDesc->mDevice,
1667 mHwModules[i]->mName);
Eric Laurente552edb2014-03-10 17:42:56 -07001668 delete outputDesc;
1669 } else {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07001670 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001671 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07001672 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07001673 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001674 // give a valid ID to an attached device once confirmed it is reachable
1675 if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) {
1676 mAvailableOutputDevices[index]->mId = nextUniqueId();
1677 }
1678 }
Eric Laurente552edb2014-03-10 17:42:56 -07001679 if (mPrimaryOutput == 0 &&
1680 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1681 mPrimaryOutput = output;
1682 }
1683 addOutput(output, outputDesc);
1684 setOutputDevice(output,
Eric Laurent3a4311c2014-03-17 12:00:47 -07001685 outputDesc->mDevice,
Eric Laurente552edb2014-03-10 17:42:56 -07001686 true);
1687 }
1688 }
1689 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07001690 // open input streams needed to access attached devices to validate
1691 // mAvailableInputDevices list
1692 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
1693 {
Eric Laurent1c333e22014-05-20 10:48:17 -07001694 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07001695
Eric Laurent3a4311c2014-03-17 12:00:47 -07001696 if (inProfile->mSupportedDevices.isEmpty()) {
1697 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
1698 continue;
1699 }
1700
1701 audio_devices_t profileTypes = inProfile->mSupportedDevices.types();
1702 if (profileTypes & inputDeviceTypes) {
1703 AudioInputDescriptor *inputDesc = new AudioInputDescriptor(inProfile);
1704
1705 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
Eric Laurent1c333e22014-05-20 10:48:17 -07001706 inputDesc->mDevice = inProfile->mSupportedDevices[0]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07001707 audio_io_handle_t input = mpClientInterface->openInput(
1708 inProfile->mModule->mHandle,
1709 &inputDesc->mDevice,
1710 &inputDesc->mSamplingRate,
1711 &inputDesc->mFormat,
1712 &inputDesc->mChannelMask);
1713
1714 if (input != 0) {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07001715 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001716 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07001717 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07001718 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001719 // give a valid ID to an attached device once confirmed it is reachable
1720 if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) {
1721 mAvailableInputDevices[index]->mId = nextUniqueId();
1722 }
1723 }
1724 mpClientInterface->closeInput(input);
1725 } else {
1726 ALOGW("Cannot open input stream for device %08x on hw module %s",
1727 inputDesc->mDevice,
1728 mHwModules[i]->mName);
1729 }
1730 delete inputDesc;
1731 }
1732 }
1733 }
1734 // make sure all attached devices have been allocated a unique ID
1735 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
1736 if (mAvailableOutputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001737 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001738 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
1739 continue;
1740 }
1741 i++;
1742 }
1743 for (size_t i = 0; i < mAvailableInputDevices.size();) {
1744 if (mAvailableInputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001745 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001746 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
1747 continue;
1748 }
1749 i++;
1750 }
1751 // make sure default device is reachable
1752 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001753 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001754 }
Eric Laurente552edb2014-03-10 17:42:56 -07001755
1756 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1757
1758 updateDevicesAndOutputs();
1759
1760#ifdef AUDIO_POLICY_TEST
1761 if (mPrimaryOutput != 0) {
1762 AudioParameter outputCmd = AudioParameter();
1763 outputCmd.addInt(String8("set_id"), 0);
1764 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1765
1766 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1767 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07001768 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
1769 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07001770 mTestLatencyMs = 0;
1771 mCurOutput = 0;
1772 mDirectOutput = false;
1773 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1774 mTestOutputs[i] = 0;
1775 }
1776
1777 const size_t SIZE = 256;
1778 char buffer[SIZE];
1779 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1780 run(buffer, ANDROID_PRIORITY_AUDIO);
1781 }
1782#endif //AUDIO_POLICY_TEST
1783}
1784
Eric Laurente0720872014-03-11 09:30:41 -07001785AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07001786{
1787#ifdef AUDIO_POLICY_TEST
1788 exit();
1789#endif //AUDIO_POLICY_TEST
1790 for (size_t i = 0; i < mOutputs.size(); i++) {
1791 mpClientInterface->closeOutput(mOutputs.keyAt(i));
1792 delete mOutputs.valueAt(i);
1793 }
1794 for (size_t i = 0; i < mInputs.size(); i++) {
1795 mpClientInterface->closeInput(mInputs.keyAt(i));
1796 delete mInputs.valueAt(i);
1797 }
1798 for (size_t i = 0; i < mHwModules.size(); i++) {
1799 delete mHwModules[i];
1800 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07001801 mAvailableOutputDevices.clear();
1802 mAvailableInputDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07001803}
1804
Eric Laurente0720872014-03-11 09:30:41 -07001805status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07001806{
1807 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1808}
1809
1810#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07001811bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07001812{
1813 ALOGV("entering threadLoop()");
1814 while (!exitPending())
1815 {
1816 String8 command;
1817 int valueInt;
1818 String8 value;
1819
1820 Mutex::Autolock _l(mLock);
1821 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1822
1823 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1824 AudioParameter param = AudioParameter(command);
1825
1826 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1827 valueInt != 0) {
1828 ALOGV("Test command %s received", command.string());
1829 String8 target;
1830 if (param.get(String8("target"), target) != NO_ERROR) {
1831 target = "Manager";
1832 }
1833 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1834 param.remove(String8("test_cmd_policy_output"));
1835 mCurOutput = valueInt;
1836 }
1837 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1838 param.remove(String8("test_cmd_policy_direct"));
1839 if (value == "false") {
1840 mDirectOutput = false;
1841 } else if (value == "true") {
1842 mDirectOutput = true;
1843 }
1844 }
1845 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1846 param.remove(String8("test_cmd_policy_input"));
1847 mTestInput = valueInt;
1848 }
1849
1850 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1851 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07001852 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07001853 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07001854 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07001855 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07001856 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07001857 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07001858 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07001859 }
Eric Laurent3b73df72014-03-11 09:06:29 -07001860 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07001861 if (target == "Manager") {
1862 mTestFormat = format;
1863 } else if (mTestOutputs[mCurOutput] != 0) {
1864 AudioParameter outputParam = AudioParameter();
1865 outputParam.addInt(String8("format"), format);
1866 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1867 }
1868 }
1869 }
1870 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1871 param.remove(String8("test_cmd_policy_channels"));
1872 int channels = 0;
1873
1874 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07001875 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07001876 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07001877 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07001878 }
1879 if (channels != 0) {
1880 if (target == "Manager") {
1881 mTestChannels = channels;
1882 } else if (mTestOutputs[mCurOutput] != 0) {
1883 AudioParameter outputParam = AudioParameter();
1884 outputParam.addInt(String8("channels"), channels);
1885 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1886 }
1887 }
1888 }
1889 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1890 param.remove(String8("test_cmd_policy_sampleRate"));
1891 if (valueInt >= 0 && valueInt <= 96000) {
1892 int samplingRate = valueInt;
1893 if (target == "Manager") {
1894 mTestSamplingRate = samplingRate;
1895 } else if (mTestOutputs[mCurOutput] != 0) {
1896 AudioParameter outputParam = AudioParameter();
1897 outputParam.addInt(String8("sampling_rate"), samplingRate);
1898 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1899 }
1900 }
1901 }
1902
1903 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1904 param.remove(String8("test_cmd_policy_reopen"));
1905
1906 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1907 mpClientInterface->closeOutput(mPrimaryOutput);
1908
1909 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
1910
1911 delete mOutputs.valueFor(mPrimaryOutput);
1912 mOutputs.removeItem(mPrimaryOutput);
1913
1914 AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1915 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
1916 mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
1917 &outputDesc->mDevice,
1918 &outputDesc->mSamplingRate,
1919 &outputDesc->mFormat,
1920 &outputDesc->mChannelMask,
1921 &outputDesc->mLatency,
1922 outputDesc->mFlags);
1923 if (mPrimaryOutput == 0) {
1924 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1925 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1926 } else {
1927 AudioParameter outputCmd = AudioParameter();
1928 outputCmd.addInt(String8("set_id"), 0);
1929 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1930 addOutput(mPrimaryOutput, outputDesc);
1931 }
1932 }
1933
1934
1935 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1936 }
1937 }
1938 return false;
1939}
1940
Eric Laurente0720872014-03-11 09:30:41 -07001941void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07001942{
1943 {
1944 AutoMutex _l(mLock);
1945 requestExit();
1946 mWaitWorkCV.signal();
1947 }
1948 requestExitAndWait();
1949}
1950
Eric Laurente0720872014-03-11 09:30:41 -07001951int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07001952{
1953 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1954 if (output == mTestOutputs[i]) return i;
1955 }
1956 return 0;
1957}
1958#endif //AUDIO_POLICY_TEST
1959
1960// ---
1961
Eric Laurent1c333e22014-05-20 10:48:17 -07001962void AudioPolicyManager::addOutput(audio_io_handle_t output, AudioOutputDescriptor *outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07001963{
Eric Laurent1c333e22014-05-20 10:48:17 -07001964 outputDesc->mIoHandle = output;
1965 outputDesc->mId = nextUniqueId();
1966 mOutputs.add(output, outputDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001967}
1968
Eric Laurent1c333e22014-05-20 10:48:17 -07001969void AudioPolicyManager::addInput(audio_io_handle_t input, AudioInputDescriptor *inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07001970{
Eric Laurent1c333e22014-05-20 10:48:17 -07001971 inputDesc->mIoHandle = input;
1972 inputDesc->mId = nextUniqueId();
1973 mInputs.add(input, inputDesc);
Eric Laurentd4692962014-05-05 18:13:44 -07001974}
Eric Laurente552edb2014-03-10 17:42:56 -07001975
Eric Laurent3a4311c2014-03-17 12:00:47 -07001976String8 AudioPolicyManager::addressToParameter(audio_devices_t device, const String8 address)
1977{
1978 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
1979 return String8("a2dp_sink_address=")+address;
1980 }
1981 return address;
1982}
1983
Eric Laurente0720872014-03-11 09:30:41 -07001984status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -07001985 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07001986 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07001987 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07001988{
1989 AudioOutputDescriptor *desc;
1990
Eric Laurent3b73df72014-03-11 09:06:29 -07001991 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001992 // first list already open outputs that can be routed to this device
1993 for (size_t i = 0; i < mOutputs.size(); i++) {
1994 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001995 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001996 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
1997 outputs.add(mOutputs.keyAt(i));
1998 }
1999 }
2000 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002001 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002002 for (size_t i = 0; i < mHwModules.size(); i++)
2003 {
2004 if (mHwModules[i]->mHandle == 0) {
2005 continue;
2006 }
2007 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2008 {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002009 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
Eric Laurentd4692962014-05-05 18:13:44 -07002010 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002011 profiles.add(mHwModules[i]->mOutputProfiles[j]);
2012 }
2013 }
2014 }
2015
2016 if (profiles.isEmpty() && outputs.isEmpty()) {
2017 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2018 return BAD_VALUE;
2019 }
2020
2021 // open outputs for matching profiles if needed. Direct outputs are also opened to
2022 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2023 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002024 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002025
2026 // nothing to do if one output is already opened for this profile
2027 size_t j;
2028 for (j = 0; j < mOutputs.size(); j++) {
2029 desc = mOutputs.valueAt(j);
2030 if (!desc->isDuplicated() && desc->mProfile == profile) {
2031 break;
2032 }
2033 }
2034 if (j != mOutputs.size()) {
2035 continue;
2036 }
2037
Eric Laurent3a4311c2014-03-17 12:00:47 -07002038 ALOGV("opening output for device %08x with params %s", device, address.string());
Eric Laurente552edb2014-03-10 17:42:56 -07002039 desc = new AudioOutputDescriptor(profile);
2040 desc->mDevice = device;
2041 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
2042 offloadInfo.sample_rate = desc->mSamplingRate;
2043 offloadInfo.format = desc->mFormat;
2044 offloadInfo.channel_mask = desc->mChannelMask;
2045
2046 audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
2047 &desc->mDevice,
2048 &desc->mSamplingRate,
2049 &desc->mFormat,
2050 &desc->mChannelMask,
2051 &desc->mLatency,
2052 desc->mFlags,
2053 &offloadInfo);
2054 if (output != 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002055 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07002056 if (!address.isEmpty()) {
2057 mpClientInterface->setParameters(output, addressToParameter(device, address));
Eric Laurente552edb2014-03-10 17:42:56 -07002058 }
2059
Eric Laurentd4692962014-05-05 18:13:44 -07002060 // Here is where we step through and resolve any "dynamic" fields
2061 String8 reply;
2062 char *value;
2063 if (profile->mSamplingRates[0] == 0) {
2064 reply = mpClientInterface->getParameters(output,
2065 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2066 ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
2067 reply.string());
2068 value = strpbrk((char *)reply.string(), "=");
2069 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002070 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002071 }
Eric Laurentd4692962014-05-05 18:13:44 -07002072 }
2073 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2074 reply = mpClientInterface->getParameters(output,
2075 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2076 ALOGV("checkOutputsForDevice() direct output sup formats %s",
2077 reply.string());
2078 value = strpbrk((char *)reply.string(), "=");
2079 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002080 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002081 }
Eric Laurentd4692962014-05-05 18:13:44 -07002082 }
2083 if (profile->mChannelMasks[0] == 0) {
2084 reply = mpClientInterface->getParameters(output,
2085 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2086 ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
2087 reply.string());
2088 value = strpbrk((char *)reply.string(), "=");
2089 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002090 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002091 }
Eric Laurentd4692962014-05-05 18:13:44 -07002092 }
2093 if (((profile->mSamplingRates[0] == 0) &&
2094 (profile->mSamplingRates.size() < 2)) ||
2095 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
2096 (profile->mFormats.size() < 2)) ||
2097 ((profile->mChannelMasks[0] == 0) &&
2098 (profile->mChannelMasks.size() < 2))) {
2099 ALOGW("checkOutputsForDevice() direct output missing param");
2100 mpClientInterface->closeOutput(output);
2101 output = 0;
2102 } else if (profile->mSamplingRates[0] == 0) {
2103 mpClientInterface->closeOutput(output);
2104 desc->mSamplingRate = profile->mSamplingRates[1];
2105 offloadInfo.sample_rate = desc->mSamplingRate;
2106 output = mpClientInterface->openOutput(
2107 profile->mModule->mHandle,
2108 &desc->mDevice,
2109 &desc->mSamplingRate,
2110 &desc->mFormat,
2111 &desc->mChannelMask,
2112 &desc->mLatency,
2113 desc->mFlags,
2114 &offloadInfo);
2115 }
2116
2117 if (output != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002118 addOutput(output, desc);
Eric Laurentd4692962014-05-05 18:13:44 -07002119 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
2120 audio_io_handle_t duplicatedOutput = 0;
Eric Laurente552edb2014-03-10 17:42:56 -07002121
Eric Laurentd4692962014-05-05 18:13:44 -07002122 // set initial stream volume for device
2123 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07002124
Eric Laurentd4692962014-05-05 18:13:44 -07002125 //TODO: configure audio effect output stage here
2126
2127 // open a duplicating output thread for the new output and the primary output
2128 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
2129 mPrimaryOutput);
2130 if (duplicatedOutput != 0) {
2131 // add duplicated output descriptor
2132 AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
2133 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
2134 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
2135 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
2136 dupOutputDesc->mFormat = desc->mFormat;
2137 dupOutputDesc->mChannelMask = desc->mChannelMask;
2138 dupOutputDesc->mLatency = desc->mLatency;
2139 addOutput(duplicatedOutput, dupOutputDesc);
2140 applyStreamVolumes(duplicatedOutput, device, 0, true);
2141 } else {
2142 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
2143 mPrimaryOutput, output);
2144 mpClientInterface->closeOutput(output);
2145 mOutputs.removeItem(output);
2146 output = 0;
2147 }
Eric Laurente552edb2014-03-10 17:42:56 -07002148 }
2149 }
2150 }
2151 if (output == 0) {
2152 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
2153 delete desc;
2154 profiles.removeAt(profile_index);
2155 profile_index--;
2156 } else {
2157 outputs.add(output);
2158 ALOGV("checkOutputsForDevice(): adding output %d", output);
2159 }
2160 }
2161
2162 if (profiles.isEmpty()) {
2163 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2164 return BAD_VALUE;
2165 }
Eric Laurentd4692962014-05-05 18:13:44 -07002166 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07002167 // check if one opened output is not needed any more after disconnecting one device
2168 for (size_t i = 0; i < mOutputs.size(); i++) {
2169 desc = mOutputs.valueAt(i);
2170 if (!desc->isDuplicated() &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07002171 !(desc->mProfile->mSupportedDevices.types() &
2172 mAvailableOutputDevices.types())) {
Eric Laurente552edb2014-03-10 17:42:56 -07002173 ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
2174 outputs.add(mOutputs.keyAt(i));
2175 }
2176 }
Eric Laurentd4692962014-05-05 18:13:44 -07002177 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002178 for (size_t i = 0; i < mHwModules.size(); i++)
2179 {
2180 if (mHwModules[i]->mHandle == 0) {
2181 continue;
2182 }
2183 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2184 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002185 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07002186 if (profile->mSupportedDevices.types() & device) {
2187 ALOGV("checkOutputsForDevice(): "
2188 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002189 if (profile->mSamplingRates[0] == 0) {
2190 profile->mSamplingRates.clear();
2191 profile->mSamplingRates.add(0);
2192 }
2193 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2194 profile->mFormats.clear();
2195 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2196 }
2197 if (profile->mChannelMasks[0] == 0) {
2198 profile->mChannelMasks.clear();
2199 profile->mChannelMasks.add(0);
2200 }
2201 }
2202 }
2203 }
2204 }
2205 return NO_ERROR;
2206}
2207
Eric Laurentd4692962014-05-05 18:13:44 -07002208status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
2209 audio_policy_dev_state_t state,
2210 SortedVector<audio_io_handle_t>& inputs,
2211 const String8 address)
2212{
2213 AudioInputDescriptor *desc;
2214 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2215 // first list already open inputs that can be routed to this device
2216 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2217 desc = mInputs.valueAt(input_index);
2218 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
2219 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2220 inputs.add(mInputs.keyAt(input_index));
2221 }
2222 }
2223
2224 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002225 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07002226 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
2227 {
2228 if (mHwModules[module_idx]->mHandle == 0) {
2229 continue;
2230 }
2231 for (size_t profile_index = 0;
2232 profile_index < mHwModules[module_idx]->mInputProfiles.size();
2233 profile_index++)
2234 {
2235 if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types()
2236 & (device & ~AUDIO_DEVICE_BIT_IN)) {
2237 ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2238 profile_index, module_idx);
2239 profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]);
2240 }
2241 }
2242 }
2243
2244 if (profiles.isEmpty() && inputs.isEmpty()) {
2245 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2246 return BAD_VALUE;
2247 }
2248
2249 // open inputs for matching profiles if needed. Direct inputs are also opened to
2250 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2251 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2252
Eric Laurent1c333e22014-05-20 10:48:17 -07002253 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002254 // nothing to do if one input is already opened for this profile
2255 size_t input_index;
2256 for (input_index = 0; input_index < mInputs.size(); input_index++) {
2257 desc = mInputs.valueAt(input_index);
2258 if (desc->mProfile == profile) {
2259 break;
2260 }
2261 }
2262 if (input_index != mInputs.size()) {
2263 continue;
2264 }
2265
2266 ALOGV("opening input for device 0x%X with params %s", device, address.string());
2267 desc = new AudioInputDescriptor(profile);
2268 desc->mDevice = device;
2269
2270 audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2271 &desc->mDevice,
2272 &desc->mSamplingRate,
2273 &desc->mFormat,
2274 &desc->mChannelMask);
2275
2276 if (input != 0) {
2277 if (!address.isEmpty()) {
2278 mpClientInterface->setParameters(input, addressToParameter(device, address));
2279 }
2280
2281 // Here is where we step through and resolve any "dynamic" fields
2282 String8 reply;
2283 char *value;
2284 if (profile->mSamplingRates[0] == 0) {
2285 reply = mpClientInterface->getParameters(input,
2286 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2287 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2288 reply.string());
2289 value = strpbrk((char *)reply.string(), "=");
2290 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002291 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002292 }
2293 }
2294 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2295 reply = mpClientInterface->getParameters(input,
2296 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2297 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2298 value = strpbrk((char *)reply.string(), "=");
2299 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002300 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002301 }
2302 }
2303 if (profile->mChannelMasks[0] == 0) {
2304 reply = mpClientInterface->getParameters(input,
2305 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2306 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2307 reply.string());
2308 value = strpbrk((char *)reply.string(), "=");
2309 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002310 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002311 }
2312 }
2313 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2314 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2315 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2316 ALOGW("checkInputsForDevice() direct input missing param");
2317 mpClientInterface->closeInput(input);
2318 input = 0;
2319 }
2320
2321 if (input != 0) {
2322 addInput(input, desc);
2323 }
2324 } // endif input != 0
2325
2326 if (input == 0) {
2327 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
2328 delete desc;
2329 profiles.removeAt(profile_index);
2330 profile_index--;
2331 } else {
2332 inputs.add(input);
2333 ALOGV("checkInputsForDevice(): adding input %d", input);
2334 }
2335 } // end scan profiles
2336
2337 if (profiles.isEmpty()) {
2338 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2339 return BAD_VALUE;
2340 }
2341 } else {
2342 // Disconnect
2343 // check if one opened input is not needed any more after disconnecting one device
2344 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2345 desc = mInputs.valueAt(input_index);
2346 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) {
2347 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2348 mInputs.keyAt(input_index));
2349 inputs.add(mInputs.keyAt(input_index));
2350 }
2351 }
2352 // Clear any profiles associated with the disconnected device.
2353 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
2354 if (mHwModules[module_index]->mHandle == 0) {
2355 continue;
2356 }
2357 for (size_t profile_index = 0;
2358 profile_index < mHwModules[module_index]->mInputProfiles.size();
2359 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002360 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002361 if (profile->mSupportedDevices.types() & device) {
2362 ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2363 profile_index, module_index);
2364 if (profile->mSamplingRates[0] == 0) {
2365 profile->mSamplingRates.clear();
2366 profile->mSamplingRates.add(0);
2367 }
2368 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2369 profile->mFormats.clear();
2370 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2371 }
2372 if (profile->mChannelMasks[0] == 0) {
2373 profile->mChannelMasks.clear();
2374 profile->mChannelMasks.add(0);
2375 }
2376 }
2377 }
2378 }
2379 } // end disconnect
2380
2381 return NO_ERROR;
2382}
2383
2384
Eric Laurente0720872014-03-11 09:30:41 -07002385void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002386{
2387 ALOGV("closeOutput(%d)", output);
2388
2389 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2390 if (outputDesc == NULL) {
2391 ALOGW("closeOutput() unknown output %d", output);
2392 return;
2393 }
2394
2395 // look for duplicated outputs connected to the output being removed.
2396 for (size_t i = 0; i < mOutputs.size(); i++) {
2397 AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2398 if (dupOutputDesc->isDuplicated() &&
2399 (dupOutputDesc->mOutput1 == outputDesc ||
2400 dupOutputDesc->mOutput2 == outputDesc)) {
2401 AudioOutputDescriptor *outputDesc2;
2402 if (dupOutputDesc->mOutput1 == outputDesc) {
2403 outputDesc2 = dupOutputDesc->mOutput2;
2404 } else {
2405 outputDesc2 = dupOutputDesc->mOutput1;
2406 }
2407 // As all active tracks on duplicated output will be deleted,
2408 // and as they were also referenced on the other output, the reference
2409 // count for their stream type must be adjusted accordingly on
2410 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07002411 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07002412 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07002413 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07002414 }
2415 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2416 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2417
2418 mpClientInterface->closeOutput(duplicatedOutput);
2419 delete mOutputs.valueFor(duplicatedOutput);
2420 mOutputs.removeItem(duplicatedOutput);
2421 }
2422 }
2423
2424 AudioParameter param;
2425 param.add(String8("closing"), String8("true"));
2426 mpClientInterface->setParameters(output, param.toString());
2427
2428 mpClientInterface->closeOutput(output);
2429 delete outputDesc;
2430 mOutputs.removeItem(output);
2431 mPreviousOutputs = mOutputs;
2432}
2433
Eric Laurente0720872014-03-11 09:30:41 -07002434SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07002435 DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2436{
2437 SortedVector<audio_io_handle_t> outputs;
2438
2439 ALOGVV("getOutputsForDevice() device %04x", device);
2440 for (size_t i = 0; i < openOutputs.size(); i++) {
2441 ALOGVV("output %d isDuplicated=%d device=%04x",
2442 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2443 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2444 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2445 outputs.add(openOutputs.keyAt(i));
2446 }
2447 }
2448 return outputs;
2449}
2450
Eric Laurente0720872014-03-11 09:30:41 -07002451bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07002452 SortedVector<audio_io_handle_t>& outputs2)
2453{
2454 if (outputs1.size() != outputs2.size()) {
2455 return false;
2456 }
2457 for (size_t i = 0; i < outputs1.size(); i++) {
2458 if (outputs1[i] != outputs2[i]) {
2459 return false;
2460 }
2461 }
2462 return true;
2463}
2464
Eric Laurente0720872014-03-11 09:30:41 -07002465void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07002466{
2467 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
2468 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2469 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2470 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2471
2472 if (!vectorsEqual(srcOutputs,dstOutputs)) {
2473 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2474 strategy, srcOutputs[0], dstOutputs[0]);
2475 // mute strategy while moving tracks from one output to another
2476 for (size_t i = 0; i < srcOutputs.size(); i++) {
2477 AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
2478 if (desc->isStrategyActive(strategy)) {
2479 setStrategyMute(strategy, true, srcOutputs[i]);
2480 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2481 }
2482 }
2483
2484 // Move effects associated to this strategy from previous output to new output
2485 if (strategy == STRATEGY_MEDIA) {
2486 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
2487 SortedVector<audio_io_handle_t> moved;
2488 for (size_t i = 0; i < mEffects.size(); i++) {
2489 EffectDescriptor *desc = mEffects.valueAt(i);
2490 if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
2491 desc->mIo != fxOutput) {
2492 if (moved.indexOf(desc->mIo) < 0) {
2493 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
2494 mEffects.keyAt(i), fxOutput);
2495 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
2496 fxOutput);
2497 moved.add(desc->mIo);
2498 }
2499 desc->mIo = fxOutput;
2500 }
2501 }
2502 }
2503 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07002504 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2505 if (getStrategy((audio_stream_type_t)i) == strategy) {
2506 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07002507 }
2508 }
2509 }
2510}
2511
Eric Laurente0720872014-03-11 09:30:41 -07002512void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07002513{
2514 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2515 checkOutputForStrategy(STRATEGY_PHONE);
2516 checkOutputForStrategy(STRATEGY_SONIFICATION);
2517 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2518 checkOutputForStrategy(STRATEGY_MEDIA);
2519 checkOutputForStrategy(STRATEGY_DTMF);
2520}
2521
Eric Laurente0720872014-03-11 09:30:41 -07002522audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07002523{
Eric Laurente552edb2014-03-10 17:42:56 -07002524 for (size_t i = 0; i < mOutputs.size(); i++) {
2525 AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2526 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2527 return mOutputs.keyAt(i);
2528 }
2529 }
2530
2531 return 0;
2532}
2533
Eric Laurente0720872014-03-11 09:30:41 -07002534void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07002535{
Eric Laurente552edb2014-03-10 17:42:56 -07002536 audio_io_handle_t a2dpOutput = getA2dpOutput();
2537 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002538 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07002539 return;
2540 }
2541
Eric Laurent3a4311c2014-03-17 12:00:47 -07002542 bool isScoConnected =
2543 (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0;
Eric Laurente552edb2014-03-10 17:42:56 -07002544 // suspend A2DP output if:
2545 // (NOT already suspended) &&
2546 // ((SCO device is connected &&
2547 // (forced usage for communication || for record is SCO))) ||
2548 // (phone state is ringing || in call)
2549 //
2550 // restore A2DP output if:
2551 // (Already suspended) &&
2552 // ((SCO device is NOT connected ||
2553 // (forced usage NOT for communication && NOT for record is SCO))) &&
2554 // (phone state is NOT ringing && NOT in call)
2555 //
2556 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002557 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07002558 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
2559 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
2560 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
2561 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002562
2563 mpClientInterface->restoreOutput(a2dpOutput);
2564 mA2dpSuspended = false;
2565 }
2566 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002567 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07002568 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
2569 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
2570 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
2571 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07002572
2573 mpClientInterface->suspendOutput(a2dpOutput);
2574 mA2dpSuspended = true;
2575 }
2576 }
2577}
2578
Eric Laurent1c333e22014-05-20 10:48:17 -07002579audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07002580{
2581 audio_devices_t device = AUDIO_DEVICE_NONE;
2582
2583 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2584 // check the following by order of priority to request a routing change if necessary:
2585 // 1: the strategy enforced audible is active on the output:
2586 // use device for strategy enforced audible
2587 // 2: we are in call or the strategy phone is active on the output:
2588 // use device for strategy phone
2589 // 3: the strategy sonification is active on the output:
2590 // use device for strategy sonification
2591 // 4: the strategy "respectful" sonification is active on the output:
2592 // use device for strategy "respectful" sonification
2593 // 5: the strategy media is active on the output:
2594 // use device for strategy media
2595 // 6: the strategy DTMF is active on the output:
2596 // use device for strategy DTMF
2597 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2598 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2599 } else if (isInCall() ||
2600 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2601 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
2602 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2603 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
2604 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
2605 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
2606 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2607 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
2608 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2609 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2610 }
2611
Eric Laurent1c333e22014-05-20 10:48:17 -07002612 ALOGV("getNewOutputDevice() selected device %x", device);
2613 return device;
2614}
2615
2616audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
2617{
2618 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
2619 audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource);
2620
2621 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07002622 return device;
2623}
2624
Eric Laurente0720872014-03-11 09:30:41 -07002625uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07002626 return (uint32_t)getStrategy(stream);
2627}
2628
Eric Laurente0720872014-03-11 09:30:41 -07002629audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07002630 audio_devices_t devices;
2631 // By checking the range of stream before calling getStrategy, we avoid
2632 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
2633 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent3b73df72014-03-11 09:06:29 -07002634 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -07002635 devices = AUDIO_DEVICE_NONE;
2636 } else {
Eric Laurente0720872014-03-11 09:30:41 -07002637 AudioPolicyManager::routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07002638 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2639 }
2640 return devices;
2641}
2642
Eric Laurente0720872014-03-11 09:30:41 -07002643AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07002644 audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07002645 // stream to strategy mapping
2646 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07002647 case AUDIO_STREAM_VOICE_CALL:
2648 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07002649 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07002650 case AUDIO_STREAM_RING:
2651 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07002652 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07002653 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07002654 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07002655 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07002656 return STRATEGY_DTMF;
2657 default:
2658 ALOGE("unknown stream type");
Eric Laurent3b73df72014-03-11 09:06:29 -07002659 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07002660 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2661 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07002662 case AUDIO_STREAM_TTS:
2663 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07002664 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07002665 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07002666 return STRATEGY_ENFORCED_AUDIBLE;
2667 }
2668}
2669
Eric Laurente0720872014-03-11 09:30:41 -07002670void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07002671 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07002672 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07002673 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2674 updateDevicesAndOutputs();
2675 break;
2676 default:
2677 break;
2678 }
2679}
2680
Eric Laurente0720872014-03-11 09:30:41 -07002681audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07002682 bool fromCache)
2683{
2684 uint32_t device = AUDIO_DEVICE_NONE;
2685
2686 if (fromCache) {
2687 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
2688 strategy, mDeviceForStrategy[strategy]);
2689 return mDeviceForStrategy[strategy];
2690 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002691 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07002692 switch (strategy) {
2693
2694 case STRATEGY_SONIFICATION_RESPECTFUL:
2695 if (isInCall()) {
2696 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07002697 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07002698 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2699 // while media is playing on a remote device, use the the sonification behavior.
2700 // Note that we test this usecase before testing if media is playing because
2701 // the isStreamActive() method only informs about the activity of a stream, not
2702 // if it's for local playback. Note also that we use the same delay between both tests
2703 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07002704 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002705 // while media is playing (or has recently played), use the same device
2706 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2707 } else {
2708 // when media is not playing anymore, fall back on the sonification behavior
2709 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2710 }
2711
2712 break;
2713
2714 case STRATEGY_DTMF:
2715 if (!isInCall()) {
2716 // when off call, DTMF strategy follows the same rules as MEDIA strategy
2717 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2718 break;
2719 }
2720 // when in call, DTMF and PHONE strategies follow the same rules
2721 // FALL THROUGH
2722
2723 case STRATEGY_PHONE:
2724 // for phone strategy, we first consider the forced use and then the available devices by order
2725 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07002726 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
2727 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07002728 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002729 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002730 if (device) break;
2731 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002732 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002733 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002734 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07002735 if (device) break;
2736 // if SCO device is requested but no SCO device is available, fall back to default case
2737 // FALL THROUGH
2738
2739 default: // FORCE_NONE
2740 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07002741 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07002742 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002743 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002744 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07002745 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002746 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07002747 if (device) break;
2748 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002749 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002750 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002751 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002752 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07002753 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002754 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07002755 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002756 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07002757 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002758 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002759 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002760 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07002761 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002762 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002763 if (device) break;
2764 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002765 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07002766 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07002767 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07002768 if (device == AUDIO_DEVICE_NONE) {
2769 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2770 }
2771 break;
2772
Eric Laurent3b73df72014-03-11 09:06:29 -07002773 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07002774 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2775 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07002776 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07002777 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002778 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002779 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07002780 if (device) break;
2781 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002782 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002783 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07002784 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002785 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07002786 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002787 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002788 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002789 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07002790 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002791 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002792 if (device) break;
2793 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002794 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07002795 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07002796 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07002797 if (device == AUDIO_DEVICE_NONE) {
2798 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2799 }
2800 break;
2801 }
2802 break;
2803
2804 case STRATEGY_SONIFICATION:
2805
2806 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2807 // handleIncallSonification().
2808 if (isInCall()) {
2809 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2810 break;
2811 }
2812 // FALL THROUGH
2813
2814 case STRATEGY_ENFORCED_AUDIBLE:
2815 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2816 // except:
2817 // - when in call where it doesn't default to STRATEGY_PHONE behavior
2818 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
2819
2820 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07002821 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002822 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07002823 if (device == AUDIO_DEVICE_NONE) {
2824 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2825 }
2826 }
2827 // The second device used for sonification is the same as the device used by media strategy
2828 // FALL THROUGH
2829
2830 case STRATEGY_MEDIA: {
2831 uint32_t device2 = AUDIO_DEVICE_NONE;
2832 if (strategy != STRATEGY_SONIFICATION) {
2833 // no sonification on remote submix (e.g. WFD)
Eric Laurent3a4311c2014-03-17 12:00:47 -07002834 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurente552edb2014-03-10 17:42:56 -07002835 }
2836 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07002837 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002838 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002839 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07002840 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002841 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07002842 }
2843 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002844 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07002845 }
2846 }
2847 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002848 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002849 }
2850 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002851 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002852 }
2853 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002854 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07002855 }
2856 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002857 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07002858 }
2859 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002860 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002861 }
2862 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
2863 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07002864 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07002865 }
2866 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07002867 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002868 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07002869 }
2870 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002871 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07002872 }
2873
2874 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2875 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2876 device |= device2;
2877 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07002878 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07002879 if (device == AUDIO_DEVICE_NONE) {
2880 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2881 }
2882 } break;
2883
2884 default:
2885 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2886 break;
2887 }
2888
2889 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2890 return device;
2891}
2892
Eric Laurente0720872014-03-11 09:30:41 -07002893void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07002894{
2895 for (int i = 0; i < NUM_STRATEGIES; i++) {
2896 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
2897 }
2898 mPreviousOutputs = mOutputs;
2899}
2900
Eric Laurente0720872014-03-11 09:30:41 -07002901uint32_t AudioPolicyManager::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07002902 audio_devices_t prevDevice,
2903 uint32_t delayMs)
2904{
2905 // mute/unmute strategies using an incompatible device combination
2906 // if muting, wait for the audio in pcm buffer to be drained before proceeding
2907 // if unmuting, unmute only after the specified delay
2908 if (outputDesc->isDuplicated()) {
2909 return 0;
2910 }
2911
2912 uint32_t muteWaitMs = 0;
2913 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07002914 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07002915
2916 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
2917 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
2918 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
2919 bool doMute = false;
2920
2921 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
2922 doMute = true;
2923 outputDesc->mStrategyMutedByDevice[i] = true;
2924 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
2925 doMute = true;
2926 outputDesc->mStrategyMutedByDevice[i] = false;
2927 }
Eric Laurent99401132014-05-07 19:48:15 -07002928 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07002929 for (size_t j = 0; j < mOutputs.size(); j++) {
2930 AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2931 // skip output if it does not share any device with current output
2932 if ((desc->supportedDevices() & outputDesc->supportedDevices())
2933 == AUDIO_DEVICE_NONE) {
2934 continue;
2935 }
2936 audio_io_handle_t curOutput = mOutputs.keyAt(j);
2937 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
2938 mute ? "muting" : "unmuting", i, curDevice, curOutput);
2939 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
2940 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07002941 if (mute) {
2942 // FIXME: should not need to double latency if volume could be applied
2943 // immediately by the audioflinger mixer. We must account for the delay
2944 // between now and the next time the audioflinger thread for this output
2945 // will process a buffer (which corresponds to one buffer size,
2946 // usually 1/2 or 1/4 of the latency).
2947 if (muteWaitMs < desc->latency() * 2) {
2948 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07002949 }
2950 }
2951 }
2952 }
2953 }
2954 }
2955
Eric Laurent99401132014-05-07 19:48:15 -07002956 // temporary mute output if device selection changes to avoid volume bursts due to
2957 // different per device volumes
2958 if (outputDesc->isActive() && (device != prevDevice)) {
2959 if (muteWaitMs < outputDesc->latency() * 2) {
2960 muteWaitMs = outputDesc->latency() * 2;
2961 }
2962 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
2963 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002964 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07002965 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07002966 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07002967 muteWaitMs *2, device);
2968 }
2969 }
2970 }
2971
Eric Laurente552edb2014-03-10 17:42:56 -07002972 // wait for the PCM output buffers to empty before proceeding with the rest of the command
2973 if (muteWaitMs > delayMs) {
2974 muteWaitMs -= delayMs;
2975 usleep(muteWaitMs * 1000);
2976 return muteWaitMs;
2977 }
2978 return 0;
2979}
2980
Eric Laurente0720872014-03-11 09:30:41 -07002981uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07002982 audio_devices_t device,
2983 bool force,
2984 int delayMs)
2985{
2986 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2987 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2988 AudioParameter param;
2989 uint32_t muteWaitMs;
2990
2991 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002992 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
2993 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07002994 return muteWaitMs;
2995 }
2996 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2997 // output profile
2998 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07002999 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003000 return 0;
3001 }
3002
3003 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07003004 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07003005
3006 audio_devices_t prevDevice = outputDesc->mDevice;
3007
3008 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
3009
3010 if (device != AUDIO_DEVICE_NONE) {
3011 outputDesc->mDevice = device;
3012 }
3013 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
3014
3015 // Do not change the routing if:
3016 // - the requested device is AUDIO_DEVICE_NONE
3017 // - the requested device is the same as current device and force is not specified.
3018 // Doing this check here allows the caller to call setOutputDevice() without conditions
3019 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
3020 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
3021 return muteWaitMs;
3022 }
3023
3024 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07003025
Eric Laurente552edb2014-03-10 17:42:56 -07003026 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07003027 if (device == AUDIO_DEVICE_NONE) {
3028 resetOutputDevice(output, delayMs);
3029 } else {
3030 DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device);
3031 if (!deviceList.isEmpty()) {
3032 struct audio_patch patch;
3033 outputDesc->toAudioPortConfig(&patch.sources[0]);
3034 patch.num_sources = 1;
3035 patch.num_sinks = 0;
3036 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
3037 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
3038 patch.sinks[i].ext.device.hw_module = patch.sources[0].ext.mix.hw_module;
3039 patch.num_sinks++;
3040 }
3041 audio_patch_handle_t patchHandle = outputDesc->mPatchHandle;
3042 status_t status = mpClientInterface->createAudioPatch(&patch,
3043 &patchHandle,
3044 delayMs);
3045 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
3046 "num_sources %d num_sinks %d",
3047 status, patchHandle, patch.num_sources, patch.num_sinks);
3048 if (status == NO_ERROR) {
3049 outputDesc->mPatchHandle = patchHandle;
3050 }
3051 }
3052 }
Eric Laurente552edb2014-03-10 17:42:56 -07003053
3054 // update stream volumes according to new device
3055 applyStreamVolumes(output, device, delayMs);
3056
3057 return muteWaitMs;
3058}
3059
Eric Laurent1c333e22014-05-20 10:48:17 -07003060status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
3061 int delayMs)
3062{
3063 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3064 if (outputDesc->mPatchHandle == 0) {
3065 return INVALID_OPERATION;
3066 }
3067 status_t status = mpClientInterface->releaseAudioPatch(outputDesc->mPatchHandle, delayMs);
3068 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
3069 outputDesc->mPatchHandle = 0;
3070 return status;
3071}
3072
3073status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
3074 audio_devices_t device,
3075 bool force)
3076{
3077 status_t status = NO_ERROR;
3078
3079 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
3080 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
3081 inputDesc->mDevice = device;
3082
3083 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
3084 if (!deviceList.isEmpty()) {
3085 struct audio_patch patch;
3086 inputDesc->toAudioPortConfig(&patch.sinks[0]);
3087 patch.num_sinks = 1;
3088 //only one input device for now
3089 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
3090 patch.sources[0].ext.device.hw_module = patch.sinks[0].ext.mix.hw_module;
3091 patch.num_sources = 1;
3092 audio_patch_handle_t patchHandle = inputDesc->mPatchHandle;
3093 status_t status = mpClientInterface->createAudioPatch(&patch,
3094 &patchHandle,
3095 0);
3096 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
3097 status, patchHandle);
3098 if (status == NO_ERROR) {
3099 inputDesc->mPatchHandle = patchHandle;
3100 }
3101 }
3102 }
3103 return status;
3104}
3105
3106status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input)
3107{
3108 AudioInputDescriptor *inputDesc = mInputs.valueFor(input);
3109 if (inputDesc->mPatchHandle == 0) {
3110 return INVALID_OPERATION;
3111 }
3112 status_t status = mpClientInterface->releaseAudioPatch(inputDesc->mPatchHandle, 0);
3113 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
3114 inputDesc->mPatchHandle = 0;
3115 return status;
3116}
3117
3118sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07003119 uint32_t samplingRate,
3120 audio_format_t format,
3121 audio_channel_mask_t channelMask)
3122{
3123 // Choose an input profile based on the requested capture parameters: select the first available
3124 // profile supporting all requested parameters.
3125
3126 for (size_t i = 0; i < mHwModules.size(); i++)
3127 {
3128 if (mHwModules[i]->mHandle == 0) {
3129 continue;
3130 }
3131 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3132 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003133 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003134 // profile->log();
Eric Laurente552edb2014-03-10 17:42:56 -07003135 if (profile->isCompatibleProfile(device, samplingRate, format,
3136 channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
3137 return profile;
3138 }
3139 }
3140 }
3141 return NULL;
3142}
3143
Eric Laurente0720872014-03-11 09:30:41 -07003144audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
Eric Laurente552edb2014-03-10 17:42:56 -07003145{
3146 uint32_t device = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003147 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
3148 ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003149 switch (inputSource) {
3150 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003151 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003152 device = AUDIO_DEVICE_IN_VOICE_CALL;
3153 break;
3154 }
3155 // FALL THROUGH
3156
3157 case AUDIO_SOURCE_DEFAULT:
3158 case AUDIO_SOURCE_MIC:
3159 case AUDIO_SOURCE_VOICE_RECOGNITION:
3160 case AUDIO_SOURCE_HOTWORD:
3161 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurent3b73df72014-03-11 09:06:29 -07003162 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003163 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003164 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003165 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003166 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07003167 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
3168 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003169 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003170 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3171 }
3172 break;
3173 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003174 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003175 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003176 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003177 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3178 }
3179 break;
3180 case AUDIO_SOURCE_VOICE_DOWNLINK:
3181 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003182 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003183 device = AUDIO_DEVICE_IN_VOICE_CALL;
3184 }
3185 break;
3186 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003187 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07003188 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
3189 }
3190 break;
3191 default:
3192 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
3193 break;
3194 }
3195 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
3196 return device;
3197}
3198
Eric Laurente0720872014-03-11 09:30:41 -07003199bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003200{
3201 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
3202 device &= ~AUDIO_DEVICE_BIT_IN;
3203 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
3204 return true;
3205 }
3206 return false;
3207}
3208
Eric Laurente0720872014-03-11 09:30:41 -07003209audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003210{
3211 for (size_t i = 0; i < mInputs.size(); i++) {
3212 const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
3213 if ((input_descriptor->mRefCount > 0)
3214 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
3215 return mInputs.keyAt(i);
3216 }
3217 }
3218 return 0;
3219}
3220
3221
Eric Laurente0720872014-03-11 09:30:41 -07003222audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003223{
3224 if (device == AUDIO_DEVICE_NONE) {
3225 // this happens when forcing a route update and no track is active on an output.
3226 // In this case the returned category is not important.
3227 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent3b73df72014-03-11 09:06:29 -07003228 } else if (popcount(device) > 1) {
Eric Laurente552edb2014-03-10 17:42:56 -07003229 // Multiple device selection is either:
3230 // - speaker + one other device: give priority to speaker in this case.
3231 // - one A2DP device + another device: happens with duplicated output. In this case
3232 // retain the device on the A2DP output as the other must not correspond to an active
3233 // selection if not the speaker.
3234 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
3235 device = AUDIO_DEVICE_OUT_SPEAKER;
3236 } else {
3237 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
3238 }
3239 }
3240
Eric Laurent3b73df72014-03-11 09:06:29 -07003241 ALOGW_IF(popcount(device) != 1,
Eric Laurente552edb2014-03-10 17:42:56 -07003242 "getDeviceForVolume() invalid device combination: %08x",
3243 device);
3244
3245 return device;
3246}
3247
Eric Laurente0720872014-03-11 09:30:41 -07003248AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003249{
3250 switch(getDeviceForVolume(device)) {
3251 case AUDIO_DEVICE_OUT_EARPIECE:
3252 return DEVICE_CATEGORY_EARPIECE;
3253 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3254 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3255 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3256 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3257 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3258 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3259 return DEVICE_CATEGORY_HEADSET;
3260 case AUDIO_DEVICE_OUT_SPEAKER:
3261 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3262 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3263 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3264 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3265 case AUDIO_DEVICE_OUT_USB_DEVICE:
3266 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3267 default:
3268 return DEVICE_CATEGORY_SPEAKER;
3269 }
3270}
3271
Eric Laurente0720872014-03-11 09:30:41 -07003272float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003273 int indexInUi)
3274{
3275 device_category deviceCategory = getDeviceCategory(device);
3276 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3277
3278 // the volume index in the UI is relative to the min and max volume indices for this stream type
3279 int nbSteps = 1 + curve[VOLMAX].mIndex -
3280 curve[VOLMIN].mIndex;
3281 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3282 (streamDesc.mIndexMax - streamDesc.mIndexMin);
3283
3284 // find what part of the curve this index volume belongs to, or if it's out of bounds
3285 int segment = 0;
3286 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
3287 return 0.0f;
3288 } else if (volIdx < curve[VOLKNEE1].mIndex) {
3289 segment = 0;
3290 } else if (volIdx < curve[VOLKNEE2].mIndex) {
3291 segment = 1;
3292 } else if (volIdx <= curve[VOLMAX].mIndex) {
3293 segment = 2;
3294 } else { // out of bounds
3295 return 1.0f;
3296 }
3297
3298 // linear interpolation in the attenuation table in dB
3299 float decibels = curve[segment].mDBAttenuation +
3300 ((float)(volIdx - curve[segment].mIndex)) *
3301 ( (curve[segment+1].mDBAttenuation -
3302 curve[segment].mDBAttenuation) /
3303 ((float)(curve[segment+1].mIndex -
3304 curve[segment].mIndex)) );
3305
3306 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3307
3308 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3309 curve[segment].mIndex, volIdx,
3310 curve[segment+1].mIndex,
3311 curve[segment].mDBAttenuation,
3312 decibels,
3313 curve[segment+1].mDBAttenuation,
3314 amplification);
3315
3316 return amplification;
3317}
3318
Eric Laurente0720872014-03-11 09:30:41 -07003319const AudioPolicyManager::VolumeCurvePoint
3320 AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003321 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3322};
3323
Eric Laurente0720872014-03-11 09:30:41 -07003324const AudioPolicyManager::VolumeCurvePoint
3325 AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003326 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3327};
3328
Eric Laurente0720872014-03-11 09:30:41 -07003329const AudioPolicyManager::VolumeCurvePoint
3330 AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003331 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3332};
3333
Eric Laurente0720872014-03-11 09:30:41 -07003334const AudioPolicyManager::VolumeCurvePoint
3335 AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003336 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3337};
3338
Eric Laurente0720872014-03-11 09:30:41 -07003339const AudioPolicyManager::VolumeCurvePoint
3340 AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003341 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
3342};
3343
3344// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
3345// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
3346// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
3347// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
3348
Eric Laurente0720872014-03-11 09:30:41 -07003349const AudioPolicyManager::VolumeCurvePoint
3350 AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003351 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
3352};
3353
Eric Laurente0720872014-03-11 09:30:41 -07003354const AudioPolicyManager::VolumeCurvePoint
3355 AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003356 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
3357};
3358
Eric Laurente0720872014-03-11 09:30:41 -07003359const AudioPolicyManager::VolumeCurvePoint
3360 AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003361 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
3362};
3363
Eric Laurente0720872014-03-11 09:30:41 -07003364const AudioPolicyManager::VolumeCurvePoint
3365 AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003366 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
3367};
3368
Eric Laurente0720872014-03-11 09:30:41 -07003369const AudioPolicyManager::VolumeCurvePoint
3370 AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003371 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
3372};
3373
Eric Laurente0720872014-03-11 09:30:41 -07003374const AudioPolicyManager::VolumeCurvePoint
3375 *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT]
3376 [AudioPolicyManager::DEVICE_CATEGORY_CNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003377 { // AUDIO_STREAM_VOICE_CALL
3378 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
3379 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3380 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
3381 },
3382 { // AUDIO_STREAM_SYSTEM
3383 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3384 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3385 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
3386 },
3387 { // AUDIO_STREAM_RING
3388 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3389 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3390 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
3391 },
3392 { // AUDIO_STREAM_MUSIC
3393 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3394 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3395 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
3396 },
3397 { // AUDIO_STREAM_ALARM
3398 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3399 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3400 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
3401 },
3402 { // AUDIO_STREAM_NOTIFICATION
3403 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3404 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3405 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
3406 },
3407 { // AUDIO_STREAM_BLUETOOTH_SCO
3408 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
3409 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3410 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
3411 },
3412 { // AUDIO_STREAM_ENFORCED_AUDIBLE
3413 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3414 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3415 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
3416 },
3417 { // AUDIO_STREAM_DTMF
3418 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3419 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3420 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
3421 },
3422 { // AUDIO_STREAM_TTS
3423 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3424 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3425 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
3426 },
3427};
3428
Eric Laurente0720872014-03-11 09:30:41 -07003429void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07003430{
3431 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3432 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
3433 mStreams[i].mVolumeCurve[j] =
3434 sVolumeProfiles[i][j];
3435 }
3436 }
3437
3438 // Check availability of DRC on speaker path: if available, override some of the speaker curves
3439 if (mSpeakerDrcEnabled) {
3440 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
3441 sDefaultSystemVolumeCurveDrc;
3442 mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
3443 sSpeakerSonificationVolumeCurveDrc;
3444 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
3445 sSpeakerSonificationVolumeCurveDrc;
3446 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
3447 sSpeakerSonificationVolumeCurveDrc;
3448 }
3449}
3450
Eric Laurente0720872014-03-11 09:30:41 -07003451float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07003452 int index,
3453 audio_io_handle_t output,
3454 audio_devices_t device)
3455{
3456 float volume = 1.0;
3457 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3458 StreamDescriptor &streamDesc = mStreams[stream];
3459
3460 if (device == AUDIO_DEVICE_NONE) {
3461 device = outputDesc->device();
3462 }
3463
3464 // if volume is not 0 (not muted), force media volume to max on digital output
Eric Laurent3b73df72014-03-11 09:06:29 -07003465 if (stream == AUDIO_STREAM_MUSIC &&
Eric Laurente552edb2014-03-10 17:42:56 -07003466 index != mStreams[stream].mIndexMin &&
3467 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
3468 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
3469 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
3470 device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
3471 return 1.0;
3472 }
3473
3474 volume = volIndexToAmpl(device, streamDesc, index);
3475
3476 // if a headset is connected, apply the following rules to ring tones and notifications
3477 // to avoid sound level bursts in user's ears:
3478 // - always attenuate ring tones and notifications volume by 6dB
3479 // - if music is playing, always limit the volume to current music volume,
3480 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07003481 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003482 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
3483 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
3484 AUDIO_DEVICE_OUT_WIRED_HEADSET |
3485 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
3486 ((stream_strategy == STRATEGY_SONIFICATION)
3487 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07003488 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07003489 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003490 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003491 streamDesc.mCanBeMuted) {
3492 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
3493 // when the phone is ringing we must consider that music could have been paused just before
3494 // by the music application and behave as if music was active if the last music track was
3495 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07003496 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07003497 mLimitRingtoneVolume) {
3498 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003499 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
3500 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07003501 output,
3502 musicDevice);
3503 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
3504 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
3505 if (volume > minVol) {
3506 volume = minVol;
3507 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
3508 }
3509 }
3510 }
3511
3512 return volume;
3513}
3514
Eric Laurente0720872014-03-11 09:30:41 -07003515status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07003516 int index,
3517 audio_io_handle_t output,
3518 audio_devices_t device,
3519 int delayMs,
3520 bool force)
3521{
3522
3523 // do not change actual stream volume if the stream is muted
3524 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
3525 ALOGVV("checkAndSetVolume() stream %d muted count %d",
3526 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
3527 return NO_ERROR;
3528 }
3529
3530 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07003531 if ((stream == AUDIO_STREAM_VOICE_CALL &&
3532 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
3533 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
3534 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003535 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07003536 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07003537 return INVALID_OPERATION;
3538 }
3539
3540 float volume = computeVolume(stream, index, output, device);
3541 // We actually change the volume if:
3542 // - the float value returned by computeVolume() changed
3543 // - the force flag is set
3544 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
3545 force) {
3546 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
3547 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
3548 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
3549 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07003550 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
3551 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003552 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003553 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003554 }
3555
Eric Laurent3b73df72014-03-11 09:06:29 -07003556 if (stream == AUDIO_STREAM_VOICE_CALL ||
3557 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07003558 float voiceVolume;
3559 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07003560 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003561 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
3562 } else {
3563 voiceVolume = 1.0;
3564 }
3565
3566 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
3567 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
3568 mLastVoiceVolume = voiceVolume;
3569 }
3570 }
3571
3572 return NO_ERROR;
3573}
3574
Eric Laurente0720872014-03-11 09:30:41 -07003575void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07003576 audio_devices_t device,
3577 int delayMs,
3578 bool force)
3579{
3580 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
3581
Eric Laurent3b73df72014-03-11 09:06:29 -07003582 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
3583 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07003584 mStreams[stream].getVolumeIndex(device),
3585 output,
3586 device,
3587 delayMs,
3588 force);
3589 }
3590}
3591
Eric Laurente0720872014-03-11 09:30:41 -07003592void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07003593 bool on,
3594 audio_io_handle_t output,
3595 int delayMs,
3596 audio_devices_t device)
3597{
3598 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07003599 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
3600 if (getStrategy((audio_stream_type_t)stream) == strategy) {
3601 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07003602 }
3603 }
3604}
3605
Eric Laurente0720872014-03-11 09:30:41 -07003606void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07003607 bool on,
3608 audio_io_handle_t output,
3609 int delayMs,
3610 audio_devices_t device)
3611{
3612 StreamDescriptor &streamDesc = mStreams[stream];
3613 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3614 if (device == AUDIO_DEVICE_NONE) {
3615 device = outputDesc->device();
3616 }
3617
3618 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
3619 stream, on, output, outputDesc->mMuteCount[stream], device);
3620
3621 if (on) {
3622 if (outputDesc->mMuteCount[stream] == 0) {
3623 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003624 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
3625 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003626 checkAndSetVolume(stream, 0, output, device, delayMs);
3627 }
3628 }
3629 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3630 outputDesc->mMuteCount[stream]++;
3631 } else {
3632 if (outputDesc->mMuteCount[stream] == 0) {
3633 ALOGV("setStreamMute() unmuting non muted stream!");
3634 return;
3635 }
3636 if (--outputDesc->mMuteCount[stream] == 0) {
3637 checkAndSetVolume(stream,
3638 streamDesc.getVolumeIndex(device),
3639 output,
3640 device,
3641 delayMs);
3642 }
3643 }
3644}
3645
Eric Laurente0720872014-03-11 09:30:41 -07003646void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07003647 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07003648{
3649 // if the stream pertains to sonification strategy and we are in call we must
3650 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3651 // in the device used for phone strategy and play the tone if the selected device does not
3652 // interfere with the device used for phone strategy
3653 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3654 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07003655 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07003656 if ((stream_strategy == STRATEGY_SONIFICATION) ||
3657 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3658 AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
3659 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3660 stream, starting, outputDesc->mDevice, stateChange);
3661 if (outputDesc->mRefCount[stream]) {
3662 int muteCount = 1;
3663 if (stateChange) {
3664 muteCount = outputDesc->mRefCount[stream];
3665 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003666 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003667 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3668 for (int i = 0; i < muteCount; i++) {
3669 setStreamMute(stream, starting, mPrimaryOutput);
3670 }
3671 } else {
3672 ALOGV("handleIncallSonification() high visibility");
3673 if (outputDesc->device() &
3674 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
3675 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3676 for (int i = 0; i < muteCount; i++) {
3677 setStreamMute(stream, starting, mPrimaryOutput);
3678 }
3679 }
3680 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003681 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
3682 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07003683 } else {
3684 mpClientInterface->stopTone();
3685 }
3686 }
3687 }
3688 }
3689}
3690
Eric Laurente0720872014-03-11 09:30:41 -07003691bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07003692{
3693 return isStateInCall(mPhoneState);
3694}
3695
Eric Laurente0720872014-03-11 09:30:41 -07003696bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003697 return ((state == AUDIO_MODE_IN_CALL) ||
3698 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07003699}
3700
Eric Laurente0720872014-03-11 09:30:41 -07003701uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07003702{
3703 return MAX_EFFECTS_CPU_LOAD;
3704}
3705
Eric Laurente0720872014-03-11 09:30:41 -07003706uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07003707{
3708 return MAX_EFFECTS_MEMORY;
3709}
3710
3711// --- AudioOutputDescriptor class implementation
3712
Eric Laurente0720872014-03-11 09:30:41 -07003713AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor(
Eric Laurent1c333e22014-05-20 10:48:17 -07003714 const sp<IOProfile>& profile)
3715 : mId(0), mIoHandle(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT),
Eric Laurente552edb2014-03-10 17:42:56 -07003716 mChannelMask(0), mLatency(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07003717 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0),
Eric Laurente552edb2014-03-10 17:42:56 -07003718 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3719{
3720 // clear usage count for all stream types
Eric Laurent3b73df72014-03-11 09:06:29 -07003721 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003722 mRefCount[i] = 0;
3723 mCurVolume[i] = -1.0;
3724 mMuteCount[i] = 0;
3725 mStopTime[i] = 0;
3726 }
3727 for (int i = 0; i < NUM_STRATEGIES; i++) {
3728 mStrategyMutedByDevice[i] = false;
3729 }
3730 if (profile != NULL) {
3731 mSamplingRate = profile->mSamplingRates[0];
3732 mFormat = profile->mFormats[0];
3733 mChannelMask = profile->mChannelMasks[0];
3734 mFlags = profile->mFlags;
3735 }
3736}
3737
Eric Laurente0720872014-03-11 09:30:41 -07003738audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const
Eric Laurente552edb2014-03-10 17:42:56 -07003739{
3740 if (isDuplicated()) {
3741 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3742 } else {
3743 return mDevice;
3744 }
3745}
3746
Eric Laurente0720872014-03-11 09:30:41 -07003747uint32_t AudioPolicyManager::AudioOutputDescriptor::latency()
Eric Laurente552edb2014-03-10 17:42:56 -07003748{
3749 if (isDuplicated()) {
3750 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
3751 } else {
3752 return mLatency;
3753 }
3754}
3755
Eric Laurente0720872014-03-11 09:30:41 -07003756bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith(
Eric Laurente552edb2014-03-10 17:42:56 -07003757 const AudioOutputDescriptor *outputDesc)
3758{
3759 if (isDuplicated()) {
3760 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
3761 } else if (outputDesc->isDuplicated()){
3762 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
3763 } else {
3764 return (mProfile->mModule == outputDesc->mProfile->mModule);
3765 }
3766}
3767
Eric Laurente0720872014-03-11 09:30:41 -07003768void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07003769 int delta)
Eric Laurente552edb2014-03-10 17:42:56 -07003770{
3771 // forward usage count change to attached outputs
3772 if (isDuplicated()) {
3773 mOutput1->changeRefCount(stream, delta);
3774 mOutput2->changeRefCount(stream, delta);
3775 }
3776 if ((delta + (int)mRefCount[stream]) < 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003777 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d",
3778 delta, stream, mRefCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07003779 mRefCount[stream] = 0;
3780 return;
3781 }
3782 mRefCount[stream] += delta;
3783 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3784}
3785
Eric Laurente0720872014-03-11 09:30:41 -07003786audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices()
Eric Laurente552edb2014-03-10 17:42:56 -07003787{
3788 if (isDuplicated()) {
3789 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3790 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003791 return mProfile->mSupportedDevices.types() ;
Eric Laurente552edb2014-03-10 17:42:56 -07003792 }
3793}
3794
Eric Laurente0720872014-03-11 09:30:41 -07003795bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07003796{
3797 return isStrategyActive(NUM_STRATEGIES, inPastMs);
3798}
3799
Eric Laurente0720872014-03-11 09:30:41 -07003800bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07003801 uint32_t inPastMs,
3802 nsecs_t sysTime) const
3803{
3804 if ((sysTime == 0) && (inPastMs != 0)) {
3805 sysTime = systemTime();
3806 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003807 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
3808 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
Eric Laurente552edb2014-03-10 17:42:56 -07003809 (NUM_STRATEGIES == strategy)) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003810 isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003811 return true;
3812 }
3813 }
3814 return false;
3815}
3816
Eric Laurente0720872014-03-11 09:30:41 -07003817bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07003818 uint32_t inPastMs,
3819 nsecs_t sysTime) const
3820{
3821 if (mRefCount[stream] != 0) {
3822 return true;
3823 }
3824 if (inPastMs == 0) {
3825 return false;
3826 }
3827 if (sysTime == 0) {
3828 sysTime = systemTime();
3829 }
3830 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
3831 return true;
3832 }
3833 return false;
3834}
3835
Eric Laurent1c333e22014-05-20 10:48:17 -07003836void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig(
3837 struct audio_port_config *config) const
3838{
3839 config->id = mId;
3840 config->role = AUDIO_PORT_ROLE_SOURCE;
3841 config->type = AUDIO_PORT_TYPE_MIX;
3842 config->sample_rate = mSamplingRate;
3843 config->channel_mask = mChannelMask;
3844 config->format = mFormat;
3845 config->gain.index = -1;
3846 config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
3847 AUDIO_PORT_CONFIG_FORMAT;
3848 config->ext.mix.hw_module = mProfile->mModule->mHandle;
3849 config->ext.mix.handle = mIoHandle;
3850 config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
3851}
3852
3853void AudioPolicyManager::AudioOutputDescriptor::toAudioPort(
3854 struct audio_port *port) const
3855{
3856 mProfile->toAudioPort(port);
3857 port->id = mId;
3858 port->ext.mix.handle = mIoHandle;
3859 port->ext.mix.latency_class =
3860 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL;
3861}
Eric Laurente552edb2014-03-10 17:42:56 -07003862
Eric Laurente0720872014-03-11 09:30:41 -07003863status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07003864{
3865 const size_t SIZE = 256;
3866 char buffer[SIZE];
3867 String8 result;
3868
3869 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3870 result.append(buffer);
3871 snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3872 result.append(buffer);
3873 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3874 result.append(buffer);
3875 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3876 result.append(buffer);
3877 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3878 result.append(buffer);
3879 snprintf(buffer, SIZE, " Devices %08x\n", device());
3880 result.append(buffer);
3881 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3882 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07003883 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
3884 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n",
3885 i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07003886 result.append(buffer);
3887 }
3888 write(fd, result.string(), result.size());
3889
3890 return NO_ERROR;
3891}
3892
3893// --- AudioInputDescriptor class implementation
3894
Eric Laurent1c333e22014-05-20 10:48:17 -07003895AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
3896 : mId(0), mIoHandle(0), mSamplingRate(0),
3897 mFormat(AUDIO_FORMAT_DEFAULT), mChannelMask(0),
3898 mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0),
Eric Laurent3b73df72014-03-11 09:06:29 -07003899 mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile)
Eric Laurente552edb2014-03-10 17:42:56 -07003900{
Eric Laurent3a4311c2014-03-17 12:00:47 -07003901 if (profile != NULL) {
3902 mSamplingRate = profile->mSamplingRates[0];
3903 mFormat = profile->mFormats[0];
3904 mChannelMask = profile->mChannelMasks[0];
3905 }
Eric Laurente552edb2014-03-10 17:42:56 -07003906}
3907
Eric Laurent1c333e22014-05-20 10:48:17 -07003908void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig(
3909 struct audio_port_config *config) const
3910{
3911 config->id = mId;
3912 config->role = AUDIO_PORT_ROLE_SINK;
3913 config->type = AUDIO_PORT_TYPE_MIX;
3914 config->sample_rate = mSamplingRate;
3915 config->channel_mask = mChannelMask;
3916 config->format = mFormat;
3917 config->gain.index = -1;
3918 config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
3919 AUDIO_PORT_CONFIG_FORMAT;
3920 config->ext.mix.hw_module = mProfile->mModule->mHandle;
3921 config->ext.mix.handle = mIoHandle;
3922 config->ext.mix.usecase.source = (mInputSource == AUDIO_SOURCE_HOTWORD) ?
3923 AUDIO_SOURCE_VOICE_RECOGNITION : mInputSource;
3924}
3925
3926void AudioPolicyManager::AudioInputDescriptor::toAudioPort(
3927 struct audio_port *port) const
3928{
3929 mProfile->toAudioPort(port);
3930 port->id = mId;
3931 port->ext.mix.handle = mIoHandle;
3932 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
3933}
3934
Eric Laurente0720872014-03-11 09:30:41 -07003935status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07003936{
3937 const size_t SIZE = 256;
3938 char buffer[SIZE];
3939 String8 result;
3940
3941 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3942 result.append(buffer);
3943 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3944 result.append(buffer);
3945 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3946 result.append(buffer);
3947 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3948 result.append(buffer);
3949 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3950 result.append(buffer);
3951 write(fd, result.string(), result.size());
3952
3953 return NO_ERROR;
3954}
3955
3956// --- StreamDescriptor class implementation
3957
Eric Laurente0720872014-03-11 09:30:41 -07003958AudioPolicyManager::StreamDescriptor::StreamDescriptor()
Eric Laurente552edb2014-03-10 17:42:56 -07003959 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3960{
3961 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3962}
3963
Eric Laurente0720872014-03-11 09:30:41 -07003964int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003965{
Eric Laurente0720872014-03-11 09:30:41 -07003966 device = AudioPolicyManager::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07003967 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3968 if (mIndexCur.indexOfKey(device) < 0) {
3969 device = AUDIO_DEVICE_OUT_DEFAULT;
3970 }
3971 return mIndexCur.valueFor(device);
3972}
3973
Eric Laurente0720872014-03-11 09:30:41 -07003974void AudioPolicyManager::StreamDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07003975{
3976 const size_t SIZE = 256;
3977 char buffer[SIZE];
3978 String8 result;
3979
3980 snprintf(buffer, SIZE, "%s %02d %02d ",
3981 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3982 result.append(buffer);
3983 for (size_t i = 0; i < mIndexCur.size(); i++) {
3984 snprintf(buffer, SIZE, "%04x : %02d, ",
3985 mIndexCur.keyAt(i),
3986 mIndexCur.valueAt(i));
3987 result.append(buffer);
3988 }
3989 result.append("\n");
3990
3991 write(fd, result.string(), result.size());
3992}
3993
3994// --- EffectDescriptor class implementation
3995
Eric Laurente0720872014-03-11 09:30:41 -07003996status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07003997{
3998 const size_t SIZE = 256;
3999 char buffer[SIZE];
4000 String8 result;
4001
4002 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
4003 result.append(buffer);
4004 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
4005 result.append(buffer);
4006 snprintf(buffer, SIZE, " Session: %d\n", mSession);
4007 result.append(buffer);
4008 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
4009 result.append(buffer);
4010 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
4011 result.append(buffer);
4012 write(fd, result.string(), result.size());
4013
4014 return NO_ERROR;
4015}
4016
Eric Laurent1c333e22014-05-20 10:48:17 -07004017// --- HwModule class implementation
Eric Laurente552edb2014-03-10 17:42:56 -07004018
Eric Laurente0720872014-03-11 09:30:41 -07004019AudioPolicyManager::HwModule::HwModule(const char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07004020 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
4021{
4022}
4023
Eric Laurente0720872014-03-11 09:30:41 -07004024AudioPolicyManager::HwModule::~HwModule()
Eric Laurente552edb2014-03-10 17:42:56 -07004025{
4026 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004027 mOutputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004028 }
4029 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004030 mInputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004031 }
4032 free((void *)mName);
4033}
4034
Eric Laurente0720872014-03-11 09:30:41 -07004035void AudioPolicyManager::HwModule::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004036{
4037 const size_t SIZE = 256;
4038 char buffer[SIZE];
4039 String8 result;
4040
4041 snprintf(buffer, SIZE, " - name: %s\n", mName);
4042 result.append(buffer);
4043 snprintf(buffer, SIZE, " - handle: %d\n", mHandle);
4044 result.append(buffer);
4045 write(fd, result.string(), result.size());
4046 if (mOutputProfiles.size()) {
4047 write(fd, " - outputs:\n", strlen(" - outputs:\n"));
4048 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004049 snprintf(buffer, SIZE, " output %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004050 write(fd, buffer, strlen(buffer));
4051 mOutputProfiles[i]->dump(fd);
4052 }
4053 }
4054 if (mInputProfiles.size()) {
4055 write(fd, " - inputs:\n", strlen(" - inputs:\n"));
4056 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004057 snprintf(buffer, SIZE, " input %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004058 write(fd, buffer, strlen(buffer));
4059 mInputProfiles[i]->dump(fd);
4060 }
4061 }
4062}
4063
Eric Laurent1c333e22014-05-20 10:48:17 -07004064// --- AudioPort class implementation
4065
4066void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const
4067{
4068 port->role = mRole;
4069 port->type = mType;
4070 unsigned int i;
4071 for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) {
4072 port->sample_rates[i] = mSamplingRates[i];
4073 }
4074 port->num_sample_rates = i;
4075 for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) {
4076 port->channel_masks[i] = mChannelMasks[i];
4077 }
4078 port->num_channel_masks = i;
4079 for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) {
4080 port->formats[i] = mFormats[i];
4081 }
4082 port->num_formats = i;
4083 port->num_gains = 0;
4084}
4085
4086
4087void AudioPolicyManager::AudioPort::loadSamplingRates(char *name)
4088{
4089 char *str = strtok(name, "|");
4090
4091 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
4092 // rates should be read from the output stream after it is opened for the first time
4093 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4094 mSamplingRates.add(0);
4095 return;
4096 }
4097
4098 while (str != NULL) {
4099 uint32_t rate = atoi(str);
4100 if (rate != 0) {
4101 ALOGV("loadSamplingRates() adding rate %d", rate);
4102 mSamplingRates.add(rate);
4103 }
4104 str = strtok(NULL, "|");
4105 }
4106 return;
4107}
4108
4109void AudioPolicyManager::AudioPort::loadFormats(char *name)
4110{
4111 char *str = strtok(name, "|");
4112
4113 // by convention, "0' in the first entry in mFormats indicates the supported formats
4114 // should be read from the output stream after it is opened for the first time
4115 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4116 mFormats.add(AUDIO_FORMAT_DEFAULT);
4117 return;
4118 }
4119
4120 while (str != NULL) {
4121 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
4122 ARRAY_SIZE(sFormatNameToEnumTable),
4123 str);
4124 if (format != AUDIO_FORMAT_DEFAULT) {
4125 mFormats.add(format);
4126 }
4127 str = strtok(NULL, "|");
4128 }
4129 return;
4130}
4131
4132void AudioPolicyManager::AudioPort::loadInChannels(char *name)
4133{
4134 const char *str = strtok(name, "|");
4135
4136 ALOGV("loadInChannels() %s", name);
4137
4138 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4139 mChannelMasks.add(0);
4140 return;
4141 }
4142
4143 while (str != NULL) {
4144 audio_channel_mask_t channelMask =
4145 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
4146 ARRAY_SIZE(sInChannelsNameToEnumTable),
4147 str);
4148 if (channelMask != 0) {
4149 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
4150 mChannelMasks.add(channelMask);
4151 }
4152 str = strtok(NULL, "|");
4153 }
4154 return;
4155}
4156
4157void AudioPolicyManager::AudioPort::loadOutChannels(char *name)
4158{
4159 const char *str = strtok(name, "|");
4160
4161 ALOGV("loadOutChannels() %s", name);
4162
4163 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
4164 // masks should be read from the output stream after it is opened for the first time
4165 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4166 mChannelMasks.add(0);
4167 return;
4168 }
4169
4170 while (str != NULL) {
4171 audio_channel_mask_t channelMask =
4172 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
4173 ARRAY_SIZE(sOutChannelsNameToEnumTable),
4174 str);
4175 if (channelMask != 0) {
4176 mChannelMasks.add(channelMask);
4177 }
4178 str = strtok(NULL, "|");
4179 }
4180 return;
4181}
4182
4183// --- IOProfile class implementation
4184
4185AudioPolicyManager::IOProfile::IOProfile(audio_port_role_t role, HwModule *module)
4186 : AudioPort(AUDIO_PORT_TYPE_MIX, role, module), mFlags((audio_output_flags_t)0)
Eric Laurente552edb2014-03-10 17:42:56 -07004187{
4188}
4189
Eric Laurente0720872014-03-11 09:30:41 -07004190AudioPolicyManager::IOProfile::~IOProfile()
Eric Laurente552edb2014-03-10 17:42:56 -07004191{
4192}
4193
4194// checks if the IO profile is compatible with specified parameters.
4195// Sampling rate, format and channel mask must be specified in order to
4196// get a valid a match
Eric Laurente0720872014-03-11 09:30:41 -07004197bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07004198 uint32_t samplingRate,
4199 audio_format_t format,
4200 audio_channel_mask_t channelMask,
4201 audio_output_flags_t flags) const
4202{
4203 if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
4204 return false;
4205 }
4206
Eric Laurent3a4311c2014-03-17 12:00:47 -07004207 if ((mSupportedDevices.types() & device) != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07004208 return false;
4209 }
4210 if ((mFlags & flags) != flags) {
4211 return false;
4212 }
4213 size_t i;
4214 for (i = 0; i < mSamplingRates.size(); i++)
4215 {
4216 if (mSamplingRates[i] == samplingRate) {
4217 break;
4218 }
4219 }
4220 if (i == mSamplingRates.size()) {
4221 return false;
4222 }
4223 for (i = 0; i < mFormats.size(); i++)
4224 {
4225 if (mFormats[i] == format) {
4226 break;
4227 }
4228 }
4229 if (i == mFormats.size()) {
4230 return false;
4231 }
4232 for (i = 0; i < mChannelMasks.size(); i++)
4233 {
4234 if (mChannelMasks[i] == channelMask) {
4235 break;
4236 }
4237 }
4238 if (i == mChannelMasks.size()) {
4239 return false;
4240 }
4241 return true;
4242}
4243
Eric Laurente0720872014-03-11 09:30:41 -07004244void AudioPolicyManager::IOProfile::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004245{
4246 const size_t SIZE = 256;
4247 char buffer[SIZE];
4248 String8 result;
4249
4250 snprintf(buffer, SIZE, " - sampling rates: ");
4251 result.append(buffer);
4252 for (size_t i = 0; i < mSamplingRates.size(); i++) {
4253 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
4254 result.append(buffer);
4255 result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
4256 }
4257
4258 snprintf(buffer, SIZE, " - channel masks: ");
4259 result.append(buffer);
4260 for (size_t i = 0; i < mChannelMasks.size(); i++) {
4261 snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
4262 result.append(buffer);
4263 result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
4264 }
4265
4266 snprintf(buffer, SIZE, " - formats: ");
4267 result.append(buffer);
4268 for (size_t i = 0; i < mFormats.size(); i++) {
4269 snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
4270 result.append(buffer);
4271 result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
4272 }
4273
Eric Laurent3a4311c2014-03-17 12:00:47 -07004274 snprintf(buffer, SIZE, " - devices:\n");
Eric Laurente552edb2014-03-10 17:42:56 -07004275 result.append(buffer);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004276 write(fd, result.string(), result.size());
4277 DeviceDescriptor::dumpHeader(fd, 6);
4278 for (size_t i = 0; i < mSupportedDevices.size(); i++) {
4279 mSupportedDevices[i]->dump(fd, 6);
4280 }
4281
Eric Laurente552edb2014-03-10 17:42:56 -07004282 snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags);
4283 result.append(buffer);
4284
4285 write(fd, result.string(), result.size());
4286}
4287
Eric Laurentd4692962014-05-05 18:13:44 -07004288void AudioPolicyManager::IOProfile::log()
4289{
4290 const size_t SIZE = 256;
4291 char buffer[SIZE];
4292 String8 result;
4293
4294 ALOGV(" - sampling rates: ");
4295 for (size_t i = 0; i < mSamplingRates.size(); i++) {
4296 ALOGV(" %d", mSamplingRates[i]);
4297 }
4298
4299 ALOGV(" - channel masks: ");
4300 for (size_t i = 0; i < mChannelMasks.size(); i++) {
4301 ALOGV(" 0x%04x", mChannelMasks[i]);
4302 }
4303
4304 ALOGV(" - formats: ");
4305 for (size_t i = 0; i < mFormats.size(); i++) {
4306 ALOGV(" 0x%08x", mFormats[i]);
4307 }
4308
4309 ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types());
4310 ALOGV(" - flags: 0x%04x\n", mFlags);
4311}
4312
4313
Eric Laurent3a4311c2014-03-17 12:00:47 -07004314// --- DeviceDescriptor implementation
Eric Laurente552edb2014-03-10 17:42:56 -07004315
Eric Laurent3a4311c2014-03-17 12:00:47 -07004316bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
Eric Laurente552edb2014-03-10 17:42:56 -07004317{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004318 // Devices are considered equal if they:
4319 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
4320 // - have the same address or one device does not specify the address
4321 // - have the same channel mask or one device does not specify the channel mask
Eric Laurent1c333e22014-05-20 10:48:17 -07004322 return (mDeviceType == other->mDeviceType) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004323 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
Eric Laurent2f8a36f2014-03-26 19:05:55 -07004324 (mChannelMask == 0 || other->mChannelMask == 0 ||
Eric Laurent3a4311c2014-03-17 12:00:47 -07004325 mChannelMask == other->mChannelMask);
4326}
4327
4328void AudioPolicyManager::DeviceVector::refreshTypes()
4329{
Eric Laurent1c333e22014-05-20 10:48:17 -07004330 mDeviceTypes = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004331 for(size_t i = 0; i < size(); i++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004332 mDeviceTypes |= itemAt(i)->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004333 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004334 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004335}
4336
4337ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
4338{
4339 for(size_t i = 0; i < size(); i++) {
4340 if (item->equals(itemAt(i))) {
4341 return i;
Eric Laurente552edb2014-03-10 17:42:56 -07004342 }
4343 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004344 return -1;
Eric Laurente552edb2014-03-10 17:42:56 -07004345}
4346
Eric Laurent3a4311c2014-03-17 12:00:47 -07004347ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item)
Eric Laurente552edb2014-03-10 17:42:56 -07004348{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004349 ssize_t ret = indexOf(item);
4350
4351 if (ret < 0) {
4352 ret = SortedVector::add(item);
4353 if (ret >= 0) {
4354 refreshTypes();
4355 }
4356 } else {
Eric Laurent1c333e22014-05-20 10:48:17 -07004357 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004358 ret = -1;
4359 }
4360 return ret;
Eric Laurente552edb2014-03-10 17:42:56 -07004361}
4362
Eric Laurent3a4311c2014-03-17 12:00:47 -07004363ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item)
4364{
4365 size_t i;
4366 ssize_t ret = indexOf(item);
4367
4368 if (ret < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004369 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004370 } else {
4371 ret = SortedVector::removeAt(ret);
4372 if (ret >= 0) {
4373 refreshTypes();
4374 }
4375 }
4376 return ret;
4377}
4378
4379void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types)
4380{
4381 DeviceVector deviceList;
4382
4383 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
4384 types &= ~role_bit;
4385
4386 while (types) {
4387 uint32_t i = 31 - __builtin_clz(types);
4388 uint32_t type = 1 << i;
4389 types &= ~type;
4390 add(new DeviceDescriptor(type | role_bit));
4391 }
4392}
4393
Eric Laurent1c333e22014-05-20 10:48:17 -07004394sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice(
4395 audio_devices_t type, String8 address) const
4396{
4397 sp<DeviceDescriptor> device;
4398 for (size_t i = 0; i < size(); i++) {
4399 if (itemAt(i)->mDeviceType == type) {
4400 device = itemAt(i);
4401 if (itemAt(i)->mAddress = address) {
4402 break;
4403 }
4404 }
4405 }
4406 ALOGV("DeviceVector::getDevice() for type %d address %s found %p",
4407 type, address.string(), device.get());
4408 return device;
4409}
4410
4411AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType(
4412 audio_devices_t type) const
4413{
4414 DeviceVector devices;
4415 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
4416 if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) {
4417 devices.add(itemAt(i));
4418 type &= ~itemAt(i)->mDeviceType;
4419 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
4420 itemAt(i)->mDeviceType, itemAt(i).get());
4421 }
4422 }
4423 return devices;
4424}
4425
4426void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig(struct audio_port_config *config) const
4427{
4428 config->id = mId;
4429 config->role = audio_is_output_device(mDeviceType) ?
4430 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
4431 config->type = AUDIO_PORT_TYPE_DEVICE;
4432 config->sample_rate = 0;
4433 config->channel_mask = mChannelMask;
4434 config->format = AUDIO_FORMAT_DEFAULT;
4435 config->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK;
4436 config->gain.index = -1;
4437 config->ext.device.type = mDeviceType;
4438 strncpy(config->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
4439}
4440
4441void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const
4442{
4443 AudioPort::toAudioPort(port);
4444 port->id = mId;
4445 port->ext.device.type = mDeviceType;
4446 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
4447}
4448
Eric Laurent3a4311c2014-03-17 12:00:47 -07004449void AudioPolicyManager::DeviceDescriptor::dumpHeader(int fd, int spaces)
4450{
4451 const size_t SIZE = 256;
4452 char buffer[SIZE];
4453
4454 snprintf(buffer, SIZE, "%*s%-48s %-2s %-8s %-32s \n",
4455 spaces, "", "Type", "ID", "Cnl Mask", "Address");
4456 write(fd, buffer, strlen(buffer));
4457}
4458
4459status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces) const
4460{
4461 const size_t SIZE = 256;
4462 char buffer[SIZE];
4463
4464 snprintf(buffer, SIZE, "%*s%-48s %2d %08x %-32s \n",
4465 spaces, "",
4466 enumToString(sDeviceNameToEnumTable,
4467 ARRAY_SIZE(sDeviceNameToEnumTable),
Eric Laurent1c333e22014-05-20 10:48:17 -07004468 mDeviceType),
Eric Laurent3a4311c2014-03-17 12:00:47 -07004469 mId, mChannelMask, mAddress.string());
4470 write(fd, buffer, strlen(buffer));
4471
4472 return NO_ERROR;
4473}
4474
4475
4476// --- audio_policy.conf file parsing
4477
Eric Laurente0720872014-03-11 09:30:41 -07004478audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07004479{
4480 uint32_t flag = 0;
4481
4482 // it is OK to cast name to non const here as we are not going to use it after
4483 // strtok() modifies it
4484 char *flagName = strtok(name, "|");
4485 while (flagName != NULL) {
4486 if (strlen(flagName) != 0) {
4487 flag |= stringToEnum(sFlagNameToEnumTable,
4488 ARRAY_SIZE(sFlagNameToEnumTable),
4489 flagName);
4490 }
4491 flagName = strtok(NULL, "|");
4492 }
4493 //force direct flag if offload flag is set: offloading implies a direct output stream
4494 // and all common behaviors are driven by checking only the direct flag
4495 // this should normally be set appropriately in the policy configuration file
4496 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
4497 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
4498 }
4499
4500 return (audio_output_flags_t)flag;
4501}
4502
Eric Laurente0720872014-03-11 09:30:41 -07004503audio_devices_t AudioPolicyManager::parseDeviceNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07004504{
4505 uint32_t device = 0;
4506
4507 char *devName = strtok(name, "|");
4508 while (devName != NULL) {
4509 if (strlen(devName) != 0) {
4510 device |= stringToEnum(sDeviceNameToEnumTable,
4511 ARRAY_SIZE(sDeviceNameToEnumTable),
4512 devName);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004513 }
Eric Laurente552edb2014-03-10 17:42:56 -07004514 devName = strtok(NULL, "|");
Eric Laurent3a4311c2014-03-17 12:00:47 -07004515 }
Eric Laurente552edb2014-03-10 17:42:56 -07004516 return device;
4517}
4518
Eric Laurente0720872014-03-11 09:30:41 -07004519status_t AudioPolicyManager::loadInput(cnode *root, HwModule *module)
Eric Laurente552edb2014-03-10 17:42:56 -07004520{
4521 cnode *node = root->first_child;
4522
Eric Laurent1c333e22014-05-20 10:48:17 -07004523 sp<IOProfile> profile = new IOProfile(AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004524
4525 while (node) {
4526 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004527 profile->loadSamplingRates((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004528 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004529 profile->loadFormats((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004530 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004531 profile->loadInChannels((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004532 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004533 profile->mSupportedDevices.loadDevicesFromType(parseDeviceNames((char *)node->value));
Eric Laurente552edb2014-03-10 17:42:56 -07004534 }
4535 node = node->next;
4536 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004537 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
Eric Laurente552edb2014-03-10 17:42:56 -07004538 "loadInput() invalid supported devices");
4539 ALOGW_IF(profile->mChannelMasks.size() == 0,
4540 "loadInput() invalid supported channel masks");
4541 ALOGW_IF(profile->mSamplingRates.size() == 0,
4542 "loadInput() invalid supported sampling rates");
4543 ALOGW_IF(profile->mFormats.size() == 0,
4544 "loadInput() invalid supported formats");
Eric Laurent3a4311c2014-03-17 12:00:47 -07004545 if (!profile->mSupportedDevices.isEmpty() &&
Eric Laurente552edb2014-03-10 17:42:56 -07004546 (profile->mChannelMasks.size() != 0) &&
4547 (profile->mSamplingRates.size() != 0) &&
4548 (profile->mFormats.size() != 0)) {
4549
Eric Laurent3a4311c2014-03-17 12:00:47 -07004550 ALOGV("loadInput() adding input Supported Devices %04x",
4551 profile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004552
4553 module->mInputProfiles.add(profile);
4554 return NO_ERROR;
4555 } else {
Eric Laurente552edb2014-03-10 17:42:56 -07004556 return BAD_VALUE;
4557 }
4558}
4559
Eric Laurente0720872014-03-11 09:30:41 -07004560status_t AudioPolicyManager::loadOutput(cnode *root, HwModule *module)
Eric Laurente552edb2014-03-10 17:42:56 -07004561{
4562 cnode *node = root->first_child;
4563
Eric Laurent1c333e22014-05-20 10:48:17 -07004564 sp<IOProfile> profile = new IOProfile(AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004565
4566 while (node) {
4567 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004568 profile->loadSamplingRates((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004569 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004570 profile->loadFormats((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004571 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004572 profile->loadOutChannels((char *)node->value);
Eric Laurente552edb2014-03-10 17:42:56 -07004573 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004574 profile->mSupportedDevices.loadDevicesFromType(parseDeviceNames((char *)node->value));
Eric Laurente552edb2014-03-10 17:42:56 -07004575 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
4576 profile->mFlags = parseFlagNames((char *)node->value);
4577 }
4578 node = node->next;
4579 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004580 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
Eric Laurente552edb2014-03-10 17:42:56 -07004581 "loadOutput() invalid supported devices");
4582 ALOGW_IF(profile->mChannelMasks.size() == 0,
4583 "loadOutput() invalid supported channel masks");
4584 ALOGW_IF(profile->mSamplingRates.size() == 0,
4585 "loadOutput() invalid supported sampling rates");
4586 ALOGW_IF(profile->mFormats.size() == 0,
4587 "loadOutput() invalid supported formats");
Eric Laurent3a4311c2014-03-17 12:00:47 -07004588 if (!profile->mSupportedDevices.isEmpty() &&
Eric Laurente552edb2014-03-10 17:42:56 -07004589 (profile->mChannelMasks.size() != 0) &&
4590 (profile->mSamplingRates.size() != 0) &&
4591 (profile->mFormats.size() != 0)) {
4592
Eric Laurent3a4311c2014-03-17 12:00:47 -07004593 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x",
4594 profile->mSupportedDevices.types(), profile->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07004595
4596 module->mOutputProfiles.add(profile);
4597 return NO_ERROR;
4598 } else {
Eric Laurente552edb2014-03-10 17:42:56 -07004599 return BAD_VALUE;
4600 }
4601}
4602
Eric Laurente0720872014-03-11 09:30:41 -07004603void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07004604{
4605 cnode *node = config_find(root, OUTPUTS_TAG);
4606 status_t status = NAME_NOT_FOUND;
4607
4608 HwModule *module = new HwModule(root->name);
4609
4610 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004611 node = node->first_child;
4612 while (node) {
4613 ALOGV("loadHwModule() loading output %s", node->name);
4614 status_t tmpStatus = loadOutput(node, module);
4615 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
4616 status = tmpStatus;
4617 }
4618 node = node->next;
4619 }
4620 }
4621 node = config_find(root, INPUTS_TAG);
4622 if (node != NULL) {
4623 node = node->first_child;
4624 while (node) {
4625 ALOGV("loadHwModule() loading input %s", node->name);
4626 status_t tmpStatus = loadInput(node, module);
4627 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
4628 status = tmpStatus;
4629 }
4630 node = node->next;
4631 }
4632 }
4633 if (status == NO_ERROR) {
4634 mHwModules.add(module);
4635 } else {
4636 delete module;
4637 }
4638}
4639
Eric Laurente0720872014-03-11 09:30:41 -07004640void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07004641{
4642 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
4643 if (node == NULL) {
4644 return;
4645 }
4646
4647 node = node->first_child;
4648 while (node) {
4649 ALOGV("loadHwModules() loading module %s", node->name);
4650 loadHwModule(node);
4651 node = node->next;
4652 }
4653}
4654
Eric Laurente0720872014-03-11 09:30:41 -07004655void AudioPolicyManager::loadGlobalConfig(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07004656{
4657 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
4658 if (node == NULL) {
4659 return;
4660 }
4661 node = node->first_child;
4662 while (node) {
4663 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004664 mAvailableOutputDevices.loadDevicesFromType(parseDeviceNames((char *)node->value));
4665 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
4666 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004667 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004668 audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
Eric Laurente552edb2014-03-10 17:42:56 -07004669 ARRAY_SIZE(sDeviceNameToEnumTable),
4670 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004671 if (device != AUDIO_DEVICE_NONE) {
4672 mDefaultOutputDevice = new DeviceDescriptor(device);
4673 } else {
4674 ALOGW("loadGlobalConfig() default device not specified");
4675 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004676 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07004677 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004678 mAvailableInputDevices.loadDevicesFromType(parseDeviceNames((char *)node->value));
4679 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004680 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
4681 mSpeakerDrcEnabled = stringToBool((char *)node->value);
4682 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
4683 }
4684 node = node->next;
4685 }
4686}
4687
Eric Laurente0720872014-03-11 09:30:41 -07004688status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07004689{
4690 cnode *root;
4691 char *data;
4692
4693 data = (char *)load_file(path, NULL);
4694 if (data == NULL) {
4695 return -ENODEV;
4696 }
4697 root = config_node("", "");
4698 config_load(root, data);
4699
4700 loadGlobalConfig(root);
4701 loadHwModules(root);
4702
4703 config_free(root);
4704 free(root);
4705 free(data);
4706
4707 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
4708
4709 return NO_ERROR;
4710}
4711
Eric Laurente0720872014-03-11 09:30:41 -07004712void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07004713{
4714 HwModule *module;
Eric Laurent1c333e22014-05-20 10:48:17 -07004715 sp<IOProfile> profile;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004716 sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC);
4717 mAvailableOutputDevices.add(mDefaultOutputDevice);
4718 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004719
4720 module = new HwModule("primary");
4721
Eric Laurent1c333e22014-05-20 10:48:17 -07004722 profile = new IOProfile(AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004723 profile->mSamplingRates.add(44100);
4724 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4725 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004726 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004727 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4728 module->mOutputProfiles.add(profile);
4729
Eric Laurent1c333e22014-05-20 10:48:17 -07004730 profile = new IOProfile(AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07004731 profile->mSamplingRates.add(8000);
4732 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4733 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07004734 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07004735 module->mInputProfiles.add(profile);
4736
4737 mHwModules.add(module);
4738}
4739
4740}; // namespace android