blob: d4c9374eab574860ba6005c70adc3ca86022cc18 [file] [log] [blame]
Eric Laurente552edb2014-03-10 17:42:56 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Laurente0720872014-03-11 09:30:41 -070017#define LOG_TAG "AudioPolicyManager"
Eric Laurente552edb2014-03-10 17:42:56 -070018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27// A device mask for all audio input devices that are considered "virtual" when evaluating
28// active inputs in getActiveInput()
29#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL AUDIO_DEVICE_IN_REMOTE_SUBMIX
30// A device mask for all audio output devices that are considered "remote" when evaluating
31// active output devices in isStreamActiveRemotely()
32#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX
33
Eric Laurentd4692962014-05-05 18:13:44 -070034#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070035#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070036
Eric Laurente552edb2014-03-10 17:42:56 -070037#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070038#include <utils/Log.h>
39#include <hardware/audio.h>
40#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070041#include <media/AudioParameter.h>
Eric Laurentd4692962014-05-05 18:13:44 -070042#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070043#include "audio_policy_conf.h"
Eric Laurente552edb2014-03-10 17:42:56 -070044
Eric Laurent3b73df72014-03-11 09:06:29 -070045namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070046
47// ----------------------------------------------------------------------------
Eric Laurent3a4311c2014-03-17 12:00:47 -070048// Definitions for audio_policy.conf file parsing
49// ----------------------------------------------------------------------------
50
51struct StringToEnum {
52 const char *name;
53 uint32_t value;
54};
55
56#define STRING_TO_ENUM(string) { #string, string }
57#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
58
59const StringToEnum sDeviceNameToEnumTable[] = {
60 STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
61 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
62 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
63 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
64 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO),
65 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET),
66 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
67 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
68 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP),
69 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES),
70 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
71 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
72 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070073 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI),
Eric Laurent3a4311c2014-03-17 12:00:47 -070074 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
75 STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
76 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
77 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
78 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
79 STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
Eric Laurent1b776232014-05-19 17:26:41 -070080 STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX),
81 STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE),
82 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC),
83 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF),
84 STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM),
Eric Laurent3a4311c2014-03-17 12:00:47 -070085 STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
86 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
87 STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO),
88 STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
89 STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070090 STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI),
Eric Laurent3a4311c2014-03-17 12:00:47 -070091 STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
Eric Laurent1b776232014-05-19 17:26:41 -070092 STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
Eric Laurent3a4311c2014-03-17 12:00:47 -070093 STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
94 STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
95 STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
96 STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
97 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
Eric Laurentd4692962014-05-05 18:13:44 -070098 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
Eric Laurent1b776232014-05-19 17:26:41 -070099 STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
100 STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
101 STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE),
102 STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF),
Mike Lockwood41b0e242014-05-13 15:23:35 -0700103 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700104};
105
106const StringToEnum sFlagNameToEnumTable[] = {
107 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
108 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
109 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
110 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
111 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
112 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
113};
114
115const StringToEnum sFormatNameToEnumTable[] = {
116 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
117 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
118 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
119 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
120 STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
121 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
122 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
123 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
124 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
125};
126
127const StringToEnum sOutChannelsNameToEnumTable[] = {
128 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
129 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
131 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
132};
133
134const StringToEnum sInChannelsNameToEnumTable[] = {
135 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
136 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
137 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
138};
139
Eric Laurent1afeecb2014-05-14 08:52:28 -0700140const StringToEnum sGainModeNameToEnumTable[] = {
141 STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT),
142 STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS),
143 STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP),
144};
145
Eric Laurent3a4311c2014-03-17 12:00:47 -0700146
147uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table,
148 size_t size,
149 const char *name)
150{
151 for (size_t i = 0; i < size; i++) {
152 if (strcmp(table[i].name, name) == 0) {
153 ALOGV("stringToEnum() found %s", table[i].name);
154 return table[i].value;
155 }
156 }
157 return 0;
158}
159
160const char *AudioPolicyManager::enumToString(const struct StringToEnum *table,
161 size_t size,
162 uint32_t value)
163{
164 for (size_t i = 0; i < size; i++) {
165 if (table[i].value == value) {
166 return table[i].name;
167 }
168 }
169 return "";
170}
171
172bool AudioPolicyManager::stringToBool(const char *value)
173{
174 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
175}
176
177
178// ----------------------------------------------------------------------------
Eric Laurente552edb2014-03-10 17:42:56 -0700179// AudioPolicyInterface implementation
180// ----------------------------------------------------------------------------
181
182
Eric Laurente0720872014-03-11 09:30:41 -0700183status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700184 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700185 const char *device_address)
186{
Eric Laurent3a4311c2014-03-17 12:00:47 -0700187 String8 address = String8(device_address);
Eric Laurente552edb2014-03-10 17:42:56 -0700188
189 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
190
191 // connect/disconnect only 1 device at a time
192 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
193
Eric Laurente552edb2014-03-10 17:42:56 -0700194 // handle output devices
195 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700196 SortedVector <audio_io_handle_t> outputs;
197
Eric Laurent1afeecb2014-05-14 08:52:28 -0700198 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
199 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700200 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
201
Eric Laurente552edb2014-03-10 17:42:56 -0700202 // save a copy of the opened output descriptors before any output is opened or closed
203 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
204 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700205 switch (state)
206 {
207 // handle output device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700208 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
Eric Laurent3a4311c2014-03-17 12:00:47 -0700209 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700210 ALOGW("setDeviceConnectionState() device already connected: %x", device);
211 return INVALID_OPERATION;
212 }
213 ALOGV("setDeviceConnectionState() connecting device %x", device);
214
Eric Laurent3a4311c2014-03-17 12:00:47 -0700215 if (checkOutputsForDevice(device, state, outputs, address) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -0700216 return INVALID_OPERATION;
217 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700218 // outputs should never be empty here
219 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
220 "checkOutputsForDevice() returned no outputs but status OK");
Eric Laurentd4692962014-05-05 18:13:44 -0700221 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
Eric Laurente552edb2014-03-10 17:42:56 -0700222 outputs.size());
223 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700224 index = mAvailableOutputDevices.add(devDesc);
225 if (index >= 0) {
226 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent1f2f2232014-06-02 12:01:23 -0700227 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700228 ALOG_ASSERT(module != NULL, "setDeviceConnectionState():"
229 "could not find HW module for device %08x", device);
230 mAvailableOutputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700231 } else {
232 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700233 }
234
235 break;
236 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700237 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700238 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700239 ALOGW("setDeviceConnectionState() device not connected: %x", device);
240 return INVALID_OPERATION;
241 }
242
243 ALOGV("setDeviceConnectionState() disconnecting device %x", device);
244 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700245 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700246
Eric Laurent3a4311c2014-03-17 12:00:47 -0700247 checkOutputsForDevice(device, state, outputs, address);
Eric Laurente552edb2014-03-10 17:42:56 -0700248 // not currently handling multiple simultaneous submixes: ignoring remote submix
249 // case and address
250 } break;
251
252 default:
253 ALOGE("setDeviceConnectionState() invalid state: %x", state);
254 return BAD_VALUE;
255 }
256
Eric Laurent3a4311c2014-03-17 12:00:47 -0700257 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
258 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700259 checkA2dpSuspend();
260 checkOutputForAllStrategies();
261 // outputs must be closed after checkOutputForAllStrategies() is executed
262 if (!outputs.isEmpty()) {
263 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700264 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700265 // close unused outputs after device disconnection or direct outputs that have been
266 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700267 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700268 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
269 (desc->mDirectOpenCount == 0))) {
270 closeOutput(outputs[i]);
271 }
272 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700273 // check again after closing A2DP output to reset mA2dpSuspended if needed
274 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700275 }
276
277 updateDevicesAndOutputs();
278 for (size_t i = 0; i < mOutputs.size(); i++) {
279 // do not force device change on duplicated output because if device is 0, it will
280 // also force a device 0 for the two outputs it is duplicated to which may override
281 // a valid device selection on those outputs.
282 setOutputDevice(mOutputs.keyAt(i),
Eric Laurent1c333e22014-05-20 10:48:17 -0700283 getNewOutputDevice(mOutputs.keyAt(i), true /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700284 !mOutputs.valueAt(i)->isDuplicated(),
285 0);
286 }
287
Eric Laurent72aa32f2014-05-30 18:51:48 -0700288 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700289 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700290 } // end if is output device
291
Eric Laurente552edb2014-03-10 17:42:56 -0700292 // handle input devices
293 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700294 SortedVector <audio_io_handle_t> inputs;
295
Eric Laurent1afeecb2014-05-14 08:52:28 -0700296 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
297 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700298 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700299 switch (state)
300 {
301 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700302 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700303 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700304 ALOGW("setDeviceConnectionState() device already connected: %d", device);
305 return INVALID_OPERATION;
306 }
Eric Laurent1f2f2232014-06-02 12:01:23 -0700307 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700308 if (module == NULL) {
309 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
310 device);
311 return INVALID_OPERATION;
312 }
Eric Laurentd4692962014-05-05 18:13:44 -0700313 if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) {
314 return INVALID_OPERATION;
315 }
316
Eric Laurent3a4311c2014-03-17 12:00:47 -0700317 index = mAvailableInputDevices.add(devDesc);
318 if (index >= 0) {
319 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700320 mAvailableInputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700321 } else {
322 return NO_MEMORY;
323 }
Eric Laurentd4692962014-05-05 18:13:44 -0700324 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700325
326 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700327 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700328 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700329 ALOGW("setDeviceConnectionState() device not connected: %d", device);
330 return INVALID_OPERATION;
331 }
Eric Laurentd4692962014-05-05 18:13:44 -0700332 checkInputsForDevice(device, state, inputs, address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700333 mAvailableInputDevices.remove(devDesc);
Eric Laurentd4692962014-05-05 18:13:44 -0700334 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700335
336 default:
337 ALOGE("setDeviceConnectionState() invalid state: %x", state);
338 return BAD_VALUE;
339 }
340
Eric Laurentd4692962014-05-05 18:13:44 -0700341 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700342
Eric Laurentb52c1522014-05-20 11:27:36 -0700343 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700344 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700345 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700346
347 ALOGW("setDeviceConnectionState() invalid device: %x", device);
348 return BAD_VALUE;
349}
350
Eric Laurente0720872014-03-11 09:30:41 -0700351audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700352 const char *device_address)
353{
Eric Laurent3b73df72014-03-11 09:06:29 -0700354 audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700355 String8 address = String8(device_address);
Eric Laurent1afeecb2014-05-14 08:52:28 -0700356 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
357 devDesc->mAddress = String8(device_address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700358 ssize_t index;
359 DeviceVector *deviceVector;
360
Eric Laurente552edb2014-03-10 17:42:56 -0700361 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700362 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700363 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700364 deviceVector = &mAvailableInputDevices;
365 } else {
366 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
367 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700368 }
369
Eric Laurent3a4311c2014-03-17 12:00:47 -0700370 index = deviceVector->indexOf(devDesc);
371 if (index >= 0) {
372 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
373 } else {
374 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
375 }
Eric Laurente552edb2014-03-10 17:42:56 -0700376}
377
Eric Laurente0720872014-03-11 09:30:41 -0700378void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700379{
380 ALOGV("setPhoneState() state %d", state);
381 audio_devices_t newDevice = AUDIO_DEVICE_NONE;
Eric Laurent3b73df72014-03-11 09:06:29 -0700382 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700383 ALOGW("setPhoneState() invalid state %d", state);
384 return;
385 }
386
387 if (state == mPhoneState ) {
388 ALOGW("setPhoneState() setting same state %d", state);
389 return;
390 }
391
392 // if leaving call state, handle special case of active streams
393 // pertaining to sonification strategy see handleIncallSonification()
394 if (isInCall()) {
395 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700396 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
397 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700398 }
399 }
400
401 // store previous phone state for management of sonification strategy below
402 int oldState = mPhoneState;
403 mPhoneState = state;
404 bool force = false;
405
406 // are we entering or starting a call
407 if (!isStateInCall(oldState) && isStateInCall(state)) {
408 ALOGV(" Entering call in setPhoneState()");
409 // force routing command to audio hardware when starting a call
410 // even if no device change is needed
411 force = true;
412 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
413 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
414 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
415 }
416 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
417 ALOGV(" Exiting call in setPhoneState()");
418 // force routing command to audio hardware when exiting a call
419 // even if no device change is needed
420 force = true;
421 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
422 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
423 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
424 }
425 } else if (isStateInCall(state) && (state != oldState)) {
426 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
427 // force routing command to audio hardware when switching between telephony and VoIP
428 // even if no device change is needed
429 force = true;
430 }
431
432 // check for device and output changes triggered by new phone state
Eric Laurent1c333e22014-05-20 10:48:17 -0700433 newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700434 checkA2dpSuspend();
435 checkOutputForAllStrategies();
436 updateDevicesAndOutputs();
437
Eric Laurent1f2f2232014-06-02 12:01:23 -0700438 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700439
440 // force routing command to audio hardware when ending call
441 // even if no device change is needed
442 if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
443 newDevice = hwOutputDesc->device();
444 }
445
446 int delayMs = 0;
447 if (isStateInCall(state)) {
448 nsecs_t sysTime = systemTime();
449 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700450 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700451 // mute media and sonification strategies and delay device switch by the largest
452 // latency of any output where either strategy is active.
453 // This avoid sending the ring tone or music tail into the earpiece or headset.
454 if ((desc->isStrategyActive(STRATEGY_MEDIA,
455 SONIFICATION_HEADSET_MUSIC_DELAY,
456 sysTime) ||
457 desc->isStrategyActive(STRATEGY_SONIFICATION,
458 SONIFICATION_HEADSET_MUSIC_DELAY,
459 sysTime)) &&
460 (delayMs < (int)desc->mLatency*2)) {
461 delayMs = desc->mLatency*2;
462 }
463 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
464 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
465 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
466 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
467 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
468 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
469 }
470 }
471
472 // change routing is necessary
473 setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
474
475 // if entering in call state, handle special case of active streams
476 // pertaining to sonification strategy see handleIncallSonification()
477 if (isStateInCall(state)) {
478 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700479 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
480 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700481 }
482 }
483
484 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700485 if (state == AUDIO_MODE_RINGTONE &&
486 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700487 mLimitRingtoneVolume = true;
488 } else {
489 mLimitRingtoneVolume = false;
490 }
491}
492
Eric Laurente0720872014-03-11 09:30:41 -0700493void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700494 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700495{
496 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
497
498 bool forceVolumeReeval = false;
499 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700500 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
501 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
502 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700503 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
504 return;
505 }
506 forceVolumeReeval = true;
507 mForceUse[usage] = config;
508 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700509 case AUDIO_POLICY_FORCE_FOR_MEDIA:
510 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
511 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
512 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
513 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
514 config != AUDIO_POLICY_FORCE_NO_BT_A2DP) {
Eric Laurente552edb2014-03-10 17:42:56 -0700515 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
516 return;
517 }
518 mForceUse[usage] = config;
519 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700520 case AUDIO_POLICY_FORCE_FOR_RECORD:
521 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
522 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700523 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
524 return;
525 }
526 mForceUse[usage] = config;
527 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700528 case AUDIO_POLICY_FORCE_FOR_DOCK:
529 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
530 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
531 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
532 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
533 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700534 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
535 }
536 forceVolumeReeval = true;
537 mForceUse[usage] = config;
538 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700539 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
540 if (config != AUDIO_POLICY_FORCE_NONE &&
541 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700542 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
543 }
544 forceVolumeReeval = true;
545 mForceUse[usage] = config;
546 break;
547 default:
548 ALOGW("setForceUse() invalid usage %d", usage);
549 break;
550 }
551
552 // check for device and output changes triggered by new force usage
553 checkA2dpSuspend();
554 checkOutputForAllStrategies();
555 updateDevicesAndOutputs();
556 for (size_t i = 0; i < mOutputs.size(); i++) {
557 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700558 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700559 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
560 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
561 applyStreamVolumes(output, newDevice, 0, true);
562 }
563 }
564
565 audio_io_handle_t activeInput = getActiveInput();
566 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700567 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700568 }
569
570}
571
Eric Laurente0720872014-03-11 09:30:41 -0700572audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700573{
574 return mForceUse[usage];
575}
576
Eric Laurente0720872014-03-11 09:30:41 -0700577void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700578{
579 ALOGV("setSystemProperty() property %s, value %s", property, value);
580}
581
582// Find a direct output profile compatible with the parameters passed, even if the input flags do
583// not explicitly request a direct output
Eric Laurent1c333e22014-05-20 10:48:17 -0700584sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700585 audio_devices_t device,
586 uint32_t samplingRate,
587 audio_format_t format,
588 audio_channel_mask_t channelMask,
589 audio_output_flags_t flags)
590{
591 for (size_t i = 0; i < mHwModules.size(); i++) {
592 if (mHwModules[i]->mHandle == 0) {
593 continue;
594 }
595 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700596 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent3a4311c2014-03-17 12:00:47 -0700597 bool found = false;
Eric Laurente552edb2014-03-10 17:42:56 -0700598 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
599 if (profile->isCompatibleProfile(device, samplingRate, format,
600 channelMask,
601 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700602 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700603 }
604 } else {
605 if (profile->isCompatibleProfile(device, samplingRate, format,
606 channelMask,
607 AUDIO_OUTPUT_FLAG_DIRECT)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700608 found = true;
Eric Laurente552edb2014-03-10 17:42:56 -0700609 }
610 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700611 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
612 return profile;
613 }
Eric Laurente552edb2014-03-10 17:42:56 -0700614 }
615 }
616 return 0;
617}
618
Eric Laurente0720872014-03-11 09:30:41 -0700619audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700620 uint32_t samplingRate,
621 audio_format_t format,
622 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700623 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700624 const audio_offload_info_t *offloadInfo)
625{
626 audio_io_handle_t output = 0;
627 uint32_t latency = 0;
Eric Laurent3b73df72014-03-11 09:06:29 -0700628 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700629 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
630 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
631 device, stream, samplingRate, format, channelMask, flags);
632
633#ifdef AUDIO_POLICY_TEST
634 if (mCurOutput != 0) {
635 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
636 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
637
638 if (mTestOutputs[mCurOutput] == 0) {
639 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700640 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700641 outputDesc->mDevice = mTestDevice;
642 outputDesc->mSamplingRate = mTestSamplingRate;
643 outputDesc->mFormat = mTestFormat;
644 outputDesc->mChannelMask = mTestChannels;
645 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700646 outputDesc->mFlags =
647 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700648 outputDesc->mRefCount[stream] = 0;
649 mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
650 &outputDesc->mSamplingRate,
651 &outputDesc->mFormat,
652 &outputDesc->mChannelMask,
653 &outputDesc->mLatency,
654 outputDesc->mFlags,
655 offloadInfo);
656 if (mTestOutputs[mCurOutput]) {
657 AudioParameter outputCmd = AudioParameter();
658 outputCmd.addInt(String8("set_id"),mCurOutput);
659 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
660 addOutput(mTestOutputs[mCurOutput], outputDesc);
661 }
662 }
663 return mTestOutputs[mCurOutput];
664 }
665#endif //AUDIO_POLICY_TEST
666
667 // open a direct output if required by specified parameters
668 //force direct flag if offload flag is set: offloading implies a direct output stream
669 // and all common behaviors are driven by checking only the direct flag
670 // this should normally be set appropriately in the policy configuration file
671 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700672 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -0700673 }
674
675 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
676 // creating an offloaded track and tearing it down immediately after start when audioflinger
677 // detects there is an active non offloadable effect.
678 // FIXME: We should check the audio session here but we do not have it in this context.
679 // This may prevent offloading in rare situations where effects are left active by apps
680 // in the background.
Eric Laurent1c333e22014-05-20 10:48:17 -0700681 sp<IOProfile> profile;
Eric Laurente552edb2014-03-10 17:42:56 -0700682 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
683 !isNonOffloadableEffectEnabled()) {
684 profile = getProfileForDirectOutput(device,
685 samplingRate,
686 format,
687 channelMask,
688 (audio_output_flags_t)flags);
689 }
690
Eric Laurent1c333e22014-05-20 10:48:17 -0700691 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700692 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -0700693
694 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700695 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700696 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
697 outputDesc = desc;
698 // reuse direct output if currently open and configured with same parameters
699 if ((samplingRate == outputDesc->mSamplingRate) &&
700 (format == outputDesc->mFormat) &&
701 (channelMask == outputDesc->mChannelMask)) {
702 outputDesc->mDirectOpenCount++;
703 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
704 return mOutputs.keyAt(i);
705 }
706 }
707 }
708 // close direct output if currently open and configured with different parameters
709 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700710 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -0700711 }
712 outputDesc = new AudioOutputDescriptor(profile);
713 outputDesc->mDevice = device;
714 outputDesc->mSamplingRate = samplingRate;
715 outputDesc->mFormat = format;
716 outputDesc->mChannelMask = channelMask;
717 outputDesc->mLatency = 0;
718 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
719 outputDesc->mRefCount[stream] = 0;
720 outputDesc->mStopTime[stream] = 0;
721 outputDesc->mDirectOpenCount = 1;
722 output = mpClientInterface->openOutput(profile->mModule->mHandle,
723 &outputDesc->mDevice,
724 &outputDesc->mSamplingRate,
725 &outputDesc->mFormat,
726 &outputDesc->mChannelMask,
727 &outputDesc->mLatency,
728 outputDesc->mFlags,
729 offloadInfo);
730
731 // only accept an output with the requested parameters
732 if (output == 0 ||
733 (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
734 (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
735 (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
736 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
737 "format %d %d, channelMask %04x %04x", output, samplingRate,
738 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
739 outputDesc->mChannelMask);
740 if (output != 0) {
741 mpClientInterface->closeOutput(output);
742 }
Eric Laurente552edb2014-03-10 17:42:56 -0700743 return 0;
744 }
745 audio_io_handle_t srcOutput = getOutputForEffect();
746 addOutput(output, outputDesc);
747 audio_io_handle_t dstOutput = getOutputForEffect();
748 if (dstOutput == output) {
749 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
750 }
751 mPreviousOutputs = mOutputs;
752 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -0700753 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700754 return output;
755 }
756
757 // ignoring channel mask due to downmix capability in mixer
758
759 // open a non direct output
760
761 // for non direct outputs, only PCM is supported
762 if (audio_is_linear_pcm(format)) {
763 // get which output is suitable for the specified stream. The actual
764 // routing change will happen when startOutput() will be called
765 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
766
767 output = selectOutput(outputs, flags);
768 }
769 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
770 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
771
772 ALOGV("getOutput() returns output %d", output);
773
774 return output;
775}
776
Eric Laurente0720872014-03-11 09:30:41 -0700777audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3b73df72014-03-11 09:06:29 -0700778 audio_output_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -0700779{
780 // select one output among several that provide a path to a particular device or set of
781 // devices (the list was previously build by getOutputsForDevice()).
782 // The priority is as follows:
783 // 1: the output with the highest number of requested policy flags
784 // 2: the primary output
785 // 3: the first output in the list
786
787 if (outputs.size() == 0) {
788 return 0;
789 }
790 if (outputs.size() == 1) {
791 return outputs[0];
792 }
793
794 int maxCommonFlags = 0;
795 audio_io_handle_t outputFlags = 0;
796 audio_io_handle_t outputPrimary = 0;
797
798 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700799 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700800 if (!outputDesc->isDuplicated()) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700801 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -0700802 if (commonFlags > maxCommonFlags) {
803 outputFlags = outputs[i];
804 maxCommonFlags = commonFlags;
805 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
806 }
807 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
808 outputPrimary = outputs[i];
809 }
810 }
811 }
812
813 if (outputFlags != 0) {
814 return outputFlags;
815 }
816 if (outputPrimary != 0) {
817 return outputPrimary;
818 }
819
820 return outputs[0];
821}
822
Eric Laurente0720872014-03-11 09:30:41 -0700823status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700824 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700825 int session)
826{
827 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
828 ssize_t index = mOutputs.indexOfKey(output);
829 if (index < 0) {
830 ALOGW("startOutput() unknown output %d", output);
831 return BAD_VALUE;
832 }
833
Eric Laurent1f2f2232014-06-02 12:01:23 -0700834 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700835
836 // increment usage count for this stream on the requested output:
837 // NOTE that the usage count is the same for duplicated output and hardware output which is
838 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
839 outputDesc->changeRefCount(stream, 1);
840
841 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700842 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700843 routing_strategy strategy = getStrategy(stream);
844 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
845 (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
846 uint32_t waitMs = 0;
847 bool force = false;
848 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700849 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700850 if (desc != outputDesc) {
851 // force a device change if any other output is managed by the same hw
852 // module and has a current device selection that differs from selected device.
853 // In this case, the audio HAL must receive the new device selection so that it can
854 // change the device currently selected by the other active output.
855 if (outputDesc->sharesHwModuleWith(desc) &&
856 desc->device() != newDevice) {
857 force = true;
858 }
859 // wait for audio on other active outputs to be presented when starting
860 // a notification so that audio focus effect can propagate.
861 uint32_t latency = desc->latency();
862 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
863 waitMs = latency;
864 }
865 }
866 }
867 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
868
869 // handle special case for sonification while in call
870 if (isInCall()) {
871 handleIncallSonification(stream, true, false);
872 }
873
874 // apply volume rules for current stream and device if necessary
875 checkAndSetVolume(stream,
876 mStreams[stream].getVolumeIndex(newDevice),
877 output,
878 newDevice);
879
880 // update the outputs if starting an output with a stream that can affect notification
881 // routing
882 handleNotificationRoutingForStream(stream);
883 if (waitMs > muteWaitMs) {
884 usleep((waitMs - muteWaitMs) * 2 * 1000);
885 }
886 }
887 return NO_ERROR;
888}
889
890
Eric Laurente0720872014-03-11 09:30:41 -0700891status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -0700892 audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700893 int session)
894{
895 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
896 ssize_t index = mOutputs.indexOfKey(output);
897 if (index < 0) {
898 ALOGW("stopOutput() unknown output %d", output);
899 return BAD_VALUE;
900 }
901
Eric Laurent1f2f2232014-06-02 12:01:23 -0700902 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700903
904 // handle special case for sonification while in call
905 if (isInCall()) {
906 handleIncallSonification(stream, false, false);
907 }
908
909 if (outputDesc->mRefCount[stream] > 0) {
910 // decrement usage count of this stream on the output
911 outputDesc->changeRefCount(stream, -1);
912 // store time at which the stream was stopped - see isStreamActive()
913 if (outputDesc->mRefCount[stream] == 0) {
914 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -0700915 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -0700916 // delay the device switch by twice the latency because stopOutput() is executed when
917 // the track stop() command is received and at that time the audio track buffer can
918 // still contain data that needs to be drained. The latency only covers the audio HAL
919 // and kernel buffers. Also the latency does not always include additional delay in the
920 // audio path (audio DSP, CODEC ...)
921 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
922
923 // force restoring the device selection on other active outputs if it differs from the
924 // one being selected for this output
925 for (size_t i = 0; i < mOutputs.size(); i++) {
926 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -0700927 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700928 if (curOutput != output &&
929 desc->isActive() &&
930 outputDesc->sharesHwModuleWith(desc) &&
931 (newDevice != desc->device())) {
932 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -0700933 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -0700934 true,
935 outputDesc->mLatency*2);
936 }
937 }
938 // update the outputs if stopping one with a stream that can affect notification routing
939 handleNotificationRoutingForStream(stream);
940 }
941 return NO_ERROR;
942 } else {
943 ALOGW("stopOutput() refcount is already 0 for output %d", output);
944 return INVALID_OPERATION;
945 }
946}
947
Eric Laurente0720872014-03-11 09:30:41 -0700948void AudioPolicyManager::releaseOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -0700949{
950 ALOGV("releaseOutput() %d", output);
951 ssize_t index = mOutputs.indexOfKey(output);
952 if (index < 0) {
953 ALOGW("releaseOutput() releasing unknown output %d", output);
954 return;
955 }
956
957#ifdef AUDIO_POLICY_TEST
958 int testIndex = testOutputIndex(output);
959 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700960 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -0700961 if (outputDesc->isActive()) {
962 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -0700963 mOutputs.removeItem(output);
964 mTestOutputs[testIndex] = 0;
965 }
966 return;
967 }
968#endif //AUDIO_POLICY_TEST
969
Eric Laurent1f2f2232014-06-02 12:01:23 -0700970 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -0700971 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700972 if (desc->mDirectOpenCount <= 0) {
973 ALOGW("releaseOutput() invalid open count %d for output %d",
974 desc->mDirectOpenCount, output);
975 return;
976 }
977 if (--desc->mDirectOpenCount == 0) {
978 closeOutput(output);
979 // If effects where present on the output, audioflinger moved them to the primary
980 // output by default: move them back to the appropriate output.
981 audio_io_handle_t dstOutput = getOutputForEffect();
982 if (dstOutput != mPrimaryOutput) {
983 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
984 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700985 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700986 }
987 }
988}
989
990
Eric Laurente0720872014-03-11 09:30:41 -0700991audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource,
Eric Laurente552edb2014-03-10 17:42:56 -0700992 uint32_t samplingRate,
993 audio_format_t format,
994 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700995 audio_in_acoustics_t acoustics)
Eric Laurente552edb2014-03-10 17:42:56 -0700996{
997 audio_io_handle_t input = 0;
998 audio_devices_t device = getDeviceForInputSource(inputSource);
999
1000 ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
1001 inputSource, samplingRate, format, channelMask, acoustics);
1002
1003 if (device == AUDIO_DEVICE_NONE) {
1004 ALOGW("getInput() could not find device for inputSource %d", inputSource);
1005 return 0;
1006 }
1007
1008 // adapt channel selection to input source
1009 switch(inputSource) {
1010 case AUDIO_SOURCE_VOICE_UPLINK:
1011 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1012 break;
1013 case AUDIO_SOURCE_VOICE_DOWNLINK:
1014 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1015 break;
1016 case AUDIO_SOURCE_VOICE_CALL:
1017 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1018 break;
1019 default:
1020 break;
1021 }
1022
Eric Laurent1c333e22014-05-20 10:48:17 -07001023 sp<IOProfile> profile = getInputProfile(device,
Eric Laurente552edb2014-03-10 17:42:56 -07001024 samplingRate,
1025 format,
1026 channelMask);
Eric Laurent1c333e22014-05-20 10:48:17 -07001027 if (profile == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07001028 ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d, "
1029 "channelMask %04x",
1030 device, samplingRate, format, channelMask);
1031 return 0;
1032 }
1033
1034 if (profile->mModule->mHandle == 0) {
1035 ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
1036 return 0;
1037 }
1038
Eric Laurent1f2f2232014-06-02 12:01:23 -07001039 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07001040
1041 inputDesc->mInputSource = inputSource;
1042 inputDesc->mDevice = device;
1043 inputDesc->mSamplingRate = samplingRate;
1044 inputDesc->mFormat = format;
1045 inputDesc->mChannelMask = channelMask;
1046 inputDesc->mRefCount = 0;
1047 input = mpClientInterface->openInput(profile->mModule->mHandle,
1048 &inputDesc->mDevice,
1049 &inputDesc->mSamplingRate,
1050 &inputDesc->mFormat,
1051 &inputDesc->mChannelMask);
1052
1053 // only accept input with the exact requested set of parameters
1054 if (input == 0 ||
1055 (samplingRate != inputDesc->mSamplingRate) ||
1056 (format != inputDesc->mFormat) ||
1057 (channelMask != inputDesc->mChannelMask)) {
1058 ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask %x",
1059 samplingRate, format, channelMask);
1060 if (input != 0) {
1061 mpClientInterface->closeInput(input);
1062 }
Eric Laurente552edb2014-03-10 17:42:56 -07001063 return 0;
1064 }
Eric Laurentd4692962014-05-05 18:13:44 -07001065 addInput(input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001066 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001067 return input;
1068}
1069
Eric Laurente0720872014-03-11 09:30:41 -07001070status_t AudioPolicyManager::startInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001071{
1072 ALOGV("startInput() input %d", input);
1073 ssize_t index = mInputs.indexOfKey(input);
1074 if (index < 0) {
1075 ALOGW("startInput() unknown input %d", input);
1076 return BAD_VALUE;
1077 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001078 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001079
1080#ifdef AUDIO_POLICY_TEST
1081 if (mTestInput == 0)
1082#endif //AUDIO_POLICY_TEST
1083 {
1084 // refuse 2 active AudioRecord clients at the same time except if the active input
1085 // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
1086 audio_io_handle_t activeInput = getActiveInput();
1087 if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001088 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001089 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1090 ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1091 stopInput(activeInput);
1092 releaseInput(activeInput);
1093 } else {
1094 ALOGW("startInput() input %d failed: other input already started", input);
1095 return INVALID_OPERATION;
1096 }
1097 }
1098 }
1099
Eric Laurent1c333e22014-05-20 10:48:17 -07001100 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001101
1102 // automatically enable the remote submix output when input is started
1103 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1104 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001105 AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001106 }
1107
Eric Laurente552edb2014-03-10 17:42:56 -07001108 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1109
Eric Laurente552edb2014-03-10 17:42:56 -07001110 inputDesc->mRefCount = 1;
1111 return NO_ERROR;
1112}
1113
Eric Laurente0720872014-03-11 09:30:41 -07001114status_t AudioPolicyManager::stopInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001115{
1116 ALOGV("stopInput() input %d", input);
1117 ssize_t index = mInputs.indexOfKey(input);
1118 if (index < 0) {
1119 ALOGW("stopInput() unknown input %d", input);
1120 return BAD_VALUE;
1121 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001122 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001123
1124 if (inputDesc->mRefCount == 0) {
1125 ALOGW("stopInput() input %d already stopped", input);
1126 return INVALID_OPERATION;
1127 } else {
1128 // automatically disable the remote submix output when input is stopped
1129 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1130 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent3b73df72014-03-11 09:06:29 -07001131 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
Eric Laurente552edb2014-03-10 17:42:56 -07001132 }
1133
Eric Laurent1c333e22014-05-20 10:48:17 -07001134 resetInputDevice(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001135 inputDesc->mRefCount = 0;
1136 return NO_ERROR;
1137 }
1138}
1139
Eric Laurente0720872014-03-11 09:30:41 -07001140void AudioPolicyManager::releaseInput(audio_io_handle_t input)
Eric Laurente552edb2014-03-10 17:42:56 -07001141{
1142 ALOGV("releaseInput() %d", input);
1143 ssize_t index = mInputs.indexOfKey(input);
1144 if (index < 0) {
1145 ALOGW("releaseInput() releasing unknown input %d", input);
1146 return;
1147 }
1148 mpClientInterface->closeInput(input);
Eric Laurente552edb2014-03-10 17:42:56 -07001149 mInputs.removeItem(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07001150 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001151 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001152 ALOGV("releaseInput() exit");
1153}
1154
Eric Laurentd4692962014-05-05 18:13:44 -07001155void AudioPolicyManager::closeAllInputs() {
1156 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1157 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1158 }
1159 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001160 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07001161}
1162
Eric Laurente0720872014-03-11 09:30:41 -07001163void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001164 int indexMin,
1165 int indexMax)
1166{
1167 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1168 if (indexMin < 0 || indexMin >= indexMax) {
1169 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1170 return;
1171 }
1172 mStreams[stream].mIndexMin = indexMin;
1173 mStreams[stream].mIndexMax = indexMax;
1174}
1175
Eric Laurente0720872014-03-11 09:30:41 -07001176status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001177 int index,
1178 audio_devices_t device)
1179{
1180
1181 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1182 return BAD_VALUE;
1183 }
1184 if (!audio_is_output_device(device)) {
1185 return BAD_VALUE;
1186 }
1187
1188 // Force max volume if stream cannot be muted
1189 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1190
1191 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1192 stream, device, index);
1193
1194 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1195 // clear all device specific values
1196 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1197 mStreams[stream].mIndexCur.clear();
1198 }
1199 mStreams[stream].mIndexCur.add(device, index);
1200
1201 // compute and apply stream volume on all outputs according to connected device
1202 status_t status = NO_ERROR;
1203 for (size_t i = 0; i < mOutputs.size(); i++) {
1204 audio_devices_t curDevice =
1205 getDeviceForVolume(mOutputs.valueAt(i)->device());
1206 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1207 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1208 if (volStatus != NO_ERROR) {
1209 status = volStatus;
1210 }
1211 }
1212 }
1213 return status;
1214}
1215
Eric Laurente0720872014-03-11 09:30:41 -07001216status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001217 int *index,
1218 audio_devices_t device)
1219{
1220 if (index == NULL) {
1221 return BAD_VALUE;
1222 }
1223 if (!audio_is_output_device(device)) {
1224 return BAD_VALUE;
1225 }
1226 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1227 // the strategy the stream belongs to.
1228 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1229 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1230 }
1231 device = getDeviceForVolume(device);
1232
1233 *index = mStreams[stream].getVolumeIndex(device);
1234 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1235 return NO_ERROR;
1236}
1237
Eric Laurente0720872014-03-11 09:30:41 -07001238audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001239 const SortedVector<audio_io_handle_t>& outputs)
1240{
1241 // select one output among several suitable for global effects.
1242 // The priority is as follows:
1243 // 1: An offloaded output. If the effect ends up not being offloadable,
1244 // AudioFlinger will invalidate the track and the offloaded output
1245 // will be closed causing the effect to be moved to a PCM output.
1246 // 2: A deep buffer output
1247 // 3: the first output in the list
1248
1249 if (outputs.size() == 0) {
1250 return 0;
1251 }
1252
1253 audio_io_handle_t outputOffloaded = 0;
1254 audio_io_handle_t outputDeepBuffer = 0;
1255
1256 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001257 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001258 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001259 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1260 outputOffloaded = outputs[i];
1261 }
1262 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1263 outputDeepBuffer = outputs[i];
1264 }
1265 }
1266
1267 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1268 outputOffloaded, outputDeepBuffer);
1269 if (outputOffloaded != 0) {
1270 return outputOffloaded;
1271 }
1272 if (outputDeepBuffer != 0) {
1273 return outputDeepBuffer;
1274 }
1275
1276 return outputs[0];
1277}
1278
Eric Laurente0720872014-03-11 09:30:41 -07001279audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001280{
1281 // apply simple rule where global effects are attached to the same output as MUSIC streams
1282
Eric Laurent3b73df72014-03-11 09:06:29 -07001283 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001284 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1285 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1286
1287 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1288 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1289 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1290
1291 return output;
1292}
1293
Eric Laurente0720872014-03-11 09:30:41 -07001294status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001295 audio_io_handle_t io,
1296 uint32_t strategy,
1297 int session,
1298 int id)
1299{
1300 ssize_t index = mOutputs.indexOfKey(io);
1301 if (index < 0) {
1302 index = mInputs.indexOfKey(io);
1303 if (index < 0) {
1304 ALOGW("registerEffect() unknown io %d", io);
1305 return INVALID_OPERATION;
1306 }
1307 }
1308
1309 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1310 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1311 desc->name, desc->memoryUsage);
1312 return INVALID_OPERATION;
1313 }
1314 mTotalEffectsMemory += desc->memoryUsage;
1315 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1316 desc->name, io, strategy, session, id);
1317 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1318
Eric Laurent1f2f2232014-06-02 12:01:23 -07001319 sp<EffectDescriptor> effectDesc = new EffectDescriptor();
1320 memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t));
1321 effectDesc->mIo = io;
1322 effectDesc->mStrategy = (routing_strategy)strategy;
1323 effectDesc->mSession = session;
1324 effectDesc->mEnabled = false;
Eric Laurente552edb2014-03-10 17:42:56 -07001325
Eric Laurent1f2f2232014-06-02 12:01:23 -07001326 mEffects.add(id, effectDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001327
1328 return NO_ERROR;
1329}
1330
Eric Laurente0720872014-03-11 09:30:41 -07001331status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001332{
1333 ssize_t index = mEffects.indexOfKey(id);
1334 if (index < 0) {
1335 ALOGW("unregisterEffect() unknown effect ID %d", id);
1336 return INVALID_OPERATION;
1337 }
1338
Eric Laurent1f2f2232014-06-02 12:01:23 -07001339 sp<EffectDescriptor> effectDesc = mEffects.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001340
Eric Laurent1f2f2232014-06-02 12:01:23 -07001341 setEffectEnabled(effectDesc, false);
Eric Laurente552edb2014-03-10 17:42:56 -07001342
Eric Laurent1f2f2232014-06-02 12:01:23 -07001343 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
Eric Laurente552edb2014-03-10 17:42:56 -07001344 ALOGW("unregisterEffect() memory %d too big for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001345 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1346 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
Eric Laurente552edb2014-03-10 17:42:56 -07001347 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001348 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
Eric Laurente552edb2014-03-10 17:42:56 -07001349 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001350 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
Eric Laurente552edb2014-03-10 17:42:56 -07001351
1352 mEffects.removeItem(id);
Eric Laurente552edb2014-03-10 17:42:56 -07001353
1354 return NO_ERROR;
1355}
1356
Eric Laurente0720872014-03-11 09:30:41 -07001357status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001358{
1359 ssize_t index = mEffects.indexOfKey(id);
1360 if (index < 0) {
1361 ALOGW("unregisterEffect() unknown effect ID %d", id);
1362 return INVALID_OPERATION;
1363 }
1364
1365 return setEffectEnabled(mEffects.valueAt(index), enabled);
1366}
1367
Eric Laurent1f2f2232014-06-02 12:01:23 -07001368status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001369{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001370 if (enabled == effectDesc->mEnabled) {
Eric Laurente552edb2014-03-10 17:42:56 -07001371 ALOGV("setEffectEnabled(%s) effect already %s",
1372 enabled?"true":"false", enabled?"enabled":"disabled");
1373 return INVALID_OPERATION;
1374 }
1375
1376 if (enabled) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001377 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001378 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001379 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
Eric Laurente552edb2014-03-10 17:42:56 -07001380 return INVALID_OPERATION;
1381 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001382 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001383 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1384 } else {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001385 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
Eric Laurente552edb2014-03-10 17:42:56 -07001386 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001387 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1388 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001389 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001390 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001391 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1392 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001393 effectDesc->mEnabled = enabled;
Eric Laurente552edb2014-03-10 17:42:56 -07001394 return NO_ERROR;
1395}
1396
Eric Laurente0720872014-03-11 09:30:41 -07001397bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001398{
1399 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001400 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
1401 if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) &&
1402 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001403 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001404 effectDesc->mDesc.name, effectDesc->mSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001405 return true;
1406 }
1407 }
1408 return false;
1409}
1410
Eric Laurente0720872014-03-11 09:30:41 -07001411bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001412{
1413 nsecs_t sysTime = systemTime();
1414 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001415 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001416 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001417 return true;
1418 }
1419 }
1420 return false;
1421}
1422
Eric Laurente0720872014-03-11 09:30:41 -07001423bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001424 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001425{
1426 nsecs_t sysTime = systemTime();
1427 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001428 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001429 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001430 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001431 return true;
1432 }
1433 }
1434 return false;
1435}
1436
Eric Laurente0720872014-03-11 09:30:41 -07001437bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001438{
1439 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001440 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001441 if ((inputDescriptor->mInputSource == (int)source ||
Eric Laurent3b73df72014-03-11 09:06:29 -07001442 (source == AUDIO_SOURCE_VOICE_RECOGNITION &&
Eric Laurente552edb2014-03-10 17:42:56 -07001443 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1444 && (inputDescriptor->mRefCount > 0)) {
1445 return true;
1446 }
1447 }
1448 return false;
1449}
1450
1451
Eric Laurente0720872014-03-11 09:30:41 -07001452status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07001453{
1454 const size_t SIZE = 256;
1455 char buffer[SIZE];
1456 String8 result;
1457
1458 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1459 result.append(buffer);
1460
1461 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1462 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001463 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1464 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001465 snprintf(buffer, SIZE, " Force use for communications %d\n",
1466 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07001467 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001468 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07001469 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001470 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07001471 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001472 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07001473 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07001474 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07001475 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07001476
Eric Laurent3a4311c2014-03-17 12:00:47 -07001477 snprintf(buffer, SIZE, " Available output devices:\n");
1478 result.append(buffer);
1479 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07001480 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001481 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001482 }
1483 snprintf(buffer, SIZE, "\n Available input devices:\n");
1484 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07001485 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07001486 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07001487 }
Eric Laurente552edb2014-03-10 17:42:56 -07001488
1489 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
1490 write(fd, buffer, strlen(buffer));
1491 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001492 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07001493 write(fd, buffer, strlen(buffer));
1494 mHwModules[i]->dump(fd);
1495 }
1496
1497 snprintf(buffer, SIZE, "\nOutputs dump:\n");
1498 write(fd, buffer, strlen(buffer));
1499 for (size_t i = 0; i < mOutputs.size(); i++) {
1500 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1501 write(fd, buffer, strlen(buffer));
1502 mOutputs.valueAt(i)->dump(fd);
1503 }
1504
1505 snprintf(buffer, SIZE, "\nInputs dump:\n");
1506 write(fd, buffer, strlen(buffer));
1507 for (size_t i = 0; i < mInputs.size(); i++) {
1508 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1509 write(fd, buffer, strlen(buffer));
1510 mInputs.valueAt(i)->dump(fd);
1511 }
1512
1513 snprintf(buffer, SIZE, "\nStreams dump:\n");
1514 write(fd, buffer, strlen(buffer));
1515 snprintf(buffer, SIZE,
1516 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
1517 write(fd, buffer, strlen(buffer));
Eric Laurent3b73df72014-03-11 09:06:29 -07001518 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07001519 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07001520 write(fd, buffer, strlen(buffer));
1521 mStreams[i].dump(fd);
1522 }
1523
1524 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1525 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1526 write(fd, buffer, strlen(buffer));
1527
1528 snprintf(buffer, SIZE, "Registered effects:\n");
1529 write(fd, buffer, strlen(buffer));
1530 for (size_t i = 0; i < mEffects.size(); i++) {
1531 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1532 write(fd, buffer, strlen(buffer));
1533 mEffects.valueAt(i)->dump(fd);
1534 }
1535
1536
1537 return NO_ERROR;
1538}
1539
1540// This function checks for the parameters which can be offloaded.
1541// This can be enhanced depending on the capability of the DSP and policy
1542// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07001543bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07001544{
1545 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07001546 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07001547 offloadInfo.sample_rate, offloadInfo.channel_mask,
1548 offloadInfo.format,
1549 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1550 offloadInfo.has_video);
1551
1552 // Check if offload has been disabled
1553 char propValue[PROPERTY_VALUE_MAX];
1554 if (property_get("audio.offload.disable", propValue, "0")) {
1555 if (atoi(propValue) != 0) {
1556 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1557 return false;
1558 }
1559 }
1560
1561 // Check if stream type is music, then only allow offload as of now.
1562 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1563 {
1564 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
1565 return false;
1566 }
1567
1568 //TODO: enable audio offloading with video when ready
1569 if (offloadInfo.has_video)
1570 {
1571 ALOGV("isOffloadSupported: has_video == true, returning false");
1572 return false;
1573 }
1574
1575 //If duration is less than minimum value defined in property, return false
1576 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1577 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1578 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1579 return false;
1580 }
1581 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1582 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1583 return false;
1584 }
1585
1586 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1587 // creating an offloaded track and tearing it down immediately after start when audioflinger
1588 // detects there is an active non offloadable effect.
1589 // FIXME: We should check the audio session here but we do not have it in this context.
1590 // This may prevent offloading in rare situations where effects are left active by apps
1591 // in the background.
1592 if (isNonOffloadableEffectEnabled()) {
1593 return false;
1594 }
1595
1596 // See if there is a profile to support this.
1597 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07001598 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07001599 offloadInfo.sample_rate,
1600 offloadInfo.format,
1601 offloadInfo.channel_mask,
1602 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07001603 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
1604 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07001605}
1606
Eric Laurent6a94d692014-05-20 11:18:06 -07001607status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
1608 audio_port_type_t type,
1609 unsigned int *num_ports,
1610 struct audio_port *ports,
1611 unsigned int *generation)
1612{
1613 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
1614 generation == NULL) {
1615 return BAD_VALUE;
1616 }
1617 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
1618 if (ports == NULL) {
1619 *num_ports = 0;
1620 }
1621
1622 size_t portsWritten = 0;
1623 size_t portsMax = *num_ports;
1624 *num_ports = 0;
1625 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
1626 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1627 for (size_t i = 0;
1628 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
1629 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
1630 }
1631 *num_ports += mAvailableOutputDevices.size();
1632 }
1633 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1634 for (size_t i = 0;
1635 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
1636 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
1637 }
1638 *num_ports += mAvailableInputDevices.size();
1639 }
1640 }
1641 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
1642 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
1643 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
1644 mInputs[i]->toAudioPort(&ports[portsWritten++]);
1645 }
1646 *num_ports += mInputs.size();
1647 }
1648 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
1649 for (size_t i = 0; i < mOutputs.size() && portsWritten < portsMax; i++) {
1650 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
1651 }
1652 *num_ports += mOutputs.size();
1653 }
1654 }
1655 *generation = curAudioPortGeneration();
1656 ALOGV("listAudioPorts() got %d ports needed %d", portsWritten, *num_ports);
1657 return NO_ERROR;
1658}
1659
1660status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
1661{
1662 return NO_ERROR;
1663}
1664
Eric Laurent1f2f2232014-06-02 12:01:23 -07001665sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07001666 audio_port_handle_t id) const
1667{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001668 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07001669 for (size_t i = 0; i < mOutputs.size(); i++) {
1670 outputDesc = mOutputs.valueAt(i);
1671 if (outputDesc->mId == id) {
1672 break;
1673 }
1674 }
1675 return outputDesc;
1676}
1677
Eric Laurent1f2f2232014-06-02 12:01:23 -07001678sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07001679 audio_port_handle_t id) const
1680{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001681 sp<AudioInputDescriptor> inputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07001682 for (size_t i = 0; i < mInputs.size(); i++) {
1683 inputDesc = mInputs.valueAt(i);
1684 if (inputDesc->mId == id) {
1685 break;
1686 }
1687 }
1688 return inputDesc;
1689}
1690
Eric Laurent1f2f2232014-06-02 12:01:23 -07001691sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice(
1692 audio_devices_t device) const
Eric Laurent6a94d692014-05-20 11:18:06 -07001693{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001694 sp <HwModule> module;
1695
Eric Laurent6a94d692014-05-20 11:18:06 -07001696 for (size_t i = 0; i < mHwModules.size(); i++) {
1697 if (mHwModules[i]->mHandle == 0) {
1698 continue;
1699 }
1700 if (audio_is_output_device(device)) {
1701 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
1702 {
1703 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
1704 return mHwModules[i];
1705 }
1706 }
1707 } else {
1708 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
1709 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
1710 device & ~AUDIO_DEVICE_BIT_IN) {
1711 return mHwModules[i];
1712 }
1713 }
1714 }
1715 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001716 return module;
Eric Laurent6a94d692014-05-20 11:18:06 -07001717}
1718
Eric Laurent1f2f2232014-06-02 12:01:23 -07001719sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const
Eric Laurent1afeecb2014-05-14 08:52:28 -07001720{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001721 sp <HwModule> module;
1722
Eric Laurent1afeecb2014-05-14 08:52:28 -07001723 for (size_t i = 0; i < mHwModules.size(); i++)
1724 {
1725 if (strcmp(mHwModules[i]->mName, name) == 0) {
1726 return mHwModules[i];
1727 }
1728 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001729 return module;
Eric Laurent1afeecb2014-05-14 08:52:28 -07001730}
1731
1732
Eric Laurent6a94d692014-05-20 11:18:06 -07001733status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
1734 audio_patch_handle_t *handle,
1735 uid_t uid)
1736{
1737 ALOGV("createAudioPatch()");
1738
1739 if (handle == NULL || patch == NULL) {
1740 return BAD_VALUE;
1741 }
1742 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
1743
1744 if (patch->num_sources > 1 || patch->num_sinks > 1) {
1745 return INVALID_OPERATION;
1746 }
1747 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE ||
1748 patch->sinks[0].role != AUDIO_PORT_ROLE_SINK) {
1749 return INVALID_OPERATION;
1750 }
1751
1752 sp<AudioPatch> patchDesc;
1753 ssize_t index = mAudioPatches.indexOfKey(*handle);
1754
1755 ALOGV("createAudioPatch sink id %d role %d type %d", patch->sinks[0].id, patch->sinks[0].role,
1756 patch->sinks[0].type);
1757 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
1758 patch->sources[0].role,
1759 patch->sources[0].type);
1760
1761 if (index >= 0) {
1762 patchDesc = mAudioPatches.valueAt(index);
1763 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
1764 mUidCached, patchDesc->mUid, uid);
1765 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
1766 return INVALID_OPERATION;
1767 }
1768 } else {
1769 *handle = 0;
1770 }
1771
1772 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
1773 // TODO add support for mix to mix connection
1774 if (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) {
1775 ALOGV("createAudioPatch() source mix sink not device");
1776 return BAD_VALUE;
1777 }
1778 // output mix to output device connection
Eric Laurent1f2f2232014-06-02 12:01:23 -07001779 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001780 if (outputDesc == NULL) {
1781 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
1782 return BAD_VALUE;
1783 }
1784 if (patchDesc != 0) {
1785 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
1786 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
1787 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
1788 return BAD_VALUE;
1789 }
1790 }
1791 sp<DeviceDescriptor> devDesc =
1792 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1793 if (devDesc == 0) {
1794 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[0].id);
1795 return BAD_VALUE;
1796 }
1797
1798 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mType,
1799 patch->sources[0].sample_rate,
1800 patch->sources[0].format,
1801 patch->sources[0].channel_mask,
1802 AUDIO_OUTPUT_FLAG_NONE)) {
1803 return INVALID_OPERATION;
1804 }
1805 // TODO: reconfigure output format and channels here
1806 ALOGV("createAudioPatch() setting device %08x on output %d",
1807 devDesc->mType, outputDesc->mIoHandle);
1808 setOutputDevice(outputDesc->mIoHandle,
1809 devDesc->mType,
1810 true,
1811 0,
1812 handle);
1813 index = mAudioPatches.indexOfKey(*handle);
1814 if (index >= 0) {
1815 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1816 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
1817 }
1818 patchDesc = mAudioPatches.valueAt(index);
1819 patchDesc->mUid = uid;
1820 ALOGV("createAudioPatch() success");
1821 } else {
1822 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
1823 return INVALID_OPERATION;
1824 }
1825 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1826 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
1827 // input device to input mix connection
Eric Laurent1f2f2232014-06-02 12:01:23 -07001828 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001829 if (inputDesc == NULL) {
1830 return BAD_VALUE;
1831 }
1832 if (patchDesc != 0) {
1833 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1834 return BAD_VALUE;
1835 }
1836 }
1837 sp<DeviceDescriptor> devDesc =
1838 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1839 if (devDesc == 0) {
1840 return BAD_VALUE;
1841 }
1842
1843 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mType,
1844 patch->sinks[0].sample_rate,
1845 patch->sinks[0].format,
1846 patch->sinks[0].channel_mask,
1847 AUDIO_OUTPUT_FLAG_NONE)) {
1848 return INVALID_OPERATION;
1849 }
1850 // TODO: reconfigure output format and channels here
1851 ALOGV("createAudioPatch() setting device %08x on output %d",
1852 devDesc->mType, inputDesc->mIoHandle);
1853 setInputDevice(inputDesc->mIoHandle,
1854 devDesc->mType,
1855 true,
1856 handle);
1857 index = mAudioPatches.indexOfKey(*handle);
1858 if (index >= 0) {
1859 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
1860 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
1861 }
1862 patchDesc = mAudioPatches.valueAt(index);
1863 patchDesc->mUid = uid;
1864 ALOGV("createAudioPatch() success");
1865 } else {
1866 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
1867 return INVALID_OPERATION;
1868 }
1869 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1870 // device to device connection
1871 if (patchDesc != 0) {
1872 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id &&
1873 patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
1874 return BAD_VALUE;
1875 }
1876 }
1877
1878 sp<DeviceDescriptor> srcDeviceDesc =
1879 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
1880 sp<DeviceDescriptor> sinkDeviceDesc =
1881 mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id);
1882 if (srcDeviceDesc == 0 || sinkDeviceDesc == 0) {
1883 return BAD_VALUE;
1884 }
1885 //update source and sink with our own data as the data passed in the patch may
1886 // be incomplete.
1887 struct audio_patch newPatch = *patch;
1888 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
1889 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[0], &patch->sinks[0]);
1890
1891 // TODO: add support for devices on different HW modules
1892 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
1893 return INVALID_OPERATION;
1894 }
1895 // TODO: check from routing capabilities in config file and other conflicting patches
1896
1897 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
1898 if (index >= 0) {
1899 afPatchHandle = patchDesc->mAfPatchHandle;
1900 }
1901
1902 status_t status = mpClientInterface->createAudioPatch(&newPatch,
1903 &afPatchHandle,
1904 0);
1905 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
1906 status, afPatchHandle);
1907 if (status == NO_ERROR) {
1908 if (index < 0) {
1909 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
1910 &newPatch, uid);
1911 addAudioPatch(patchDesc->mHandle, patchDesc);
1912 } else {
1913 patchDesc->mPatch = newPatch;
1914 }
1915 patchDesc->mAfPatchHandle = afPatchHandle;
1916 *handle = patchDesc->mHandle;
1917 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001918 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07001919 } else {
1920 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
1921 status);
1922 return INVALID_OPERATION;
1923 }
1924 } else {
1925 return BAD_VALUE;
1926 }
1927 } else {
1928 return BAD_VALUE;
1929 }
1930 return NO_ERROR;
1931}
1932
1933status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
1934 uid_t uid)
1935{
1936 ALOGV("releaseAudioPatch() patch %d", handle);
1937
1938 ssize_t index = mAudioPatches.indexOfKey(handle);
1939
1940 if (index < 0) {
1941 return BAD_VALUE;
1942 }
1943 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
1944 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
1945 mUidCached, patchDesc->mUid, uid);
1946 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
1947 return INVALID_OPERATION;
1948 }
1949
1950 struct audio_patch *patch = &patchDesc->mPatch;
1951 patchDesc->mUid = mUidCached;
1952 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001953 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001954 if (outputDesc == NULL) {
1955 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
1956 return BAD_VALUE;
1957 }
1958
1959 setOutputDevice(outputDesc->mIoHandle,
1960 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
1961 true,
1962 0,
1963 NULL);
1964 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1965 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001966 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07001967 if (inputDesc == NULL) {
1968 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
1969 return BAD_VALUE;
1970 }
1971 setInputDevice(inputDesc->mIoHandle,
1972 getNewInputDevice(inputDesc->mIoHandle),
1973 true,
1974 NULL);
1975 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
1976 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
1977 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1978 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
1979 status, patchDesc->mAfPatchHandle);
1980 removeAudioPatch(patchDesc->mHandle);
1981 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07001982 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07001983 } else {
1984 return BAD_VALUE;
1985 }
1986 } else {
1987 return BAD_VALUE;
1988 }
1989 return NO_ERROR;
1990}
1991
1992status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
1993 struct audio_patch *patches,
1994 unsigned int *generation)
1995{
1996 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
1997 generation == NULL) {
1998 return BAD_VALUE;
1999 }
2000 ALOGV("listAudioPatches() num_patches %d patches %p available patches %d",
2001 *num_patches, patches, mAudioPatches.size());
2002 if (patches == NULL) {
2003 *num_patches = 0;
2004 }
2005
2006 size_t patchesWritten = 0;
2007 size_t patchesMax = *num_patches;
2008 for (size_t i = 0;
2009 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2010 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2011 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
2012 ALOGV("listAudioPatches() patch %d num_sources %d num_sinks %d",
2013 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2014 }
2015 *num_patches = mAudioPatches.size();
2016
2017 *generation = curAudioPortGeneration();
2018 ALOGV("listAudioPatches() got %d patches needed %d", patchesWritten, *num_patches);
2019 return NO_ERROR;
2020}
2021
Eric Laurente1715a42014-05-20 11:30:42 -07002022status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002023{
Eric Laurente1715a42014-05-20 11:30:42 -07002024 ALOGV("setAudioPortConfig()");
2025
2026 if (config == NULL) {
2027 return BAD_VALUE;
2028 }
2029 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2030 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002031 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2032 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002033 }
2034
Eric Laurenta121f902014-06-03 13:32:54 -07002035 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002036 if (config->type == AUDIO_PORT_TYPE_MIX) {
2037 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002038 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002039 if (outputDesc == NULL) {
2040 return BAD_VALUE;
2041 }
Eric Laurenta121f902014-06-03 13:32:54 -07002042 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002043 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002044 sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002045 if (inputDesc == NULL) {
2046 return BAD_VALUE;
2047 }
Eric Laurenta121f902014-06-03 13:32:54 -07002048 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002049 } else {
2050 return BAD_VALUE;
2051 }
2052 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2053 sp<DeviceDescriptor> deviceDesc;
2054 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2055 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2056 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2057 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2058 } else {
2059 return BAD_VALUE;
2060 }
2061 if (deviceDesc == NULL) {
2062 return BAD_VALUE;
2063 }
Eric Laurenta121f902014-06-03 13:32:54 -07002064 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002065 } else {
2066 return BAD_VALUE;
2067 }
2068
Eric Laurenta121f902014-06-03 13:32:54 -07002069 struct audio_port_config backupConfig;
2070 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2071 if (status == NO_ERROR) {
2072 struct audio_port_config newConfig;
2073 audioPortConfig->toAudioPortConfig(&newConfig, config);
2074 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002075 }
Eric Laurenta121f902014-06-03 13:32:54 -07002076 if (status != NO_ERROR) {
2077 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002078 }
Eric Laurente1715a42014-05-20 11:30:42 -07002079
2080 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002081}
2082
2083void AudioPolicyManager::clearAudioPatches(uid_t uid)
2084{
2085 for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++) {
2086 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2087 if (patchDesc->mUid == uid) {
2088 // releaseAudioPatch() removes the patch from mAudioPatches
2089 if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) {
2090 i--;
2091 }
2092 }
2093 }
2094}
2095
2096status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2097 const sp<AudioPatch>& patch)
2098{
2099 ssize_t index = mAudioPatches.indexOfKey(handle);
2100
2101 if (index >= 0) {
2102 ALOGW("addAudioPatch() patch %d already in", handle);
2103 return ALREADY_EXISTS;
2104 }
2105 mAudioPatches.add(handle, patch);
2106 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2107 "sink handle %d",
2108 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2109 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2110 return NO_ERROR;
2111}
2112
2113status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2114{
2115 ssize_t index = mAudioPatches.indexOfKey(handle);
2116
2117 if (index < 0) {
2118 ALOGW("removeAudioPatch() patch %d not in", handle);
2119 return ALREADY_EXISTS;
2120 }
2121 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2122 mAudioPatches.valueAt(index)->mAfPatchHandle);
2123 mAudioPatches.removeItemsAt(index);
2124 return NO_ERROR;
2125}
2126
Eric Laurente552edb2014-03-10 17:42:56 -07002127// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002128// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002129// ----------------------------------------------------------------------------
2130
Eric Laurent3a4311c2014-03-17 12:00:47 -07002131uint32_t AudioPolicyManager::nextUniqueId()
2132{
2133 return android_atomic_inc(&mNextUniqueId);
2134}
2135
Eric Laurent6a94d692014-05-20 11:18:06 -07002136uint32_t AudioPolicyManager::nextAudioPortGeneration()
2137{
2138 return android_atomic_inc(&mAudioPortGeneration);
2139}
2140
Eric Laurente0720872014-03-11 09:30:41 -07002141AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002142 :
2143#ifdef AUDIO_POLICY_TEST
2144 Thread(false),
2145#endif //AUDIO_POLICY_TEST
2146 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002147 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002148 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2149 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002150 mA2dpSuspended(false),
Eric Laurent6a94d692014-05-20 11:18:06 -07002151 mSpeakerDrcEnabled(false), mNextUniqueId(1),
2152 mAudioPortGeneration(1)
Eric Laurente552edb2014-03-10 17:42:56 -07002153{
Eric Laurent6a94d692014-05-20 11:18:06 -07002154 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002155 mpClientInterface = clientInterface;
2156
Eric Laurent3b73df72014-03-11 09:06:29 -07002157 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2158 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002159 }
2160
Eric Laurent1afeecb2014-05-14 08:52:28 -07002161 mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002162 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2163 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2164 ALOGE("could not load audio policy configuration file, setting defaults");
2165 defaultAudioPolicyConfig();
2166 }
2167 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002168 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002169
2170 // must be done after reading the policy
2171 initializeVolumeCurves();
2172
2173 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002174 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2175 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002176 for (size_t i = 0; i < mHwModules.size(); i++) {
2177 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2178 if (mHwModules[i]->mHandle == 0) {
2179 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2180 continue;
2181 }
2182 // open all output streams needed to access attached devices
2183 // except for direct output streams that are only opened when they are actually
2184 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002185 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002186 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2187 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002188 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002189
Eric Laurent3a4311c2014-03-17 12:00:47 -07002190 if (outProfile->mSupportedDevices.isEmpty()) {
2191 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2192 continue;
2193 }
2194
2195 audio_devices_t profileTypes = outProfile->mSupportedDevices.types();
2196 if ((profileTypes & outputDeviceTypes) &&
Eric Laurente552edb2014-03-10 17:42:56 -07002197 ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002198 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002199
Eric Laurent1c333e22014-05-20 10:48:17 -07002200 outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice->mDeviceType & profileTypes);
Eric Laurente552edb2014-03-10 17:42:56 -07002201 audio_io_handle_t output = mpClientInterface->openOutput(
2202 outProfile->mModule->mHandle,
2203 &outputDesc->mDevice,
2204 &outputDesc->mSamplingRate,
2205 &outputDesc->mFormat,
2206 &outputDesc->mChannelMask,
2207 &outputDesc->mLatency,
2208 outputDesc->mFlags);
2209 if (output == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002210 ALOGW("Cannot open output stream for device %08x on hw module %s",
2211 outputDesc->mDevice,
2212 mHwModules[i]->mName);
Eric Laurente552edb2014-03-10 17:42:56 -07002213 } else {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002214 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002215 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002216 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002217 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002218 // give a valid ID to an attached device once confirmed it is reachable
2219 if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) {
2220 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002221 mAvailableOutputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002222 }
2223 }
Eric Laurente552edb2014-03-10 17:42:56 -07002224 if (mPrimaryOutput == 0 &&
2225 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2226 mPrimaryOutput = output;
2227 }
2228 addOutput(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002229 ALOGI("CSTOR setOutputDevice %08x", outputDesc->mDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07002230 setOutputDevice(output,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002231 outputDesc->mDevice,
Eric Laurente552edb2014-03-10 17:42:56 -07002232 true);
2233 }
2234 }
2235 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002236 // open input streams needed to access attached devices to validate
2237 // mAvailableInputDevices list
2238 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
2239 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002240 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002241
Eric Laurent3a4311c2014-03-17 12:00:47 -07002242 if (inProfile->mSupportedDevices.isEmpty()) {
2243 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
2244 continue;
2245 }
2246
2247 audio_devices_t profileTypes = inProfile->mSupportedDevices.types();
2248 if (profileTypes & inputDeviceTypes) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002249 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002250
2251 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
Eric Laurent1c333e22014-05-20 10:48:17 -07002252 inputDesc->mDevice = inProfile->mSupportedDevices[0]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002253 audio_io_handle_t input = mpClientInterface->openInput(
2254 inProfile->mModule->mHandle,
2255 &inputDesc->mDevice,
2256 &inputDesc->mSamplingRate,
2257 &inputDesc->mFormat,
2258 &inputDesc->mChannelMask);
2259
2260 if (input != 0) {
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002261 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002262 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002263 ssize_t index =
Eric Laurent5b61ddd2014-05-07 09:10:01 -07002264 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002265 // give a valid ID to an attached device once confirmed it is reachable
2266 if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) {
2267 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -07002268 mAvailableInputDevices[index]->mModule = mHwModules[i];
Eric Laurent3a4311c2014-03-17 12:00:47 -07002269 }
2270 }
2271 mpClientInterface->closeInput(input);
2272 } else {
2273 ALOGW("Cannot open input stream for device %08x on hw module %s",
2274 inputDesc->mDevice,
2275 mHwModules[i]->mName);
2276 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002277 }
2278 }
2279 }
2280 // make sure all attached devices have been allocated a unique ID
2281 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
2282 if (mAvailableOutputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002283 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002284 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
2285 continue;
2286 }
2287 i++;
2288 }
2289 for (size_t i = 0; i < mAvailableInputDevices.size();) {
2290 if (mAvailableInputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002291 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002292 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
2293 continue;
2294 }
2295 i++;
2296 }
2297 // make sure default device is reachable
2298 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002299 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002300 }
Eric Laurente552edb2014-03-10 17:42:56 -07002301
2302 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
2303
2304 updateDevicesAndOutputs();
2305
2306#ifdef AUDIO_POLICY_TEST
2307 if (mPrimaryOutput != 0) {
2308 AudioParameter outputCmd = AudioParameter();
2309 outputCmd.addInt(String8("set_id"), 0);
2310 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2311
2312 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
2313 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07002314 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
2315 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002316 mTestLatencyMs = 0;
2317 mCurOutput = 0;
2318 mDirectOutput = false;
2319 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2320 mTestOutputs[i] = 0;
2321 }
2322
2323 const size_t SIZE = 256;
2324 char buffer[SIZE];
2325 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
2326 run(buffer, ANDROID_PRIORITY_AUDIO);
2327 }
2328#endif //AUDIO_POLICY_TEST
2329}
2330
Eric Laurente0720872014-03-11 09:30:41 -07002331AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07002332{
2333#ifdef AUDIO_POLICY_TEST
2334 exit();
2335#endif //AUDIO_POLICY_TEST
2336 for (size_t i = 0; i < mOutputs.size(); i++) {
2337 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002338 }
2339 for (size_t i = 0; i < mInputs.size(); i++) {
2340 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07002341 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002342 mAvailableOutputDevices.clear();
2343 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07002344 mOutputs.clear();
2345 mInputs.clear();
2346 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07002347}
2348
Eric Laurente0720872014-03-11 09:30:41 -07002349status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07002350{
2351 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
2352}
2353
2354#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07002355bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07002356{
2357 ALOGV("entering threadLoop()");
2358 while (!exitPending())
2359 {
2360 String8 command;
2361 int valueInt;
2362 String8 value;
2363
2364 Mutex::Autolock _l(mLock);
2365 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
2366
2367 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
2368 AudioParameter param = AudioParameter(command);
2369
2370 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
2371 valueInt != 0) {
2372 ALOGV("Test command %s received", command.string());
2373 String8 target;
2374 if (param.get(String8("target"), target) != NO_ERROR) {
2375 target = "Manager";
2376 }
2377 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
2378 param.remove(String8("test_cmd_policy_output"));
2379 mCurOutput = valueInt;
2380 }
2381 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
2382 param.remove(String8("test_cmd_policy_direct"));
2383 if (value == "false") {
2384 mDirectOutput = false;
2385 } else if (value == "true") {
2386 mDirectOutput = true;
2387 }
2388 }
2389 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
2390 param.remove(String8("test_cmd_policy_input"));
2391 mTestInput = valueInt;
2392 }
2393
2394 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
2395 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07002396 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07002397 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002398 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002399 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002400 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07002401 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002402 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07002403 }
Eric Laurent3b73df72014-03-11 09:06:29 -07002404 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07002405 if (target == "Manager") {
2406 mTestFormat = format;
2407 } else if (mTestOutputs[mCurOutput] != 0) {
2408 AudioParameter outputParam = AudioParameter();
2409 outputParam.addInt(String8("format"), format);
2410 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2411 }
2412 }
2413 }
2414 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
2415 param.remove(String8("test_cmd_policy_channels"));
2416 int channels = 0;
2417
2418 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002419 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07002420 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07002421 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07002422 }
2423 if (channels != 0) {
2424 if (target == "Manager") {
2425 mTestChannels = channels;
2426 } else if (mTestOutputs[mCurOutput] != 0) {
2427 AudioParameter outputParam = AudioParameter();
2428 outputParam.addInt(String8("channels"), channels);
2429 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2430 }
2431 }
2432 }
2433 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
2434 param.remove(String8("test_cmd_policy_sampleRate"));
2435 if (valueInt >= 0 && valueInt <= 96000) {
2436 int samplingRate = valueInt;
2437 if (target == "Manager") {
2438 mTestSamplingRate = samplingRate;
2439 } else if (mTestOutputs[mCurOutput] != 0) {
2440 AudioParameter outputParam = AudioParameter();
2441 outputParam.addInt(String8("sampling_rate"), samplingRate);
2442 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
2443 }
2444 }
2445 }
2446
2447 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
2448 param.remove(String8("test_cmd_policy_reopen"));
2449
Eric Laurent1f2f2232014-06-02 12:01:23 -07002450 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07002451 mpClientInterface->closeOutput(mPrimaryOutput);
2452
2453 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
2454
Eric Laurente552edb2014-03-10 17:42:56 -07002455 mOutputs.removeItem(mPrimaryOutput);
2456
Eric Laurent1f2f2232014-06-02 12:01:23 -07002457 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07002458 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
2459 mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
2460 &outputDesc->mDevice,
2461 &outputDesc->mSamplingRate,
2462 &outputDesc->mFormat,
2463 &outputDesc->mChannelMask,
2464 &outputDesc->mLatency,
2465 outputDesc->mFlags);
2466 if (mPrimaryOutput == 0) {
2467 ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
2468 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
2469 } else {
2470 AudioParameter outputCmd = AudioParameter();
2471 outputCmd.addInt(String8("set_id"), 0);
2472 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
2473 addOutput(mPrimaryOutput, outputDesc);
2474 }
2475 }
2476
2477
2478 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
2479 }
2480 }
2481 return false;
2482}
2483
Eric Laurente0720872014-03-11 09:30:41 -07002484void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07002485{
2486 {
2487 AutoMutex _l(mLock);
2488 requestExit();
2489 mWaitWorkCV.signal();
2490 }
2491 requestExitAndWait();
2492}
2493
Eric Laurente0720872014-03-11 09:30:41 -07002494int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002495{
2496 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
2497 if (output == mTestOutputs[i]) return i;
2498 }
2499 return 0;
2500}
2501#endif //AUDIO_POLICY_TEST
2502
2503// ---
2504
Eric Laurent1f2f2232014-06-02 12:01:23 -07002505void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07002506{
Eric Laurent1c333e22014-05-20 10:48:17 -07002507 outputDesc->mIoHandle = output;
2508 outputDesc->mId = nextUniqueId();
2509 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002510 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002511}
2512
Eric Laurent1f2f2232014-06-02 12:01:23 -07002513void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07002514{
Eric Laurent1c333e22014-05-20 10:48:17 -07002515 inputDesc->mIoHandle = input;
2516 inputDesc->mId = nextUniqueId();
2517 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07002518 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002519}
Eric Laurente552edb2014-03-10 17:42:56 -07002520
Eric Laurent3a4311c2014-03-17 12:00:47 -07002521String8 AudioPolicyManager::addressToParameter(audio_devices_t device, const String8 address)
2522{
2523 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
2524 return String8("a2dp_sink_address=")+address;
2525 }
2526 return address;
2527}
2528
Eric Laurente0720872014-03-11 09:30:41 -07002529status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -07002530 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07002531 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07002532 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07002533{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002534 sp<AudioOutputDescriptor> desc;
Eric Laurente552edb2014-03-10 17:42:56 -07002535
Eric Laurent3b73df72014-03-11 09:06:29 -07002536 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07002537 // first list already open outputs that can be routed to this device
2538 for (size_t i = 0; i < mOutputs.size(); i++) {
2539 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002540 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Eric Laurente552edb2014-03-10 17:42:56 -07002541 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
2542 outputs.add(mOutputs.keyAt(i));
2543 }
2544 }
2545 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002546 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07002547 for (size_t i = 0; i < mHwModules.size(); i++)
2548 {
2549 if (mHwModules[i]->mHandle == 0) {
2550 continue;
2551 }
2552 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2553 {
Eric Laurent3a4311c2014-03-17 12:00:47 -07002554 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
Eric Laurentd4692962014-05-05 18:13:44 -07002555 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002556 profiles.add(mHwModules[i]->mOutputProfiles[j]);
2557 }
2558 }
2559 }
2560
2561 if (profiles.isEmpty() && outputs.isEmpty()) {
2562 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2563 return BAD_VALUE;
2564 }
2565
2566 // open outputs for matching profiles if needed. Direct outputs are also opened to
2567 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2568 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002569 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07002570
2571 // nothing to do if one output is already opened for this profile
2572 size_t j;
2573 for (j = 0; j < mOutputs.size(); j++) {
2574 desc = mOutputs.valueAt(j);
2575 if (!desc->isDuplicated() && desc->mProfile == profile) {
2576 break;
2577 }
2578 }
2579 if (j != mOutputs.size()) {
2580 continue;
2581 }
2582
Eric Laurent3a4311c2014-03-17 12:00:47 -07002583 ALOGV("opening output for device %08x with params %s", device, address.string());
Eric Laurente552edb2014-03-10 17:42:56 -07002584 desc = new AudioOutputDescriptor(profile);
2585 desc->mDevice = device;
2586 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
2587 offloadInfo.sample_rate = desc->mSamplingRate;
2588 offloadInfo.format = desc->mFormat;
2589 offloadInfo.channel_mask = desc->mChannelMask;
2590
2591 audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
2592 &desc->mDevice,
2593 &desc->mSamplingRate,
2594 &desc->mFormat,
2595 &desc->mChannelMask,
2596 &desc->mLatency,
2597 desc->mFlags,
2598 &offloadInfo);
2599 if (output != 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07002600 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07002601 if (!address.isEmpty()) {
2602 mpClientInterface->setParameters(output, addressToParameter(device, address));
Eric Laurente552edb2014-03-10 17:42:56 -07002603 }
2604
Eric Laurentd4692962014-05-05 18:13:44 -07002605 // Here is where we step through and resolve any "dynamic" fields
2606 String8 reply;
2607 char *value;
2608 if (profile->mSamplingRates[0] == 0) {
2609 reply = mpClientInterface->getParameters(output,
2610 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2611 ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
2612 reply.string());
2613 value = strpbrk((char *)reply.string(), "=");
2614 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002615 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002616 }
Eric Laurentd4692962014-05-05 18:13:44 -07002617 }
2618 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2619 reply = mpClientInterface->getParameters(output,
2620 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2621 ALOGV("checkOutputsForDevice() direct output sup formats %s",
2622 reply.string());
2623 value = strpbrk((char *)reply.string(), "=");
2624 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002625 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002626 }
Eric Laurentd4692962014-05-05 18:13:44 -07002627 }
2628 if (profile->mChannelMasks[0] == 0) {
2629 reply = mpClientInterface->getParameters(output,
2630 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2631 ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
2632 reply.string());
2633 value = strpbrk((char *)reply.string(), "=");
2634 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002635 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002636 }
Eric Laurentd4692962014-05-05 18:13:44 -07002637 }
2638 if (((profile->mSamplingRates[0] == 0) &&
2639 (profile->mSamplingRates.size() < 2)) ||
2640 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
2641 (profile->mFormats.size() < 2)) ||
2642 ((profile->mChannelMasks[0] == 0) &&
2643 (profile->mChannelMasks.size() < 2))) {
2644 ALOGW("checkOutputsForDevice() direct output missing param");
2645 mpClientInterface->closeOutput(output);
2646 output = 0;
2647 } else if (profile->mSamplingRates[0] == 0) {
2648 mpClientInterface->closeOutput(output);
2649 desc->mSamplingRate = profile->mSamplingRates[1];
2650 offloadInfo.sample_rate = desc->mSamplingRate;
2651 output = mpClientInterface->openOutput(
2652 profile->mModule->mHandle,
2653 &desc->mDevice,
2654 &desc->mSamplingRate,
2655 &desc->mFormat,
2656 &desc->mChannelMask,
2657 &desc->mLatency,
2658 desc->mFlags,
2659 &offloadInfo);
2660 }
2661
2662 if (output != 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07002663 addOutput(output, desc);
Eric Laurentd4692962014-05-05 18:13:44 -07002664 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
2665 audio_io_handle_t duplicatedOutput = 0;
Eric Laurente552edb2014-03-10 17:42:56 -07002666
Eric Laurentd4692962014-05-05 18:13:44 -07002667 // set initial stream volume for device
2668 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07002669
Eric Laurentd4692962014-05-05 18:13:44 -07002670 //TODO: configure audio effect output stage here
2671
2672 // open a duplicating output thread for the new output and the primary output
2673 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
2674 mPrimaryOutput);
2675 if (duplicatedOutput != 0) {
2676 // add duplicated output descriptor
Eric Laurent1f2f2232014-06-02 12:01:23 -07002677 sp<AudioOutputDescriptor> dupOutputDesc = new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07002678 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
2679 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
2680 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
2681 dupOutputDesc->mFormat = desc->mFormat;
2682 dupOutputDesc->mChannelMask = desc->mChannelMask;
2683 dupOutputDesc->mLatency = desc->mLatency;
2684 addOutput(duplicatedOutput, dupOutputDesc);
2685 applyStreamVolumes(duplicatedOutput, device, 0, true);
2686 } else {
2687 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
2688 mPrimaryOutput, output);
2689 mpClientInterface->closeOutput(output);
2690 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07002691 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07002692 output = 0;
2693 }
Eric Laurente552edb2014-03-10 17:42:56 -07002694 }
2695 }
2696 }
2697 if (output == 0) {
2698 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07002699 profiles.removeAt(profile_index);
2700 profile_index--;
2701 } else {
2702 outputs.add(output);
2703 ALOGV("checkOutputsForDevice(): adding output %d", output);
2704 }
2705 }
2706
2707 if (profiles.isEmpty()) {
2708 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
2709 return BAD_VALUE;
2710 }
Eric Laurentd4692962014-05-05 18:13:44 -07002711 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07002712 // check if one opened output is not needed any more after disconnecting one device
2713 for (size_t i = 0; i < mOutputs.size(); i++) {
2714 desc = mOutputs.valueAt(i);
2715 if (!desc->isDuplicated() &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07002716 !(desc->mProfile->mSupportedDevices.types() &
2717 mAvailableOutputDevices.types())) {
Eric Laurente552edb2014-03-10 17:42:56 -07002718 ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
2719 outputs.add(mOutputs.keyAt(i));
2720 }
2721 }
Eric Laurentd4692962014-05-05 18:13:44 -07002722 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07002723 for (size_t i = 0; i < mHwModules.size(); i++)
2724 {
2725 if (mHwModules[i]->mHandle == 0) {
2726 continue;
2727 }
2728 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2729 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002730 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07002731 if (profile->mSupportedDevices.types() & device) {
2732 ALOGV("checkOutputsForDevice(): "
2733 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07002734 if (profile->mSamplingRates[0] == 0) {
2735 profile->mSamplingRates.clear();
2736 profile->mSamplingRates.add(0);
2737 }
2738 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2739 profile->mFormats.clear();
2740 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2741 }
2742 if (profile->mChannelMasks[0] == 0) {
2743 profile->mChannelMasks.clear();
2744 profile->mChannelMasks.add(0);
2745 }
2746 }
2747 }
2748 }
2749 }
2750 return NO_ERROR;
2751}
2752
Eric Laurentd4692962014-05-05 18:13:44 -07002753status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
2754 audio_policy_dev_state_t state,
2755 SortedVector<audio_io_handle_t>& inputs,
2756 const String8 address)
2757{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002758 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07002759 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
2760 // first list already open inputs that can be routed to this device
2761 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2762 desc = mInputs.valueAt(input_index);
2763 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
2764 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2765 inputs.add(mInputs.keyAt(input_index));
2766 }
2767 }
2768
2769 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07002770 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07002771 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
2772 {
2773 if (mHwModules[module_idx]->mHandle == 0) {
2774 continue;
2775 }
2776 for (size_t profile_index = 0;
2777 profile_index < mHwModules[module_idx]->mInputProfiles.size();
2778 profile_index++)
2779 {
2780 if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types()
2781 & (device & ~AUDIO_DEVICE_BIT_IN)) {
2782 ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2783 profile_index, module_idx);
2784 profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]);
2785 }
2786 }
2787 }
2788
2789 if (profiles.isEmpty() && inputs.isEmpty()) {
2790 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2791 return BAD_VALUE;
2792 }
2793
2794 // open inputs for matching profiles if needed. Direct inputs are also opened to
2795 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2796 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2797
Eric Laurent1c333e22014-05-20 10:48:17 -07002798 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002799 // nothing to do if one input is already opened for this profile
2800 size_t input_index;
2801 for (input_index = 0; input_index < mInputs.size(); input_index++) {
2802 desc = mInputs.valueAt(input_index);
2803 if (desc->mProfile == profile) {
2804 break;
2805 }
2806 }
2807 if (input_index != mInputs.size()) {
2808 continue;
2809 }
2810
2811 ALOGV("opening input for device 0x%X with params %s", device, address.string());
2812 desc = new AudioInputDescriptor(profile);
2813 desc->mDevice = device;
2814
2815 audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2816 &desc->mDevice,
2817 &desc->mSamplingRate,
2818 &desc->mFormat,
2819 &desc->mChannelMask);
2820
2821 if (input != 0) {
2822 if (!address.isEmpty()) {
2823 mpClientInterface->setParameters(input, addressToParameter(device, address));
2824 }
2825
2826 // Here is where we step through and resolve any "dynamic" fields
2827 String8 reply;
2828 char *value;
2829 if (profile->mSamplingRates[0] == 0) {
2830 reply = mpClientInterface->getParameters(input,
2831 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2832 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2833 reply.string());
2834 value = strpbrk((char *)reply.string(), "=");
2835 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002836 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002837 }
2838 }
2839 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2840 reply = mpClientInterface->getParameters(input,
2841 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2842 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2843 value = strpbrk((char *)reply.string(), "=");
2844 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002845 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002846 }
2847 }
2848 if (profile->mChannelMasks[0] == 0) {
2849 reply = mpClientInterface->getParameters(input,
2850 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2851 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2852 reply.string());
2853 value = strpbrk((char *)reply.string(), "=");
2854 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002855 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07002856 }
2857 }
2858 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2859 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2860 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2861 ALOGW("checkInputsForDevice() direct input missing param");
2862 mpClientInterface->closeInput(input);
2863 input = 0;
2864 }
2865
2866 if (input != 0) {
2867 addInput(input, desc);
2868 }
2869 } // endif input != 0
2870
2871 if (input == 0) {
2872 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07002873 profiles.removeAt(profile_index);
2874 profile_index--;
2875 } else {
2876 inputs.add(input);
2877 ALOGV("checkInputsForDevice(): adding input %d", input);
2878 }
2879 } // end scan profiles
2880
2881 if (profiles.isEmpty()) {
2882 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2883 return BAD_VALUE;
2884 }
2885 } else {
2886 // Disconnect
2887 // check if one opened input is not needed any more after disconnecting one device
2888 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2889 desc = mInputs.valueAt(input_index);
2890 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) {
2891 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2892 mInputs.keyAt(input_index));
2893 inputs.add(mInputs.keyAt(input_index));
2894 }
2895 }
2896 // Clear any profiles associated with the disconnected device.
2897 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
2898 if (mHwModules[module_index]->mHandle == 0) {
2899 continue;
2900 }
2901 for (size_t profile_index = 0;
2902 profile_index < mHwModules[module_index]->mInputProfiles.size();
2903 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07002904 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07002905 if (profile->mSupportedDevices.types() & device) {
2906 ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2907 profile_index, module_index);
2908 if (profile->mSamplingRates[0] == 0) {
2909 profile->mSamplingRates.clear();
2910 profile->mSamplingRates.add(0);
2911 }
2912 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2913 profile->mFormats.clear();
2914 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2915 }
2916 if (profile->mChannelMasks[0] == 0) {
2917 profile->mChannelMasks.clear();
2918 profile->mChannelMasks.add(0);
2919 }
2920 }
2921 }
2922 }
2923 } // end disconnect
2924
2925 return NO_ERROR;
2926}
2927
2928
Eric Laurente0720872014-03-11 09:30:41 -07002929void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07002930{
2931 ALOGV("closeOutput(%d)", output);
2932
Eric Laurent1f2f2232014-06-02 12:01:23 -07002933 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07002934 if (outputDesc == NULL) {
2935 ALOGW("closeOutput() unknown output %d", output);
2936 return;
2937 }
2938
2939 // look for duplicated outputs connected to the output being removed.
2940 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002941 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07002942 if (dupOutputDesc->isDuplicated() &&
2943 (dupOutputDesc->mOutput1 == outputDesc ||
2944 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002945 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07002946 if (dupOutputDesc->mOutput1 == outputDesc) {
2947 outputDesc2 = dupOutputDesc->mOutput2;
2948 } else {
2949 outputDesc2 = dupOutputDesc->mOutput1;
2950 }
2951 // As all active tracks on duplicated output will be deleted,
2952 // and as they were also referenced on the other output, the reference
2953 // count for their stream type must be adjusted accordingly on
2954 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07002955 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07002956 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07002957 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07002958 }
2959 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2960 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2961
2962 mpClientInterface->closeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07002963 mOutputs.removeItem(duplicatedOutput);
2964 }
2965 }
2966
2967 AudioParameter param;
2968 param.add(String8("closing"), String8("true"));
2969 mpClientInterface->setParameters(output, param.toString());
2970
2971 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07002972 mOutputs.removeItem(output);
2973 mPreviousOutputs = mOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002974 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07002975}
2976
Eric Laurente0720872014-03-11 09:30:41 -07002977SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -07002978 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07002979{
2980 SortedVector<audio_io_handle_t> outputs;
2981
2982 ALOGVV("getOutputsForDevice() device %04x", device);
2983 for (size_t i = 0; i < openOutputs.size(); i++) {
2984 ALOGVV("output %d isDuplicated=%d device=%04x",
2985 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2986 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2987 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2988 outputs.add(openOutputs.keyAt(i));
2989 }
2990 }
2991 return outputs;
2992}
2993
Eric Laurente0720872014-03-11 09:30:41 -07002994bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07002995 SortedVector<audio_io_handle_t>& outputs2)
2996{
2997 if (outputs1.size() != outputs2.size()) {
2998 return false;
2999 }
3000 for (size_t i = 0; i < outputs1.size(); i++) {
3001 if (outputs1[i] != outputs2[i]) {
3002 return false;
3003 }
3004 }
3005 return true;
3006}
3007
Eric Laurente0720872014-03-11 09:30:41 -07003008void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003009{
3010 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3011 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3012 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3013 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3014
3015 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3016 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3017 strategy, srcOutputs[0], dstOutputs[0]);
3018 // mute strategy while moving tracks from one output to another
3019 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003020 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07003021 if (desc->isStrategyActive(strategy)) {
3022 setStrategyMute(strategy, true, srcOutputs[i]);
3023 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3024 }
3025 }
3026
3027 // Move effects associated to this strategy from previous output to new output
3028 if (strategy == STRATEGY_MEDIA) {
3029 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3030 SortedVector<audio_io_handle_t> moved;
3031 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003032 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3033 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3034 effectDesc->mIo != fxOutput) {
3035 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003036 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3037 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003038 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003039 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003040 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07003041 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07003042 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07003043 }
3044 }
3045 }
3046 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07003047 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3048 if (getStrategy((audio_stream_type_t)i) == strategy) {
3049 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07003050 }
3051 }
3052 }
3053}
3054
Eric Laurente0720872014-03-11 09:30:41 -07003055void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07003056{
3057 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
3058 checkOutputForStrategy(STRATEGY_PHONE);
3059 checkOutputForStrategy(STRATEGY_SONIFICATION);
3060 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3061 checkOutputForStrategy(STRATEGY_MEDIA);
3062 checkOutputForStrategy(STRATEGY_DTMF);
3063}
3064
Eric Laurente0720872014-03-11 09:30:41 -07003065audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07003066{
Eric Laurente552edb2014-03-10 17:42:56 -07003067 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003068 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003069 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
3070 return mOutputs.keyAt(i);
3071 }
3072 }
3073
3074 return 0;
3075}
3076
Eric Laurente0720872014-03-11 09:30:41 -07003077void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07003078{
Eric Laurente552edb2014-03-10 17:42:56 -07003079 audio_io_handle_t a2dpOutput = getA2dpOutput();
3080 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003081 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07003082 return;
3083 }
3084
Eric Laurent3a4311c2014-03-17 12:00:47 -07003085 bool isScoConnected =
3086 (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0;
Eric Laurente552edb2014-03-10 17:42:56 -07003087 // suspend A2DP output if:
3088 // (NOT already suspended) &&
3089 // ((SCO device is connected &&
3090 // (forced usage for communication || for record is SCO))) ||
3091 // (phone state is ringing || in call)
3092 //
3093 // restore A2DP output if:
3094 // (Already suspended) &&
3095 // ((SCO device is NOT connected ||
3096 // (forced usage NOT for communication && NOT for record is SCO))) &&
3097 // (phone state is NOT ringing && NOT in call)
3098 //
3099 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003100 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003101 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
3102 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
3103 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
3104 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003105
3106 mpClientInterface->restoreOutput(a2dpOutput);
3107 mA2dpSuspended = false;
3108 }
3109 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003110 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003111 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
3112 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
3113 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
3114 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07003115
3116 mpClientInterface->suspendOutput(a2dpOutput);
3117 mA2dpSuspended = true;
3118 }
3119 }
3120}
3121
Eric Laurent1c333e22014-05-20 10:48:17 -07003122audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07003123{
3124 audio_devices_t device = AUDIO_DEVICE_NONE;
3125
Eric Laurent1f2f2232014-06-02 12:01:23 -07003126 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003127
3128 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3129 if (index >= 0) {
3130 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3131 if (patchDesc->mUid != mUidCached) {
3132 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
3133 outputDesc->device(), outputDesc->mPatchHandle);
3134 return outputDesc->device();
3135 }
3136 }
3137
Eric Laurente552edb2014-03-10 17:42:56 -07003138 // check the following by order of priority to request a routing change if necessary:
3139 // 1: the strategy enforced audible is active on the output:
3140 // use device for strategy enforced audible
3141 // 2: we are in call or the strategy phone is active on the output:
3142 // use device for strategy phone
3143 // 3: the strategy sonification is active on the output:
3144 // use device for strategy sonification
3145 // 4: the strategy "respectful" sonification is active on the output:
3146 // use device for strategy "respectful" sonification
3147 // 5: the strategy media is active on the output:
3148 // use device for strategy media
3149 // 6: the strategy DTMF is active on the output:
3150 // use device for strategy DTMF
3151 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
3152 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
3153 } else if (isInCall() ||
3154 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
3155 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
3156 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
3157 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
3158 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
3159 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
3160 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
3161 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
3162 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
3163 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
3164 }
3165
Eric Laurent1c333e22014-05-20 10:48:17 -07003166 ALOGV("getNewOutputDevice() selected device %x", device);
3167 return device;
3168}
3169
3170audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
3171{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003172 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003173
3174 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3175 if (index >= 0) {
3176 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3177 if (patchDesc->mUid != mUidCached) {
3178 ALOGV("getNewInputDevice() device %08x forced by patch %d",
3179 inputDesc->mDevice, inputDesc->mPatchHandle);
3180 return inputDesc->mDevice;
3181 }
3182 }
3183
Eric Laurent1c333e22014-05-20 10:48:17 -07003184 audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource);
3185
3186 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003187 return device;
3188}
3189
Eric Laurente0720872014-03-11 09:30:41 -07003190uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003191 return (uint32_t)getStrategy(stream);
3192}
3193
Eric Laurente0720872014-03-11 09:30:41 -07003194audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003195 // By checking the range of stream before calling getStrategy, we avoid
3196 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
3197 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent3b73df72014-03-11 09:06:29 -07003198 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003199 return AUDIO_DEVICE_NONE;
3200 }
3201 audio_devices_t devices;
3202 AudioPolicyManager::routing_strategy strategy = getStrategy(stream);
3203 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
3204 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
3205 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003206 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent6a94d692014-05-20 11:18:06 -07003207 if (outputDesc->isStrategyActive(strategy)) {
3208 devices = outputDesc->device();
3209 break;
3210 }
Eric Laurente552edb2014-03-10 17:42:56 -07003211 }
3212 return devices;
3213}
3214
Eric Laurente0720872014-03-11 09:30:41 -07003215AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07003216 audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003217 // stream to strategy mapping
3218 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003219 case AUDIO_STREAM_VOICE_CALL:
3220 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003221 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07003222 case AUDIO_STREAM_RING:
3223 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07003224 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07003225 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07003226 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07003227 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07003228 return STRATEGY_DTMF;
3229 default:
3230 ALOGE("unknown stream type");
Eric Laurent3b73df72014-03-11 09:06:29 -07003231 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07003232 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
3233 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07003234 case AUDIO_STREAM_TTS:
3235 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003236 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07003237 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07003238 return STRATEGY_ENFORCED_AUDIBLE;
3239 }
3240}
3241
Eric Laurente0720872014-03-11 09:30:41 -07003242void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07003243 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07003244 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07003245 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
3246 updateDevicesAndOutputs();
3247 break;
3248 default:
3249 break;
3250 }
3251}
3252
Eric Laurente0720872014-03-11 09:30:41 -07003253audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07003254 bool fromCache)
3255{
3256 uint32_t device = AUDIO_DEVICE_NONE;
3257
3258 if (fromCache) {
3259 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
3260 strategy, mDeviceForStrategy[strategy]);
3261 return mDeviceForStrategy[strategy];
3262 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003263 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07003264 switch (strategy) {
3265
3266 case STRATEGY_SONIFICATION_RESPECTFUL:
3267 if (isInCall()) {
3268 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003269 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07003270 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
3271 // while media is playing on a remote device, use the the sonification behavior.
3272 // Note that we test this usecase before testing if media is playing because
3273 // the isStreamActive() method only informs about the activity of a stream, not
3274 // if it's for local playback. Note also that we use the same delay between both tests
3275 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07003276 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003277 // while media is playing (or has recently played), use the same device
3278 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3279 } else {
3280 // when media is not playing anymore, fall back on the sonification behavior
3281 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
3282 }
3283
3284 break;
3285
3286 case STRATEGY_DTMF:
3287 if (!isInCall()) {
3288 // when off call, DTMF strategy follows the same rules as MEDIA strategy
3289 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
3290 break;
3291 }
3292 // when in call, DTMF and PHONE strategies follow the same rules
3293 // FALL THROUGH
3294
3295 case STRATEGY_PHONE:
3296 // for phone strategy, we first consider the forced use and then the available devices by order
3297 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07003298 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
3299 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07003300 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003301 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003302 if (device) break;
3303 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003304 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003305 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003306 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07003307 if (device) break;
3308 // if SCO device is requested but no SCO device is available, fall back to default case
3309 // FALL THROUGH
3310
3311 default: // FORCE_NONE
3312 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07003313 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003314 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003315 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003316 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003317 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003318 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003319 if (device) break;
3320 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003321 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003322 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003323 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003324 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07003325 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003326 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003327 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003328 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003329 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003330 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003331 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003332 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003333 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003334 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003335 if (device) break;
3336 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003337 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07003338 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003339 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003340 if (device == AUDIO_DEVICE_NONE) {
3341 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
3342 }
3343 break;
3344
Eric Laurent3b73df72014-03-11 09:06:29 -07003345 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07003346 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
3347 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07003348 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003349 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003350 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003351 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003352 if (device) break;
3353 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003354 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003355 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003356 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003357 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003358 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003359 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003360 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003361 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003362 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003363 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003364 if (device) break;
3365 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003366 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003367 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003368 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003369 if (device == AUDIO_DEVICE_NONE) {
3370 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
3371 }
3372 break;
3373 }
3374 break;
3375
3376 case STRATEGY_SONIFICATION:
3377
3378 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
3379 // handleIncallSonification().
3380 if (isInCall()) {
3381 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
3382 break;
3383 }
3384 // FALL THROUGH
3385
3386 case STRATEGY_ENFORCED_AUDIBLE:
3387 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
3388 // except:
3389 // - when in call where it doesn't default to STRATEGY_PHONE behavior
3390 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
3391
3392 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07003393 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003394 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003395 if (device == AUDIO_DEVICE_NONE) {
3396 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
3397 }
3398 }
3399 // The second device used for sonification is the same as the device used by media strategy
3400 // FALL THROUGH
3401
3402 case STRATEGY_MEDIA: {
3403 uint32_t device2 = AUDIO_DEVICE_NONE;
3404 if (strategy != STRATEGY_SONIFICATION) {
3405 // no sonification on remote submix (e.g. WFD)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003406 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
Eric Laurente552edb2014-03-10 17:42:56 -07003407 }
3408 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003409 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurente552edb2014-03-10 17:42:56 -07003410 (getA2dpOutput() != 0) && !mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003411 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07003412 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003413 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07003414 }
3415 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003416 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003417 }
3418 }
3419 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003420 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003421 }
3422 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003423 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003424 }
3425 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003426 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07003427 }
3428 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003429 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07003430 }
3431 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003432 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003433 }
3434 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
3435 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07003436 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07003437 }
3438 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07003439 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003440 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07003441 }
3442 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07003443 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07003444 }
3445
3446 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
3447 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
3448 device |= device2;
3449 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07003450 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07003451 if (device == AUDIO_DEVICE_NONE) {
3452 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
3453 }
3454 } break;
3455
3456 default:
3457 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
3458 break;
3459 }
3460
3461 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
3462 return device;
3463}
3464
Eric Laurente0720872014-03-11 09:30:41 -07003465void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07003466{
3467 for (int i = 0; i < NUM_STRATEGIES; i++) {
3468 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3469 }
3470 mPreviousOutputs = mOutputs;
3471}
3472
Eric Laurent1f2f2232014-06-02 12:01:23 -07003473uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003474 audio_devices_t prevDevice,
3475 uint32_t delayMs)
3476{
3477 // mute/unmute strategies using an incompatible device combination
3478 // if muting, wait for the audio in pcm buffer to be drained before proceeding
3479 // if unmuting, unmute only after the specified delay
3480 if (outputDesc->isDuplicated()) {
3481 return 0;
3482 }
3483
3484 uint32_t muteWaitMs = 0;
3485 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07003486 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07003487
3488 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3489 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
3490 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
3491 bool doMute = false;
3492
3493 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
3494 doMute = true;
3495 outputDesc->mStrategyMutedByDevice[i] = true;
3496 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
3497 doMute = true;
3498 outputDesc->mStrategyMutedByDevice[i] = false;
3499 }
Eric Laurent99401132014-05-07 19:48:15 -07003500 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07003501 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003502 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07003503 // skip output if it does not share any device with current output
3504 if ((desc->supportedDevices() & outputDesc->supportedDevices())
3505 == AUDIO_DEVICE_NONE) {
3506 continue;
3507 }
3508 audio_io_handle_t curOutput = mOutputs.keyAt(j);
3509 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
3510 mute ? "muting" : "unmuting", i, curDevice, curOutput);
3511 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
3512 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07003513 if (mute) {
3514 // FIXME: should not need to double latency if volume could be applied
3515 // immediately by the audioflinger mixer. We must account for the delay
3516 // between now and the next time the audioflinger thread for this output
3517 // will process a buffer (which corresponds to one buffer size,
3518 // usually 1/2 or 1/4 of the latency).
3519 if (muteWaitMs < desc->latency() * 2) {
3520 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07003521 }
3522 }
3523 }
3524 }
3525 }
3526 }
3527
Eric Laurent99401132014-05-07 19:48:15 -07003528 // temporary mute output if device selection changes to avoid volume bursts due to
3529 // different per device volumes
3530 if (outputDesc->isActive() && (device != prevDevice)) {
3531 if (muteWaitMs < outputDesc->latency() * 2) {
3532 muteWaitMs = outputDesc->latency() * 2;
3533 }
3534 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
3535 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003536 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07003537 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07003538 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07003539 muteWaitMs *2, device);
3540 }
3541 }
3542 }
3543
Eric Laurente552edb2014-03-10 17:42:56 -07003544 // wait for the PCM output buffers to empty before proceeding with the rest of the command
3545 if (muteWaitMs > delayMs) {
3546 muteWaitMs -= delayMs;
3547 usleep(muteWaitMs * 1000);
3548 return muteWaitMs;
3549 }
3550 return 0;
3551}
3552
Eric Laurente0720872014-03-11 09:30:41 -07003553uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07003554 audio_devices_t device,
3555 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07003556 int delayMs,
3557 audio_patch_handle_t *patchHandle)
Eric Laurente552edb2014-03-10 17:42:56 -07003558{
3559 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003560 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003561 AudioParameter param;
3562 uint32_t muteWaitMs;
3563
3564 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003565 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
3566 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07003567 return muteWaitMs;
3568 }
3569 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
3570 // output profile
3571 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003572 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07003573 return 0;
3574 }
3575
3576 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07003577 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07003578
3579 audio_devices_t prevDevice = outputDesc->mDevice;
3580
3581 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
3582
3583 if (device != AUDIO_DEVICE_NONE) {
3584 outputDesc->mDevice = device;
3585 }
3586 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
3587
3588 // Do not change the routing if:
3589 // - the requested device is AUDIO_DEVICE_NONE
3590 // - the requested device is the same as current device and force is not specified.
3591 // Doing this check here allows the caller to call setOutputDevice() without conditions
3592 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
3593 ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
3594 return muteWaitMs;
3595 }
3596
3597 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07003598
Eric Laurente552edb2014-03-10 17:42:56 -07003599 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07003600 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003601 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07003602 } else {
3603 DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device);
3604 if (!deviceList.isEmpty()) {
3605 struct audio_patch patch;
3606 outputDesc->toAudioPortConfig(&patch.sources[0]);
3607 patch.num_sources = 1;
3608 patch.num_sinks = 0;
3609 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
3610 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07003611 patch.num_sinks++;
3612 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003613 ssize_t index;
3614 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3615 index = mAudioPatches.indexOfKey(*patchHandle);
3616 } else {
3617 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3618 }
3619 sp< AudioPatch> patchDesc;
3620 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3621 if (index >= 0) {
3622 patchDesc = mAudioPatches.valueAt(index);
3623 afPatchHandle = patchDesc->mAfPatchHandle;
3624 }
3625
Eric Laurent1c333e22014-05-20 10:48:17 -07003626 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003627 &afPatchHandle,
3628 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07003629 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
3630 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003631 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07003632 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003633 if (index < 0) {
3634 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3635 &patch, mUidCached);
3636 addAudioPatch(patchDesc->mHandle, patchDesc);
3637 } else {
3638 patchDesc->mPatch = patch;
3639 }
3640 patchDesc->mAfPatchHandle = afPatchHandle;
3641 patchDesc->mUid = mUidCached;
3642 if (patchHandle) {
3643 *patchHandle = patchDesc->mHandle;
3644 }
3645 outputDesc->mPatchHandle = patchDesc->mHandle;
3646 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003647 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003648 }
3649 }
3650 }
Eric Laurente552edb2014-03-10 17:42:56 -07003651
3652 // update stream volumes according to new device
3653 applyStreamVolumes(output, device, delayMs);
3654
3655 return muteWaitMs;
3656}
3657
Eric Laurent1c333e22014-05-20 10:48:17 -07003658status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07003659 int delayMs,
3660 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003661{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003662 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003663 ssize_t index;
3664 if (patchHandle) {
3665 index = mAudioPatches.indexOfKey(*patchHandle);
3666 } else {
3667 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3668 }
3669 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003670 return INVALID_OPERATION;
3671 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003672 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3673 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07003674 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
3675 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003676 removeAudioPatch(patchDesc->mHandle);
3677 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003678 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003679 return status;
3680}
3681
3682status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
3683 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07003684 bool force,
3685 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003686{
3687 status_t status = NO_ERROR;
3688
Eric Laurent1f2f2232014-06-02 12:01:23 -07003689 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07003690 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
3691 inputDesc->mDevice = device;
3692
3693 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
3694 if (!deviceList.isEmpty()) {
3695 struct audio_patch patch;
3696 inputDesc->toAudioPortConfig(&patch.sinks[0]);
3697 patch.num_sinks = 1;
3698 //only one input device for now
3699 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07003700 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07003701 ssize_t index;
3702 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
3703 index = mAudioPatches.indexOfKey(*patchHandle);
3704 } else {
3705 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3706 }
3707 sp< AudioPatch> patchDesc;
3708 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
3709 if (index >= 0) {
3710 patchDesc = mAudioPatches.valueAt(index);
3711 afPatchHandle = patchDesc->mAfPatchHandle;
3712 }
3713
Eric Laurent1c333e22014-05-20 10:48:17 -07003714 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07003715 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07003716 0);
3717 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07003718 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07003719 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07003720 if (index < 0) {
3721 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
3722 &patch, mUidCached);
3723 addAudioPatch(patchDesc->mHandle, patchDesc);
3724 } else {
3725 patchDesc->mPatch = patch;
3726 }
3727 patchDesc->mAfPatchHandle = afPatchHandle;
3728 patchDesc->mUid = mUidCached;
3729 if (patchHandle) {
3730 *patchHandle = patchDesc->mHandle;
3731 }
3732 inputDesc->mPatchHandle = patchDesc->mHandle;
3733 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003734 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003735 }
3736 }
3737 }
3738 return status;
3739}
3740
Eric Laurent6a94d692014-05-20 11:18:06 -07003741status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
3742 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07003743{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003744 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07003745 ssize_t index;
3746 if (patchHandle) {
3747 index = mAudioPatches.indexOfKey(*patchHandle);
3748 } else {
3749 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3750 }
3751 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003752 return INVALID_OPERATION;
3753 }
Eric Laurent6a94d692014-05-20 11:18:06 -07003754 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3755 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07003756 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
3757 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07003758 removeAudioPatch(patchDesc->mHandle);
3759 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07003760 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07003761 return status;
3762}
3763
3764sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07003765 uint32_t samplingRate,
3766 audio_format_t format,
3767 audio_channel_mask_t channelMask)
3768{
3769 // Choose an input profile based on the requested capture parameters: select the first available
3770 // profile supporting all requested parameters.
3771
3772 for (size_t i = 0; i < mHwModules.size(); i++)
3773 {
3774 if (mHwModules[i]->mHandle == 0) {
3775 continue;
3776 }
3777 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3778 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003779 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003780 // profile->log();
Eric Laurente552edb2014-03-10 17:42:56 -07003781 if (profile->isCompatibleProfile(device, samplingRate, format,
3782 channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
3783 return profile;
3784 }
3785 }
3786 }
3787 return NULL;
3788}
3789
Eric Laurente0720872014-03-11 09:30:41 -07003790audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource)
Eric Laurente552edb2014-03-10 17:42:56 -07003791{
3792 uint32_t device = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003793 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
3794 ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07003795 switch (inputSource) {
3796 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003797 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003798 device = AUDIO_DEVICE_IN_VOICE_CALL;
3799 break;
3800 }
3801 // FALL THROUGH
3802
3803 case AUDIO_SOURCE_DEFAULT:
3804 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07003805 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
3806 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
3807 break;
3808 }
3809 // FALL THROUGH
3810
Eric Laurente552edb2014-03-10 17:42:56 -07003811 case AUDIO_SOURCE_VOICE_RECOGNITION:
3812 case AUDIO_SOURCE_HOTWORD:
3813 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurent3b73df72014-03-11 09:06:29 -07003814 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07003815 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003816 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003817 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07003818 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07003819 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
3820 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003821 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003822 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3823 }
3824 break;
3825 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003826 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003827 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003828 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07003829 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
3830 }
3831 break;
3832 case AUDIO_SOURCE_VOICE_DOWNLINK:
3833 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003834 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07003835 device = AUDIO_DEVICE_IN_VOICE_CALL;
3836 }
3837 break;
3838 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07003839 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07003840 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
3841 }
3842 break;
3843 default:
3844 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
3845 break;
3846 }
3847 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
3848 return device;
3849}
3850
Eric Laurente0720872014-03-11 09:30:41 -07003851bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003852{
3853 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
3854 device &= ~AUDIO_DEVICE_BIT_IN;
3855 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
3856 return true;
3857 }
3858 return false;
3859}
3860
Eric Laurente0720872014-03-11 09:30:41 -07003861audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003862{
3863 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003864 const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003865 if ((input_descriptor->mRefCount > 0)
3866 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
3867 return mInputs.keyAt(i);
3868 }
3869 }
3870 return 0;
3871}
3872
3873
Eric Laurente0720872014-03-11 09:30:41 -07003874audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003875{
3876 if (device == AUDIO_DEVICE_NONE) {
3877 // this happens when forcing a route update and no track is active on an output.
3878 // In this case the returned category is not important.
3879 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent3b73df72014-03-11 09:06:29 -07003880 } else if (popcount(device) > 1) {
Eric Laurente552edb2014-03-10 17:42:56 -07003881 // Multiple device selection is either:
3882 // - speaker + one other device: give priority to speaker in this case.
3883 // - one A2DP device + another device: happens with duplicated output. In this case
3884 // retain the device on the A2DP output as the other must not correspond to an active
3885 // selection if not the speaker.
3886 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
3887 device = AUDIO_DEVICE_OUT_SPEAKER;
3888 } else {
3889 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
3890 }
3891 }
3892
Eric Laurent3b73df72014-03-11 09:06:29 -07003893 ALOGW_IF(popcount(device) != 1,
Eric Laurente552edb2014-03-10 17:42:56 -07003894 "getDeviceForVolume() invalid device combination: %08x",
3895 device);
3896
3897 return device;
3898}
3899
Eric Laurente0720872014-03-11 09:30:41 -07003900AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07003901{
3902 switch(getDeviceForVolume(device)) {
3903 case AUDIO_DEVICE_OUT_EARPIECE:
3904 return DEVICE_CATEGORY_EARPIECE;
3905 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3906 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3907 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3908 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3909 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3910 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3911 return DEVICE_CATEGORY_HEADSET;
3912 case AUDIO_DEVICE_OUT_SPEAKER:
3913 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3914 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3915 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3916 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3917 case AUDIO_DEVICE_OUT_USB_DEVICE:
3918 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3919 default:
3920 return DEVICE_CATEGORY_SPEAKER;
3921 }
3922}
3923
Eric Laurente0720872014-03-11 09:30:41 -07003924float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07003925 int indexInUi)
3926{
3927 device_category deviceCategory = getDeviceCategory(device);
3928 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3929
3930 // the volume index in the UI is relative to the min and max volume indices for this stream type
3931 int nbSteps = 1 + curve[VOLMAX].mIndex -
3932 curve[VOLMIN].mIndex;
3933 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3934 (streamDesc.mIndexMax - streamDesc.mIndexMin);
3935
3936 // find what part of the curve this index volume belongs to, or if it's out of bounds
3937 int segment = 0;
3938 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
3939 return 0.0f;
3940 } else if (volIdx < curve[VOLKNEE1].mIndex) {
3941 segment = 0;
3942 } else if (volIdx < curve[VOLKNEE2].mIndex) {
3943 segment = 1;
3944 } else if (volIdx <= curve[VOLMAX].mIndex) {
3945 segment = 2;
3946 } else { // out of bounds
3947 return 1.0f;
3948 }
3949
3950 // linear interpolation in the attenuation table in dB
3951 float decibels = curve[segment].mDBAttenuation +
3952 ((float)(volIdx - curve[segment].mIndex)) *
3953 ( (curve[segment+1].mDBAttenuation -
3954 curve[segment].mDBAttenuation) /
3955 ((float)(curve[segment+1].mIndex -
3956 curve[segment].mIndex)) );
3957
3958 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3959
3960 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3961 curve[segment].mIndex, volIdx,
3962 curve[segment+1].mIndex,
3963 curve[segment].mDBAttenuation,
3964 decibels,
3965 curve[segment+1].mDBAttenuation,
3966 amplification);
3967
3968 return amplification;
3969}
3970
Eric Laurente0720872014-03-11 09:30:41 -07003971const AudioPolicyManager::VolumeCurvePoint
3972 AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003973 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3974};
3975
Eric Laurente0720872014-03-11 09:30:41 -07003976const AudioPolicyManager::VolumeCurvePoint
3977 AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003978 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3979};
3980
Eric Laurente0720872014-03-11 09:30:41 -07003981const AudioPolicyManager::VolumeCurvePoint
3982 AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003983 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3984};
3985
Eric Laurente0720872014-03-11 09:30:41 -07003986const AudioPolicyManager::VolumeCurvePoint
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07003987 AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
3988 {1, -56.0f}, {20, -34.0f}, {86, -10.0f}, {100, 0.0f}
3989};
3990
3991const AudioPolicyManager::VolumeCurvePoint
Eric Laurente0720872014-03-11 09:30:41 -07003992 AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003993 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3994};
3995
Eric Laurente0720872014-03-11 09:30:41 -07003996const AudioPolicyManager::VolumeCurvePoint
3997 AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07003998 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
3999};
4000
4001// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
4002// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
4003// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
4004// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
4005
Eric Laurente0720872014-03-11 09:30:41 -07004006const AudioPolicyManager::VolumeCurvePoint
4007 AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004008 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
4009};
4010
Eric Laurente0720872014-03-11 09:30:41 -07004011const AudioPolicyManager::VolumeCurvePoint
4012 AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004013 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
4014};
4015
Eric Laurente0720872014-03-11 09:30:41 -07004016const AudioPolicyManager::VolumeCurvePoint
4017 AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004018 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
4019};
4020
Eric Laurente0720872014-03-11 09:30:41 -07004021const AudioPolicyManager::VolumeCurvePoint
4022 AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004023 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
4024};
4025
Eric Laurente0720872014-03-11 09:30:41 -07004026const AudioPolicyManager::VolumeCurvePoint
4027 AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004028 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
4029};
4030
Eric Laurente0720872014-03-11 09:30:41 -07004031const AudioPolicyManager::VolumeCurvePoint
4032 *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT]
4033 [AudioPolicyManager::DEVICE_CATEGORY_CNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07004034 { // AUDIO_STREAM_VOICE_CALL
4035 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
4036 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4037 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
4038 },
4039 { // AUDIO_STREAM_SYSTEM
4040 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4041 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4042 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4043 },
4044 { // AUDIO_STREAM_RING
4045 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4046 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4047 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4048 },
4049 { // AUDIO_STREAM_MUSIC
4050 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4051 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4052 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4053 },
4054 { // AUDIO_STREAM_ALARM
4055 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4056 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4057 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4058 },
4059 { // AUDIO_STREAM_NOTIFICATION
4060 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
4061 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4062 sDefaultVolumeCurve // DEVICE_CATEGORY_EARPIECE
4063 },
4064 { // AUDIO_STREAM_BLUETOOTH_SCO
4065 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
4066 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4067 sDefaultVoiceVolumeCurve // DEVICE_CATEGORY_EARPIECE
4068 },
4069 { // AUDIO_STREAM_ENFORCED_AUDIBLE
4070 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4071 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4072 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4073 },
4074 { // AUDIO_STREAM_DTMF
4075 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
4076 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4077 sDefaultSystemVolumeCurve // DEVICE_CATEGORY_EARPIECE
4078 },
4079 { // AUDIO_STREAM_TTS
4080 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
4081 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
4082 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EARPIECE
4083 },
4084};
4085
Eric Laurente0720872014-03-11 09:30:41 -07004086void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07004087{
4088 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
4089 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
4090 mStreams[i].mVolumeCurve[j] =
4091 sVolumeProfiles[i][j];
4092 }
4093 }
4094
4095 // Check availability of DRC on speaker path: if available, override some of the speaker curves
4096 if (mSpeakerDrcEnabled) {
4097 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4098 sDefaultSystemVolumeCurveDrc;
4099 mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4100 sSpeakerSonificationVolumeCurveDrc;
4101 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4102 sSpeakerSonificationVolumeCurveDrc;
4103 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4104 sSpeakerSonificationVolumeCurveDrc;
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07004105 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
4106 sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07004107 }
4108}
4109
Eric Laurente0720872014-03-11 09:30:41 -07004110float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004111 int index,
4112 audio_io_handle_t output,
4113 audio_devices_t device)
4114{
4115 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07004116 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004117 StreamDescriptor &streamDesc = mStreams[stream];
4118
4119 if (device == AUDIO_DEVICE_NONE) {
4120 device = outputDesc->device();
4121 }
4122
4123 // if volume is not 0 (not muted), force media volume to max on digital output
Eric Laurent3b73df72014-03-11 09:06:29 -07004124 if (stream == AUDIO_STREAM_MUSIC &&
Eric Laurente552edb2014-03-10 17:42:56 -07004125 index != mStreams[stream].mIndexMin &&
4126 (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
4127 device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
4128 device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
4129 device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
4130 return 1.0;
4131 }
4132
4133 volume = volIndexToAmpl(device, streamDesc, index);
4134
4135 // if a headset is connected, apply the following rules to ring tones and notifications
4136 // to avoid sound level bursts in user's ears:
4137 // - always attenuate ring tones and notifications volume by 6dB
4138 // - if music is playing, always limit the volume to current music volume,
4139 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07004140 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004141 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
4142 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
4143 AUDIO_DEVICE_OUT_WIRED_HEADSET |
4144 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
4145 ((stream_strategy == STRATEGY_SONIFICATION)
4146 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07004147 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07004148 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004149 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07004150 streamDesc.mCanBeMuted) {
4151 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
4152 // when the phone is ringing we must consider that music could have been paused just before
4153 // by the music application and behave as if music was active if the last music track was
4154 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07004155 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004156 mLimitRingtoneVolume) {
4157 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004158 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
4159 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07004160 output,
4161 musicDevice);
4162 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
4163 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
4164 if (volume > minVol) {
4165 volume = minVol;
4166 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
4167 }
4168 }
4169 }
4170
4171 return volume;
4172}
4173
Eric Laurente0720872014-03-11 09:30:41 -07004174status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004175 int index,
4176 audio_io_handle_t output,
4177 audio_devices_t device,
4178 int delayMs,
4179 bool force)
4180{
4181
4182 // do not change actual stream volume if the stream is muted
4183 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
4184 ALOGVV("checkAndSetVolume() stream %d muted count %d",
4185 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
4186 return NO_ERROR;
4187 }
4188
4189 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07004190 if ((stream == AUDIO_STREAM_VOICE_CALL &&
4191 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4192 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
4193 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004194 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07004195 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07004196 return INVALID_OPERATION;
4197 }
4198
4199 float volume = computeVolume(stream, index, output, device);
4200 // We actually change the volume if:
4201 // - the float value returned by computeVolume() changed
4202 // - the force flag is set
4203 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
4204 force) {
4205 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
4206 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
4207 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
4208 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07004209 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
4210 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004211 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004212 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004213 }
4214
Eric Laurent3b73df72014-03-11 09:06:29 -07004215 if (stream == AUDIO_STREAM_VOICE_CALL ||
4216 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07004217 float voiceVolume;
4218 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07004219 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07004220 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
4221 } else {
4222 voiceVolume = 1.0;
4223 }
4224
4225 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
4226 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
4227 mLastVoiceVolume = voiceVolume;
4228 }
4229 }
4230
4231 return NO_ERROR;
4232}
4233
Eric Laurente0720872014-03-11 09:30:41 -07004234void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004235 audio_devices_t device,
4236 int delayMs,
4237 bool force)
4238{
4239 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
4240
Eric Laurent3b73df72014-03-11 09:06:29 -07004241 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4242 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004243 mStreams[stream].getVolumeIndex(device),
4244 output,
4245 device,
4246 delayMs,
4247 force);
4248 }
4249}
4250
Eric Laurente0720872014-03-11 09:30:41 -07004251void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004252 bool on,
4253 audio_io_handle_t output,
4254 int delayMs,
4255 audio_devices_t device)
4256{
4257 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07004258 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
4259 if (getStrategy((audio_stream_type_t)stream) == strategy) {
4260 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07004261 }
4262 }
4263}
4264
Eric Laurente0720872014-03-11 09:30:41 -07004265void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004266 bool on,
4267 audio_io_handle_t output,
4268 int delayMs,
4269 audio_devices_t device)
4270{
4271 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07004272 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004273 if (device == AUDIO_DEVICE_NONE) {
4274 device = outputDesc->device();
4275 }
4276
4277 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
4278 stream, on, output, outputDesc->mMuteCount[stream], device);
4279
4280 if (on) {
4281 if (outputDesc->mMuteCount[stream] == 0) {
4282 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004283 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
4284 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004285 checkAndSetVolume(stream, 0, output, device, delayMs);
4286 }
4287 }
4288 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
4289 outputDesc->mMuteCount[stream]++;
4290 } else {
4291 if (outputDesc->mMuteCount[stream] == 0) {
4292 ALOGV("setStreamMute() unmuting non muted stream!");
4293 return;
4294 }
4295 if (--outputDesc->mMuteCount[stream] == 0) {
4296 checkAndSetVolume(stream,
4297 streamDesc.getVolumeIndex(device),
4298 output,
4299 device,
4300 delayMs);
4301 }
4302 }
4303}
4304
Eric Laurente0720872014-03-11 09:30:41 -07004305void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004306 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07004307{
4308 // if the stream pertains to sonification strategy and we are in call we must
4309 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
4310 // in the device used for phone strategy and play the tone if the selected device does not
4311 // interfere with the device used for phone strategy
4312 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
4313 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07004314 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07004315 if ((stream_strategy == STRATEGY_SONIFICATION) ||
4316 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004317 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07004318 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
4319 stream, starting, outputDesc->mDevice, stateChange);
4320 if (outputDesc->mRefCount[stream]) {
4321 int muteCount = 1;
4322 if (stateChange) {
4323 muteCount = outputDesc->mRefCount[stream];
4324 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004325 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004326 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
4327 for (int i = 0; i < muteCount; i++) {
4328 setStreamMute(stream, starting, mPrimaryOutput);
4329 }
4330 } else {
4331 ALOGV("handleIncallSonification() high visibility");
4332 if (outputDesc->device() &
4333 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
4334 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
4335 for (int i = 0; i < muteCount; i++) {
4336 setStreamMute(stream, starting, mPrimaryOutput);
4337 }
4338 }
4339 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004340 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
4341 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07004342 } else {
4343 mpClientInterface->stopTone();
4344 }
4345 }
4346 }
4347 }
4348}
4349
Eric Laurente0720872014-03-11 09:30:41 -07004350bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07004351{
4352 return isStateInCall(mPhoneState);
4353}
4354
Eric Laurente0720872014-03-11 09:30:41 -07004355bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004356 return ((state == AUDIO_MODE_IN_CALL) ||
4357 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07004358}
4359
Eric Laurente0720872014-03-11 09:30:41 -07004360uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07004361{
4362 return MAX_EFFECTS_CPU_LOAD;
4363}
4364
Eric Laurente0720872014-03-11 09:30:41 -07004365uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07004366{
4367 return MAX_EFFECTS_MEMORY;
4368}
4369
Eric Laurent6a94d692014-05-20 11:18:06 -07004370
Eric Laurente552edb2014-03-10 17:42:56 -07004371// --- AudioOutputDescriptor class implementation
4372
Eric Laurente0720872014-03-11 09:30:41 -07004373AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor(
Eric Laurent1c333e22014-05-20 10:48:17 -07004374 const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07004375 : mId(0), mIoHandle(0), mLatency(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07004376 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0),
Eric Laurente552edb2014-03-10 17:42:56 -07004377 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
4378{
4379 // clear usage count for all stream types
Eric Laurent3b73df72014-03-11 09:06:29 -07004380 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurente552edb2014-03-10 17:42:56 -07004381 mRefCount[i] = 0;
4382 mCurVolume[i] = -1.0;
4383 mMuteCount[i] = 0;
4384 mStopTime[i] = 0;
4385 }
4386 for (int i = 0; i < NUM_STRATEGIES; i++) {
4387 mStrategyMutedByDevice[i] = false;
4388 }
4389 if (profile != NULL) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004390 mAudioPort = profile;
Eric Laurente552edb2014-03-10 17:42:56 -07004391 mSamplingRate = profile->mSamplingRates[0];
4392 mFormat = profile->mFormats[0];
4393 mChannelMask = profile->mChannelMasks[0];
Eric Laurenta121f902014-06-03 13:32:54 -07004394 if (profile->mGains.size() > 0) {
4395 profile->mGains[0]->getDefaultConfig(&mGain);
4396 }
Eric Laurente552edb2014-03-10 17:42:56 -07004397 mFlags = profile->mFlags;
4398 }
4399}
4400
Eric Laurente0720872014-03-11 09:30:41 -07004401audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const
Eric Laurente552edb2014-03-10 17:42:56 -07004402{
4403 if (isDuplicated()) {
4404 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
4405 } else {
4406 return mDevice;
4407 }
4408}
4409
Eric Laurente0720872014-03-11 09:30:41 -07004410uint32_t AudioPolicyManager::AudioOutputDescriptor::latency()
Eric Laurente552edb2014-03-10 17:42:56 -07004411{
4412 if (isDuplicated()) {
4413 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
4414 } else {
4415 return mLatency;
4416 }
4417}
4418
Eric Laurente0720872014-03-11 09:30:41 -07004419bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith(
Eric Laurent1f2f2232014-06-02 12:01:23 -07004420 const sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07004421{
4422 if (isDuplicated()) {
4423 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
4424 } else if (outputDesc->isDuplicated()){
4425 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
4426 } else {
4427 return (mProfile->mModule == outputDesc->mProfile->mModule);
4428 }
4429}
4430
Eric Laurente0720872014-03-11 09:30:41 -07004431void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07004432 int delta)
Eric Laurente552edb2014-03-10 17:42:56 -07004433{
4434 // forward usage count change to attached outputs
4435 if (isDuplicated()) {
4436 mOutput1->changeRefCount(stream, delta);
4437 mOutput2->changeRefCount(stream, delta);
4438 }
4439 if ((delta + (int)mRefCount[stream]) < 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004440 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d",
4441 delta, stream, mRefCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07004442 mRefCount[stream] = 0;
4443 return;
4444 }
4445 mRefCount[stream] += delta;
4446 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
4447}
4448
Eric Laurente0720872014-03-11 09:30:41 -07004449audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices()
Eric Laurente552edb2014-03-10 17:42:56 -07004450{
4451 if (isDuplicated()) {
4452 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
4453 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004454 return mProfile->mSupportedDevices.types() ;
Eric Laurente552edb2014-03-10 17:42:56 -07004455 }
4456}
4457
Eric Laurente0720872014-03-11 09:30:41 -07004458bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07004459{
4460 return isStrategyActive(NUM_STRATEGIES, inPastMs);
4461}
4462
Eric Laurente0720872014-03-11 09:30:41 -07004463bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004464 uint32_t inPastMs,
4465 nsecs_t sysTime) const
4466{
4467 if ((sysTime == 0) && (inPastMs != 0)) {
4468 sysTime = systemTime();
4469 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004470 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4471 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
Eric Laurente552edb2014-03-10 17:42:56 -07004472 (NUM_STRATEGIES == strategy)) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004473 isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004474 return true;
4475 }
4476 }
4477 return false;
4478}
4479
Eric Laurente0720872014-03-11 09:30:41 -07004480bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07004481 uint32_t inPastMs,
4482 nsecs_t sysTime) const
4483{
4484 if (mRefCount[stream] != 0) {
4485 return true;
4486 }
4487 if (inPastMs == 0) {
4488 return false;
4489 }
4490 if (sysTime == 0) {
4491 sysTime = systemTime();
4492 }
4493 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
4494 return true;
4495 }
4496 return false;
4497}
4498
Eric Laurent1c333e22014-05-20 10:48:17 -07004499void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004500 struct audio_port_config *dstConfig,
4501 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07004502{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004503 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
4504 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
4505 if (srcConfig != NULL) {
4506 dstConfig->config_mask &= srcConfig->config_mask;
4507 }
4508 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
4509
Eric Laurent6a94d692014-05-20 11:18:06 -07004510 dstConfig->id = mId;
4511 dstConfig->role = AUDIO_PORT_ROLE_SOURCE;
4512 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent6a94d692014-05-20 11:18:06 -07004513 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
4514 dstConfig->ext.mix.handle = mIoHandle;
4515 dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
Eric Laurent1c333e22014-05-20 10:48:17 -07004516}
4517
4518void AudioPolicyManager::AudioOutputDescriptor::toAudioPort(
4519 struct audio_port *port) const
4520{
4521 mProfile->toAudioPort(port);
4522 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004523 toAudioPortConfig(&port->active_config);
4524 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07004525 port->ext.mix.handle = mIoHandle;
4526 port->ext.mix.latency_class =
4527 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL;
4528}
Eric Laurente552edb2014-03-10 17:42:56 -07004529
Eric Laurente0720872014-03-11 09:30:41 -07004530status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004531{
4532 const size_t SIZE = 256;
4533 char buffer[SIZE];
4534 String8 result;
4535
4536 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4537 result.append(buffer);
4538 snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
4539 result.append(buffer);
4540 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4541 result.append(buffer);
4542 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
4543 result.append(buffer);
4544 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
4545 result.append(buffer);
4546 snprintf(buffer, SIZE, " Devices %08x\n", device());
4547 result.append(buffer);
4548 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
4549 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07004550 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
4551 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n",
4552 i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07004553 result.append(buffer);
4554 }
4555 write(fd, result.string(), result.size());
4556
4557 return NO_ERROR;
4558}
4559
4560// --- AudioInputDescriptor class implementation
4561
Eric Laurent1c333e22014-05-20 10:48:17 -07004562AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07004563 : mId(0), mIoHandle(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07004564 mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0),
Eric Laurent3b73df72014-03-11 09:06:29 -07004565 mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile)
Eric Laurente552edb2014-03-10 17:42:56 -07004566{
Eric Laurent3a4311c2014-03-17 12:00:47 -07004567 if (profile != NULL) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004568 mAudioPort = profile;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004569 mSamplingRate = profile->mSamplingRates[0];
4570 mFormat = profile->mFormats[0];
4571 mChannelMask = profile->mChannelMasks[0];
Eric Laurenta121f902014-06-03 13:32:54 -07004572 if (profile->mGains.size() > 0) {
4573 profile->mGains[0]->getDefaultConfig(&mGain);
4574 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004575 } else {
4576 mSamplingRate = 0;
4577 mFormat = AUDIO_FORMAT_DEFAULT;
4578 mChannelMask = 0;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004579 }
Eric Laurente552edb2014-03-10 17:42:56 -07004580}
4581
Eric Laurent1c333e22014-05-20 10:48:17 -07004582void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07004583 struct audio_port_config *dstConfig,
4584 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07004585{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004586 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
4587 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
4588 if (srcConfig != NULL) {
4589 dstConfig->config_mask &= srcConfig->config_mask;
4590 }
4591
4592 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
4593
Eric Laurent6a94d692014-05-20 11:18:06 -07004594 dstConfig->id = mId;
4595 dstConfig->role = AUDIO_PORT_ROLE_SINK;
4596 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent62aaabb2014-06-02 10:40:54 -07004597 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
4598 dstConfig->ext.mix.handle = mIoHandle;
4599 dstConfig->ext.mix.usecase.source = mInputSource;
Eric Laurent1c333e22014-05-20 10:48:17 -07004600}
4601
4602void AudioPolicyManager::AudioInputDescriptor::toAudioPort(
4603 struct audio_port *port) const
4604{
4605 mProfile->toAudioPort(port);
4606 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07004607 toAudioPortConfig(&port->active_config);
4608 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07004609 port->ext.mix.handle = mIoHandle;
4610 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
4611}
4612
Eric Laurente0720872014-03-11 09:30:41 -07004613status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004614{
4615 const size_t SIZE = 256;
4616 char buffer[SIZE];
4617 String8 result;
4618
4619 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
4620 result.append(buffer);
4621 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
4622 result.append(buffer);
4623 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
4624 result.append(buffer);
4625 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
4626 result.append(buffer);
4627 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
4628 result.append(buffer);
4629 write(fd, result.string(), result.size());
4630
4631 return NO_ERROR;
4632}
4633
4634// --- StreamDescriptor class implementation
4635
Eric Laurente0720872014-03-11 09:30:41 -07004636AudioPolicyManager::StreamDescriptor::StreamDescriptor()
Eric Laurente552edb2014-03-10 17:42:56 -07004637 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
4638{
4639 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
4640}
4641
Eric Laurente0720872014-03-11 09:30:41 -07004642int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07004643{
Eric Laurente0720872014-03-11 09:30:41 -07004644 device = AudioPolicyManager::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07004645 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
4646 if (mIndexCur.indexOfKey(device) < 0) {
4647 device = AUDIO_DEVICE_OUT_DEFAULT;
4648 }
4649 return mIndexCur.valueFor(device);
4650}
4651
Eric Laurente0720872014-03-11 09:30:41 -07004652void AudioPolicyManager::StreamDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004653{
4654 const size_t SIZE = 256;
4655 char buffer[SIZE];
4656 String8 result;
4657
4658 snprintf(buffer, SIZE, "%s %02d %02d ",
4659 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
4660 result.append(buffer);
4661 for (size_t i = 0; i < mIndexCur.size(); i++) {
4662 snprintf(buffer, SIZE, "%04x : %02d, ",
4663 mIndexCur.keyAt(i),
4664 mIndexCur.valueAt(i));
4665 result.append(buffer);
4666 }
4667 result.append("\n");
4668
4669 write(fd, result.string(), result.size());
4670}
4671
4672// --- EffectDescriptor class implementation
4673
Eric Laurente0720872014-03-11 09:30:41 -07004674status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004675{
4676 const size_t SIZE = 256;
4677 char buffer[SIZE];
4678 String8 result;
4679
4680 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
4681 result.append(buffer);
4682 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
4683 result.append(buffer);
4684 snprintf(buffer, SIZE, " Session: %d\n", mSession);
4685 result.append(buffer);
4686 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
4687 result.append(buffer);
4688 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
4689 result.append(buffer);
4690 write(fd, result.string(), result.size());
4691
4692 return NO_ERROR;
4693}
4694
Eric Laurent1c333e22014-05-20 10:48:17 -07004695// --- HwModule class implementation
Eric Laurente552edb2014-03-10 17:42:56 -07004696
Eric Laurente0720872014-03-11 09:30:41 -07004697AudioPolicyManager::HwModule::HwModule(const char *name)
Eric Laurenteb108a42014-06-06 14:56:52 -07004698 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)),
4699 mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0)
Eric Laurente552edb2014-03-10 17:42:56 -07004700{
4701}
4702
Eric Laurente0720872014-03-11 09:30:41 -07004703AudioPolicyManager::HwModule::~HwModule()
Eric Laurente552edb2014-03-10 17:42:56 -07004704{
4705 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004706 mOutputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004707 }
4708 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004709 mInputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07004710 }
4711 free((void *)mName);
4712}
4713
Eric Laurent1afeecb2014-05-14 08:52:28 -07004714status_t AudioPolicyManager::HwModule::loadInput(cnode *root)
4715{
4716 cnode *node = root->first_child;
4717
4718 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this);
4719
4720 while (node) {
4721 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4722 profile->loadSamplingRates((char *)node->value);
4723 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4724 profile->loadFormats((char *)node->value);
4725 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4726 profile->loadInChannels((char *)node->value);
4727 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4728 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4729 mDeclaredDevices);
4730 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4731 profile->loadGains(node);
4732 }
4733 node = node->next;
4734 }
4735 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4736 "loadInput() invalid supported devices");
4737 ALOGW_IF(profile->mChannelMasks.size() == 0,
4738 "loadInput() invalid supported channel masks");
4739 ALOGW_IF(profile->mSamplingRates.size() == 0,
4740 "loadInput() invalid supported sampling rates");
4741 ALOGW_IF(profile->mFormats.size() == 0,
4742 "loadInput() invalid supported formats");
4743 if (!profile->mSupportedDevices.isEmpty() &&
4744 (profile->mChannelMasks.size() != 0) &&
4745 (profile->mSamplingRates.size() != 0) &&
4746 (profile->mFormats.size() != 0)) {
4747
4748 ALOGV("loadInput() adding input Supported Devices %04x",
4749 profile->mSupportedDevices.types());
4750
4751 mInputProfiles.add(profile);
4752 return NO_ERROR;
4753 } else {
4754 return BAD_VALUE;
4755 }
4756}
4757
4758status_t AudioPolicyManager::HwModule::loadOutput(cnode *root)
4759{
4760 cnode *node = root->first_child;
4761
4762 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this);
4763
4764 while (node) {
4765 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
4766 profile->loadSamplingRates((char *)node->value);
4767 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
4768 profile->loadFormats((char *)node->value);
4769 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4770 profile->loadOutChannels((char *)node->value);
4771 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
4772 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
4773 mDeclaredDevices);
4774 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
4775 profile->mFlags = parseFlagNames((char *)node->value);
4776 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4777 profile->loadGains(node);
4778 }
4779 node = node->next;
4780 }
4781 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
4782 "loadOutput() invalid supported devices");
4783 ALOGW_IF(profile->mChannelMasks.size() == 0,
4784 "loadOutput() invalid supported channel masks");
4785 ALOGW_IF(profile->mSamplingRates.size() == 0,
4786 "loadOutput() invalid supported sampling rates");
4787 ALOGW_IF(profile->mFormats.size() == 0,
4788 "loadOutput() invalid supported formats");
4789 if (!profile->mSupportedDevices.isEmpty() &&
4790 (profile->mChannelMasks.size() != 0) &&
4791 (profile->mSamplingRates.size() != 0) &&
4792 (profile->mFormats.size() != 0)) {
4793
4794 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x",
4795 profile->mSupportedDevices.types(), profile->mFlags);
4796
4797 mOutputProfiles.add(profile);
4798 return NO_ERROR;
4799 } else {
4800 return BAD_VALUE;
4801 }
4802}
4803
4804status_t AudioPolicyManager::HwModule::loadDevice(cnode *root)
4805{
4806 cnode *node = root->first_child;
4807
4808 audio_devices_t type = AUDIO_DEVICE_NONE;
4809 while (node) {
4810 if (strcmp(node->name, DEVICE_TYPE) == 0) {
4811 type = parseDeviceNames((char *)node->value);
4812 break;
4813 }
4814 node = node->next;
4815 }
4816 if (type == AUDIO_DEVICE_NONE ||
4817 (!audio_is_input_device(type) && !audio_is_output_device(type))) {
4818 ALOGW("loadDevice() bad type %08x", type);
4819 return BAD_VALUE;
4820 }
4821 sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type);
4822 deviceDesc->mModule = this;
4823
4824 node = root->first_child;
4825 while (node) {
4826 if (strcmp(node->name, DEVICE_ADDRESS) == 0) {
4827 deviceDesc->mAddress = String8((char *)node->value);
4828 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
4829 if (audio_is_input_device(type)) {
4830 deviceDesc->loadInChannels((char *)node->value);
4831 } else {
4832 deviceDesc->loadOutChannels((char *)node->value);
4833 }
4834 } else if (strcmp(node->name, GAINS_TAG) == 0) {
4835 deviceDesc->loadGains(node);
4836 }
4837 node = node->next;
4838 }
4839
4840 ALOGV("loadDevice() adding device name %s type %08x address %s",
4841 deviceDesc->mName.string(), type, deviceDesc->mAddress.string());
4842
4843 mDeclaredDevices.add(deviceDesc);
4844
4845 return NO_ERROR;
4846}
4847
Eric Laurente0720872014-03-11 09:30:41 -07004848void AudioPolicyManager::HwModule::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07004849{
4850 const size_t SIZE = 256;
4851 char buffer[SIZE];
4852 String8 result;
4853
4854 snprintf(buffer, SIZE, " - name: %s\n", mName);
4855 result.append(buffer);
4856 snprintf(buffer, SIZE, " - handle: %d\n", mHandle);
4857 result.append(buffer);
Eric Laurenteb108a42014-06-06 14:56:52 -07004858 snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF);
4859 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07004860 write(fd, result.string(), result.size());
4861 if (mOutputProfiles.size()) {
4862 write(fd, " - outputs:\n", strlen(" - outputs:\n"));
4863 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004864 snprintf(buffer, SIZE, " output %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004865 write(fd, buffer, strlen(buffer));
4866 mOutputProfiles[i]->dump(fd);
4867 }
4868 }
4869 if (mInputProfiles.size()) {
4870 write(fd, " - inputs:\n", strlen(" - inputs:\n"));
4871 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07004872 snprintf(buffer, SIZE, " input %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07004873 write(fd, buffer, strlen(buffer));
4874 mInputProfiles[i]->dump(fd);
4875 }
4876 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07004877 if (mDeclaredDevices.size()) {
4878 write(fd, " - devices:\n", strlen(" - devices:\n"));
4879 for (size_t i = 0; i < mDeclaredDevices.size(); i++) {
4880 mDeclaredDevices[i]->dump(fd, 4, i);
4881 }
4882 }
Eric Laurente552edb2014-03-10 17:42:56 -07004883}
4884
Eric Laurent1c333e22014-05-20 10:48:17 -07004885// --- AudioPort class implementation
4886
Eric Laurenta121f902014-06-03 13:32:54 -07004887
4888AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type,
4889 audio_port_role_t role, const sp<HwModule>& module) :
4890 mName(name), mType(type), mRole(role), mModule(module)
4891{
4892 mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) ||
4893 ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK));
4894}
4895
Eric Laurent1c333e22014-05-20 10:48:17 -07004896void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const
4897{
4898 port->role = mRole;
4899 port->type = mType;
4900 unsigned int i;
4901 for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) {
4902 port->sample_rates[i] = mSamplingRates[i];
4903 }
4904 port->num_sample_rates = i;
4905 for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) {
4906 port->channel_masks[i] = mChannelMasks[i];
4907 }
4908 port->num_channel_masks = i;
4909 for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) {
4910 port->formats[i] = mFormats[i];
4911 }
4912 port->num_formats = i;
Eric Laurente1715a42014-05-20 11:30:42 -07004913
4914 ALOGV("AudioPort::toAudioPort() num gains %d", mGains.size());
4915
4916 for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) {
4917 port->gains[i] = mGains[i]->mGain;
4918 }
4919 port->num_gains = i;
Eric Laurent1c333e22014-05-20 10:48:17 -07004920}
4921
4922
4923void AudioPolicyManager::AudioPort::loadSamplingRates(char *name)
4924{
4925 char *str = strtok(name, "|");
4926
4927 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
4928 // rates should be read from the output stream after it is opened for the first time
4929 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4930 mSamplingRates.add(0);
4931 return;
4932 }
4933
4934 while (str != NULL) {
4935 uint32_t rate = atoi(str);
4936 if (rate != 0) {
4937 ALOGV("loadSamplingRates() adding rate %d", rate);
4938 mSamplingRates.add(rate);
4939 }
4940 str = strtok(NULL, "|");
4941 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004942}
4943
4944void AudioPolicyManager::AudioPort::loadFormats(char *name)
4945{
4946 char *str = strtok(name, "|");
4947
4948 // by convention, "0' in the first entry in mFormats indicates the supported formats
4949 // should be read from the output stream after it is opened for the first time
4950 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4951 mFormats.add(AUDIO_FORMAT_DEFAULT);
4952 return;
4953 }
4954
4955 while (str != NULL) {
4956 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
4957 ARRAY_SIZE(sFormatNameToEnumTable),
4958 str);
4959 if (format != AUDIO_FORMAT_DEFAULT) {
4960 mFormats.add(format);
4961 }
4962 str = strtok(NULL, "|");
4963 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004964}
4965
4966void AudioPolicyManager::AudioPort::loadInChannels(char *name)
4967{
4968 const char *str = strtok(name, "|");
4969
4970 ALOGV("loadInChannels() %s", name);
4971
4972 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4973 mChannelMasks.add(0);
4974 return;
4975 }
4976
4977 while (str != NULL) {
4978 audio_channel_mask_t channelMask =
4979 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
4980 ARRAY_SIZE(sInChannelsNameToEnumTable),
4981 str);
4982 if (channelMask != 0) {
4983 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
4984 mChannelMasks.add(channelMask);
4985 }
4986 str = strtok(NULL, "|");
4987 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004988}
4989
4990void AudioPolicyManager::AudioPort::loadOutChannels(char *name)
4991{
4992 const char *str = strtok(name, "|");
4993
4994 ALOGV("loadOutChannels() %s", name);
4995
4996 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
4997 // masks should be read from the output stream after it is opened for the first time
4998 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
4999 mChannelMasks.add(0);
5000 return;
5001 }
5002
5003 while (str != NULL) {
5004 audio_channel_mask_t channelMask =
5005 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
5006 ARRAY_SIZE(sOutChannelsNameToEnumTable),
5007 str);
5008 if (channelMask != 0) {
5009 mChannelMasks.add(channelMask);
5010 }
5011 str = strtok(NULL, "|");
5012 }
5013 return;
5014}
5015
Eric Laurent1afeecb2014-05-14 08:52:28 -07005016audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name)
5017{
5018 const char *str = strtok(name, "|");
5019
5020 ALOGV("loadGainMode() %s", name);
5021 audio_gain_mode_t mode = 0;
5022 while (str != NULL) {
5023 mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable,
5024 ARRAY_SIZE(sGainModeNameToEnumTable),
5025 str);
5026 str = strtok(NULL, "|");
5027 }
5028 return mode;
5029}
5030
Eric Laurenta121f902014-06-03 13:32:54 -07005031void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index)
Eric Laurent1afeecb2014-05-14 08:52:28 -07005032{
5033 cnode *node = root->first_child;
5034
Eric Laurenta121f902014-06-03 13:32:54 -07005035 sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005036
5037 while (node) {
5038 if (strcmp(node->name, GAIN_MODE) == 0) {
5039 gain->mGain.mode = loadGainMode((char *)node->value);
5040 } else if (strcmp(node->name, GAIN_CHANNELS) == 0) {
Eric Laurenta121f902014-06-03 13:32:54 -07005041 if (mUseInChannelMask) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005042 gain->mGain.channel_mask =
5043 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
5044 ARRAY_SIZE(sInChannelsNameToEnumTable),
5045 (char *)node->value);
5046 } else {
5047 gain->mGain.channel_mask =
5048 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
5049 ARRAY_SIZE(sOutChannelsNameToEnumTable),
5050 (char *)node->value);
5051 }
5052 } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) {
5053 gain->mGain.min_value = atoi((char *)node->value);
5054 } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) {
5055 gain->mGain.max_value = atoi((char *)node->value);
5056 } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) {
5057 gain->mGain.default_value = atoi((char *)node->value);
5058 } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) {
5059 gain->mGain.step_value = atoi((char *)node->value);
5060 } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) {
5061 gain->mGain.min_ramp_ms = atoi((char *)node->value);
5062 } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) {
5063 gain->mGain.max_ramp_ms = atoi((char *)node->value);
5064 }
5065 node = node->next;
5066 }
5067
5068 ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d",
5069 gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value);
5070
5071 if (gain->mGain.mode == 0) {
5072 return;
5073 }
5074 mGains.add(gain);
5075}
5076
5077void AudioPolicyManager::AudioPort::loadGains(cnode *root)
5078{
5079 cnode *node = root->first_child;
Eric Laurenta121f902014-06-03 13:32:54 -07005080 int index = 0;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005081 while (node) {
5082 ALOGV("loadGains() loading gain %s", node->name);
Eric Laurenta121f902014-06-03 13:32:54 -07005083 loadGain(node, index++);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005084 node = node->next;
5085 }
5086}
5087
Eric Laurenta121f902014-06-03 13:32:54 -07005088status_t AudioPolicyManager::AudioPort::checkSamplingRate(uint32_t samplingRate) const
5089{
5090 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
5091 if (mSamplingRates[i] == samplingRate) {
5092 return NO_ERROR;
5093 }
5094 }
5095 return BAD_VALUE;
5096}
5097
5098status_t AudioPolicyManager::AudioPort::checkChannelMask(audio_channel_mask_t channelMask) const
5099{
5100 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
5101 if (mChannelMasks[i] == channelMask) {
5102 return NO_ERROR;
5103 }
5104 }
5105 return BAD_VALUE;
5106}
5107
5108status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const
5109{
5110 for (size_t i = 0; i < mFormats.size(); i ++) {
5111 if (mFormats[i] == format) {
5112 return NO_ERROR;
5113 }
5114 }
5115 return BAD_VALUE;
5116}
5117
5118status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig,
5119 int index) const
5120{
5121 if (index < 0 || (size_t)index >= mGains.size()) {
5122 return BAD_VALUE;
5123 }
5124 return mGains[index]->checkConfig(gainConfig);
5125}
5126
Eric Laurent1afeecb2014-05-14 08:52:28 -07005127void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const
5128{
5129 const size_t SIZE = 256;
5130 char buffer[SIZE];
5131 String8 result;
5132
5133 if (mName.size() != 0) {
5134 snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string());
5135 result.append(buffer);
5136 }
5137
5138 if (mSamplingRates.size() != 0) {
5139 snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, "");
5140 result.append(buffer);
5141 for (size_t i = 0; i < mSamplingRates.size(); i++) {
5142 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
5143 result.append(buffer);
5144 result.append(i == (mSamplingRates.size() - 1) ? "" : ", ");
5145 }
5146 result.append("\n");
5147 }
5148
5149 if (mChannelMasks.size() != 0) {
5150 snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, "");
5151 result.append(buffer);
5152 for (size_t i = 0; i < mChannelMasks.size(); i++) {
5153 snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
5154 result.append(buffer);
5155 result.append(i == (mChannelMasks.size() - 1) ? "" : ", ");
5156 }
5157 result.append("\n");
5158 }
5159
5160 if (mFormats.size() != 0) {
5161 snprintf(buffer, SIZE, "%*s- formats: ", spaces, "");
5162 result.append(buffer);
5163 for (size_t i = 0; i < mFormats.size(); i++) {
5164 snprintf(buffer, SIZE, "%-48s", enumToString(sFormatNameToEnumTable,
5165 ARRAY_SIZE(sFormatNameToEnumTable),
5166 mFormats[i]));
5167 result.append(buffer);
5168 result.append(i == (mFormats.size() - 1) ? "" : ", ");
5169 }
5170 result.append("\n");
5171 }
5172 write(fd, result.string(), result.size());
5173 if (mGains.size() != 0) {
5174 snprintf(buffer, SIZE, "%*s- gains:\n", spaces, "");
5175 write(fd, buffer, strlen(buffer) + 1);
5176 result.append(buffer);
5177 for (size_t i = 0; i < mGains.size(); i++) {
5178 mGains[i]->dump(fd, spaces + 2, i);
5179 }
5180 }
5181}
5182
5183// --- AudioGain class implementation
5184
Eric Laurenta121f902014-06-03 13:32:54 -07005185AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask)
Eric Laurent1afeecb2014-05-14 08:52:28 -07005186{
Eric Laurenta121f902014-06-03 13:32:54 -07005187 mIndex = index;
5188 mUseInChannelMask = useInChannelMask;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005189 memset(&mGain, 0, sizeof(struct audio_gain));
5190}
5191
Eric Laurenta121f902014-06-03 13:32:54 -07005192void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config)
5193{
5194 config->index = mIndex;
5195 config->mode = mGain.mode;
5196 config->channel_mask = mGain.channel_mask;
5197 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
5198 config->values[0] = mGain.default_value;
5199 } else {
5200 uint32_t numValues;
5201 if (mUseInChannelMask) {
5202 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
5203 } else {
5204 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
5205 }
5206 for (size_t i = 0; i < numValues; i++) {
5207 config->values[i] = mGain.default_value;
5208 }
5209 }
5210 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
5211 config->ramp_duration_ms = mGain.min_ramp_ms;
5212 }
5213}
5214
5215status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config)
5216{
5217 if ((config->mode & ~mGain.mode) != 0) {
5218 return BAD_VALUE;
5219 }
5220 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
5221 if ((config->values[0] < mGain.min_value) ||
5222 (config->values[0] > mGain.max_value)) {
5223 return BAD_VALUE;
5224 }
5225 } else {
5226 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
5227 return BAD_VALUE;
5228 }
5229 uint32_t numValues;
5230 if (mUseInChannelMask) {
5231 numValues = audio_channel_count_from_in_mask(config->channel_mask);
5232 } else {
5233 numValues = audio_channel_count_from_out_mask(config->channel_mask);
5234 }
5235 for (size_t i = 0; i < numValues; i++) {
5236 if ((config->values[i] < mGain.min_value) ||
5237 (config->values[i] > mGain.max_value)) {
5238 return BAD_VALUE;
5239 }
5240 }
5241 }
5242 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
5243 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
5244 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
5245 return BAD_VALUE;
5246 }
5247 }
5248 return NO_ERROR;
5249}
5250
Eric Laurent1afeecb2014-05-14 08:52:28 -07005251void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const
5252{
5253 const size_t SIZE = 256;
5254 char buffer[SIZE];
5255 String8 result;
5256
5257 snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
5258 result.append(buffer);
5259 snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
5260 result.append(buffer);
5261 snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
5262 result.append(buffer);
5263 snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
5264 result.append(buffer);
5265 snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
5266 result.append(buffer);
5267 snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
5268 result.append(buffer);
5269 snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
5270 result.append(buffer);
5271 snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
5272 result.append(buffer);
5273 snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
5274 result.append(buffer);
5275
5276 write(fd, result.string(), result.size());
5277}
5278
Eric Laurent1f2f2232014-06-02 12:01:23 -07005279// --- AudioPortConfig class implementation
5280
5281AudioPolicyManager::AudioPortConfig::AudioPortConfig()
5282{
5283 mSamplingRate = 0;
5284 mChannelMask = AUDIO_CHANNEL_NONE;
5285 mFormat = AUDIO_FORMAT_INVALID;
5286 mGain.index = -1;
5287}
5288
Eric Laurenta121f902014-06-03 13:32:54 -07005289status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig(
5290 const struct audio_port_config *config,
5291 struct audio_port_config *backupConfig)
5292{
5293 struct audio_port_config localBackupConfig;
5294 status_t status = NO_ERROR;
5295
5296 localBackupConfig.config_mask = config->config_mask;
5297 toAudioPortConfig(&localBackupConfig);
5298
5299 if (mAudioPort == 0) {
5300 status = NO_INIT;
5301 goto exit;
5302 }
5303 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
5304 status = mAudioPort->checkSamplingRate(config->sample_rate);
5305 if (status != NO_ERROR) {
5306 goto exit;
5307 }
5308 mSamplingRate = config->sample_rate;
5309 }
5310 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
5311 status = mAudioPort->checkChannelMask(config->channel_mask);
5312 if (status != NO_ERROR) {
5313 goto exit;
5314 }
5315 mChannelMask = config->channel_mask;
5316 }
5317 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
5318 status = mAudioPort->checkFormat(config->format);
5319 if (status != NO_ERROR) {
5320 goto exit;
5321 }
5322 mFormat = config->format;
5323 }
5324 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
5325 status = mAudioPort->checkGain(&config->gain, config->gain.index);
5326 if (status != NO_ERROR) {
5327 goto exit;
5328 }
5329 mGain = config->gain;
5330 }
5331
5332exit:
5333 if (status != NO_ERROR) {
5334 applyAudioPortConfig(&localBackupConfig);
5335 }
5336 if (backupConfig != NULL) {
5337 *backupConfig = localBackupConfig;
5338 }
5339 return status;
5340}
5341
Eric Laurent1f2f2232014-06-02 12:01:23 -07005342void AudioPolicyManager::AudioPortConfig::toAudioPortConfig(
5343 struct audio_port_config *dstConfig,
5344 const struct audio_port_config *srcConfig) const
5345{
5346 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
5347 dstConfig->sample_rate = mSamplingRate;
5348 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) {
5349 dstConfig->sample_rate = srcConfig->sample_rate;
5350 }
5351 } else {
5352 dstConfig->sample_rate = 0;
5353 }
5354 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
5355 dstConfig->channel_mask = mChannelMask;
5356 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) {
5357 dstConfig->channel_mask = srcConfig->channel_mask;
5358 }
5359 } else {
5360 dstConfig->channel_mask = AUDIO_CHANNEL_NONE;
5361 }
5362 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
5363 dstConfig->format = mFormat;
5364 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) {
5365 dstConfig->format = srcConfig->format;
5366 }
5367 } else {
5368 dstConfig->format = AUDIO_FORMAT_INVALID;
5369 }
5370 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
5371 dstConfig->gain = mGain;
5372 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) {
5373 dstConfig->gain = srcConfig->gain;
5374 }
5375 } else {
5376 dstConfig->gain.index = -1;
5377 }
5378 if (dstConfig->gain.index != -1) {
5379 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
5380 } else {
5381 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
5382 }
5383}
5384
Eric Laurent1c333e22014-05-20 10:48:17 -07005385// --- IOProfile class implementation
5386
Eric Laurent1afeecb2014-05-14 08:52:28 -07005387AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role,
Eric Laurent1f2f2232014-06-02 12:01:23 -07005388 const sp<HwModule>& module)
Eric Laurent1afeecb2014-05-14 08:52:28 -07005389 : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module), mFlags((audio_output_flags_t)0)
Eric Laurente552edb2014-03-10 17:42:56 -07005390{
5391}
5392
Eric Laurente0720872014-03-11 09:30:41 -07005393AudioPolicyManager::IOProfile::~IOProfile()
Eric Laurente552edb2014-03-10 17:42:56 -07005394{
5395}
5396
5397// checks if the IO profile is compatible with specified parameters.
5398// Sampling rate, format and channel mask must be specified in order to
5399// get a valid a match
Eric Laurente0720872014-03-11 09:30:41 -07005400bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -07005401 uint32_t samplingRate,
5402 audio_format_t format,
5403 audio_channel_mask_t channelMask,
5404 audio_output_flags_t flags) const
5405{
5406 if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
5407 return false;
5408 }
5409
Eric Laurent3a4311c2014-03-17 12:00:47 -07005410 if ((mSupportedDevices.types() & device) != device) {
Eric Laurente552edb2014-03-10 17:42:56 -07005411 return false;
5412 }
5413 if ((mFlags & flags) != flags) {
5414 return false;
5415 }
Eric Laurenta121f902014-06-03 13:32:54 -07005416 if (checkSamplingRate(samplingRate) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005417 return false;
5418 }
Eric Laurenta121f902014-06-03 13:32:54 -07005419 if (checkChannelMask(channelMask) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005420 return false;
5421 }
Eric Laurenta121f902014-06-03 13:32:54 -07005422 if (checkFormat(format) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07005423 return false;
5424 }
5425 return true;
5426}
5427
Eric Laurente0720872014-03-11 09:30:41 -07005428void AudioPolicyManager::IOProfile::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005429{
5430 const size_t SIZE = 256;
5431 char buffer[SIZE];
5432 String8 result;
5433
Eric Laurent1afeecb2014-05-14 08:52:28 -07005434 AudioPort::dump(fd, 4);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005435
Eric Laurente552edb2014-03-10 17:42:56 -07005436 snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags);
5437 result.append(buffer);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005438 snprintf(buffer, SIZE, " - devices:\n");
5439 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07005440 write(fd, result.string(), result.size());
Eric Laurent1afeecb2014-05-14 08:52:28 -07005441 for (size_t i = 0; i < mSupportedDevices.size(); i++) {
5442 mSupportedDevices[i]->dump(fd, 6, i);
5443 }
Eric Laurente552edb2014-03-10 17:42:56 -07005444}
5445
Eric Laurentd4692962014-05-05 18:13:44 -07005446void AudioPolicyManager::IOProfile::log()
5447{
5448 const size_t SIZE = 256;
5449 char buffer[SIZE];
5450 String8 result;
5451
5452 ALOGV(" - sampling rates: ");
5453 for (size_t i = 0; i < mSamplingRates.size(); i++) {
5454 ALOGV(" %d", mSamplingRates[i]);
5455 }
5456
5457 ALOGV(" - channel masks: ");
5458 for (size_t i = 0; i < mChannelMasks.size(); i++) {
5459 ALOGV(" 0x%04x", mChannelMasks[i]);
5460 }
5461
5462 ALOGV(" - formats: ");
5463 for (size_t i = 0; i < mFormats.size(); i++) {
5464 ALOGV(" 0x%08x", mFormats[i]);
5465 }
5466
5467 ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types());
5468 ALOGV(" - flags: 0x%04x\n", mFlags);
5469}
5470
5471
Eric Laurent3a4311c2014-03-17 12:00:47 -07005472// --- DeviceDescriptor implementation
Eric Laurente552edb2014-03-10 17:42:56 -07005473
Eric Laurent1f2f2232014-06-02 12:01:23 -07005474
5475AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) :
5476 AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
5477 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
5478 AUDIO_PORT_ROLE_SOURCE,
5479 NULL),
5480 mDeviceType(type), mAddress(""),
5481 mChannelMask(AUDIO_CHANNEL_NONE), mId(0)
5482{
5483 mAudioPort = this;
Eric Laurenta121f902014-06-03 13:32:54 -07005484 if (mGains.size() > 0) {
5485 mGains[0]->getDefaultConfig(&mGain);
5486 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07005487}
5488
Eric Laurent3a4311c2014-03-17 12:00:47 -07005489bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
Eric Laurente552edb2014-03-10 17:42:56 -07005490{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005491 // Devices are considered equal if they:
5492 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
5493 // - have the same address or one device does not specify the address
5494 // - have the same channel mask or one device does not specify the channel mask
Eric Laurent1c333e22014-05-20 10:48:17 -07005495 return (mDeviceType == other->mDeviceType) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005496 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
Eric Laurent2f8a36f2014-03-26 19:05:55 -07005497 (mChannelMask == 0 || other->mChannelMask == 0 ||
Eric Laurent3a4311c2014-03-17 12:00:47 -07005498 mChannelMask == other->mChannelMask);
5499}
5500
5501void AudioPolicyManager::DeviceVector::refreshTypes()
5502{
Eric Laurent1c333e22014-05-20 10:48:17 -07005503 mDeviceTypes = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005504 for(size_t i = 0; i < size(); i++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005505 mDeviceTypes |= itemAt(i)->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005506 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005507 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005508}
5509
5510ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
5511{
5512 for(size_t i = 0; i < size(); i++) {
5513 if (item->equals(itemAt(i))) {
5514 return i;
Eric Laurente552edb2014-03-10 17:42:56 -07005515 }
5516 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07005517 return -1;
Eric Laurente552edb2014-03-10 17:42:56 -07005518}
5519
Eric Laurent3a4311c2014-03-17 12:00:47 -07005520ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item)
Eric Laurente552edb2014-03-10 17:42:56 -07005521{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005522 ssize_t ret = indexOf(item);
5523
5524 if (ret < 0) {
5525 ret = SortedVector::add(item);
5526 if (ret >= 0) {
5527 refreshTypes();
5528 }
5529 } else {
Eric Laurent1c333e22014-05-20 10:48:17 -07005530 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005531 ret = -1;
5532 }
5533 return ret;
Eric Laurente552edb2014-03-10 17:42:56 -07005534}
5535
Eric Laurent3a4311c2014-03-17 12:00:47 -07005536ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item)
5537{
5538 size_t i;
5539 ssize_t ret = indexOf(item);
5540
5541 if (ret < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07005542 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005543 } else {
5544 ret = SortedVector::removeAt(ret);
5545 if (ret >= 0) {
5546 refreshTypes();
5547 }
5548 }
5549 return ret;
5550}
5551
5552void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types)
5553{
5554 DeviceVector deviceList;
5555
5556 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
5557 types &= ~role_bit;
5558
5559 while (types) {
5560 uint32_t i = 31 - __builtin_clz(types);
5561 uint32_t type = 1 << i;
5562 types &= ~type;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005563 add(new DeviceDescriptor(String8(""), type | role_bit));
Eric Laurent3a4311c2014-03-17 12:00:47 -07005564 }
5565}
5566
Eric Laurent1afeecb2014-05-14 08:52:28 -07005567void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name,
5568 const DeviceVector& declaredDevices)
5569{
5570 char *devName = strtok(name, "|");
5571 while (devName != NULL) {
5572 if (strlen(devName) != 0) {
5573 audio_devices_t type = stringToEnum(sDeviceNameToEnumTable,
5574 ARRAY_SIZE(sDeviceNameToEnumTable),
5575 devName);
5576 if (type != AUDIO_DEVICE_NONE) {
5577 add(new DeviceDescriptor(String8(""), type));
5578 } else {
5579 sp<DeviceDescriptor> deviceDesc =
5580 declaredDevices.getDeviceFromName(String8(devName));
5581 if (deviceDesc != 0) {
5582 add(deviceDesc);
5583 }
5584 }
5585 }
5586 devName = strtok(NULL, "|");
5587 }
5588}
5589
Eric Laurent1c333e22014-05-20 10:48:17 -07005590sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice(
5591 audio_devices_t type, String8 address) const
5592{
5593 sp<DeviceDescriptor> device;
5594 for (size_t i = 0; i < size(); i++) {
5595 if (itemAt(i)->mDeviceType == type) {
5596 device = itemAt(i);
5597 if (itemAt(i)->mAddress = address) {
5598 break;
5599 }
5600 }
5601 }
5602 ALOGV("DeviceVector::getDevice() for type %d address %s found %p",
5603 type, address.string(), device.get());
5604 return device;
5605}
5606
Eric Laurent6a94d692014-05-20 11:18:06 -07005607sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId(
5608 audio_port_handle_t id) const
5609{
5610 sp<DeviceDescriptor> device;
5611 for (size_t i = 0; i < size(); i++) {
5612 ALOGV("DeviceVector::getDeviceFromId(%d) itemAt(%d)->mId %d", id, i, itemAt(i)->mId);
5613 if (itemAt(i)->mId == id) {
5614 device = itemAt(i);
5615 break;
5616 }
5617 }
5618 return device;
5619}
5620
Eric Laurent1c333e22014-05-20 10:48:17 -07005621AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType(
5622 audio_devices_t type) const
5623{
5624 DeviceVector devices;
5625 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
5626 if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) {
5627 devices.add(itemAt(i));
5628 type &= ~itemAt(i)->mDeviceType;
5629 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
5630 itemAt(i)->mDeviceType, itemAt(i).get());
5631 }
5632 }
5633 return devices;
5634}
5635
Eric Laurent1afeecb2014-05-14 08:52:28 -07005636sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName(
5637 const String8& name) const
5638{
5639 sp<DeviceDescriptor> device;
5640 for (size_t i = 0; i < size(); i++) {
5641 if (itemAt(i)->mName == name) {
5642 device = itemAt(i);
5643 break;
5644 }
5645 }
5646 return device;
5647}
5648
Eric Laurent6a94d692014-05-20 11:18:06 -07005649void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig(
5650 struct audio_port_config *dstConfig,
5651 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07005652{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005653 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN;
5654 if (srcConfig != NULL) {
5655 dstConfig->config_mask &= srcConfig->config_mask;
5656 }
5657
5658 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
5659
Eric Laurent6a94d692014-05-20 11:18:06 -07005660 dstConfig->id = mId;
5661 dstConfig->role = audio_is_output_device(mDeviceType) ?
Eric Laurent1c333e22014-05-20 10:48:17 -07005662 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
Eric Laurent6a94d692014-05-20 11:18:06 -07005663 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
Eric Laurent6a94d692014-05-20 11:18:06 -07005664 dstConfig->ext.device.type = mDeviceType;
5665 dstConfig->ext.device.hw_module = mModule->mHandle;
5666 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
Eric Laurent1c333e22014-05-20 10:48:17 -07005667}
5668
5669void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const
5670{
Eric Laurente1715a42014-05-20 11:30:42 -07005671 ALOGV("DeviceVector::toAudioPort() handle %d type %x", mId, mDeviceType);
Eric Laurent1c333e22014-05-20 10:48:17 -07005672 AudioPort::toAudioPort(port);
5673 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07005674 toAudioPortConfig(&port->active_config);
Eric Laurent1c333e22014-05-20 10:48:17 -07005675 port->ext.device.type = mDeviceType;
Eric Laurent6a94d692014-05-20 11:18:06 -07005676 port->ext.device.hw_module = mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07005677 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
5678}
5679
Eric Laurent1afeecb2014-05-14 08:52:28 -07005680status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const
Eric Laurent3a4311c2014-03-17 12:00:47 -07005681{
5682 const size_t SIZE = 256;
5683 char buffer[SIZE];
Eric Laurent1afeecb2014-05-14 08:52:28 -07005684 String8 result;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005685
Eric Laurent1afeecb2014-05-14 08:52:28 -07005686 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
5687 result.append(buffer);
5688 if (mId != 0) {
5689 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
5690 result.append(buffer);
5691 }
5692 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
5693 enumToString(sDeviceNameToEnumTable,
5694 ARRAY_SIZE(sDeviceNameToEnumTable),
5695 mDeviceType));
5696 result.append(buffer);
5697 if (mAddress.size() != 0) {
5698 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
5699 result.append(buffer);
5700 }
5701 if (mChannelMask != AUDIO_CHANNEL_NONE) {
5702 snprintf(buffer, SIZE, "%*s- channel mask: %08x\n", spaces, "", mChannelMask);
5703 result.append(buffer);
5704 }
5705 write(fd, result.string(), result.size());
5706 AudioPort::dump(fd, spaces);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005707
5708 return NO_ERROR;
5709}
5710
5711
5712// --- audio_policy.conf file parsing
5713
Eric Laurente0720872014-03-11 09:30:41 -07005714audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005715{
5716 uint32_t flag = 0;
5717
5718 // it is OK to cast name to non const here as we are not going to use it after
5719 // strtok() modifies it
5720 char *flagName = strtok(name, "|");
5721 while (flagName != NULL) {
5722 if (strlen(flagName) != 0) {
5723 flag |= stringToEnum(sFlagNameToEnumTable,
5724 ARRAY_SIZE(sFlagNameToEnumTable),
5725 flagName);
5726 }
5727 flagName = strtok(NULL, "|");
5728 }
5729 //force direct flag if offload flag is set: offloading implies a direct output stream
5730 // and all common behaviors are driven by checking only the direct flag
5731 // this should normally be set appropriately in the policy configuration file
5732 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
5733 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
5734 }
5735
5736 return (audio_output_flags_t)flag;
5737}
5738
Eric Laurente0720872014-03-11 09:30:41 -07005739audio_devices_t AudioPolicyManager::parseDeviceNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07005740{
5741 uint32_t device = 0;
5742
5743 char *devName = strtok(name, "|");
5744 while (devName != NULL) {
5745 if (strlen(devName) != 0) {
5746 device |= stringToEnum(sDeviceNameToEnumTable,
5747 ARRAY_SIZE(sDeviceNameToEnumTable),
5748 devName);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005749 }
Eric Laurente552edb2014-03-10 17:42:56 -07005750 devName = strtok(NULL, "|");
Eric Laurent3a4311c2014-03-17 12:00:47 -07005751 }
Eric Laurente552edb2014-03-10 17:42:56 -07005752 return device;
5753}
5754
Eric Laurente0720872014-03-11 09:30:41 -07005755void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005756{
Eric Laurente552edb2014-03-10 17:42:56 -07005757 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07005758 cnode *node;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005759 sp<HwModule> module = new HwModule(root->name);
Eric Laurente552edb2014-03-10 17:42:56 -07005760
Eric Laurent1afeecb2014-05-14 08:52:28 -07005761 node = config_find(root, DEVICES_TAG);
5762 if (node != NULL) {
5763 node = node->first_child;
5764 while (node) {
5765 ALOGV("loadHwModule() loading device %s", node->name);
5766 status_t tmpStatus = module->loadDevice(node);
5767 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5768 status = tmpStatus;
5769 }
5770 node = node->next;
5771 }
5772 }
5773 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07005774 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005775 node = node->first_child;
5776 while (node) {
5777 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005778 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005779 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5780 status = tmpStatus;
5781 }
5782 node = node->next;
5783 }
5784 }
5785 node = config_find(root, INPUTS_TAG);
5786 if (node != NULL) {
5787 node = node->first_child;
5788 while (node) {
5789 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005790 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07005791 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
5792 status = tmpStatus;
5793 }
5794 node = node->next;
5795 }
5796 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005797 loadGlobalConfig(root, module);
5798
Eric Laurente552edb2014-03-10 17:42:56 -07005799 if (status == NO_ERROR) {
5800 mHwModules.add(module);
Eric Laurente552edb2014-03-10 17:42:56 -07005801 }
5802}
5803
Eric Laurente0720872014-03-11 09:30:41 -07005804void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07005805{
5806 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
5807 if (node == NULL) {
5808 return;
5809 }
5810
5811 node = node->first_child;
5812 while (node) {
5813 ALOGV("loadHwModules() loading module %s", node->name);
5814 loadHwModule(node);
5815 node = node->next;
5816 }
5817}
5818
Eric Laurent1f2f2232014-06-02 12:01:23 -07005819void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module)
Eric Laurente552edb2014-03-10 17:42:56 -07005820{
5821 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
Eric Laurenteb108a42014-06-06 14:56:52 -07005822
Eric Laurente552edb2014-03-10 17:42:56 -07005823 if (node == NULL) {
5824 return;
5825 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07005826 DeviceVector declaredDevices;
5827 if (module != NULL) {
5828 declaredDevices = module->mDeclaredDevices;
5829 }
5830
Eric Laurente552edb2014-03-10 17:42:56 -07005831 node = node->first_child;
5832 while (node) {
5833 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005834 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
5835 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005836 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
5837 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005838 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07005839 audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
Eric Laurente552edb2014-03-10 17:42:56 -07005840 ARRAY_SIZE(sDeviceNameToEnumTable),
5841 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005842 if (device != AUDIO_DEVICE_NONE) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005843 mDefaultOutputDevice = new DeviceDescriptor(String8(""), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005844 } else {
5845 ALOGW("loadGlobalConfig() default device not specified");
5846 }
Eric Laurent1c333e22014-05-20 10:48:17 -07005847 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07005848 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07005849 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
5850 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005851 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07005852 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
5853 mSpeakerDrcEnabled = stringToBool((char *)node->value);
5854 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
Eric Laurenteb108a42014-06-06 14:56:52 -07005855 } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) {
5856 uint32_t major, minor;
5857 sscanf((char *)node->value, "%u.%u", &major, &minor);
5858 module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor);
5859 ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u",
5860 module->mHalVersion, major, minor);
Eric Laurente552edb2014-03-10 17:42:56 -07005861 }
5862 node = node->next;
5863 }
5864}
5865
Eric Laurente0720872014-03-11 09:30:41 -07005866status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07005867{
5868 cnode *root;
5869 char *data;
5870
5871 data = (char *)load_file(path, NULL);
5872 if (data == NULL) {
5873 return -ENODEV;
5874 }
5875 root = config_node("", "");
5876 config_load(root, data);
5877
Eric Laurente552edb2014-03-10 17:42:56 -07005878 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07005879 // legacy audio_policy.conf files have one global_configuration section
5880 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07005881 config_free(root);
5882 free(root);
5883 free(data);
5884
5885 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
5886
5887 return NO_ERROR;
5888}
5889
Eric Laurente0720872014-03-11 09:30:41 -07005890void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07005891{
Eric Laurent1f2f2232014-06-02 12:01:23 -07005892 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07005893 sp<IOProfile> profile;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005894 sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""),
5895 AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005896 mAvailableOutputDevices.add(mDefaultOutputDevice);
5897 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005898
5899 module = new HwModule("primary");
5900
Eric Laurent1afeecb2014-05-14 08:52:28 -07005901 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005902 profile->mSamplingRates.add(44100);
5903 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5904 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005905 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005906 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
5907 module->mOutputProfiles.add(profile);
5908
Eric Laurent1afeecb2014-05-14 08:52:28 -07005909 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07005910 profile->mSamplingRates.add(8000);
5911 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
5912 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07005913 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07005914 module->mInputProfiles.add(profile);
5915
5916 mHwModules.add(module);
5917}
5918
5919}; // namespace android