blob: 9918b6f8a38d391230a65926965bb94e08431adc [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()
Hochi Huang327cb702014-09-21 09:47:31 +080029#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_FM_TUNER)
Eric Laurente552edb2014-03-10 17:42:56 -070030// 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
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -070033// A device mask for all audio input and output devices where matching inputs/outputs on device
34// type alone is not enough: the address must match too
35#define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \
36 AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
Eric Laurente552edb2014-03-10 17:42:56 -070037
Eric Laurentd4692962014-05-05 18:13:44 -070038#include <inttypes.h>
Eric Laurente552edb2014-03-10 17:42:56 -070039#include <math.h>
Eric Laurentd4692962014-05-05 18:13:44 -070040
Eric Laurente552edb2014-03-10 17:42:56 -070041#include <cutils/properties.h>
Eric Laurentd4692962014-05-05 18:13:44 -070042#include <utils/Log.h>
43#include <hardware/audio.h>
44#include <hardware/audio_effect.h>
Eric Laurent3b73df72014-03-11 09:06:29 -070045#include <media/AudioParameter.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080046#include <media/AudioPolicyHelper.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070047#include <soundtrigger/SoundTrigger.h>
Eric Laurentd4692962014-05-05 18:13:44 -070048#include "AudioPolicyManager.h"
Eric Laurent1afeecb2014-05-14 08:52:28 -070049#include "audio_policy_conf.h"
Eric Laurente552edb2014-03-10 17:42:56 -070050
Eric Laurent3b73df72014-03-11 09:06:29 -070051namespace android {
Eric Laurente552edb2014-03-10 17:42:56 -070052
53// ----------------------------------------------------------------------------
Eric Laurent3a4311c2014-03-17 12:00:47 -070054// Definitions for audio_policy.conf file parsing
55// ----------------------------------------------------------------------------
56
57struct StringToEnum {
58 const char *name;
59 uint32_t value;
60};
61
62#define STRING_TO_ENUM(string) { #string, string }
63#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64
65const StringToEnum sDeviceNameToEnumTable[] = {
66 STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
67 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
Jon Eklund11c9fb12014-06-23 14:47:03 -050068 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER_SAFE),
Eric Laurent3a4311c2014-03-17 12:00:47 -070069 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
70 STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
71 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO),
72 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET),
73 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
74 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
75 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP),
76 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES),
77 STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
78 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
79 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070080 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI),
Eric Laurent3a4311c2014-03-17 12:00:47 -070081 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
82 STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
83 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
84 STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
85 STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
86 STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
Eric Laurent1b776232014-05-19 17:26:41 -070087 STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX),
88 STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE),
89 STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC),
90 STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF),
91 STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM),
Eric Laurente1d37b72014-07-29 10:26:26 -070092 STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_LINE),
Eric Laurenta57ab8d2014-07-30 10:01:42 -050093 STRING_TO_ENUM(AUDIO_DEVICE_IN_AMBIENT),
Eric Laurent3a4311c2014-03-17 12:00:47 -070094 STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
95 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
96 STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO),
97 STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
98 STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
Eric Laurent1b776232014-05-19 17:26:41 -070099 STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI),
Eric Laurent1b776232014-05-19 17:26:41 -0700100 STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
Eric Laurentc2730ba2014-07-20 15:47:07 -0700101 STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700102 STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
103 STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
104 STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
105 STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
106 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
Eric Laurentd4692962014-05-05 18:13:44 -0700107 STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
Eric Laurent1b776232014-05-19 17:26:41 -0700108 STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
109 STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
110 STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE),
111 STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF),
Mike Lockwood41b0e242014-05-13 15:23:35 -0700112 STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
Terry Heo7999a222014-06-27 15:23:36 +0900113 STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700114};
115
Eric Laurent5dbe4712014-09-19 19:04:57 -0700116const StringToEnum sOutputFlagNameToEnumTable[] = {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700117 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
118 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
119 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
120 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
121 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
122 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
Eric Laurent93c3d412014-08-01 14:48:35 -0700123 STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700124};
125
Eric Laurent5dbe4712014-09-19 19:04:57 -0700126const StringToEnum sInputFlagNameToEnumTable[] = {
127 STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST),
128 STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD),
129};
130
Eric Laurent3a4311c2014-03-17 12:00:47 -0700131const StringToEnum sFormatNameToEnumTable[] = {
132 STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
133 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
134 STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
135 STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
136 STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
137 STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
138 STRING_TO_ENUM(AUDIO_FORMAT_MP3),
139 STRING_TO_ENUM(AUDIO_FORMAT_AAC),
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530140 STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN),
141 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC),
142 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR),
143 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP),
144 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1),
145 STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE),
146 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC),
147 STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD),
148 STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2),
149 STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700150 STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
Eric Laurentab5cdba2014-06-09 17:22:27 -0700151 STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1),
152 STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2),
153 STRING_TO_ENUM(AUDIO_FORMAT_OPUS),
154 STRING_TO_ENUM(AUDIO_FORMAT_AC3),
155 STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700156};
157
158const StringToEnum sOutChannelsNameToEnumTable[] = {
159 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
160 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
Andy Hung3a0fe122014-07-29 17:56:46 -0700161 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD),
Eric Laurent3a4311c2014-03-17 12:00:47 -0700162 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
163 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
164};
165
166const StringToEnum sInChannelsNameToEnumTable[] = {
167 STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
168 STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
169 STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
170};
171
Eric Laurent1afeecb2014-05-14 08:52:28 -0700172const StringToEnum sGainModeNameToEnumTable[] = {
173 STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT),
174 STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS),
175 STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP),
176};
177
Eric Laurent3a4311c2014-03-17 12:00:47 -0700178
179uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table,
180 size_t size,
181 const char *name)
182{
183 for (size_t i = 0; i < size; i++) {
184 if (strcmp(table[i].name, name) == 0) {
185 ALOGV("stringToEnum() found %s", table[i].name);
186 return table[i].value;
187 }
188 }
189 return 0;
190}
191
192const char *AudioPolicyManager::enumToString(const struct StringToEnum *table,
193 size_t size,
194 uint32_t value)
195{
196 for (size_t i = 0; i < size; i++) {
197 if (table[i].value == value) {
198 return table[i].name;
199 }
200 }
201 return "";
202}
203
204bool AudioPolicyManager::stringToBool(const char *value)
205{
206 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
207}
208
209
210// ----------------------------------------------------------------------------
Eric Laurente552edb2014-03-10 17:42:56 -0700211// AudioPolicyInterface implementation
212// ----------------------------------------------------------------------------
213
Eric Laurente0720872014-03-11 09:30:41 -0700214status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device,
Eric Laurent3b73df72014-03-11 09:06:29 -0700215 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -0700216 const char *device_address)
217{
Eric Laurent22226012014-08-01 17:00:54 -0700218 String8 address = (device_address == NULL) ? String8("") : String8(device_address);
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700219 // handle legacy remote submix case where the address was not always specified
220 if (deviceDistinguishesOnAddress(device) && (address.length() == 0)) {
221 address = String8("0");
222 }
Eric Laurente552edb2014-03-10 17:42:56 -0700223
Eric Laurent22226012014-08-01 17:00:54 -0700224 ALOGV("setDeviceConnectionState() device: %x, state %d, address %s",
225 device, state, address.string());
Eric Laurente552edb2014-03-10 17:42:56 -0700226
227 // connect/disconnect only 1 device at a time
228 if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
229
Eric Laurente552edb2014-03-10 17:42:56 -0700230 // handle output devices
231 if (audio_is_output_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700232 SortedVector <audio_io_handle_t> outputs;
233
Eric Laurent1afeecb2014-05-14 08:52:28 -0700234 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
235 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700236 ssize_t index = mAvailableOutputDevices.indexOf(devDesc);
237
Eric Laurente552edb2014-03-10 17:42:56 -0700238 // save a copy of the opened output descriptors before any output is opened or closed
239 // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
240 mPreviousOutputs = mOutputs;
Eric Laurente552edb2014-03-10 17:42:56 -0700241 switch (state)
242 {
243 // handle output device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700244 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE:
Eric Laurent3a4311c2014-03-17 12:00:47 -0700245 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700246 ALOGW("setDeviceConnectionState() device already connected: %x", device);
247 return INVALID_OPERATION;
248 }
249 ALOGV("setDeviceConnectionState() connecting device %x", device);
250
Eric Laurente552edb2014-03-10 17:42:56 -0700251 // register new device as available
Eric Laurent3a4311c2014-03-17 12:00:47 -0700252 index = mAvailableOutputDevices.add(devDesc);
253 if (index >= 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700254 sp<HwModule> module = getModuleForDevice(device);
Eric Laurentcf817a22014-08-04 20:36:31 -0700255 if (module == 0) {
256 ALOGD("setDeviceConnectionState() could not find HW module for device %08x",
257 device);
258 mAvailableOutputDevices.remove(devDesc);
259 return INVALID_OPERATION;
260 }
261 mAvailableOutputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700262 mAvailableOutputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700263 } else {
264 return NO_MEMORY;
Eric Laurente552edb2014-03-10 17:42:56 -0700265 }
266
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700267 if (checkOutputsForDevice(devDesc, state, outputs, address) != NO_ERROR) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -0700268 mAvailableOutputDevices.remove(devDesc);
269 return INVALID_OPERATION;
270 }
271 // outputs should never be empty here
272 ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():"
273 "checkOutputsForDevice() returned no outputs but status OK");
274 ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
275 outputs.size());
Eric Laurente552edb2014-03-10 17:42:56 -0700276 break;
277 // handle output device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700278 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700279 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700280 ALOGW("setDeviceConnectionState() device not connected: %x", device);
281 return INVALID_OPERATION;
282 }
283
Paul McLean5c477aa2014-08-20 16:47:57 -0700284 ALOGV("setDeviceConnectionState() disconnecting output device %x", device);
285
286 // Set Disconnect to HALs
287 AudioParameter param = AudioParameter(address);
288 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
289 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
290
Eric Laurente552edb2014-03-10 17:42:56 -0700291 // remove device from available output devices
Eric Laurent3a4311c2014-03-17 12:00:47 -0700292 mAvailableOutputDevices.remove(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700293
Jean-Michel Trivif17026d2014-08-10 14:30:48 -0700294 checkOutputsForDevice(devDesc, state, outputs, address);
Eric Laurente552edb2014-03-10 17:42:56 -0700295 } break;
296
297 default:
298 ALOGE("setDeviceConnectionState() invalid state: %x", state);
299 return BAD_VALUE;
300 }
301
Eric Laurent3a4311c2014-03-17 12:00:47 -0700302 // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP
303 // output is suspended before any tracks are moved to it
Eric Laurente552edb2014-03-10 17:42:56 -0700304 checkA2dpSuspend();
305 checkOutputForAllStrategies();
306 // outputs must be closed after checkOutputForAllStrategies() is executed
307 if (!outputs.isEmpty()) {
308 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700309 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -0700310 // close unused outputs after device disconnection or direct outputs that have been
311 // opened by checkOutputsForDevice() to query dynamic parameters
Eric Laurent3b73df72014-03-11 09:06:29 -0700312 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) ||
Eric Laurente552edb2014-03-10 17:42:56 -0700313 (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
314 (desc->mDirectOpenCount == 0))) {
315 closeOutput(outputs[i]);
316 }
317 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700318 // check again after closing A2DP output to reset mA2dpSuspended if needed
319 checkA2dpSuspend();
Eric Laurente552edb2014-03-10 17:42:56 -0700320 }
321
322 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700323 if (mPhoneState == AUDIO_MODE_IN_CALL) {
324 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
325 updateCallRouting(newDevice);
326 }
Eric Laurente552edb2014-03-10 17:42:56 -0700327 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurentc2730ba2014-07-20 15:47:07 -0700328 audio_io_handle_t output = mOutputs.keyAt(i);
329 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
330 audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i),
331 true /*fromCache*/);
332 // do not force device change on duplicated output because if device is 0, it will
333 // also force a device 0 for the two outputs it is duplicated to which may override
334 // a valid device selection on those outputs.
335 bool force = !mOutputs.valueAt(i)->isDuplicated()
336 && (!deviceDistinguishesOnAddress(device)
337 // always force when disconnecting (a non-duplicated device)
338 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
339 setOutputDevice(output, newDevice, force, 0);
340 }
Eric Laurente552edb2014-03-10 17:42:56 -0700341 }
342
Eric Laurent72aa32f2014-05-30 18:51:48 -0700343 mpClientInterface->onAudioPortListUpdate();
Eric Laurentb71e58b2014-05-29 16:08:11 -0700344 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700345 } // end if is output device
346
Eric Laurente552edb2014-03-10 17:42:56 -0700347 // handle input devices
348 if (audio_is_input_device(device)) {
Eric Laurentd4692962014-05-05 18:13:44 -0700349 SortedVector <audio_io_handle_t> inputs;
350
Eric Laurent1afeecb2014-05-14 08:52:28 -0700351 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
352 devDesc->mAddress = address;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700353 ssize_t index = mAvailableInputDevices.indexOf(devDesc);
Eric Laurente552edb2014-03-10 17:42:56 -0700354 switch (state)
355 {
356 // handle input device connection
Eric Laurent3b73df72014-03-11 09:06:29 -0700357 case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700358 if (index >= 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700359 ALOGW("setDeviceConnectionState() device already connected: %d", device);
360 return INVALID_OPERATION;
361 }
Eric Laurent1f2f2232014-06-02 12:01:23 -0700362 sp<HwModule> module = getModuleForDevice(device);
Eric Laurent6a94d692014-05-20 11:18:06 -0700363 if (module == NULL) {
364 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x",
365 device);
366 return INVALID_OPERATION;
367 }
Eric Laurentd4692962014-05-05 18:13:44 -0700368 if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) {
369 return INVALID_OPERATION;
370 }
371
Eric Laurent3a4311c2014-03-17 12:00:47 -0700372 index = mAvailableInputDevices.add(devDesc);
373 if (index >= 0) {
374 mAvailableInputDevices[index]->mId = nextUniqueId();
Eric Laurent6a94d692014-05-20 11:18:06 -0700375 mAvailableInputDevices[index]->mModule = module;
Eric Laurent3a4311c2014-03-17 12:00:47 -0700376 } else {
377 return NO_MEMORY;
378 }
Eric Laurentd4692962014-05-05 18:13:44 -0700379 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700380
381 // handle input device disconnection
Eric Laurent3b73df72014-03-11 09:06:29 -0700382 case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700383 if (index < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -0700384 ALOGW("setDeviceConnectionState() device not connected: %d", device);
385 return INVALID_OPERATION;
386 }
Paul McLean5c477aa2014-08-20 16:47:57 -0700387
388 ALOGV("setDeviceConnectionState() disconnecting input device %x", device);
389
390 // Set Disconnect to HALs
391 AudioParameter param = AudioParameter(address);
392 param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device);
393 mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString());
394
Eric Laurentd4692962014-05-05 18:13:44 -0700395 checkInputsForDevice(device, state, inputs, address);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700396 mAvailableInputDevices.remove(devDesc);
Paul McLean5c477aa2014-08-20 16:47:57 -0700397
Eric Laurentd4692962014-05-05 18:13:44 -0700398 } break;
Eric Laurente552edb2014-03-10 17:42:56 -0700399
400 default:
401 ALOGE("setDeviceConnectionState() invalid state: %x", state);
402 return BAD_VALUE;
403 }
404
Eric Laurentd4692962014-05-05 18:13:44 -0700405 closeAllInputs();
Eric Laurente552edb2014-03-10 17:42:56 -0700406
Eric Laurentc2730ba2014-07-20 15:47:07 -0700407 if (mPhoneState == AUDIO_MODE_IN_CALL) {
408 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
409 updateCallRouting(newDevice);
410 }
411
Eric Laurentb52c1522014-05-20 11:27:36 -0700412 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -0700413 return NO_ERROR;
Eric Laurentd4692962014-05-05 18:13:44 -0700414 } // end if is input device
Eric Laurente552edb2014-03-10 17:42:56 -0700415
416 ALOGW("setDeviceConnectionState() invalid device: %x", device);
417 return BAD_VALUE;
418}
419
Eric Laurente0720872014-03-11 09:30:41 -0700420audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device,
Eric Laurente552edb2014-03-10 17:42:56 -0700421 const char *device_address)
422{
Eric Laurent3b73df72014-03-11 09:06:29 -0700423 audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurent1afeecb2014-05-14 08:52:28 -0700424 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
Eric Laurent22226012014-08-01 17:00:54 -0700425 devDesc->mAddress = (device_address == NULL) ? String8("") : String8(device_address);
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700426 // handle legacy remote submix case where the address was not always specified
427 if (deviceDistinguishesOnAddress(device) && (devDesc->mAddress.length() == 0)) {
428 devDesc->mAddress = String8("0");
429 }
Eric Laurent3a4311c2014-03-17 12:00:47 -0700430 ssize_t index;
431 DeviceVector *deviceVector;
432
Eric Laurente552edb2014-03-10 17:42:56 -0700433 if (audio_is_output_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700434 deviceVector = &mAvailableOutputDevices;
Eric Laurente552edb2014-03-10 17:42:56 -0700435 } else if (audio_is_input_device(device)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -0700436 deviceVector = &mAvailableInputDevices;
437 } else {
438 ALOGW("getDeviceConnectionState() invalid device type %08x", device);
439 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurente552edb2014-03-10 17:42:56 -0700440 }
441
Eric Laurent3a4311c2014-03-17 12:00:47 -0700442 index = deviceVector->indexOf(devDesc);
443 if (index >= 0) {
444 return AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
445 } else {
446 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
447 }
Eric Laurente552edb2014-03-10 17:42:56 -0700448}
449
Eric Laurentc2730ba2014-07-20 15:47:07 -0700450void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs)
451{
452 bool createTxPatch = false;
453 struct audio_patch patch;
454 patch.num_sources = 1;
455 patch.num_sinks = 1;
456 status_t status;
457 audio_patch_handle_t afPatchHandle;
458 DeviceVector deviceList;
459
460 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
461 ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice);
462
463 // release existing RX patch if any
464 if (mCallRxPatch != 0) {
465 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
466 mCallRxPatch.clear();
467 }
468 // release TX patch if any
469 if (mCallTxPatch != 0) {
470 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
471 mCallTxPatch.clear();
472 }
473
474 // If the RX device is on the primary HW module, then use legacy routing method for voice calls
475 // via setOutputDevice() on primary output.
476 // Otherwise, create two audio patches for TX and RX path.
477 if (availablePrimaryOutputDevices() & rxDevice) {
478 setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs);
479 // If the TX device is also on the primary HW module, setOutputDevice() will take care
480 // of it due to legacy implementation. If not, create a patch.
481 if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN)
482 == AUDIO_DEVICE_NONE) {
483 createTxPatch = true;
484 }
485 } else {
486 // create RX path audio patch
487 deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice);
488 ALOG_ASSERT(!deviceList.isEmpty(),
489 "updateCallRouting() selected device not in output device list");
490 sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0);
491 deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX);
492 ALOG_ASSERT(!deviceList.isEmpty(),
493 "updateCallRouting() no telephony RX device");
494 sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0);
495
496 rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
497 rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
498
499 // request to reuse existing output stream if one is already opened to reach the RX device
500 SortedVector<audio_io_handle_t> outputs =
501 getOutputsForDevice(rxDevice, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700502 audio_io_handle_t output = selectOutput(outputs,
503 AUDIO_OUTPUT_FLAG_NONE,
504 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700505 if (output != AUDIO_IO_HANDLE_NONE) {
506 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
507 ALOG_ASSERT(!outputDesc->isDuplicated(),
508 "updateCallRouting() RX device output is duplicated");
509 outputDesc->toAudioPortConfig(&patch.sources[1]);
510 patch.num_sources = 2;
511 }
512
513 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
514 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
515 ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch",
516 status);
517 if (status == NO_ERROR) {
518 mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
519 &patch, mUidCached);
520 mCallRxPatch->mAfPatchHandle = afPatchHandle;
521 mCallRxPatch->mUid = mUidCached;
522 }
523 createTxPatch = true;
524 }
525 if (createTxPatch) {
526
527 struct audio_patch patch;
528 patch.num_sources = 1;
529 patch.num_sinks = 1;
530 deviceList = mAvailableInputDevices.getDevicesFromType(txDevice);
531 ALOG_ASSERT(!deviceList.isEmpty(),
532 "updateCallRouting() selected device not in input device list");
533 sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0);
534 txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]);
535 deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
536 ALOG_ASSERT(!deviceList.isEmpty(),
537 "updateCallRouting() no telephony TX device");
538 sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0);
539 txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]);
540
541 SortedVector<audio_io_handle_t> outputs =
542 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs);
Eric Laurent8838a382014-09-08 16:44:28 -0700543 audio_io_handle_t output = selectOutput(outputs,
544 AUDIO_OUTPUT_FLAG_NONE,
545 AUDIO_FORMAT_INVALID);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700546 // request to reuse existing output stream if one is already opened to reach the TX
547 // path output device
548 if (output != AUDIO_IO_HANDLE_NONE) {
549 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
550 ALOG_ASSERT(!outputDesc->isDuplicated(),
551 "updateCallRouting() RX device output is duplicated");
552 outputDesc->toAudioPortConfig(&patch.sources[1]);
553 patch.num_sources = 2;
554 }
555
556 afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
557 status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0);
558 ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch",
559 status);
560 if (status == NO_ERROR) {
561 mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
562 &patch, mUidCached);
563 mCallTxPatch->mAfPatchHandle = afPatchHandle;
564 mCallTxPatch->mUid = mUidCached;
565 }
566 }
567}
568
Eric Laurente0720872014-03-11 09:30:41 -0700569void AudioPolicyManager::setPhoneState(audio_mode_t state)
Eric Laurente552edb2014-03-10 17:42:56 -0700570{
571 ALOGV("setPhoneState() state %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700572 if (state < 0 || state >= AUDIO_MODE_CNT) {
Eric Laurente552edb2014-03-10 17:42:56 -0700573 ALOGW("setPhoneState() invalid state %d", state);
574 return;
575 }
576
577 if (state == mPhoneState ) {
578 ALOGW("setPhoneState() setting same state %d", state);
579 return;
580 }
581
582 // if leaving call state, handle special case of active streams
583 // pertaining to sonification strategy see handleIncallSonification()
584 if (isInCall()) {
585 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700586 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800587 if (stream == AUDIO_STREAM_PATCH) {
588 continue;
589 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700590 handleIncallSonification((audio_stream_type_t)stream, false, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700591 }
592 }
593
594 // store previous phone state for management of sonification strategy below
595 int oldState = mPhoneState;
596 mPhoneState = state;
597 bool force = false;
598
599 // are we entering or starting a call
600 if (!isStateInCall(oldState) && isStateInCall(state)) {
601 ALOGV(" Entering call in setPhoneState()");
602 // force routing command to audio hardware when starting a call
603 // even if no device change is needed
604 force = true;
605 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
606 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
607 sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
608 }
609 } else if (isStateInCall(oldState) && !isStateInCall(state)) {
610 ALOGV(" Exiting call in setPhoneState()");
611 // force routing command to audio hardware when exiting a call
612 // even if no device change is needed
613 force = true;
614 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
615 mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
616 sVolumeProfiles[AUDIO_STREAM_DTMF][j];
617 }
618 } else if (isStateInCall(state) && (state != oldState)) {
619 ALOGV(" Switching between telephony and VoIP in setPhoneState()");
620 // force routing command to audio hardware when switching between telephony and VoIP
621 // even if no device change is needed
622 force = true;
623 }
624
625 // check for device and output changes triggered by new phone state
Eric Laurente552edb2014-03-10 17:42:56 -0700626 checkA2dpSuspend();
627 checkOutputForAllStrategies();
628 updateDevicesAndOutputs();
629
Eric Laurent1f2f2232014-06-02 12:01:23 -0700630 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -0700631
Eric Laurente552edb2014-03-10 17:42:56 -0700632 int delayMs = 0;
633 if (isStateInCall(state)) {
634 nsecs_t sysTime = systemTime();
635 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -0700636 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -0700637 // mute media and sonification strategies and delay device switch by the largest
638 // latency of any output where either strategy is active.
639 // This avoid sending the ring tone or music tail into the earpiece or headset.
640 if ((desc->isStrategyActive(STRATEGY_MEDIA,
641 SONIFICATION_HEADSET_MUSIC_DELAY,
642 sysTime) ||
643 desc->isStrategyActive(STRATEGY_SONIFICATION,
644 SONIFICATION_HEADSET_MUSIC_DELAY,
645 sysTime)) &&
646 (delayMs < (int)desc->mLatency*2)) {
647 delayMs = desc->mLatency*2;
648 }
649 setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
650 setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
651 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
652 setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
653 setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
654 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
655 }
656 }
657
Eric Laurentc2730ba2014-07-20 15:47:07 -0700658 // Note that despite the fact that getNewOutputDevice() is called on the primary output,
659 // the device returned is not necessarily reachable via this output
660 audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
661 // force routing command to audio hardware when ending call
662 // even if no device change is needed
663 if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) {
664 rxDevice = hwOutputDesc->device();
665 }
Eric Laurente552edb2014-03-10 17:42:56 -0700666
Eric Laurentc2730ba2014-07-20 15:47:07 -0700667 if (state == AUDIO_MODE_IN_CALL) {
668 updateCallRouting(rxDevice, delayMs);
669 } else if (oldState == AUDIO_MODE_IN_CALL) {
670 if (mCallRxPatch != 0) {
671 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0);
672 mCallRxPatch.clear();
673 }
674 if (mCallTxPatch != 0) {
675 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0);
676 mCallTxPatch.clear();
677 }
678 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
679 } else {
680 setOutputDevice(mPrimaryOutput, rxDevice, force, 0);
681 }
Eric Laurente552edb2014-03-10 17:42:56 -0700682 // if entering in call state, handle special case of active streams
683 // pertaining to sonification strategy see handleIncallSonification()
684 if (isStateInCall(state)) {
685 ALOGV("setPhoneState() in call state management: new state is %d", state);
Eric Laurent3b73df72014-03-11 09:06:29 -0700686 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800687 if (stream == AUDIO_STREAM_PATCH) {
688 continue;
689 }
Eric Laurent3b73df72014-03-11 09:06:29 -0700690 handleIncallSonification((audio_stream_type_t)stream, true, true);
Eric Laurente552edb2014-03-10 17:42:56 -0700691 }
692 }
693
694 // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
Eric Laurent3b73df72014-03-11 09:06:29 -0700695 if (state == AUDIO_MODE_RINGTONE &&
696 isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -0700697 mLimitRingtoneVolume = true;
698 } else {
699 mLimitRingtoneVolume = false;
700 }
701}
702
Eric Laurente0720872014-03-11 09:30:41 -0700703void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage,
Eric Laurent3b73df72014-03-11 09:06:29 -0700704 audio_policy_forced_cfg_t config)
Eric Laurente552edb2014-03-10 17:42:56 -0700705{
706 ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
707
708 bool forceVolumeReeval = false;
709 switch(usage) {
Eric Laurent3b73df72014-03-11 09:06:29 -0700710 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
711 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
712 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700713 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
714 return;
715 }
716 forceVolumeReeval = true;
717 mForceUse[usage] = config;
718 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700719 case AUDIO_POLICY_FORCE_FOR_MEDIA:
720 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
721 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
722 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
723 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
Hochi Huang327cb702014-09-21 09:47:31 +0800724 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Eric Laurente552edb2014-03-10 17:42:56 -0700725 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
726 return;
727 }
728 mForceUse[usage] = config;
729 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700730 case AUDIO_POLICY_FORCE_FOR_RECORD:
731 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
732 config != AUDIO_POLICY_FORCE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -0700733 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
734 return;
735 }
736 mForceUse[usage] = config;
737 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700738 case AUDIO_POLICY_FORCE_FOR_DOCK:
739 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
740 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
741 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
742 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
743 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Eric Laurente552edb2014-03-10 17:42:56 -0700744 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
745 }
746 forceVolumeReeval = true;
747 mForceUse[usage] = config;
748 break;
Eric Laurent3b73df72014-03-11 09:06:29 -0700749 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
750 if (config != AUDIO_POLICY_FORCE_NONE &&
751 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -0700752 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
753 }
754 forceVolumeReeval = true;
755 mForceUse[usage] = config;
756 break;
Jungshik Jang7b24ee32014-07-15 19:38:42 +0900757 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
758 if (config != AUDIO_POLICY_FORCE_NONE &&
759 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
760 ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config);
761 }
762 mForceUse[usage] = config;
763 break;
Eric Laurente552edb2014-03-10 17:42:56 -0700764 default:
765 ALOGW("setForceUse() invalid usage %d", usage);
766 break;
767 }
768
769 // check for device and output changes triggered by new force usage
770 checkA2dpSuspend();
771 checkOutputForAllStrategies();
772 updateDevicesAndOutputs();
Eric Laurentc2730ba2014-07-20 15:47:07 -0700773 if (mPhoneState == AUDIO_MODE_IN_CALL) {
774 audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
775 updateCallRouting(newDevice);
776 }
Eric Laurente552edb2014-03-10 17:42:56 -0700777 for (size_t i = 0; i < mOutputs.size(); i++) {
778 audio_io_handle_t output = mOutputs.keyAt(i);
Eric Laurent1c333e22014-05-20 10:48:17 -0700779 audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/);
Eric Laurentc2730ba2014-07-20 15:47:07 -0700780 if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) {
781 setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
782 }
Eric Laurente552edb2014-03-10 17:42:56 -0700783 if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
784 applyStreamVolumes(output, newDevice, 0, true);
785 }
786 }
787
788 audio_io_handle_t activeInput = getActiveInput();
789 if (activeInput != 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700790 setInputDevice(activeInput, getNewInputDevice(activeInput));
Eric Laurente552edb2014-03-10 17:42:56 -0700791 }
792
793}
794
Eric Laurente0720872014-03-11 09:30:41 -0700795audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage)
Eric Laurente552edb2014-03-10 17:42:56 -0700796{
797 return mForceUse[usage];
798}
799
Eric Laurente0720872014-03-11 09:30:41 -0700800void AudioPolicyManager::setSystemProperty(const char* property, const char* value)
Eric Laurente552edb2014-03-10 17:42:56 -0700801{
802 ALOGV("setSystemProperty() property %s, value %s", property, value);
803}
804
805// Find a direct output profile compatible with the parameters passed, even if the input flags do
806// not explicitly request a direct output
Eric Laurent1c333e22014-05-20 10:48:17 -0700807sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput(
Eric Laurente552edb2014-03-10 17:42:56 -0700808 audio_devices_t device,
809 uint32_t samplingRate,
810 audio_format_t format,
811 audio_channel_mask_t channelMask,
812 audio_output_flags_t flags)
813{
814 for (size_t i = 0; i < mHwModules.size(); i++) {
815 if (mHwModules[i]->mHandle == 0) {
816 continue;
817 }
818 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
Eric Laurent1c333e22014-05-20 10:48:17 -0700819 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurent275e8e92014-11-30 15:14:47 -0800820 bool found = profile->isCompatibleProfile(device, String8(""), samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -0700821 NULL /*updatedSamplingRate*/, format, channelMask,
822 flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ?
823 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurent3a4311c2014-03-17 12:00:47 -0700824 if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) {
825 return profile;
826 }
Eric Laurente552edb2014-03-10 17:42:56 -0700827 }
828 }
829 return 0;
830}
831
Eric Laurente0720872014-03-11 09:30:41 -0700832audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -0700833 uint32_t samplingRate,
834 audio_format_t format,
835 audio_channel_mask_t channelMask,
Eric Laurent3b73df72014-03-11 09:06:29 -0700836 audio_output_flags_t flags,
Eric Laurente552edb2014-03-10 17:42:56 -0700837 const audio_offload_info_t *offloadInfo)
838{
Eric Laurent3b73df72014-03-11 09:06:29 -0700839 routing_strategy strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -0700840 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
841 ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
842 device, stream, samplingRate, format, channelMask, flags);
843
Eric Laurente83b55d2014-11-14 10:06:21 -0800844 return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE,
845 stream, samplingRate,format, channelMask,
846 flags, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700847}
848
Eric Laurente83b55d2014-11-14 10:06:21 -0800849status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr,
850 audio_io_handle_t *output,
851 audio_session_t session,
852 audio_stream_type_t *stream,
853 uint32_t samplingRate,
854 audio_format_t format,
855 audio_channel_mask_t channelMask,
856 audio_output_flags_t flags,
857 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700858{
Eric Laurente83b55d2014-11-14 10:06:21 -0800859 audio_attributes_t attributes;
860 if (attr != NULL) {
861 if (!isValidAttributes(attr)) {
862 ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]",
863 attr->usage, attr->content_type, attr->flags,
864 attr->tags);
865 return BAD_VALUE;
866 }
867 attributes = *attr;
868 } else {
869 if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) {
870 ALOGE("getOutputForAttr(): invalid stream type");
871 return BAD_VALUE;
872 }
873 stream_type_to_audio_attributes(*stream, &attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700874 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800875
Eric Laurent275e8e92014-11-30 15:14:47 -0800876 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
877 sp<AudioOutputDescriptor> desc;
878 if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_PLAYERS) {
879 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
880 if ((RULE_MATCH_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
881 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage == attributes.usage) ||
882 (RULE_EXCLUDE_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
883 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage != attributes.usage)) {
884 desc = mPolicyMixes[i]->mOutput;
885 break;
886 }
887 if (strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
888 strncmp(attributes.tags + strlen("addr="),
889 mPolicyMixes[i]->mMix.mRegistrationId.string(),
890 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
891 desc = mPolicyMixes[i]->mOutput;
892 break;
893 }
894 }
895 } else if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_RECORDERS) {
896 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE &&
897 strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 &&
898 strncmp(attributes.tags + strlen("addr="),
899 mPolicyMixes[i]->mMix.mRegistrationId.string(),
900 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) {
901 desc = mPolicyMixes[i]->mOutput;
902 break;
903 }
904 }
905 if (desc != 0) {
906 if (!audio_is_linear_pcm(format)) {
907 return BAD_VALUE;
908 }
909 desc->mPolicyMixAddress = mPolicyMixes[i]->mMix.mRegistrationId;
910 *stream = streamTypefromAttributesInt(&attributes);
911 *output = desc->mIoHandle;
912 ALOGV("getOutputForAttr() returns output %d", *output);
913 return NO_ERROR;
914 }
915 }
916 if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) {
917 ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE");
918 return BAD_VALUE;
919 }
920
Eric Laurent93c3d412014-08-01 14:48:35 -0700921 ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x",
Eric Laurente83b55d2014-11-14 10:06:21 -0800922 attributes.usage, attributes.content_type, attributes.tags, attributes.flags);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700923
Eric Laurente83b55d2014-11-14 10:06:21 -0800924 routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700925 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
Eric Laurent93c3d412014-08-01 14:48:35 -0700926
Eric Laurente83b55d2014-11-14 10:06:21 -0800927 if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
Eric Laurent93c3d412014-08-01 14:48:35 -0700928 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC);
929 }
930
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -0700931 ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x",
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700932 device, samplingRate, format, channelMask, flags);
933
Eric Laurente83b55d2014-11-14 10:06:21 -0800934 *stream = streamTypefromAttributesInt(&attributes);
935 *output = getOutputForDevice(device, session, *stream,
936 samplingRate, format, channelMask,
937 flags, offloadInfo);
938 if (*output == AUDIO_IO_HANDLE_NONE) {
939 return INVALID_OPERATION;
940 }
941 return NO_ERROR;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700942}
943
944audio_io_handle_t AudioPolicyManager::getOutputForDevice(
945 audio_devices_t device,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800946 audio_session_t session __unused,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700947 audio_stream_type_t stream,
948 uint32_t samplingRate,
949 audio_format_t format,
950 audio_channel_mask_t channelMask,
951 audio_output_flags_t flags,
952 const audio_offload_info_t *offloadInfo)
953{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700954 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700955 uint32_t latency = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700956 status_t status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700957
Eric Laurente552edb2014-03-10 17:42:56 -0700958#ifdef AUDIO_POLICY_TEST
959 if (mCurOutput != 0) {
960 ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
961 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
962
963 if (mTestOutputs[mCurOutput] == 0) {
964 ALOGV("getOutput() opening test output");
Eric Laurent1f2f2232014-06-02 12:01:23 -0700965 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -0700966 outputDesc->mDevice = mTestDevice;
Eric Laurente552edb2014-03-10 17:42:56 -0700967 outputDesc->mLatency = mTestLatencyMs;
Eric Laurent3b73df72014-03-11 09:06:29 -0700968 outputDesc->mFlags =
969 (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0);
Eric Laurente552edb2014-03-10 17:42:56 -0700970 outputDesc->mRefCount[stream] = 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700971 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
972 config.sample_rate = mTestSamplingRate;
973 config.channel_mask = mTestChannels;
974 config.format = mTestFormat;
Phil Burk77cce802014-08-04 16:18:15 -0700975 if (offloadInfo != NULL) {
976 config.offload_info = *offloadInfo;
977 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700978 status = mpClientInterface->openOutput(0,
979 &mTestOutputs[mCurOutput],
980 &config,
981 &outputDesc->mDevice,
982 String8(""),
983 &outputDesc->mLatency,
984 outputDesc->mFlags);
985 if (status == NO_ERROR) {
986 outputDesc->mSamplingRate = config.sample_rate;
987 outputDesc->mFormat = config.format;
988 outputDesc->mChannelMask = config.channel_mask;
Eric Laurente552edb2014-03-10 17:42:56 -0700989 AudioParameter outputCmd = AudioParameter();
990 outputCmd.addInt(String8("set_id"),mCurOutput);
991 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
992 addOutput(mTestOutputs[mCurOutput], outputDesc);
993 }
994 }
995 return mTestOutputs[mCurOutput];
996 }
997#endif //AUDIO_POLICY_TEST
998
999 // open a direct output if required by specified parameters
1000 //force direct flag if offload flag is set: offloading implies a direct output stream
1001 // and all common behaviors are driven by checking only the direct flag
1002 // this should normally be set appropriately in the policy configuration file
1003 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07001004 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
Eric Laurente552edb2014-03-10 17:42:56 -07001005 }
Eric Laurent93c3d412014-08-01 14:48:35 -07001006 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
1007 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
1008 }
Eric Laurente83b55d2014-11-14 10:06:21 -08001009 // only allow deep buffering for music stream type
1010 if (stream != AUDIO_STREAM_MUSIC) {
1011 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
1012 }
Eric Laurente552edb2014-03-10 17:42:56 -07001013
Eric Laurentb732cf52014-09-24 19:08:21 -07001014 sp<IOProfile> profile;
1015
1016 // skip direct output selection if the request can obviously be attached to a mixed output
Eric Laurentc2607842014-09-29 09:43:03 -07001017 // and not explicitly requested
1018 if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) &&
1019 audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE &&
Eric Laurentb732cf52014-09-24 19:08:21 -07001020 audio_channel_count_from_out_mask(channelMask) <= 2) {
1021 goto non_direct_output;
1022 }
1023
Eric Laurente552edb2014-03-10 17:42:56 -07001024 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1025 // creating an offloaded track and tearing it down immediately after start when audioflinger
1026 // detects there is an active non offloadable effect.
1027 // FIXME: We should check the audio session here but we do not have it in this context.
1028 // This may prevent offloading in rare situations where effects are left active by apps
1029 // in the background.
Eric Laurentb732cf52014-09-24 19:08:21 -07001030
Eric Laurente552edb2014-03-10 17:42:56 -07001031 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
1032 !isNonOffloadableEffectEnabled()) {
1033 profile = getProfileForDirectOutput(device,
1034 samplingRate,
1035 format,
1036 channelMask,
1037 (audio_output_flags_t)flags);
1038 }
1039
Eric Laurent1c333e22014-05-20 10:48:17 -07001040 if (profile != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001041 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurente552edb2014-03-10 17:42:56 -07001042
1043 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001044 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001045 if (!desc->isDuplicated() && (profile == desc->mProfile)) {
1046 outputDesc = desc;
1047 // reuse direct output if currently open and configured with same parameters
1048 if ((samplingRate == outputDesc->mSamplingRate) &&
1049 (format == outputDesc->mFormat) &&
1050 (channelMask == outputDesc->mChannelMask)) {
1051 outputDesc->mDirectOpenCount++;
1052 ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
1053 return mOutputs.keyAt(i);
1054 }
1055 }
1056 }
1057 // close direct output if currently open and configured with different parameters
1058 if (outputDesc != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07001059 closeOutput(outputDesc->mIoHandle);
Eric Laurente552edb2014-03-10 17:42:56 -07001060 }
1061 outputDesc = new AudioOutputDescriptor(profile);
1062 outputDesc->mDevice = device;
Eric Laurente552edb2014-03-10 17:42:56 -07001063 outputDesc->mLatency = 0;
1064 outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001065 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1066 config.sample_rate = samplingRate;
1067 config.channel_mask = channelMask;
1068 config.format = format;
Phil Burk77cce802014-08-04 16:18:15 -07001069 if (offloadInfo != NULL) {
1070 config.offload_info = *offloadInfo;
1071 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001072 status = mpClientInterface->openOutput(profile->mModule->mHandle,
1073 &output,
1074 &config,
1075 &outputDesc->mDevice,
1076 String8(""),
1077 &outputDesc->mLatency,
1078 outputDesc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001079
1080 // only accept an output with the requested parameters
Eric Laurentcf2c0212014-07-25 16:20:43 -07001081 if (status != NO_ERROR ||
1082 (samplingRate != 0 && samplingRate != config.sample_rate) ||
1083 (format != AUDIO_FORMAT_DEFAULT && format != config.format) ||
1084 (channelMask != 0 && channelMask != config.channel_mask)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001085 ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
1086 "format %d %d, channelMask %04x %04x", output, samplingRate,
1087 outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
1088 outputDesc->mChannelMask);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001089 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07001090 mpClientInterface->closeOutput(output);
1091 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001092 return AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07001093 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001094 outputDesc->mSamplingRate = config.sample_rate;
1095 outputDesc->mChannelMask = config.channel_mask;
1096 outputDesc->mFormat = config.format;
1097 outputDesc->mRefCount[stream] = 0;
1098 outputDesc->mStopTime[stream] = 0;
1099 outputDesc->mDirectOpenCount = 1;
1100
Eric Laurente552edb2014-03-10 17:42:56 -07001101 audio_io_handle_t srcOutput = getOutputForEffect();
1102 addOutput(output, outputDesc);
1103 audio_io_handle_t dstOutput = getOutputForEffect();
1104 if (dstOutput == output) {
1105 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
1106 }
1107 mPreviousOutputs = mOutputs;
1108 ALOGV("getOutput() returns new direct output %d", output);
Eric Laurentb52c1522014-05-20 11:27:36 -07001109 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001110 return output;
1111 }
1112
Eric Laurentb732cf52014-09-24 19:08:21 -07001113non_direct_output:
1114
Eric Laurente552edb2014-03-10 17:42:56 -07001115 // ignoring channel mask due to downmix capability in mixer
1116
1117 // open a non direct output
1118
1119 // for non direct outputs, only PCM is supported
1120 if (audio_is_linear_pcm(format)) {
1121 // get which output is suitable for the specified stream. The actual
1122 // routing change will happen when startOutput() will be called
1123 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
1124
Eric Laurent8838a382014-09-08 16:44:28 -07001125 // at this stage we should ignore the DIRECT flag as no direct output could be found earlier
1126 flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1127 output = selectOutput(outputs, flags, format);
Eric Laurente552edb2014-03-10 17:42:56 -07001128 }
1129 ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
1130 "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
1131
1132 ALOGV("getOutput() returns output %d", output);
1133
1134 return output;
1135}
1136
Eric Laurente0720872014-03-11 09:30:41 -07001137audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
Eric Laurent8838a382014-09-08 16:44:28 -07001138 audio_output_flags_t flags,
1139 audio_format_t format)
Eric Laurente552edb2014-03-10 17:42:56 -07001140{
1141 // select one output among several that provide a path to a particular device or set of
1142 // devices (the list was previously build by getOutputsForDevice()).
1143 // The priority is as follows:
1144 // 1: the output with the highest number of requested policy flags
1145 // 2: the primary output
1146 // 3: the first output in the list
1147
1148 if (outputs.size() == 0) {
1149 return 0;
1150 }
1151 if (outputs.size() == 1) {
1152 return outputs[0];
1153 }
1154
1155 int maxCommonFlags = 0;
1156 audio_io_handle_t outputFlags = 0;
1157 audio_io_handle_t outputPrimary = 0;
1158
1159 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001160 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07001161 if (!outputDesc->isDuplicated()) {
Eric Laurent8838a382014-09-08 16:44:28 -07001162 // if a valid format is specified, skip output if not compatible
1163 if (format != AUDIO_FORMAT_INVALID) {
1164 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1165 if (format != outputDesc->mFormat) {
1166 continue;
1167 }
1168 } else if (!audio_is_linear_pcm(format)) {
1169 continue;
1170 }
1171 }
1172
Eric Laurent3b73df72014-03-11 09:06:29 -07001173 int commonFlags = popcount(outputDesc->mProfile->mFlags & flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001174 if (commonFlags > maxCommonFlags) {
1175 outputFlags = outputs[i];
1176 maxCommonFlags = commonFlags;
1177 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
1178 }
1179 if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1180 outputPrimary = outputs[i];
1181 }
1182 }
1183 }
1184
1185 if (outputFlags != 0) {
1186 return outputFlags;
1187 }
1188 if (outputPrimary != 0) {
1189 return outputPrimary;
1190 }
1191
1192 return outputs[0];
1193}
1194
Eric Laurente0720872014-03-11 09:30:41 -07001195status_t AudioPolicyManager::startOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001196 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001197 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001198{
1199 ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
1200 ssize_t index = mOutputs.indexOfKey(output);
1201 if (index < 0) {
1202 ALOGW("startOutput() unknown output %d", output);
1203 return BAD_VALUE;
1204 }
1205
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001206 // cannot start playback of STREAM_TTS if any other output is being used
1207 uint32_t beaconMuteLatency = 0;
1208 if (stream == AUDIO_STREAM_TTS) {
1209 ALOGV("\t found BEACON stream");
1210 if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) {
1211 return INVALID_OPERATION;
1212 } else {
1213 beaconMuteLatency = handleEventForBeacon(STARTING_BEACON);
1214 }
1215 } else {
1216 // some playback other than beacon starts
1217 beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT);
1218 }
1219
Eric Laurent1f2f2232014-06-02 12:01:23 -07001220 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001221
1222 // increment usage count for this stream on the requested output:
1223 // NOTE that the usage count is the same for duplicated output and hardware output which is
1224 // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
1225 outputDesc->changeRefCount(stream, 1);
1226
1227 if (outputDesc->mRefCount[stream] == 1) {
Eric Laurent275e8e92014-11-30 15:14:47 -08001228 // starting an output being rerouted?
1229 audio_devices_t newDevice;
1230 if (outputDesc->mPolicyMixAddress != String8("")
1231 && outputDesc->mPolicyMixAddress != String8("0")) {
1232 newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
1233 } else {
1234 newDevice = getNewOutputDevice(output, false /*fromCache*/);
1235 }
Eric Laurente552edb2014-03-10 17:42:56 -07001236 routing_strategy strategy = getStrategy(stream);
1237 bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001238 (strategy == STRATEGY_SONIFICATION_RESPECTFUL) ||
1239 (beaconMuteLatency > 0);
1240 uint32_t waitMs = beaconMuteLatency;
Eric Laurente552edb2014-03-10 17:42:56 -07001241 bool force = false;
1242 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001243 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001244 if (desc != outputDesc) {
1245 // force a device change if any other output is managed by the same hw
1246 // module and has a current device selection that differs from selected device.
1247 // In this case, the audio HAL must receive the new device selection so that it can
1248 // change the device currently selected by the other active output.
1249 if (outputDesc->sharesHwModuleWith(desc) &&
1250 desc->device() != newDevice) {
1251 force = true;
1252 }
1253 // wait for audio on other active outputs to be presented when starting
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001254 // a notification so that audio focus effect can propagate, or that a mute/unmute
1255 // event occurred for beacon
Eric Laurente552edb2014-03-10 17:42:56 -07001256 uint32_t latency = desc->latency();
1257 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
1258 waitMs = latency;
1259 }
1260 }
1261 }
1262 uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
1263
1264 // handle special case for sonification while in call
1265 if (isInCall()) {
1266 handleIncallSonification(stream, true, false);
1267 }
1268
1269 // apply volume rules for current stream and device if necessary
1270 checkAndSetVolume(stream,
1271 mStreams[stream].getVolumeIndex(newDevice),
1272 output,
1273 newDevice);
1274
1275 // update the outputs if starting an output with a stream that can affect notification
1276 // routing
1277 handleNotificationRoutingForStream(stream);
1278 if (waitMs > muteWaitMs) {
1279 usleep((waitMs - muteWaitMs) * 2 * 1000);
1280 }
1281 }
1282 return NO_ERROR;
1283}
1284
1285
Eric Laurente0720872014-03-11 09:30:41 -07001286status_t AudioPolicyManager::stopOutput(audio_io_handle_t output,
Eric Laurent3b73df72014-03-11 09:06:29 -07001287 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -08001288 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001289{
1290 ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
1291 ssize_t index = mOutputs.indexOfKey(output);
1292 if (index < 0) {
1293 ALOGW("stopOutput() unknown output %d", output);
1294 return BAD_VALUE;
1295 }
1296
Eric Laurent1f2f2232014-06-02 12:01:23 -07001297 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001298
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07001299 // always handle stream stop, check which stream type is stopping
1300 handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT);
1301
Eric Laurente552edb2014-03-10 17:42:56 -07001302 // handle special case for sonification while in call
1303 if (isInCall()) {
1304 handleIncallSonification(stream, false, false);
1305 }
1306
1307 if (outputDesc->mRefCount[stream] > 0) {
1308 // decrement usage count of this stream on the output
1309 outputDesc->changeRefCount(stream, -1);
1310 // store time at which the stream was stopped - see isStreamActive()
1311 if (outputDesc->mRefCount[stream] == 0) {
1312 outputDesc->mStopTime[stream] = systemTime();
Eric Laurent1c333e22014-05-20 10:48:17 -07001313 audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/);
Eric Laurente552edb2014-03-10 17:42:56 -07001314 // delay the device switch by twice the latency because stopOutput() is executed when
1315 // the track stop() command is received and at that time the audio track buffer can
1316 // still contain data that needs to be drained. The latency only covers the audio HAL
1317 // and kernel buffers. Also the latency does not always include additional delay in the
1318 // audio path (audio DSP, CODEC ...)
1319 setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
1320
1321 // force restoring the device selection on other active outputs if it differs from the
1322 // one being selected for this output
1323 for (size_t i = 0; i < mOutputs.size(); i++) {
1324 audio_io_handle_t curOutput = mOutputs.keyAt(i);
Eric Laurent1f2f2232014-06-02 12:01:23 -07001325 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001326 if (curOutput != output &&
1327 desc->isActive() &&
1328 outputDesc->sharesHwModuleWith(desc) &&
1329 (newDevice != desc->device())) {
1330 setOutputDevice(curOutput,
Eric Laurent1c333e22014-05-20 10:48:17 -07001331 getNewOutputDevice(curOutput, false /*fromCache*/),
Eric Laurente552edb2014-03-10 17:42:56 -07001332 true,
1333 outputDesc->mLatency*2);
1334 }
1335 }
1336 // update the outputs if stopping one with a stream that can affect notification routing
1337 handleNotificationRoutingForStream(stream);
1338 }
1339 return NO_ERROR;
1340 } else {
1341 ALOGW("stopOutput() refcount is already 0 for output %d", output);
1342 return INVALID_OPERATION;
1343 }
1344}
1345
Eric Laurente83b55d2014-11-14 10:06:21 -08001346void AudioPolicyManager::releaseOutput(audio_io_handle_t output,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001347 audio_stream_type_t stream __unused,
1348 audio_session_t session __unused)
Eric Laurente552edb2014-03-10 17:42:56 -07001349{
1350 ALOGV("releaseOutput() %d", output);
1351 ssize_t index = mOutputs.indexOfKey(output);
1352 if (index < 0) {
1353 ALOGW("releaseOutput() releasing unknown output %d", output);
1354 return;
1355 }
1356
1357#ifdef AUDIO_POLICY_TEST
1358 int testIndex = testOutputIndex(output);
1359 if (testIndex != 0) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001360 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001361 if (outputDesc->isActive()) {
1362 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07001363 mOutputs.removeItem(output);
1364 mTestOutputs[testIndex] = 0;
1365 }
1366 return;
1367 }
1368#endif //AUDIO_POLICY_TEST
1369
Eric Laurent1f2f2232014-06-02 12:01:23 -07001370 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index);
Eric Laurent3b73df72014-03-11 09:06:29 -07001371 if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
Eric Laurente552edb2014-03-10 17:42:56 -07001372 if (desc->mDirectOpenCount <= 0) {
1373 ALOGW("releaseOutput() invalid open count %d for output %d",
1374 desc->mDirectOpenCount, output);
1375 return;
1376 }
1377 if (--desc->mDirectOpenCount == 0) {
1378 closeOutput(output);
1379 // If effects where present on the output, audioflinger moved them to the primary
1380 // output by default: move them back to the appropriate output.
1381 audio_io_handle_t dstOutput = getOutputForEffect();
1382 if (dstOutput != mPrimaryOutput) {
1383 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
1384 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001385 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001386 }
1387 }
1388}
1389
1390
Eric Laurentcaf7f482014-11-25 17:50:47 -08001391status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr,
1392 audio_io_handle_t *input,
1393 audio_session_t session,
1394 uint32_t samplingRate,
1395 audio_format_t format,
1396 audio_channel_mask_t channelMask,
1397 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07001398{
Eric Laurentcaf7f482014-11-25 17:50:47 -08001399 ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x,"
1400 "session %d, flags %#x",
1401 attr->source, samplingRate, format, channelMask, session, flags);
Eric Laurente552edb2014-03-10 17:42:56 -07001402
Eric Laurentcaf7f482014-11-25 17:50:47 -08001403 *input = AUDIO_IO_HANDLE_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001404 audio_devices_t device;
1405 // handle legacy remote submix case where the address was not always specified
1406 String8 address = String8("");
Eric Laurent5dbe4712014-09-19 19:04:57 -07001407 bool isSoundTrigger = false;
Eric Laurentcaf7f482014-11-25 17:50:47 -08001408 audio_source_t halInputSource = attr->source;
Eric Laurent275e8e92014-11-30 15:14:47 -08001409
1410 if (attr->source == AUDIO_SOURCE_REMOTE_SUBMIX &&
1411 strncmp(attr->tags, "addr=", strlen("addr=")) == 0) {
1412 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1413 address = String8(attr->tags + strlen("addr="));
1414 ssize_t index = mPolicyMixes.indexOfKey(address);
1415 if (index < 0) {
1416 ALOGW("getInputForAttr() no policy for address %s", address.string());
1417 return BAD_VALUE;
1418 }
1419 } else {
1420 device = getDeviceForInputSource(attr->source, &address);
1421
1422 if (device == AUDIO_DEVICE_NONE) {
1423 ALOGW("getInputForAttr() could not find device for source %d", attr->source);
1424 return BAD_VALUE;
1425 }
1426 // adapt channel selection to input source
1427 switch (attr->source) {
1428 case AUDIO_SOURCE_VOICE_UPLINK:
1429 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
1430 break;
1431 case AUDIO_SOURCE_VOICE_DOWNLINK:
1432 channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
1433 break;
1434 case AUDIO_SOURCE_VOICE_CALL:
1435 channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
1436 break;
1437 default:
1438 break;
1439 }
1440 if (attr->source == AUDIO_SOURCE_HOTWORD) {
1441 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
1442 if (index >= 0) {
1443 *input = mSoundTriggerSessions.valueFor(session);
1444 isSoundTrigger = true;
1445 flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD);
1446 ALOGV("SoundTrigger capture on session %d input %d", session, *input);
1447 } else {
1448 halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION;
1449 }
Eric Laurent5dbe4712014-09-19 19:04:57 -07001450 }
1451 }
1452
Eric Laurent275e8e92014-11-30 15:14:47 -08001453 sp<IOProfile> profile = getInputProfile(device, address,
1454 samplingRate, format, channelMask,
1455 flags);
Eric Laurent1c333e22014-05-20 10:48:17 -07001456 if (profile == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07001457 //retry without flags
1458 audio_input_flags_t log_flags = flags;
1459 flags = AUDIO_INPUT_FLAG_NONE;
Eric Laurent275e8e92014-11-30 15:14:47 -08001460 profile = getInputProfile(device, address,
1461 samplingRate, format, channelMask,
1462 flags);
Eric Laurent5dbe4712014-09-19 19:04:57 -07001463 if (profile == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001464 ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u,"
1465 "format %#x, channelMask 0x%X, flags %#x",
Eric Laurent5dbe4712014-09-19 19:04:57 -07001466 device, samplingRate, format, channelMask, log_flags);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001467 return BAD_VALUE;
Eric Laurent5dbe4712014-09-19 19:04:57 -07001468 }
Eric Laurente552edb2014-03-10 17:42:56 -07001469 }
1470
1471 if (profile->mModule->mHandle == 0) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001472 ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName);
1473 return NO_INIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001474 }
1475
1476 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
1477 config.sample_rate = samplingRate;
1478 config.channel_mask = channelMask;
1479 config.format = format;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001480
Eric Laurentcf2c0212014-07-25 16:20:43 -07001481 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
Eric Laurentcaf7f482014-11-25 17:50:47 -08001482 input,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001483 &config,
1484 &device,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001485 address,
Eric Laurent1c9c2cc2014-08-28 19:37:25 -07001486 halInputSource,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001487 flags);
1488
1489 // only accept input with the exact requested set of parameters
Eric Laurentcaf7f482014-11-25 17:50:47 -08001490 if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE ||
Eric Laurentcf2c0212014-07-25 16:20:43 -07001491 (samplingRate != config.sample_rate) ||
1492 (format != config.format) ||
1493 (channelMask != config.channel_mask)) {
Eric Laurentcaf7f482014-11-25 17:50:47 -08001494 ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x",
Eric Laurentcf2c0212014-07-25 16:20:43 -07001495 samplingRate, format, channelMask);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001496 if (*input != AUDIO_IO_HANDLE_NONE) {
1497 mpClientInterface->closeInput(*input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001498 }
Eric Laurentcaf7f482014-11-25 17:50:47 -08001499 return BAD_VALUE;
Eric Laurente552edb2014-03-10 17:42:56 -07001500 }
1501
Eric Laurent1f2f2232014-06-02 12:01:23 -07001502 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001503 inputDesc->mInputSource = attr->source;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001504 inputDesc->mRefCount = 0;
1505 inputDesc->mOpenRefCount = 1;
Eric Laurente552edb2014-03-10 17:42:56 -07001506 inputDesc->mSamplingRate = samplingRate;
1507 inputDesc->mFormat = format;
1508 inputDesc->mChannelMask = channelMask;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001509 inputDesc->mDevice = device;
Eric Laurent275e8e92014-11-30 15:14:47 -08001510 inputDesc->mSessions.add(session, address);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001511 inputDesc->mIsSoundTrigger = isSoundTrigger;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001512
Eric Laurentcaf7f482014-11-25 17:50:47 -08001513 addInput(*input, inputDesc);
Eric Laurentb52c1522014-05-20 11:27:36 -07001514 mpClientInterface->onAudioPortListUpdate();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001515 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001516}
1517
Eric Laurent4dc68062014-07-28 17:26:49 -07001518status_t AudioPolicyManager::startInput(audio_io_handle_t input,
1519 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001520{
1521 ALOGV("startInput() input %d", input);
1522 ssize_t index = mInputs.indexOfKey(input);
1523 if (index < 0) {
1524 ALOGW("startInput() unknown input %d", input);
1525 return BAD_VALUE;
1526 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001527 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001528
Eric Laurent275e8e92014-11-30 15:14:47 -08001529 index = inputDesc->mSessions.indexOfKey(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001530 if (index < 0) {
1531 ALOGW("startInput() unknown session %d on input %d", session, input);
1532 return BAD_VALUE;
1533 }
1534
Glenn Kasten74a8e252014-07-24 14:09:55 -07001535 // virtual input devices are compatible with other input devices
1536 if (!isVirtualInputDevice(inputDesc->mDevice)) {
1537
1538 // for a non-virtual input device, check if there is another (non-virtual) active input
Eric Laurente552edb2014-03-10 17:42:56 -07001539 audio_io_handle_t activeInput = getActiveInput();
Glenn Kasten74a8e252014-07-24 14:09:55 -07001540 if (activeInput != 0 && activeInput != input) {
1541
1542 // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed,
1543 // otherwise the active input continues and the new input cannot be started.
Eric Laurent1f2f2232014-06-02 12:01:23 -07001544 sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001545 if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001546 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput);
Eric Laurent275e8e92014-11-30 15:14:47 -08001547 stopInput(activeInput, activeDesc->mSessions.keyAt(0));
1548 releaseInput(activeInput, activeDesc->mSessions.keyAt(0));
Eric Laurente552edb2014-03-10 17:42:56 -07001549 } else {
Glenn Kasten74a8e252014-07-24 14:09:55 -07001550 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput);
Eric Laurente552edb2014-03-10 17:42:56 -07001551 return INVALID_OPERATION;
1552 }
1553 }
1554 }
1555
Glenn Kasten74a8e252014-07-24 14:09:55 -07001556 if (inputDesc->mRefCount == 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001557 if (activeInputsCount() == 0) {
1558 SoundTrigger::setCaptureState(true);
1559 }
Glenn Kasten74a8e252014-07-24 14:09:55 -07001560 setInputDevice(input, getNewInputDevice(input), true /* force */);
Eric Laurente552edb2014-03-10 17:42:56 -07001561
Glenn Kasten74a8e252014-07-24 14:09:55 -07001562 // Automatically enable the remote submix output when input is started.
1563 // For remote submix (a virtual device), we open only one input per capture request.
1564 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1565 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001566 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
1567 inputDesc->mSessions.valueAt(index));
Glenn Kasten74a8e252014-07-24 14:09:55 -07001568 }
Eric Laurente552edb2014-03-10 17:42:56 -07001569 }
1570
Eric Laurente552edb2014-03-10 17:42:56 -07001571 ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1572
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001573 inputDesc->mRefCount++;
Eric Laurente552edb2014-03-10 17:42:56 -07001574 return NO_ERROR;
1575}
1576
Eric Laurent4dc68062014-07-28 17:26:49 -07001577status_t AudioPolicyManager::stopInput(audio_io_handle_t input,
1578 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001579{
1580 ALOGV("stopInput() input %d", input);
1581 ssize_t index = mInputs.indexOfKey(input);
1582 if (index < 0) {
1583 ALOGW("stopInput() unknown input %d", input);
1584 return BAD_VALUE;
1585 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001586 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001587
Eric Laurent275e8e92014-11-30 15:14:47 -08001588 index = inputDesc->mSessions.indexOfKey(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001589 if (index < 0) {
1590 ALOGW("stopInput() unknown session %d on input %d", session, input);
1591 return BAD_VALUE;
1592 }
1593
Eric Laurente552edb2014-03-10 17:42:56 -07001594 if (inputDesc->mRefCount == 0) {
1595 ALOGW("stopInput() input %d already stopped", input);
1596 return INVALID_OPERATION;
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001597 }
1598
1599 inputDesc->mRefCount--;
1600 if (inputDesc->mRefCount == 0) {
1601
Eric Laurente552edb2014-03-10 17:42:56 -07001602 // automatically disable the remote submix output when input is stopped
1603 if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1604 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Eric Laurent275e8e92014-11-30 15:14:47 -08001605 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
1606 inputDesc->mSessions.valueAt(index));
Eric Laurente552edb2014-03-10 17:42:56 -07001607 }
1608
Eric Laurent1c333e22014-05-20 10:48:17 -07001609 resetInputDevice(input);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001610
1611 if (activeInputsCount() == 0) {
1612 SoundTrigger::setCaptureState(false);
1613 }
Eric Laurente552edb2014-03-10 17:42:56 -07001614 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001615 return NO_ERROR;
Eric Laurente552edb2014-03-10 17:42:56 -07001616}
1617
Eric Laurent4dc68062014-07-28 17:26:49 -07001618void AudioPolicyManager::releaseInput(audio_io_handle_t input,
1619 audio_session_t session)
Eric Laurente552edb2014-03-10 17:42:56 -07001620{
1621 ALOGV("releaseInput() %d", input);
1622 ssize_t index = mInputs.indexOfKey(input);
1623 if (index < 0) {
1624 ALOGW("releaseInput() releasing unknown input %d", input);
1625 return;
1626 }
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001627 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index);
1628 ALOG_ASSERT(inputDesc != 0);
Eric Laurent4dc68062014-07-28 17:26:49 -07001629
Eric Laurent275e8e92014-11-30 15:14:47 -08001630 index = inputDesc->mSessions.indexOfKey(session);
Eric Laurent4dc68062014-07-28 17:26:49 -07001631 if (index < 0) {
1632 ALOGW("releaseInput() unknown session %d on input %d", session, input);
1633 return;
1634 }
Eric Laurent275e8e92014-11-30 15:14:47 -08001635 inputDesc->mSessions.removeItem(session);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07001636 if (inputDesc->mOpenRefCount == 0) {
1637 ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
1638 return;
1639 }
1640 inputDesc->mOpenRefCount--;
1641 if (inputDesc->mOpenRefCount > 0) {
1642 ALOGV("releaseInput() exit > 0");
1643 return;
1644 }
1645
Eric Laurent05b90f82014-08-27 15:32:29 -07001646 closeInput(input);
Eric Laurentb52c1522014-05-20 11:27:36 -07001647 mpClientInterface->onAudioPortListUpdate();
Eric Laurente552edb2014-03-10 17:42:56 -07001648 ALOGV("releaseInput() exit");
1649}
1650
Eric Laurentd4692962014-05-05 18:13:44 -07001651void AudioPolicyManager::closeAllInputs() {
Eric Laurent05b90f82014-08-27 15:32:29 -07001652 bool patchRemoved = false;
1653
Eric Laurentd4692962014-05-05 18:13:44 -07001654 for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
Eric Laurent05b90f82014-08-27 15:32:29 -07001655 sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index);
1656 ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
1657 if (patch_index >= 0) {
1658 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index);
1659 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
1660 mAudioPatches.removeItemsAt(patch_index);
1661 patchRemoved = true;
1662 }
Eric Laurentd4692962014-05-05 18:13:44 -07001663 mpClientInterface->closeInput(mInputs.keyAt(input_index));
1664 }
1665 mInputs.clear();
Eric Laurent6a94d692014-05-20 11:18:06 -07001666 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07001667
1668 if (patchRemoved) {
1669 mpClientInterface->onAudioPatchListUpdate();
1670 }
Eric Laurentd4692962014-05-05 18:13:44 -07001671}
1672
Eric Laurente0720872014-03-11 09:30:41 -07001673void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001674 int indexMin,
1675 int indexMax)
1676{
1677 ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1678 if (indexMin < 0 || indexMin >= indexMax) {
1679 ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1680 return;
1681 }
1682 mStreams[stream].mIndexMin = indexMin;
1683 mStreams[stream].mIndexMax = indexMax;
Eric Laurent223fd5c2014-11-11 13:43:36 -08001684 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1685 if (stream == AUDIO_STREAM_MUSIC) {
1686 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin;
1687 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax;
1688 }
Eric Laurente552edb2014-03-10 17:42:56 -07001689}
1690
Eric Laurente0720872014-03-11 09:30:41 -07001691status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001692 int index,
1693 audio_devices_t device)
1694{
1695
1696 if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1697 return BAD_VALUE;
1698 }
1699 if (!audio_is_output_device(device)) {
1700 return BAD_VALUE;
1701 }
1702
1703 // Force max volume if stream cannot be muted
1704 if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1705
1706 ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1707 stream, device, index);
1708
1709 // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1710 // clear all device specific values
1711 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1712 mStreams[stream].mIndexCur.clear();
1713 }
1714 mStreams[stream].mIndexCur.add(device, index);
1715
Eric Laurent31551f82014-10-10 18:21:56 -07001716 // update volume on all outputs whose current device is also selected by the same
1717 // strategy as the device specified by the caller
1718 audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
Eric Laurent223fd5c2014-11-11 13:43:36 -08001719
1720
1721 //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now
1722 audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE;
1723 if (stream == AUDIO_STREAM_MUSIC) {
1724 mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index);
1725 accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/);
1726 }
1727 if ((device != AUDIO_DEVICE_OUT_DEFAULT) &&
1728 (device & (strategyDevice | accessibilityDevice)) == 0) {
Eric Laurent31551f82014-10-10 18:21:56 -07001729 return NO_ERROR;
1730 }
Eric Laurente552edb2014-03-10 17:42:56 -07001731 status_t status = NO_ERROR;
1732 for (size_t i = 0; i < mOutputs.size(); i++) {
1733 audio_devices_t curDevice =
1734 getDeviceForVolume(mOutputs.valueAt(i)->device());
Eric Laurent31551f82014-10-10 18:21:56 -07001735 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001736 status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1737 if (volStatus != NO_ERROR) {
1738 status = volStatus;
1739 }
1740 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08001741 if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) {
1742 status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY,
1743 index, mOutputs.keyAt(i), curDevice);
1744 }
Eric Laurente552edb2014-03-10 17:42:56 -07001745 }
1746 return status;
1747}
1748
Eric Laurente0720872014-03-11 09:30:41 -07001749status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07001750 int *index,
1751 audio_devices_t device)
1752{
1753 if (index == NULL) {
1754 return BAD_VALUE;
1755 }
1756 if (!audio_is_output_device(device)) {
1757 return BAD_VALUE;
1758 }
1759 // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1760 // the strategy the stream belongs to.
1761 if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1762 device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1763 }
1764 device = getDeviceForVolume(device);
1765
1766 *index = mStreams[stream].getVolumeIndex(device);
1767 ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1768 return NO_ERROR;
1769}
1770
Eric Laurente0720872014-03-11 09:30:41 -07001771audio_io_handle_t AudioPolicyManager::selectOutputForEffects(
Eric Laurente552edb2014-03-10 17:42:56 -07001772 const SortedVector<audio_io_handle_t>& outputs)
1773{
1774 // select one output among several suitable for global effects.
1775 // The priority is as follows:
1776 // 1: An offloaded output. If the effect ends up not being offloadable,
1777 // AudioFlinger will invalidate the track and the offloaded output
1778 // will be closed causing the effect to be moved to a PCM output.
1779 // 2: A deep buffer output
1780 // 3: the first output in the list
1781
1782 if (outputs.size() == 0) {
1783 return 0;
1784 }
1785
1786 audio_io_handle_t outputOffloaded = 0;
1787 audio_io_handle_t outputDeepBuffer = 0;
1788
1789 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001790 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]);
Eric Laurentd4692962014-05-05 18:13:44 -07001791 ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
Eric Laurente552edb2014-03-10 17:42:56 -07001792 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1793 outputOffloaded = outputs[i];
1794 }
1795 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
1796 outputDeepBuffer = outputs[i];
1797 }
1798 }
1799
1800 ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
1801 outputOffloaded, outputDeepBuffer);
1802 if (outputOffloaded != 0) {
1803 return outputOffloaded;
1804 }
1805 if (outputDeepBuffer != 0) {
1806 return outputDeepBuffer;
1807 }
1808
1809 return outputs[0];
1810}
1811
Eric Laurente0720872014-03-11 09:30:41 -07001812audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurente552edb2014-03-10 17:42:56 -07001813{
1814 // apply simple rule where global effects are attached to the same output as MUSIC streams
1815
Eric Laurent3b73df72014-03-11 09:06:29 -07001816 routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC);
Eric Laurente552edb2014-03-10 17:42:56 -07001817 audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1818 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
1819
1820 audio_io_handle_t output = selectOutputForEffects(dstOutputs);
1821 ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
1822 output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags);
1823
1824 return output;
1825}
1826
Eric Laurente0720872014-03-11 09:30:41 -07001827status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc,
Eric Laurente552edb2014-03-10 17:42:56 -07001828 audio_io_handle_t io,
1829 uint32_t strategy,
1830 int session,
1831 int id)
1832{
1833 ssize_t index = mOutputs.indexOfKey(io);
1834 if (index < 0) {
1835 index = mInputs.indexOfKey(io);
1836 if (index < 0) {
1837 ALOGW("registerEffect() unknown io %d", io);
1838 return INVALID_OPERATION;
1839 }
1840 }
1841
1842 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
1843 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1844 desc->name, desc->memoryUsage);
1845 return INVALID_OPERATION;
1846 }
1847 mTotalEffectsMemory += desc->memoryUsage;
1848 ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
1849 desc->name, io, strategy, session, id);
1850 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1851
Eric Laurent1f2f2232014-06-02 12:01:23 -07001852 sp<EffectDescriptor> effectDesc = new EffectDescriptor();
1853 memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t));
1854 effectDesc->mIo = io;
1855 effectDesc->mStrategy = (routing_strategy)strategy;
1856 effectDesc->mSession = session;
1857 effectDesc->mEnabled = false;
Eric Laurente552edb2014-03-10 17:42:56 -07001858
Eric Laurent1f2f2232014-06-02 12:01:23 -07001859 mEffects.add(id, effectDesc);
Eric Laurente552edb2014-03-10 17:42:56 -07001860
1861 return NO_ERROR;
1862}
1863
Eric Laurente0720872014-03-11 09:30:41 -07001864status_t AudioPolicyManager::unregisterEffect(int id)
Eric Laurente552edb2014-03-10 17:42:56 -07001865{
1866 ssize_t index = mEffects.indexOfKey(id);
1867 if (index < 0) {
1868 ALOGW("unregisterEffect() unknown effect ID %d", id);
1869 return INVALID_OPERATION;
1870 }
1871
Eric Laurent1f2f2232014-06-02 12:01:23 -07001872 sp<EffectDescriptor> effectDesc = mEffects.valueAt(index);
Eric Laurente552edb2014-03-10 17:42:56 -07001873
Eric Laurent1f2f2232014-06-02 12:01:23 -07001874 setEffectEnabled(effectDesc, false);
Eric Laurente552edb2014-03-10 17:42:56 -07001875
Eric Laurent1f2f2232014-06-02 12:01:23 -07001876 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
Eric Laurente552edb2014-03-10 17:42:56 -07001877 ALOGW("unregisterEffect() memory %d too big for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001878 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1879 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
Eric Laurente552edb2014-03-10 17:42:56 -07001880 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001881 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
Eric Laurente552edb2014-03-10 17:42:56 -07001882 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001883 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
Eric Laurente552edb2014-03-10 17:42:56 -07001884
1885 mEffects.removeItem(id);
Eric Laurente552edb2014-03-10 17:42:56 -07001886
1887 return NO_ERROR;
1888}
1889
Eric Laurente0720872014-03-11 09:30:41 -07001890status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001891{
1892 ssize_t index = mEffects.indexOfKey(id);
1893 if (index < 0) {
1894 ALOGW("unregisterEffect() unknown effect ID %d", id);
1895 return INVALID_OPERATION;
1896 }
1897
1898 return setEffectEnabled(mEffects.valueAt(index), enabled);
1899}
1900
Eric Laurent1f2f2232014-06-02 12:01:23 -07001901status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled)
Eric Laurente552edb2014-03-10 17:42:56 -07001902{
Eric Laurent1f2f2232014-06-02 12:01:23 -07001903 if (enabled == effectDesc->mEnabled) {
Eric Laurente552edb2014-03-10 17:42:56 -07001904 ALOGV("setEffectEnabled(%s) effect already %s",
1905 enabled?"true":"false", enabled?"enabled":"disabled");
1906 return INVALID_OPERATION;
1907 }
1908
1909 if (enabled) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001910 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
Eric Laurente552edb2014-03-10 17:42:56 -07001911 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001912 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
Eric Laurente552edb2014-03-10 17:42:56 -07001913 return INVALID_OPERATION;
1914 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001915 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001916 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1917 } else {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001918 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
Eric Laurente552edb2014-03-10 17:42:56 -07001919 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001920 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1921 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001922 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001923 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
Eric Laurente552edb2014-03-10 17:42:56 -07001924 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1925 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07001926 effectDesc->mEnabled = enabled;
Eric Laurente552edb2014-03-10 17:42:56 -07001927 return NO_ERROR;
1928}
1929
Eric Laurente0720872014-03-11 09:30:41 -07001930bool AudioPolicyManager::isNonOffloadableEffectEnabled()
Eric Laurente552edb2014-03-10 17:42:56 -07001931{
1932 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001933 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
1934 if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) &&
1935 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001936 ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
Eric Laurent1f2f2232014-06-02 12:01:23 -07001937 effectDesc->mDesc.name, effectDesc->mSession);
Eric Laurente552edb2014-03-10 17:42:56 -07001938 return true;
1939 }
1940 }
1941 return false;
1942}
1943
Eric Laurente0720872014-03-11 09:30:41 -07001944bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001945{
1946 nsecs_t sysTime = systemTime();
1947 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001948 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurent3b73df72014-03-11 09:06:29 -07001949 if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07001950 return true;
1951 }
1952 }
1953 return false;
1954}
1955
Eric Laurente0720872014-03-11 09:30:41 -07001956bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07001957 uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07001958{
1959 nsecs_t sysTime = systemTime();
1960 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001961 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001962 if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07001963 outputDesc->isStreamActive(stream, inPastMs, sysTime)) {
Jean-Michel Trivi1767df72014-12-09 18:11:49 -08001964 // only consider empty or "0" address to only qualify the screen mirroring case
1965 // as "remote playback" (vs rerouting when the output is going to a dynamic policy)
1966 if (outputDesc->mPolicyMixAddress == String8("")
1967 || outputDesc->mPolicyMixAddress == String8("0")) {
1968 return true;
1969 }
Eric Laurente552edb2014-03-10 17:42:56 -07001970 }
1971 }
1972 return false;
1973}
1974
Eric Laurente0720872014-03-11 09:30:41 -07001975bool AudioPolicyManager::isSourceActive(audio_source_t source) const
Eric Laurente552edb2014-03-10 17:42:56 -07001976{
1977 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07001978 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07001979 if ((inputDescriptor->mInputSource == (int)source ||
Eric Laurent3b73df72014-03-11 09:06:29 -07001980 (source == AUDIO_SOURCE_VOICE_RECOGNITION &&
Eric Laurente552edb2014-03-10 17:42:56 -07001981 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1982 && (inputDescriptor->mRefCount > 0)) {
1983 return true;
1984 }
1985 }
1986 return false;
1987}
1988
Eric Laurent275e8e92014-11-30 15:14:47 -08001989// Register a list of custom mixes with their attributes and format.
1990// When a mix is registered, corresponding input and output profiles are
1991// added to the remote submix hw module. The profile contains only the
1992// parameters (sampling rate, format...) specified by the mix.
1993// The corresponding input remote submix device is also connected.
1994//
1995// When a remote submix device is connected, the address is checked to select the
1996// appropriate profile and the corresponding input or output stream is opened.
1997//
1998// When capture starts, getInputForAttr() will:
1999// - 1 look for a mix matching the address passed in attribtutes tags if any
2000// - 2 if none found, getDeviceForInputSource() will:
2001// - 2.1 look for a mix matching the attributes source
2002// - 2.2 if none found, default to device selection by policy rules
2003// At this time, the corresponding output remote submix device is also connected
2004// and active playback use cases can be transferred to this mix if needed when reconnecting
2005// after AudioTracks are invalidated
2006//
2007// When playback starts, getOutputForAttr() will:
2008// - 1 look for a mix matching the address passed in attribtutes tags if any
2009// - 2 if none found, look for a mix matching the attributes usage
2010// - 3 if none found, default to device and output selection by policy rules.
2011
2012status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes)
2013{
2014 sp<HwModule> module;
2015 for (size_t i = 0; i < mHwModules.size(); i++) {
2016 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2017 mHwModules[i]->mHandle != 0) {
2018 module = mHwModules[i];
2019 break;
2020 }
2021 }
2022
2023 if (module == 0) {
2024 return INVALID_OPERATION;
2025 }
2026
2027 ALOGV("registerPolicyMixes() num mixes %d", mixes.size());
2028
2029 for (size_t i = 0; i < mixes.size(); i++) {
2030 String8 address = mixes[i].mRegistrationId;
2031 ssize_t index = mPolicyMixes.indexOfKey(address);
2032 if (index >= 0) {
2033 ALOGE("registerPolicyMixes(): mix for address %s already registered", address.string());
2034 continue;
2035 }
2036 audio_config_t outputConfig = mixes[i].mFormat;
2037 audio_config_t inputConfig = mixes[i].mFormat;
2038 // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in
2039 // stereo and let audio flinger do the channel conversion if needed.
2040 outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2041 inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO;
2042 module->addOutputProfile(address, &outputConfig,
2043 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address);
2044 module->addInputProfile(address, &inputConfig,
2045 AUDIO_DEVICE_IN_REMOTE_SUBMIX, address);
2046 sp<AudioPolicyMix> policyMix = new AudioPolicyMix();
2047 policyMix->mMix = mixes[i];
2048 mPolicyMixes.add(address, policyMix);
2049 setDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2050 AUDIO_POLICY_DEVICE_STATE_AVAILABLE,
2051 address.string());
2052 }
2053 return NO_ERROR;
2054}
2055
2056status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes)
2057{
2058 sp<HwModule> module;
2059 for (size_t i = 0; i < mHwModules.size(); i++) {
2060 if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 &&
2061 mHwModules[i]->mHandle != 0) {
2062 module = mHwModules[i];
2063 break;
2064 }
2065 }
2066
2067 if (module == 0) {
2068 return INVALID_OPERATION;
2069 }
2070
2071 ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size());
2072
2073 for (size_t i = 0; i < mixes.size(); i++) {
2074 String8 address = mixes[i].mRegistrationId;
2075 ssize_t index = mPolicyMixes.indexOfKey(address);
2076 if (index < 0) {
2077 ALOGE("unregisterPolicyMixes(): mix for address %s not registered", address.string());
2078 continue;
2079 }
2080
2081 mPolicyMixes.removeItemsAt(index);
2082
2083 setDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX,
2084 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2085 address.string());
2086
2087 if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) ==
2088 AUDIO_POLICY_DEVICE_STATE_AVAILABLE)
2089 {
2090 setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
2091 AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE,
2092 address.string());
2093 }
2094 module->removeOutputProfile(address);
2095 module->removeInputProfile(address);
2096 }
2097 return NO_ERROR;
2098}
2099
Eric Laurente552edb2014-03-10 17:42:56 -07002100
Eric Laurente0720872014-03-11 09:30:41 -07002101status_t AudioPolicyManager::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07002102{
2103 const size_t SIZE = 256;
2104 char buffer[SIZE];
2105 String8 result;
2106
2107 snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
2108 result.append(buffer);
2109
2110 snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
2111 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002112 snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
2113 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002114 snprintf(buffer, SIZE, " Force use for communications %d\n",
2115 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07002116 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002117 snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]);
Eric Laurente552edb2014-03-10 17:42:56 -07002118 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002119 snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]);
Eric Laurente552edb2014-03-10 17:42:56 -07002120 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002121 snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]);
Eric Laurente552edb2014-03-10 17:42:56 -07002122 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07002123 snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]);
Eric Laurente552edb2014-03-10 17:42:56 -07002124 result.append(buffer);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09002125 snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n",
2126 mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]);
2127 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07002128
Eric Laurent3a4311c2014-03-17 12:00:47 -07002129 snprintf(buffer, SIZE, " Available output devices:\n");
2130 result.append(buffer);
2131 write(fd, result.string(), result.size());
Eric Laurent3a4311c2014-03-17 12:00:47 -07002132 for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002133 mAvailableOutputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002134 }
2135 snprintf(buffer, SIZE, "\n Available input devices:\n");
2136 write(fd, buffer, strlen(buffer));
Eric Laurent3a4311c2014-03-17 12:00:47 -07002137 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07002138 mAvailableInputDevices[i]->dump(fd, 2, i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07002139 }
Eric Laurente552edb2014-03-10 17:42:56 -07002140
2141 snprintf(buffer, SIZE, "\nHW Modules dump:\n");
2142 write(fd, buffer, strlen(buffer));
2143 for (size_t i = 0; i < mHwModules.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002144 snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07002145 write(fd, buffer, strlen(buffer));
2146 mHwModules[i]->dump(fd);
2147 }
2148
2149 snprintf(buffer, SIZE, "\nOutputs dump:\n");
2150 write(fd, buffer, strlen(buffer));
2151 for (size_t i = 0; i < mOutputs.size(); i++) {
2152 snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
2153 write(fd, buffer, strlen(buffer));
2154 mOutputs.valueAt(i)->dump(fd);
2155 }
2156
2157 snprintf(buffer, SIZE, "\nInputs dump:\n");
2158 write(fd, buffer, strlen(buffer));
2159 for (size_t i = 0; i < mInputs.size(); i++) {
2160 snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
2161 write(fd, buffer, strlen(buffer));
2162 mInputs.valueAt(i)->dump(fd);
2163 }
2164
2165 snprintf(buffer, SIZE, "\nStreams dump:\n");
2166 write(fd, buffer, strlen(buffer));
2167 snprintf(buffer, SIZE,
2168 " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n");
2169 write(fd, buffer, strlen(buffer));
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002170 for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07002171 snprintf(buffer, SIZE, " %02zu ", i);
Eric Laurente552edb2014-03-10 17:42:56 -07002172 write(fd, buffer, strlen(buffer));
2173 mStreams[i].dump(fd);
2174 }
2175
2176 snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
2177 (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
2178 write(fd, buffer, strlen(buffer));
2179
2180 snprintf(buffer, SIZE, "Registered effects:\n");
2181 write(fd, buffer, strlen(buffer));
2182 for (size_t i = 0; i < mEffects.size(); i++) {
2183 snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
2184 write(fd, buffer, strlen(buffer));
2185 mEffects.valueAt(i)->dump(fd);
2186 }
2187
Eric Laurent4d416952014-08-10 14:07:09 -07002188 snprintf(buffer, SIZE, "\nAudio Patches:\n");
2189 write(fd, buffer, strlen(buffer));
2190 for (size_t i = 0; i < mAudioPatches.size(); i++) {
2191 mAudioPatches[i]->dump(fd, 2, i);
2192 }
Eric Laurente552edb2014-03-10 17:42:56 -07002193
2194 return NO_ERROR;
2195}
2196
2197// This function checks for the parameters which can be offloaded.
2198// This can be enhanced depending on the capability of the DSP and policy
2199// of the system.
Eric Laurente0720872014-03-11 09:30:41 -07002200bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo)
Eric Laurente552edb2014-03-10 17:42:56 -07002201{
2202 ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
Eric Laurentd4692962014-05-05 18:13:44 -07002203 " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
Eric Laurente552edb2014-03-10 17:42:56 -07002204 offloadInfo.sample_rate, offloadInfo.channel_mask,
2205 offloadInfo.format,
2206 offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
2207 offloadInfo.has_video);
2208
2209 // Check if offload has been disabled
2210 char propValue[PROPERTY_VALUE_MAX];
2211 if (property_get("audio.offload.disable", propValue, "0")) {
2212 if (atoi(propValue) != 0) {
2213 ALOGV("offload disabled by audio.offload.disable=%s", propValue );
2214 return false;
2215 }
2216 }
2217
2218 // Check if stream type is music, then only allow offload as of now.
2219 if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
2220 {
2221 ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
2222 return false;
2223 }
2224
2225 //TODO: enable audio offloading with video when ready
2226 if (offloadInfo.has_video)
2227 {
2228 ALOGV("isOffloadSupported: has_video == true, returning false");
2229 return false;
2230 }
2231
2232 //If duration is less than minimum value defined in property, return false
2233 if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
2234 if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
2235 ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
2236 return false;
2237 }
2238 } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
2239 ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
2240 return false;
2241 }
2242
2243 // Do not allow offloading if one non offloadable effect is enabled. This prevents from
2244 // creating an offloaded track and tearing it down immediately after start when audioflinger
2245 // detects there is an active non offloadable effect.
2246 // FIXME: We should check the audio session here but we do not have it in this context.
2247 // This may prevent offloading in rare situations where effects are left active by apps
2248 // in the background.
2249 if (isNonOffloadableEffectEnabled()) {
2250 return false;
2251 }
2252
2253 // See if there is a profile to support this.
2254 // AUDIO_DEVICE_NONE
Eric Laurent1c333e22014-05-20 10:48:17 -07002255 sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
Eric Laurente552edb2014-03-10 17:42:56 -07002256 offloadInfo.sample_rate,
2257 offloadInfo.format,
2258 offloadInfo.channel_mask,
2259 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Eric Laurent1c333e22014-05-20 10:48:17 -07002260 ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT ");
2261 return (profile != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07002262}
2263
Eric Laurent6a94d692014-05-20 11:18:06 -07002264status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role,
2265 audio_port_type_t type,
2266 unsigned int *num_ports,
2267 struct audio_port *ports,
2268 unsigned int *generation)
2269{
2270 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
2271 generation == NULL) {
2272 return BAD_VALUE;
2273 }
2274 ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports);
2275 if (ports == NULL) {
2276 *num_ports = 0;
2277 }
2278
2279 size_t portsWritten = 0;
2280 size_t portsMax = *num_ports;
2281 *num_ports = 0;
2282 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) {
2283 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2284 for (size_t i = 0;
2285 i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) {
2286 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]);
2287 }
2288 *num_ports += mAvailableOutputDevices.size();
2289 }
2290 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
2291 for (size_t i = 0;
2292 i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) {
2293 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]);
2294 }
2295 *num_ports += mAvailableInputDevices.size();
2296 }
2297 }
2298 if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) {
2299 if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) {
2300 for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) {
2301 mInputs[i]->toAudioPort(&ports[portsWritten++]);
2302 }
2303 *num_ports += mInputs.size();
2304 }
2305 if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) {
Eric Laurent84c70242014-06-23 08:46:27 -07002306 size_t numOutputs = 0;
2307 for (size_t i = 0; i < mOutputs.size(); i++) {
2308 if (!mOutputs[i]->isDuplicated()) {
2309 numOutputs++;
2310 if (portsWritten < portsMax) {
2311 mOutputs[i]->toAudioPort(&ports[portsWritten++]);
2312 }
2313 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002314 }
Eric Laurent84c70242014-06-23 08:46:27 -07002315 *num_ports += numOutputs;
Eric Laurent6a94d692014-05-20 11:18:06 -07002316 }
2317 }
2318 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002319 ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports);
Eric Laurent6a94d692014-05-20 11:18:06 -07002320 return NO_ERROR;
2321}
2322
2323status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused)
2324{
2325 return NO_ERROR;
2326}
2327
Eric Laurent1f2f2232014-06-02 12:01:23 -07002328sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002329 audio_port_handle_t id) const
2330{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002331 sp<AudioOutputDescriptor> outputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002332 for (size_t i = 0; i < mOutputs.size(); i++) {
2333 outputDesc = mOutputs.valueAt(i);
2334 if (outputDesc->mId == id) {
2335 break;
2336 }
2337 }
2338 return outputDesc;
2339}
2340
Eric Laurent1f2f2232014-06-02 12:01:23 -07002341sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId(
Eric Laurent6a94d692014-05-20 11:18:06 -07002342 audio_port_handle_t id) const
2343{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002344 sp<AudioInputDescriptor> inputDesc = NULL;
Eric Laurent6a94d692014-05-20 11:18:06 -07002345 for (size_t i = 0; i < mInputs.size(); i++) {
2346 inputDesc = mInputs.valueAt(i);
2347 if (inputDesc->mId == id) {
2348 break;
2349 }
2350 }
2351 return inputDesc;
2352}
2353
Eric Laurent1f2f2232014-06-02 12:01:23 -07002354sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice(
2355 audio_devices_t device) const
Eric Laurent6a94d692014-05-20 11:18:06 -07002356{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002357 sp <HwModule> module;
2358
Eric Laurent6a94d692014-05-20 11:18:06 -07002359 for (size_t i = 0; i < mHwModules.size(); i++) {
2360 if (mHwModules[i]->mHandle == 0) {
2361 continue;
2362 }
2363 if (audio_is_output_device(device)) {
2364 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2365 {
2366 if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) {
2367 return mHwModules[i];
2368 }
2369 }
2370 } else {
2371 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) {
2372 if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() &
2373 device & ~AUDIO_DEVICE_BIT_IN) {
2374 return mHwModules[i];
2375 }
2376 }
2377 }
2378 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002379 return module;
Eric Laurent6a94d692014-05-20 11:18:06 -07002380}
2381
Eric Laurent1f2f2232014-06-02 12:01:23 -07002382sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const
Eric Laurent1afeecb2014-05-14 08:52:28 -07002383{
Eric Laurent1f2f2232014-06-02 12:01:23 -07002384 sp <HwModule> module;
2385
Eric Laurent1afeecb2014-05-14 08:52:28 -07002386 for (size_t i = 0; i < mHwModules.size(); i++)
2387 {
2388 if (strcmp(mHwModules[i]->mName, name) == 0) {
2389 return mHwModules[i];
2390 }
2391 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002392 return module;
Eric Laurent1afeecb2014-05-14 08:52:28 -07002393}
2394
Eric Laurentc2730ba2014-07-20 15:47:07 -07002395audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices()
2396{
2397 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
2398 audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types();
2399 return devices & mAvailableOutputDevices.types();
2400}
2401
2402audio_devices_t AudioPolicyManager::availablePrimaryInputDevices()
2403{
2404 audio_module_handle_t primaryHandle =
2405 mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle;
2406 audio_devices_t devices = AUDIO_DEVICE_NONE;
2407 for (size_t i = 0; i < mAvailableInputDevices.size(); i++) {
2408 if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) {
2409 devices |= mAvailableInputDevices[i]->mDeviceType;
2410 }
2411 }
2412 return devices;
2413}
Eric Laurent1afeecb2014-05-14 08:52:28 -07002414
Eric Laurent6a94d692014-05-20 11:18:06 -07002415status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch,
2416 audio_patch_handle_t *handle,
2417 uid_t uid)
2418{
2419 ALOGV("createAudioPatch()");
2420
2421 if (handle == NULL || patch == NULL) {
2422 return BAD_VALUE;
2423 }
2424 ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks);
2425
Eric Laurent874c42872014-08-08 15:13:39 -07002426 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
2427 patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
2428 return BAD_VALUE;
2429 }
2430 // only one source per audio patch supported for now
2431 if (patch->num_sources > 1) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002432 return INVALID_OPERATION;
2433 }
Eric Laurent874c42872014-08-08 15:13:39 -07002434
2435 if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002436 return INVALID_OPERATION;
2437 }
Eric Laurent874c42872014-08-08 15:13:39 -07002438 for (size_t i = 0; i < patch->num_sinks; i++) {
2439 if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) {
2440 return INVALID_OPERATION;
2441 }
2442 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002443
2444 sp<AudioPatch> patchDesc;
2445 ssize_t index = mAudioPatches.indexOfKey(*handle);
2446
Eric Laurent6a94d692014-05-20 11:18:06 -07002447 ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id,
2448 patch->sources[0].role,
2449 patch->sources[0].type);
Eric Laurent874c42872014-08-08 15:13:39 -07002450#if LOG_NDEBUG == 0
2451 for (size_t i = 0; i < patch->num_sinks; i++) {
2452 ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id,
2453 patch->sinks[i].role,
2454 patch->sinks[i].type);
2455 }
2456#endif
Eric Laurent6a94d692014-05-20 11:18:06 -07002457
2458 if (index >= 0) {
2459 patchDesc = mAudioPatches.valueAt(index);
2460 ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2461 mUidCached, patchDesc->mUid, uid);
2462 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2463 return INVALID_OPERATION;
2464 }
2465 } else {
2466 *handle = 0;
2467 }
2468
2469 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002470 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002471 if (outputDesc == NULL) {
2472 ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id);
2473 return BAD_VALUE;
2474 }
Eric Laurent84c70242014-06-23 08:46:27 -07002475 ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports",
2476 outputDesc->mIoHandle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002477 if (patchDesc != 0) {
2478 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
2479 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d",
2480 patchDesc->mPatch.sources[0].id, patch->sources[0].id);
2481 return BAD_VALUE;
2482 }
2483 }
Eric Laurent874c42872014-08-08 15:13:39 -07002484 DeviceVector devices;
2485 for (size_t i = 0; i < patch->num_sinks; i++) {
2486 // Only support mix to devices connection
2487 // TODO add support for mix to mix connection
2488 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2489 ALOGV("createAudioPatch() source mix but sink is not a device");
2490 return INVALID_OPERATION;
2491 }
2492 sp<DeviceDescriptor> devDesc =
2493 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2494 if (devDesc == 0) {
2495 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id);
2496 return BAD_VALUE;
2497 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002498
Eric Laurent874c42872014-08-08 15:13:39 -07002499 if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002500 devDesc->mAddress,
Eric Laurent874c42872014-08-08 15:13:39 -07002501 patch->sources[0].sample_rate,
2502 NULL, // updatedSamplingRate
2503 patch->sources[0].format,
2504 patch->sources[0].channel_mask,
2505 AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) {
2506 ALOGV("createAudioPatch() profile not supported for device %08x",
2507 devDesc->mDeviceType);
2508 return INVALID_OPERATION;
2509 }
2510 devices.add(devDesc);
2511 }
2512 if (devices.size() == 0) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002513 return INVALID_OPERATION;
2514 }
Eric Laurent874c42872014-08-08 15:13:39 -07002515
Eric Laurent6a94d692014-05-20 11:18:06 -07002516 // TODO: reconfigure output format and channels here
2517 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent874c42872014-08-08 15:13:39 -07002518 devices.types(), outputDesc->mIoHandle);
2519 setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002520 index = mAudioPatches.indexOfKey(*handle);
2521 if (index >= 0) {
2522 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2523 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided");
2524 }
2525 patchDesc = mAudioPatches.valueAt(index);
2526 patchDesc->mUid = uid;
2527 ALOGV("createAudioPatch() success");
2528 } else {
2529 ALOGW("createAudioPatch() setOutputDevice() failed to create a patch");
2530 return INVALID_OPERATION;
2531 }
2532 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2533 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
2534 // input device to input mix connection
Eric Laurent874c42872014-08-08 15:13:39 -07002535 // only one sink supported when connecting an input device to a mix
2536 if (patch->num_sinks > 1) {
2537 return INVALID_OPERATION;
2538 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07002539 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002540 if (inputDesc == NULL) {
2541 return BAD_VALUE;
2542 }
2543 if (patchDesc != 0) {
2544 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) {
2545 return BAD_VALUE;
2546 }
2547 }
2548 sp<DeviceDescriptor> devDesc =
2549 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
2550 if (devDesc == 0) {
2551 return BAD_VALUE;
2552 }
2553
Eric Laurent84c70242014-06-23 08:46:27 -07002554 if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType,
Eric Laurent275e8e92014-11-30 15:14:47 -08002555 devDesc->mAddress,
2556 patch->sinks[0].sample_rate,
2557 NULL, /*updatedSampleRate*/
2558 patch->sinks[0].format,
2559 patch->sinks[0].channel_mask,
2560 // FIXME for the parameter type,
2561 // and the NONE
2562 (audio_output_flags_t)
Glenn Kasten6a8ab052014-07-24 14:08:35 -07002563 AUDIO_INPUT_FLAG_NONE)) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002564 return INVALID_OPERATION;
2565 }
2566 // TODO: reconfigure output format and channels here
2567 ALOGV("createAudioPatch() setting device %08x on output %d",
Eric Laurent84c70242014-06-23 08:46:27 -07002568 devDesc->mDeviceType, inputDesc->mIoHandle);
Eric Laurent874c42872014-08-08 15:13:39 -07002569 setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle);
Eric Laurent6a94d692014-05-20 11:18:06 -07002570 index = mAudioPatches.indexOfKey(*handle);
2571 if (index >= 0) {
2572 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) {
2573 ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided");
2574 }
2575 patchDesc = mAudioPatches.valueAt(index);
2576 patchDesc->mUid = uid;
2577 ALOGV("createAudioPatch() success");
2578 } else {
2579 ALOGW("createAudioPatch() setInputDevice() failed to create a patch");
2580 return INVALID_OPERATION;
2581 }
2582 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2583 // device to device connection
2584 if (patchDesc != 0) {
Eric Laurent874c42872014-08-08 15:13:39 -07002585 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002586 return BAD_VALUE;
2587 }
2588 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002589 sp<DeviceDescriptor> srcDeviceDesc =
2590 mAvailableInputDevices.getDeviceFromId(patch->sources[0].id);
Eric Laurent58f8eb72014-09-12 16:19:41 -07002591 if (srcDeviceDesc == 0) {
2592 return BAD_VALUE;
2593 }
Eric Laurent874c42872014-08-08 15:13:39 -07002594
Eric Laurent6a94d692014-05-20 11:18:06 -07002595 //update source and sink with our own data as the data passed in the patch may
2596 // be incomplete.
2597 struct audio_patch newPatch = *patch;
2598 srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]);
Eric Laurent6a94d692014-05-20 11:18:06 -07002599
Eric Laurent874c42872014-08-08 15:13:39 -07002600 for (size_t i = 0; i < patch->num_sinks; i++) {
2601 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
2602 ALOGV("createAudioPatch() source device but one sink is not a device");
2603 return INVALID_OPERATION;
2604 }
2605
2606 sp<DeviceDescriptor> sinkDeviceDesc =
2607 mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id);
2608 if (sinkDeviceDesc == 0) {
2609 return BAD_VALUE;
2610 }
2611 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]);
2612
2613 if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) {
2614 // only one sink supported when connected devices across HW modules
2615 if (patch->num_sinks > 1) {
Eric Laurent83b88082014-06-20 18:31:16 -07002616 return INVALID_OPERATION;
2617 }
Eric Laurent874c42872014-08-08 15:13:39 -07002618 SortedVector<audio_io_handle_t> outputs =
2619 getOutputsForDevice(sinkDeviceDesc->mDeviceType,
2620 mOutputs);
2621 // if the sink device is reachable via an opened output stream, request to go via
2622 // this output stream by adding a second source to the patch description
Eric Laurent8838a382014-09-08 16:44:28 -07002623 audio_io_handle_t output = selectOutput(outputs,
2624 AUDIO_OUTPUT_FLAG_NONE,
2625 AUDIO_FORMAT_INVALID);
Eric Laurent874c42872014-08-08 15:13:39 -07002626 if (output != AUDIO_IO_HANDLE_NONE) {
2627 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
2628 if (outputDesc->isDuplicated()) {
2629 return INVALID_OPERATION;
2630 }
2631 outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]);
2632 newPatch.num_sources = 2;
2633 }
Eric Laurent83b88082014-06-20 18:31:16 -07002634 }
Eric Laurent6a94d692014-05-20 11:18:06 -07002635 }
2636 // TODO: check from routing capabilities in config file and other conflicting patches
2637
2638 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
2639 if (index >= 0) {
2640 afPatchHandle = patchDesc->mAfPatchHandle;
2641 }
2642
2643 status_t status = mpClientInterface->createAudioPatch(&newPatch,
2644 &afPatchHandle,
2645 0);
2646 ALOGV("createAudioPatch() patch panel returned %d patchHandle %d",
2647 status, afPatchHandle);
2648 if (status == NO_ERROR) {
2649 if (index < 0) {
2650 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
2651 &newPatch, uid);
2652 addAudioPatch(patchDesc->mHandle, patchDesc);
2653 } else {
2654 patchDesc->mPatch = newPatch;
2655 }
2656 patchDesc->mAfPatchHandle = afPatchHandle;
2657 *handle = patchDesc->mHandle;
2658 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002659 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002660 } else {
2661 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d",
2662 status);
2663 return INVALID_OPERATION;
2664 }
2665 } else {
2666 return BAD_VALUE;
2667 }
2668 } else {
2669 return BAD_VALUE;
2670 }
2671 return NO_ERROR;
2672}
2673
2674status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle,
2675 uid_t uid)
2676{
2677 ALOGV("releaseAudioPatch() patch %d", handle);
2678
2679 ssize_t index = mAudioPatches.indexOfKey(handle);
2680
2681 if (index < 0) {
2682 return BAD_VALUE;
2683 }
2684 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
2685 ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d",
2686 mUidCached, patchDesc->mUid, uid);
2687 if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) {
2688 return INVALID_OPERATION;
2689 }
2690
2691 struct audio_patch *patch = &patchDesc->mPatch;
2692 patchDesc->mUid = mUidCached;
2693 if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002694 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002695 if (outputDesc == NULL) {
2696 ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id);
2697 return BAD_VALUE;
2698 }
2699
2700 setOutputDevice(outputDesc->mIoHandle,
2701 getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/),
2702 true,
2703 0,
2704 NULL);
2705 } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
2706 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002707 sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id);
Eric Laurent6a94d692014-05-20 11:18:06 -07002708 if (inputDesc == NULL) {
2709 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id);
2710 return BAD_VALUE;
2711 }
2712 setInputDevice(inputDesc->mIoHandle,
2713 getNewInputDevice(inputDesc->mIoHandle),
2714 true,
2715 NULL);
2716 } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) {
2717 audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle;
2718 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
2719 ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d",
2720 status, patchDesc->mAfPatchHandle);
2721 removeAudioPatch(patchDesc->mHandle);
2722 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07002723 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent6a94d692014-05-20 11:18:06 -07002724 } else {
2725 return BAD_VALUE;
2726 }
2727 } else {
2728 return BAD_VALUE;
2729 }
2730 return NO_ERROR;
2731}
2732
2733status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches,
2734 struct audio_patch *patches,
2735 unsigned int *generation)
2736{
2737 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
2738 generation == NULL) {
2739 return BAD_VALUE;
2740 }
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002741 ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
Eric Laurent6a94d692014-05-20 11:18:06 -07002742 *num_patches, patches, mAudioPatches.size());
2743 if (patches == NULL) {
2744 *num_patches = 0;
2745 }
2746
2747 size_t patchesWritten = 0;
2748 size_t patchesMax = *num_patches;
2749 for (size_t i = 0;
2750 i < mAudioPatches.size() && patchesWritten < patchesMax; i++) {
2751 patches[patchesWritten] = mAudioPatches[i]->mPatch;
2752 patches[patchesWritten++].id = mAudioPatches[i]->mHandle;
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002753 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07002754 i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks);
2755 }
2756 *num_patches = mAudioPatches.size();
2757
2758 *generation = curAudioPortGeneration();
Mark Salyzynbeb9e302014-06-18 16:33:15 -07002759 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
Eric Laurent6a94d692014-05-20 11:18:06 -07002760 return NO_ERROR;
2761}
2762
Eric Laurente1715a42014-05-20 11:30:42 -07002763status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent6a94d692014-05-20 11:18:06 -07002764{
Eric Laurente1715a42014-05-20 11:30:42 -07002765 ALOGV("setAudioPortConfig()");
2766
2767 if (config == NULL) {
2768 return BAD_VALUE;
2769 }
2770 ALOGV("setAudioPortConfig() on port handle %d", config->id);
2771 // Only support gain configuration for now
Eric Laurenta121f902014-06-03 13:32:54 -07002772 if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) {
2773 return INVALID_OPERATION;
Eric Laurente1715a42014-05-20 11:30:42 -07002774 }
2775
Eric Laurenta121f902014-06-03 13:32:54 -07002776 sp<AudioPortConfig> audioPortConfig;
Eric Laurente1715a42014-05-20 11:30:42 -07002777 if (config->type == AUDIO_PORT_TYPE_MIX) {
2778 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002779 sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002780 if (outputDesc == NULL) {
2781 return BAD_VALUE;
2782 }
Eric Laurent84c70242014-06-23 08:46:27 -07002783 ALOG_ASSERT(!outputDesc->isDuplicated(),
2784 "setAudioPortConfig() called on duplicated output %d",
2785 outputDesc->mIoHandle);
Eric Laurenta121f902014-06-03 13:32:54 -07002786 audioPortConfig = outputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002787 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07002788 sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id);
Eric Laurente1715a42014-05-20 11:30:42 -07002789 if (inputDesc == NULL) {
2790 return BAD_VALUE;
2791 }
Eric Laurenta121f902014-06-03 13:32:54 -07002792 audioPortConfig = inputDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002793 } else {
2794 return BAD_VALUE;
2795 }
2796 } else if (config->type == AUDIO_PORT_TYPE_DEVICE) {
2797 sp<DeviceDescriptor> deviceDesc;
2798 if (config->role == AUDIO_PORT_ROLE_SOURCE) {
2799 deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id);
2800 } else if (config->role == AUDIO_PORT_ROLE_SINK) {
2801 deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id);
2802 } else {
2803 return BAD_VALUE;
2804 }
2805 if (deviceDesc == NULL) {
2806 return BAD_VALUE;
2807 }
Eric Laurenta121f902014-06-03 13:32:54 -07002808 audioPortConfig = deviceDesc;
Eric Laurente1715a42014-05-20 11:30:42 -07002809 } else {
2810 return BAD_VALUE;
2811 }
2812
Eric Laurenta121f902014-06-03 13:32:54 -07002813 struct audio_port_config backupConfig;
2814 status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig);
2815 if (status == NO_ERROR) {
2816 struct audio_port_config newConfig;
2817 audioPortConfig->toAudioPortConfig(&newConfig, config);
2818 status = mpClientInterface->setAudioPortConfig(&newConfig, 0);
Eric Laurente1715a42014-05-20 11:30:42 -07002819 }
Eric Laurenta121f902014-06-03 13:32:54 -07002820 if (status != NO_ERROR) {
2821 audioPortConfig->applyAudioPortConfig(&backupConfig);
Eric Laurente1715a42014-05-20 11:30:42 -07002822 }
Eric Laurente1715a42014-05-20 11:30:42 -07002823
2824 return status;
Eric Laurent6a94d692014-05-20 11:18:06 -07002825}
2826
2827void AudioPolicyManager::clearAudioPatches(uid_t uid)
2828{
Eric Laurent0add0fd2014-12-04 18:58:14 -08002829 for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) {
Eric Laurent6a94d692014-05-20 11:18:06 -07002830 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i);
2831 if (patchDesc->mUid == uid) {
Eric Laurent0add0fd2014-12-04 18:58:14 -08002832 releaseAudioPatch(mAudioPatches.keyAt(i), uid);
Eric Laurent6a94d692014-05-20 11:18:06 -07002833 }
2834 }
2835}
2836
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07002837status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session,
2838 audio_io_handle_t *ioHandle,
2839 audio_devices_t *device)
2840{
2841 *session = (audio_session_t)mpClientInterface->newAudioUniqueId();
2842 *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId();
2843 *device = getDeviceForInputSource(AUDIO_SOURCE_HOTWORD);
2844
2845 mSoundTriggerSessions.add(*session, *ioHandle);
2846
2847 return NO_ERROR;
2848}
2849
2850status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session)
2851{
2852 ssize_t index = mSoundTriggerSessions.indexOfKey(session);
2853 if (index < 0) {
2854 ALOGW("acquireSoundTriggerSession() session %d not registered", session);
2855 return BAD_VALUE;
2856 }
2857
2858 mSoundTriggerSessions.removeItem(session);
2859 return NO_ERROR;
2860}
2861
Eric Laurent6a94d692014-05-20 11:18:06 -07002862status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle,
2863 const sp<AudioPatch>& patch)
2864{
2865 ssize_t index = mAudioPatches.indexOfKey(handle);
2866
2867 if (index >= 0) {
2868 ALOGW("addAudioPatch() patch %d already in", handle);
2869 return ALREADY_EXISTS;
2870 }
2871 mAudioPatches.add(handle, patch);
2872 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
2873 "sink handle %d",
2874 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
2875 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
2876 return NO_ERROR;
2877}
2878
2879status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle)
2880{
2881 ssize_t index = mAudioPatches.indexOfKey(handle);
2882
2883 if (index < 0) {
2884 ALOGW("removeAudioPatch() patch %d not in", handle);
2885 return ALREADY_EXISTS;
2886 }
2887 ALOGV("removeAudioPatch() handle %d af handle %d", handle,
2888 mAudioPatches.valueAt(index)->mAfPatchHandle);
2889 mAudioPatches.removeItemsAt(index);
2890 return NO_ERROR;
2891}
2892
Eric Laurente552edb2014-03-10 17:42:56 -07002893// ----------------------------------------------------------------------------
Eric Laurente0720872014-03-11 09:30:41 -07002894// AudioPolicyManager
Eric Laurente552edb2014-03-10 17:42:56 -07002895// ----------------------------------------------------------------------------
2896
Eric Laurent3a4311c2014-03-17 12:00:47 -07002897uint32_t AudioPolicyManager::nextUniqueId()
2898{
2899 return android_atomic_inc(&mNextUniqueId);
2900}
2901
Eric Laurent6a94d692014-05-20 11:18:06 -07002902uint32_t AudioPolicyManager::nextAudioPortGeneration()
2903{
2904 return android_atomic_inc(&mAudioPortGeneration);
2905}
2906
Eric Laurente0720872014-03-11 09:30:41 -07002907AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
Eric Laurente552edb2014-03-10 17:42:56 -07002908 :
2909#ifdef AUDIO_POLICY_TEST
2910 Thread(false),
2911#endif //AUDIO_POLICY_TEST
2912 mPrimaryOutput((audio_io_handle_t)0),
Eric Laurent3b73df72014-03-11 09:06:29 -07002913 mPhoneState(AUDIO_MODE_NORMAL),
Eric Laurente552edb2014-03-10 17:42:56 -07002914 mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
2915 mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
Eric Laurent3a4311c2014-03-17 12:00:47 -07002916 mA2dpSuspended(false),
Eric Laurent6a94d692014-05-20 11:18:06 -07002917 mSpeakerDrcEnabled(false), mNextUniqueId(1),
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07002918 mAudioPortGeneration(1),
2919 mBeaconMuteRefCount(0),
2920 mBeaconPlayingRefCount(0),
2921 mBeaconMuted(false)
Eric Laurente552edb2014-03-10 17:42:56 -07002922{
Eric Laurent6a94d692014-05-20 11:18:06 -07002923 mUidCached = getuid();
Eric Laurente552edb2014-03-10 17:42:56 -07002924 mpClientInterface = clientInterface;
2925
Eric Laurent3b73df72014-03-11 09:06:29 -07002926 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
2927 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07002928 }
2929
Eric Laurent1afeecb2014-05-14 08:52:28 -07002930 mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER);
Eric Laurente552edb2014-03-10 17:42:56 -07002931 if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
2932 if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
2933 ALOGE("could not load audio policy configuration file, setting defaults");
2934 defaultAudioPolicyConfig();
2935 }
2936 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07002937 // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices
Eric Laurente552edb2014-03-10 17:42:56 -07002938
2939 // must be done after reading the policy
2940 initializeVolumeCurves();
2941
2942 // open all output streams needed to access attached devices
Eric Laurent3a4311c2014-03-17 12:00:47 -07002943 audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types();
2944 audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
Eric Laurente552edb2014-03-10 17:42:56 -07002945 for (size_t i = 0; i < mHwModules.size(); i++) {
2946 mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
2947 if (mHwModules[i]->mHandle == 0) {
2948 ALOGW("could not open HW module %s", mHwModules[i]->mName);
2949 continue;
2950 }
2951 // open all output streams needed to access attached devices
2952 // except for direct output streams that are only opened when they are actually
2953 // required by an app.
Eric Laurent3a4311c2014-03-17 12:00:47 -07002954 // This also validates mAvailableOutputDevices list
Eric Laurente552edb2014-03-10 17:42:56 -07002955 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
2956 {
Eric Laurent1c333e22014-05-20 10:48:17 -07002957 const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07002958
Eric Laurent3a4311c2014-03-17 12:00:47 -07002959 if (outProfile->mSupportedDevices.isEmpty()) {
2960 ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName);
2961 continue;
2962 }
2963
Eric Laurentd78f1532014-09-16 16:38:20 -07002964 if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
2965 continue;
2966 }
Eric Laurent83b88082014-06-20 18:31:16 -07002967 audio_devices_t profileType = outProfile->mSupportedDevices.types();
2968 if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) {
2969 profileType = mDefaultOutputDevice->mDeviceType;
2970 } else {
Eric Laurentd78f1532014-09-16 16:38:20 -07002971 // chose first device present in mSupportedDevices also part of
2972 // outputDeviceTypes
2973 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
2974 profileType = outProfile->mSupportedDevices[k]->mDeviceType;
2975 if ((profileType & outputDeviceTypes) != 0) {
2976 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07002977 }
Eric Laurente552edb2014-03-10 17:42:56 -07002978 }
2979 }
Eric Laurentd78f1532014-09-16 16:38:20 -07002980 if ((profileType & outputDeviceTypes) == 0) {
2981 continue;
2982 }
2983 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile);
2984
2985 outputDesc->mDevice = profileType;
2986 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
2987 config.sample_rate = outputDesc->mSamplingRate;
2988 config.channel_mask = outputDesc->mChannelMask;
2989 config.format = outputDesc->mFormat;
2990 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
2991 status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle,
2992 &output,
2993 &config,
2994 &outputDesc->mDevice,
2995 String8(""),
2996 &outputDesc->mLatency,
2997 outputDesc->mFlags);
2998
2999 if (status != NO_ERROR) {
3000 ALOGW("Cannot open output stream for device %08x on hw module %s",
3001 outputDesc->mDevice,
3002 mHwModules[i]->mName);
3003 } else {
3004 outputDesc->mSamplingRate = config.sample_rate;
3005 outputDesc->mChannelMask = config.channel_mask;
3006 outputDesc->mFormat = config.format;
3007
3008 for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) {
3009 audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType;
3010 ssize_t index =
3011 mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]);
3012 // give a valid ID to an attached device once confirmed it is reachable
3013 if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) {
3014 mAvailableOutputDevices[index]->mId = nextUniqueId();
3015 mAvailableOutputDevices[index]->mModule = mHwModules[i];
3016 }
3017 }
3018 if (mPrimaryOutput == 0 &&
3019 outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
3020 mPrimaryOutput = output;
3021 }
3022 addOutput(output, outputDesc);
3023 setOutputDevice(output,
3024 outputDesc->mDevice,
3025 true);
3026 }
Eric Laurente552edb2014-03-10 17:42:56 -07003027 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003028 // open input streams needed to access attached devices to validate
3029 // mAvailableInputDevices list
3030 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
3031 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003032 const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j];
Eric Laurente552edb2014-03-10 17:42:56 -07003033
Eric Laurent3a4311c2014-03-17 12:00:47 -07003034 if (inProfile->mSupportedDevices.isEmpty()) {
3035 ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName);
3036 continue;
3037 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003038 // chose first device present in mSupportedDevices also part of
3039 // inputDeviceTypes
3040 audio_devices_t profileType = AUDIO_DEVICE_NONE;
3041 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3042 profileType = inProfile->mSupportedDevices[k]->mDeviceType;
3043 if (profileType & inputDeviceTypes) {
3044 break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07003045 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003046 }
Eric Laurentd78f1532014-09-16 16:38:20 -07003047 if ((profileType & inputDeviceTypes) == 0) {
3048 continue;
3049 }
3050 sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile);
3051
3052 inputDesc->mInputSource = AUDIO_SOURCE_MIC;
3053 inputDesc->mDevice = profileType;
3054
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003055 // find the address
3056 DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType);
3057 // the inputs vector must be of size 1, but we don't want to crash here
3058 String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress
3059 : String8("");
3060 ALOGV(" for input device 0x%x using address %s", profileType, address.string());
3061 ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!");
3062
Eric Laurentd78f1532014-09-16 16:38:20 -07003063 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3064 config.sample_rate = inputDesc->mSamplingRate;
3065 config.channel_mask = inputDesc->mChannelMask;
3066 config.format = inputDesc->mFormat;
3067 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3068 status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle,
3069 &input,
3070 &config,
3071 &inputDesc->mDevice,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07003072 address,
Eric Laurentd78f1532014-09-16 16:38:20 -07003073 AUDIO_SOURCE_MIC,
3074 AUDIO_INPUT_FLAG_NONE);
3075
3076 if (status == NO_ERROR) {
3077 for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) {
3078 audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType;
3079 ssize_t index =
3080 mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]);
3081 // give a valid ID to an attached device once confirmed it is reachable
3082 if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) {
3083 mAvailableInputDevices[index]->mId = nextUniqueId();
3084 mAvailableInputDevices[index]->mModule = mHwModules[i];
3085 }
3086 }
3087 mpClientInterface->closeInput(input);
3088 } else {
3089 ALOGW("Cannot open input stream for device %08x on hw module %s",
3090 inputDesc->mDevice,
3091 mHwModules[i]->mName);
3092 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003093 }
3094 }
3095 // make sure all attached devices have been allocated a unique ID
3096 for (size_t i = 0; i < mAvailableOutputDevices.size();) {
3097 if (mAvailableOutputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003098 ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003099 mAvailableOutputDevices.remove(mAvailableOutputDevices[i]);
3100 continue;
3101 }
3102 i++;
3103 }
3104 for (size_t i = 0; i < mAvailableInputDevices.size();) {
3105 if (mAvailableInputDevices[i]->mId == 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003106 ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003107 mAvailableInputDevices.remove(mAvailableInputDevices[i]);
3108 continue;
3109 }
3110 i++;
3111 }
3112 // make sure default device is reachable
3113 if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003114 ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003115 }
Eric Laurente552edb2014-03-10 17:42:56 -07003116
3117 ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
3118
3119 updateDevicesAndOutputs();
3120
3121#ifdef AUDIO_POLICY_TEST
3122 if (mPrimaryOutput != 0) {
3123 AudioParameter outputCmd = AudioParameter();
3124 outputCmd.addInt(String8("set_id"), 0);
3125 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3126
3127 mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
3128 mTestSamplingRate = 44100;
Eric Laurent3b73df72014-03-11 09:06:29 -07003129 mTestFormat = AUDIO_FORMAT_PCM_16_BIT;
3130 mTestChannels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003131 mTestLatencyMs = 0;
3132 mCurOutput = 0;
3133 mDirectOutput = false;
3134 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3135 mTestOutputs[i] = 0;
3136 }
3137
3138 const size_t SIZE = 256;
3139 char buffer[SIZE];
3140 snprintf(buffer, SIZE, "AudioPolicyManagerTest");
3141 run(buffer, ANDROID_PRIORITY_AUDIO);
3142 }
3143#endif //AUDIO_POLICY_TEST
3144}
3145
Eric Laurente0720872014-03-11 09:30:41 -07003146AudioPolicyManager::~AudioPolicyManager()
Eric Laurente552edb2014-03-10 17:42:56 -07003147{
3148#ifdef AUDIO_POLICY_TEST
3149 exit();
3150#endif //AUDIO_POLICY_TEST
3151 for (size_t i = 0; i < mOutputs.size(); i++) {
3152 mpClientInterface->closeOutput(mOutputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003153 }
3154 for (size_t i = 0; i < mInputs.size(); i++) {
3155 mpClientInterface->closeInput(mInputs.keyAt(i));
Eric Laurente552edb2014-03-10 17:42:56 -07003156 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07003157 mAvailableOutputDevices.clear();
3158 mAvailableInputDevices.clear();
Eric Laurent1f2f2232014-06-02 12:01:23 -07003159 mOutputs.clear();
3160 mInputs.clear();
3161 mHwModules.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07003162}
3163
Eric Laurente0720872014-03-11 09:30:41 -07003164status_t AudioPolicyManager::initCheck()
Eric Laurente552edb2014-03-10 17:42:56 -07003165{
3166 return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
3167}
3168
3169#ifdef AUDIO_POLICY_TEST
Eric Laurente0720872014-03-11 09:30:41 -07003170bool AudioPolicyManager::threadLoop()
Eric Laurente552edb2014-03-10 17:42:56 -07003171{
3172 ALOGV("entering threadLoop()");
3173 while (!exitPending())
3174 {
3175 String8 command;
3176 int valueInt;
3177 String8 value;
3178
3179 Mutex::Autolock _l(mLock);
3180 mWaitWorkCV.waitRelative(mLock, milliseconds(50));
3181
3182 command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
3183 AudioParameter param = AudioParameter(command);
3184
3185 if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
3186 valueInt != 0) {
3187 ALOGV("Test command %s received", command.string());
3188 String8 target;
3189 if (param.get(String8("target"), target) != NO_ERROR) {
3190 target = "Manager";
3191 }
3192 if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
3193 param.remove(String8("test_cmd_policy_output"));
3194 mCurOutput = valueInt;
3195 }
3196 if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
3197 param.remove(String8("test_cmd_policy_direct"));
3198 if (value == "false") {
3199 mDirectOutput = false;
3200 } else if (value == "true") {
3201 mDirectOutput = true;
3202 }
3203 }
3204 if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
3205 param.remove(String8("test_cmd_policy_input"));
3206 mTestInput = valueInt;
3207 }
3208
3209 if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
3210 param.remove(String8("test_cmd_policy_format"));
Eric Laurent3b73df72014-03-11 09:06:29 -07003211 int format = AUDIO_FORMAT_INVALID;
Eric Laurente552edb2014-03-10 17:42:56 -07003212 if (value == "PCM 16 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003213 format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003214 } else if (value == "PCM 8 bits") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003215 format = AUDIO_FORMAT_PCM_8_BIT;
Eric Laurente552edb2014-03-10 17:42:56 -07003216 } else if (value == "Compressed MP3") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003217 format = AUDIO_FORMAT_MP3;
Eric Laurente552edb2014-03-10 17:42:56 -07003218 }
Eric Laurent3b73df72014-03-11 09:06:29 -07003219 if (format != AUDIO_FORMAT_INVALID) {
Eric Laurente552edb2014-03-10 17:42:56 -07003220 if (target == "Manager") {
3221 mTestFormat = format;
3222 } else if (mTestOutputs[mCurOutput] != 0) {
3223 AudioParameter outputParam = AudioParameter();
3224 outputParam.addInt(String8("format"), format);
3225 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3226 }
3227 }
3228 }
3229 if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
3230 param.remove(String8("test_cmd_policy_channels"));
3231 int channels = 0;
3232
3233 if (value == "Channels Stereo") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003234 channels = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurente552edb2014-03-10 17:42:56 -07003235 } else if (value == "Channels Mono") {
Eric Laurent3b73df72014-03-11 09:06:29 -07003236 channels = AUDIO_CHANNEL_OUT_MONO;
Eric Laurente552edb2014-03-10 17:42:56 -07003237 }
3238 if (channels != 0) {
3239 if (target == "Manager") {
3240 mTestChannels = channels;
3241 } else if (mTestOutputs[mCurOutput] != 0) {
3242 AudioParameter outputParam = AudioParameter();
3243 outputParam.addInt(String8("channels"), channels);
3244 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3245 }
3246 }
3247 }
3248 if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
3249 param.remove(String8("test_cmd_policy_sampleRate"));
3250 if (valueInt >= 0 && valueInt <= 96000) {
3251 int samplingRate = valueInt;
3252 if (target == "Manager") {
3253 mTestSamplingRate = samplingRate;
3254 } else if (mTestOutputs[mCurOutput] != 0) {
3255 AudioParameter outputParam = AudioParameter();
3256 outputParam.addInt(String8("sampling_rate"), samplingRate);
3257 mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
3258 }
3259 }
3260 }
3261
3262 if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
3263 param.remove(String8("test_cmd_policy_reopen"));
3264
Eric Laurent1f2f2232014-06-02 12:01:23 -07003265 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003266 mpClientInterface->closeOutput(mPrimaryOutput);
3267
3268 audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
3269
Eric Laurente552edb2014-03-10 17:42:56 -07003270 mOutputs.removeItem(mPrimaryOutput);
3271
Eric Laurent1f2f2232014-06-02 12:01:23 -07003272 sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL);
Eric Laurente552edb2014-03-10 17:42:56 -07003273 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003274 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3275 config.sample_rate = outputDesc->mSamplingRate;
3276 config.channel_mask = outputDesc->mChannelMask;
3277 config.format = outputDesc->mFormat;
3278 status_t status = mpClientInterface->openOutput(moduleHandle,
3279 &mPrimaryOutput,
3280 &config,
3281 &outputDesc->mDevice,
3282 String8(""),
3283 &outputDesc->mLatency,
3284 outputDesc->mFlags);
3285 if (status != NO_ERROR) {
3286 ALOGE("Failed to reopen hardware output stream, "
3287 "samplingRate: %d, format %d, channels %d",
3288 outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
Eric Laurente552edb2014-03-10 17:42:56 -07003289 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003290 outputDesc->mSamplingRate = config.sample_rate;
3291 outputDesc->mChannelMask = config.channel_mask;
3292 outputDesc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003293 AudioParameter outputCmd = AudioParameter();
3294 outputCmd.addInt(String8("set_id"), 0);
3295 mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
3296 addOutput(mPrimaryOutput, outputDesc);
3297 }
3298 }
3299
3300
3301 mpClientInterface->setParameters(0, String8("test_cmd_policy="));
3302 }
3303 }
3304 return false;
3305}
3306
Eric Laurente0720872014-03-11 09:30:41 -07003307void AudioPolicyManager::exit()
Eric Laurente552edb2014-03-10 17:42:56 -07003308{
3309 {
3310 AutoMutex _l(mLock);
3311 requestExit();
3312 mWaitWorkCV.signal();
3313 }
3314 requestExitAndWait();
3315}
3316
Eric Laurente0720872014-03-11 09:30:41 -07003317int AudioPolicyManager::testOutputIndex(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003318{
3319 for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
3320 if (output == mTestOutputs[i]) return i;
3321 }
3322 return 0;
3323}
3324#endif //AUDIO_POLICY_TEST
3325
3326// ---
3327
Eric Laurent1f2f2232014-06-02 12:01:23 -07003328void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07003329{
Eric Laurent1c333e22014-05-20 10:48:17 -07003330 outputDesc->mIoHandle = output;
3331 outputDesc->mId = nextUniqueId();
3332 mOutputs.add(output, outputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003333 nextAudioPortGeneration();
Eric Laurente552edb2014-03-10 17:42:56 -07003334}
3335
Eric Laurent1f2f2232014-06-02 12:01:23 -07003336void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc)
Eric Laurentd4692962014-05-05 18:13:44 -07003337{
Eric Laurent1c333e22014-05-20 10:48:17 -07003338 inputDesc->mIoHandle = input;
3339 inputDesc->mId = nextUniqueId();
3340 mInputs.add(input, inputDesc);
Eric Laurent6a94d692014-05-20 11:18:06 -07003341 nextAudioPortGeneration();
Eric Laurentd4692962014-05-05 18:13:44 -07003342}
Eric Laurente552edb2014-03-10 17:42:56 -07003343
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003344void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/,
3345 const String8 address /*in*/,
3346 SortedVector<audio_io_handle_t>& outputs /*out*/) {
3347 // look for a match on the given address on the addresses of the outputs:
3348 // find the address by finding the patch that maps to this output
3349 ssize_t patchIdx = mAudioPatches.indexOfKey(desc->mPatchHandle);
3350 //ALOGV(" inspecting output %d (patch %d) for supported device=0x%x",
3351 // outputIdx, patchIdx, desc->mProfile->mSupportedDevices.types());
3352 if (patchIdx >= 0) {
3353 const sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patchIdx);
3354 const int numSinks = patchDesc->mPatch.num_sinks;
3355 for (ssize_t j=0; j < numSinks; j++) {
3356 if (patchDesc->mPatch.sinks[j].type == AUDIO_PORT_TYPE_DEVICE) {
3357 const char* patchAddr =
3358 patchDesc->mPatch.sinks[j].ext.device.address;
3359 if (strncmp(patchAddr,
3360 address.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003361 ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s",
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003362 desc->mIoHandle, patchDesc->mPatch.sinks[j].ext.device.address);
3363 outputs.add(desc->mIoHandle);
3364 break;
3365 }
3366 }
3367 }
3368 }
3369}
3370
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003371status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc,
Eric Laurent3b73df72014-03-11 09:06:29 -07003372 audio_policy_dev_state_t state,
Eric Laurente552edb2014-03-10 17:42:56 -07003373 SortedVector<audio_io_handle_t>& outputs,
Eric Laurent3a4311c2014-03-17 12:00:47 -07003374 const String8 address)
Eric Laurente552edb2014-03-10 17:42:56 -07003375{
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003376 audio_devices_t device = devDesc->mDeviceType;
Eric Laurent1f2f2232014-06-02 12:01:23 -07003377 sp<AudioOutputDescriptor> desc;
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003378 // erase all current sample rates, formats and channel masks
3379 devDesc->clearCapabilities();
Eric Laurente552edb2014-03-10 17:42:56 -07003380
Eric Laurent3b73df72014-03-11 09:06:29 -07003381 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003382 // first list already open outputs that can be routed to this device
3383 for (size_t i = 0; i < mOutputs.size(); i++) {
3384 desc = mOutputs.valueAt(i);
Eric Laurent3a4311c2014-03-17 12:00:47 -07003385 if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003386 if (!deviceDistinguishesOnAddress(device)) {
3387 ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
3388 outputs.add(mOutputs.keyAt(i));
3389 } else {
3390 ALOGV(" checking address match due to device 0x%x", device);
3391 findIoHandlesByAddress(desc, address, outputs);
3392 }
Eric Laurente552edb2014-03-10 17:42:56 -07003393 }
3394 }
3395 // then look for output profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003396 SortedVector< sp<IOProfile> > profiles;
Eric Laurente552edb2014-03-10 17:42:56 -07003397 for (size_t i = 0; i < mHwModules.size(); i++)
3398 {
3399 if (mHwModules[i]->mHandle == 0) {
3400 continue;
3401 }
3402 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3403 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003404 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
3405 if (profile->mSupportedDevices.types() & device) {
3406 if (!deviceDistinguishesOnAddress(device) ||
3407 address == profile->mSupportedDevices[0]->mAddress) {
3408 profiles.add(profile);
3409 ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
3410 }
Eric Laurente552edb2014-03-10 17:42:56 -07003411 }
3412 }
3413 }
3414
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003415 ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size());
3416
Eric Laurente552edb2014-03-10 17:42:56 -07003417 if (profiles.isEmpty() && outputs.isEmpty()) {
3418 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3419 return BAD_VALUE;
3420 }
3421
3422 // open outputs for matching profiles if needed. Direct outputs are also opened to
3423 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3424 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003425 sp<IOProfile> profile = profiles[profile_index];
Eric Laurente552edb2014-03-10 17:42:56 -07003426
3427 // nothing to do if one output is already opened for this profile
3428 size_t j;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003429 for (j = 0; j < outputs.size(); j++) {
3430 desc = mOutputs.valueFor(outputs.itemAt(j));
Eric Laurente552edb2014-03-10 17:42:56 -07003431 if (!desc->isDuplicated() && desc->mProfile == profile) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003432 // matching profile: save the sample rates, format and channel masks supported
3433 // by the profile in our device descriptor
3434 devDesc->importAudioPort(profile);
Eric Laurente552edb2014-03-10 17:42:56 -07003435 break;
3436 }
3437 }
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003438 if (j != outputs.size()) {
Eric Laurente552edb2014-03-10 17:42:56 -07003439 continue;
3440 }
3441
Eric Laurent83b88082014-06-20 18:31:16 -07003442 ALOGV("opening output for device %08x with params %s profile %p",
3443 device, address.string(), profile.get());
Eric Laurente552edb2014-03-10 17:42:56 -07003444 desc = new AudioOutputDescriptor(profile);
3445 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003446 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3447 config.sample_rate = desc->mSamplingRate;
3448 config.channel_mask = desc->mChannelMask;
3449 config.format = desc->mFormat;
3450 config.offload_info.sample_rate = desc->mSamplingRate;
3451 config.offload_info.channel_mask = desc->mChannelMask;
3452 config.offload_info.format = desc->mFormat;
3453 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
3454 status_t status = mpClientInterface->openOutput(profile->mModule->mHandle,
3455 &output,
3456 &config,
3457 &desc->mDevice,
3458 address,
3459 &desc->mLatency,
3460 desc->mFlags);
3461 if (status == NO_ERROR) {
3462 desc->mSamplingRate = config.sample_rate;
3463 desc->mChannelMask = config.channel_mask;
3464 desc->mFormat = config.format;
Eric Laurente552edb2014-03-10 17:42:56 -07003465
Eric Laurentd4692962014-05-05 18:13:44 -07003466 // Here is where the out_set_parameters() for card & device gets called
Eric Laurent3a4311c2014-03-17 12:00:47 -07003467 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003468 char *param = audio_device_address_to_parameter(device, address);
3469 mpClientInterface->setParameters(output, String8(param));
3470 free(param);
Eric Laurente552edb2014-03-10 17:42:56 -07003471 }
3472
Eric Laurentd4692962014-05-05 18:13:44 -07003473 // Here is where we step through and resolve any "dynamic" fields
3474 String8 reply;
3475 char *value;
3476 if (profile->mSamplingRates[0] == 0) {
3477 reply = mpClientInterface->getParameters(output,
3478 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003479 ALOGV("checkOutputsForDevice() supported sampling rates %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003480 reply.string());
3481 value = strpbrk((char *)reply.string(), "=");
3482 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003483 profile->loadSamplingRates(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003484 }
Eric Laurentd4692962014-05-05 18:13:44 -07003485 }
3486 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3487 reply = mpClientInterface->getParameters(output,
3488 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003489 ALOGV("checkOutputsForDevice() supported formats %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003490 reply.string());
3491 value = strpbrk((char *)reply.string(), "=");
3492 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003493 profile->loadFormats(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003494 }
Eric Laurentd4692962014-05-05 18:13:44 -07003495 }
3496 if (profile->mChannelMasks[0] == 0) {
3497 reply = mpClientInterface->getParameters(output,
3498 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003499 ALOGV("checkOutputsForDevice() supported channel masks %s",
Eric Laurentd4692962014-05-05 18:13:44 -07003500 reply.string());
3501 value = strpbrk((char *)reply.string(), "=");
3502 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003503 profile->loadOutChannels(value + 1);
Eric Laurente552edb2014-03-10 17:42:56 -07003504 }
Eric Laurentd4692962014-05-05 18:13:44 -07003505 }
3506 if (((profile->mSamplingRates[0] == 0) &&
3507 (profile->mSamplingRates.size() < 2)) ||
3508 ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) &&
3509 (profile->mFormats.size() < 2)) ||
3510 ((profile->mChannelMasks[0] == 0) &&
3511 (profile->mChannelMasks.size() < 2))) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003512 ALOGW("checkOutputsForDevice() missing param");
Eric Laurentd4692962014-05-05 18:13:44 -07003513 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003514 output = AUDIO_IO_HANDLE_NONE;
Eric Laurent1e693b52014-07-09 15:03:28 -07003515 } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 ||
3516 profile->mChannelMasks[0] == 0) {
Eric Laurentd4692962014-05-05 18:13:44 -07003517 mpClientInterface->closeOutput(output);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003518 config.sample_rate = profile->pickSamplingRate();
3519 config.channel_mask = profile->pickChannelMask();
3520 config.format = profile->pickFormat();
3521 config.offload_info.sample_rate = config.sample_rate;
3522 config.offload_info.channel_mask = config.channel_mask;
3523 config.offload_info.format = config.format;
3524 status = mpClientInterface->openOutput(profile->mModule->mHandle,
3525 &output,
3526 &config,
3527 &desc->mDevice,
3528 address,
3529 &desc->mLatency,
3530 desc->mFlags);
3531 if (status == NO_ERROR) {
3532 desc->mSamplingRate = config.sample_rate;
3533 desc->mChannelMask = config.channel_mask;
3534 desc->mFormat = config.format;
3535 } else {
3536 output = AUDIO_IO_HANDLE_NONE;
3537 }
Eric Laurentd4692962014-05-05 18:13:44 -07003538 }
3539
Eric Laurentcf2c0212014-07-25 16:20:43 -07003540 if (output != AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003541 addOutput(output, desc);
Eric Laurent275e8e92014-11-30 15:14:47 -08003542 if (deviceDistinguishesOnAddress(device) && address != "0") {
3543 ssize_t index = mPolicyMixes.indexOfKey(address);
3544 if (index >= 0) {
3545 mPolicyMixes[index]->mOutput = desc;
3546 } else {
3547 ALOGE("checkOutputsForDevice() cannot find policy for address %s",
3548 address.string());
3549 }
3550 }
Eric Laurentd4692962014-05-05 18:13:44 -07003551 if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003552 audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003553
Eric Laurentd4692962014-05-05 18:13:44 -07003554 // set initial stream volume for device
3555 applyStreamVolumes(output, device, 0, true);
Eric Laurente552edb2014-03-10 17:42:56 -07003556
Eric Laurentd4692962014-05-05 18:13:44 -07003557 //TODO: configure audio effect output stage here
3558
3559 // open a duplicating output thread for the new output and the primary output
3560 duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
3561 mPrimaryOutput);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003562 if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003563 // add duplicated output descriptor
Eric Laurentcf2c0212014-07-25 16:20:43 -07003564 sp<AudioOutputDescriptor> dupOutputDesc =
3565 new AudioOutputDescriptor(NULL);
Eric Laurentd4692962014-05-05 18:13:44 -07003566 dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
3567 dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
3568 dupOutputDesc->mSamplingRate = desc->mSamplingRate;
3569 dupOutputDesc->mFormat = desc->mFormat;
3570 dupOutputDesc->mChannelMask = desc->mChannelMask;
3571 dupOutputDesc->mLatency = desc->mLatency;
3572 addOutput(duplicatedOutput, dupOutputDesc);
3573 applyStreamVolumes(duplicatedOutput, device, 0, true);
3574 } else {
3575 ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
3576 mPrimaryOutput, output);
3577 mpClientInterface->closeOutput(output);
3578 mOutputs.removeItem(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07003579 nextAudioPortGeneration();
Eric Laurentcf2c0212014-07-25 16:20:43 -07003580 output = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003581 }
Eric Laurente552edb2014-03-10 17:42:56 -07003582 }
3583 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003584 } else {
3585 output = AUDIO_IO_HANDLE_NONE;
Eric Laurente552edb2014-03-10 17:42:56 -07003586 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07003587 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurente552edb2014-03-10 17:42:56 -07003588 ALOGW("checkOutputsForDevice() could not open output for device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07003589 profiles.removeAt(profile_index);
3590 profile_index--;
3591 } else {
3592 outputs.add(output);
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07003593 devDesc->importAudioPort(profile);
3594
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003595 if (deviceDistinguishesOnAddress(device)) {
3596 ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)",
3597 device, address.string());
3598 setOutputDevice(output, device, true/*force*/, 0/*delay*/,
3599 NULL/*patch handle*/, address.string());
3600 }
Eric Laurente552edb2014-03-10 17:42:56 -07003601 ALOGV("checkOutputsForDevice(): adding output %d", output);
3602 }
3603 }
3604
3605 if (profiles.isEmpty()) {
3606 ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
3607 return BAD_VALUE;
3608 }
Eric Laurentd4692962014-05-05 18:13:44 -07003609 } else { // Disconnect
Eric Laurente552edb2014-03-10 17:42:56 -07003610 // check if one opened output is not needed any more after disconnecting one device
3611 for (size_t i = 0; i < mOutputs.size(); i++) {
3612 desc = mOutputs.valueAt(i);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003613 if (!desc->isDuplicated()) {
Eric Laurent275e8e92014-11-30 15:14:47 -08003614 // exact match on device
3615 if (deviceDistinguishesOnAddress(device) &&
3616 (desc->mProfile->mSupportedDevices.types() == device)) {
3617 findIoHandlesByAddress(desc, address, outputs);
3618 } else if (!(desc->mProfile->mSupportedDevices.types()
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003619 & mAvailableOutputDevices.types())) {
3620 ALOGV("checkOutputsForDevice(): disconnecting adding output %d",
3621 mOutputs.keyAt(i));
3622 outputs.add(mOutputs.keyAt(i));
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07003623 }
Eric Laurente552edb2014-03-10 17:42:56 -07003624 }
3625 }
Eric Laurentd4692962014-05-05 18:13:44 -07003626 // Clear any profiles associated with the disconnected device.
Eric Laurente552edb2014-03-10 17:42:56 -07003627 for (size_t i = 0; i < mHwModules.size(); i++)
3628 {
3629 if (mHwModules[i]->mHandle == 0) {
3630 continue;
3631 }
3632 for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
3633 {
Eric Laurent1c333e22014-05-20 10:48:17 -07003634 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07003635 if (profile->mSupportedDevices.types() & device) {
3636 ALOGV("checkOutputsForDevice(): "
3637 "clearing direct output profile %zu on module %zu", j, i);
Eric Laurente552edb2014-03-10 17:42:56 -07003638 if (profile->mSamplingRates[0] == 0) {
3639 profile->mSamplingRates.clear();
3640 profile->mSamplingRates.add(0);
3641 }
3642 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3643 profile->mFormats.clear();
3644 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3645 }
3646 if (profile->mChannelMasks[0] == 0) {
3647 profile->mChannelMasks.clear();
3648 profile->mChannelMasks.add(0);
3649 }
3650 }
3651 }
3652 }
3653 }
3654 return NO_ERROR;
3655}
3656
Eric Laurentd4692962014-05-05 18:13:44 -07003657status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device,
3658 audio_policy_dev_state_t state,
3659 SortedVector<audio_io_handle_t>& inputs,
3660 const String8 address)
3661{
Eric Laurent1f2f2232014-06-02 12:01:23 -07003662 sp<AudioInputDescriptor> desc;
Eric Laurentd4692962014-05-05 18:13:44 -07003663 if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
3664 // first list already open inputs that can be routed to this device
3665 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3666 desc = mInputs.valueAt(input_index);
3667 if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3668 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
3669 inputs.add(mInputs.keyAt(input_index));
3670 }
3671 }
3672
3673 // then look for input profiles that can be routed to this device
Eric Laurent1c333e22014-05-20 10:48:17 -07003674 SortedVector< sp<IOProfile> > profiles;
Eric Laurentd4692962014-05-05 18:13:44 -07003675 for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++)
3676 {
3677 if (mHwModules[module_idx]->mHandle == 0) {
3678 continue;
3679 }
3680 for (size_t profile_index = 0;
3681 profile_index < mHwModules[module_idx]->mInputProfiles.size();
3682 profile_index++)
3683 {
Eric Laurent275e8e92014-11-30 15:14:47 -08003684 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index];
3685
3686 if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) {
3687 if (!deviceDistinguishesOnAddress(device) ||
3688 address == profile->mSupportedDevices[0]->mAddress) {
3689 profiles.add(profile);
3690 ALOGV("checkInputsForDevice(): adding profile %zu from module %zu",
3691 profile_index, module_idx);
3692 }
Eric Laurentd4692962014-05-05 18:13:44 -07003693 }
3694 }
3695 }
3696
3697 if (profiles.isEmpty() && inputs.isEmpty()) {
3698 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3699 return BAD_VALUE;
3700 }
3701
3702 // open inputs for matching profiles if needed. Direct inputs are also opened to
3703 // query for dynamic parameters and will be closed later by setDeviceConnectionState()
3704 for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
3705
Eric Laurent1c333e22014-05-20 10:48:17 -07003706 sp<IOProfile> profile = profiles[profile_index];
Eric Laurentd4692962014-05-05 18:13:44 -07003707 // nothing to do if one input is already opened for this profile
3708 size_t input_index;
3709 for (input_index = 0; input_index < mInputs.size(); input_index++) {
3710 desc = mInputs.valueAt(input_index);
3711 if (desc->mProfile == profile) {
3712 break;
3713 }
3714 }
3715 if (input_index != mInputs.size()) {
3716 continue;
3717 }
3718
3719 ALOGV("opening input for device 0x%X with params %s", device, address.string());
3720 desc = new AudioInputDescriptor(profile);
3721 desc->mDevice = device;
Eric Laurentcf2c0212014-07-25 16:20:43 -07003722 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
3723 config.sample_rate = desc->mSamplingRate;
3724 config.channel_mask = desc->mChannelMask;
3725 config.format = desc->mFormat;
3726 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
3727 status_t status = mpClientInterface->openInput(profile->mModule->mHandle,
3728 &input,
3729 &config,
3730 &desc->mDevice,
3731 address,
3732 AUDIO_SOURCE_MIC,
3733 AUDIO_INPUT_FLAG_NONE /*FIXME*/);
Eric Laurentd4692962014-05-05 18:13:44 -07003734
Eric Laurentcf2c0212014-07-25 16:20:43 -07003735 if (status == NO_ERROR) {
3736 desc->mSamplingRate = config.sample_rate;
3737 desc->mChannelMask = config.channel_mask;
3738 desc->mFormat = config.format;
Eric Laurentd4692962014-05-05 18:13:44 -07003739
Eric Laurentd4692962014-05-05 18:13:44 -07003740 if (!address.isEmpty()) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07003741 char *param = audio_device_address_to_parameter(device, address);
3742 mpClientInterface->setParameters(input, String8(param));
3743 free(param);
Eric Laurentd4692962014-05-05 18:13:44 -07003744 }
3745
3746 // Here is where we step through and resolve any "dynamic" fields
3747 String8 reply;
3748 char *value;
3749 if (profile->mSamplingRates[0] == 0) {
3750 reply = mpClientInterface->getParameters(input,
3751 String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
3752 ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
3753 reply.string());
3754 value = strpbrk((char *)reply.string(), "=");
3755 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003756 profile->loadSamplingRates(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003757 }
3758 }
3759 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3760 reply = mpClientInterface->getParameters(input,
3761 String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
3762 ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
3763 value = strpbrk((char *)reply.string(), "=");
3764 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003765 profile->loadFormats(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003766 }
3767 }
3768 if (profile->mChannelMasks[0] == 0) {
3769 reply = mpClientInterface->getParameters(input,
3770 String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
3771 ALOGV("checkInputsForDevice() direct input sup channel masks %s",
3772 reply.string());
3773 value = strpbrk((char *)reply.string(), "=");
3774 if (value != NULL) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003775 profile->loadInChannels(value + 1);
Eric Laurentd4692962014-05-05 18:13:44 -07003776 }
3777 }
3778 if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
3779 ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
3780 ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
3781 ALOGW("checkInputsForDevice() direct input missing param");
3782 mpClientInterface->closeInput(input);
Eric Laurentcf2c0212014-07-25 16:20:43 -07003783 input = AUDIO_IO_HANDLE_NONE;
Eric Laurentd4692962014-05-05 18:13:44 -07003784 }
3785
3786 if (input != 0) {
3787 addInput(input, desc);
3788 }
3789 } // endif input != 0
3790
Eric Laurentcf2c0212014-07-25 16:20:43 -07003791 if (input == AUDIO_IO_HANDLE_NONE) {
Eric Laurentd4692962014-05-05 18:13:44 -07003792 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
Eric Laurentd4692962014-05-05 18:13:44 -07003793 profiles.removeAt(profile_index);
3794 profile_index--;
3795 } else {
3796 inputs.add(input);
3797 ALOGV("checkInputsForDevice(): adding input %d", input);
3798 }
3799 } // end scan profiles
3800
3801 if (profiles.isEmpty()) {
3802 ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
3803 return BAD_VALUE;
3804 }
3805 } else {
3806 // Disconnect
3807 // check if one opened input is not needed any more after disconnecting one device
3808 for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
3809 desc = mInputs.valueAt(input_index);
Eric Laurentddbc6652014-11-13 15:13:44 -08003810 if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() &
3811 ~AUDIO_DEVICE_BIT_IN)) {
Eric Laurentd4692962014-05-05 18:13:44 -07003812 ALOGV("checkInputsForDevice(): disconnecting adding input %d",
3813 mInputs.keyAt(input_index));
3814 inputs.add(mInputs.keyAt(input_index));
3815 }
3816 }
3817 // Clear any profiles associated with the disconnected device.
3818 for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) {
3819 if (mHwModules[module_index]->mHandle == 0) {
3820 continue;
3821 }
3822 for (size_t profile_index = 0;
3823 profile_index < mHwModules[module_index]->mInputProfiles.size();
3824 profile_index++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07003825 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index];
Eric Laurentddbc6652014-11-13 15:13:44 -08003826 if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07003827 ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu",
Eric Laurentd4692962014-05-05 18:13:44 -07003828 profile_index, module_index);
3829 if (profile->mSamplingRates[0] == 0) {
3830 profile->mSamplingRates.clear();
3831 profile->mSamplingRates.add(0);
3832 }
3833 if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
3834 profile->mFormats.clear();
3835 profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
3836 }
3837 if (profile->mChannelMasks[0] == 0) {
3838 profile->mChannelMasks.clear();
3839 profile->mChannelMasks.add(0);
3840 }
3841 }
3842 }
3843 }
3844 } // end disconnect
3845
3846 return NO_ERROR;
3847}
3848
3849
Eric Laurente0720872014-03-11 09:30:41 -07003850void AudioPolicyManager::closeOutput(audio_io_handle_t output)
Eric Laurente552edb2014-03-10 17:42:56 -07003851{
3852 ALOGV("closeOutput(%d)", output);
3853
Eric Laurent1f2f2232014-06-02 12:01:23 -07003854 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003855 if (outputDesc == NULL) {
3856 ALOGW("closeOutput() unknown output %d", output);
3857 return;
3858 }
3859
Eric Laurent275e8e92014-11-30 15:14:47 -08003860 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
3861 if (mPolicyMixes[i]->mOutput == outputDesc) {
3862 mPolicyMixes[i]->mOutput.clear();
3863 }
3864 }
3865
Eric Laurente552edb2014-03-10 17:42:56 -07003866 // look for duplicated outputs connected to the output being removed.
3867 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003868 sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07003869 if (dupOutputDesc->isDuplicated() &&
3870 (dupOutputDesc->mOutput1 == outputDesc ||
3871 dupOutputDesc->mOutput2 == outputDesc)) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003872 sp<AudioOutputDescriptor> outputDesc2;
Eric Laurente552edb2014-03-10 17:42:56 -07003873 if (dupOutputDesc->mOutput1 == outputDesc) {
3874 outputDesc2 = dupOutputDesc->mOutput2;
3875 } else {
3876 outputDesc2 = dupOutputDesc->mOutput1;
3877 }
3878 // As all active tracks on duplicated output will be deleted,
3879 // and as they were also referenced on the other output, the reference
3880 // count for their stream type must be adjusted accordingly on
3881 // the other output.
Eric Laurent3b73df72014-03-11 09:06:29 -07003882 for (int j = 0; j < AUDIO_STREAM_CNT; j++) {
Eric Laurente552edb2014-03-10 17:42:56 -07003883 int refCount = dupOutputDesc->mRefCount[j];
Eric Laurent3b73df72014-03-11 09:06:29 -07003884 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount);
Eric Laurente552edb2014-03-10 17:42:56 -07003885 }
3886 audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
3887 ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
3888
3889 mpClientInterface->closeOutput(duplicatedOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07003890 mOutputs.removeItem(duplicatedOutput);
3891 }
3892 }
3893
Eric Laurent05b90f82014-08-27 15:32:29 -07003894 nextAudioPortGeneration();
3895
3896 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
3897 if (index >= 0) {
3898 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3899 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3900 mAudioPatches.removeItemsAt(index);
3901 mpClientInterface->onAudioPatchListUpdate();
3902 }
3903
Eric Laurente552edb2014-03-10 17:42:56 -07003904 AudioParameter param;
3905 param.add(String8("closing"), String8("true"));
3906 mpClientInterface->setParameters(output, param.toString());
3907
3908 mpClientInterface->closeOutput(output);
Eric Laurente552edb2014-03-10 17:42:56 -07003909 mOutputs.removeItem(output);
3910 mPreviousOutputs = mOutputs;
Eric Laurent05b90f82014-08-27 15:32:29 -07003911}
3912
3913void AudioPolicyManager::closeInput(audio_io_handle_t input)
3914{
3915 ALOGV("closeInput(%d)", input);
3916
3917 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
3918 if (inputDesc == NULL) {
3919 ALOGW("closeInput() unknown input %d", input);
3920 return;
3921 }
3922
Eric Laurent6a94d692014-05-20 11:18:06 -07003923 nextAudioPortGeneration();
Eric Laurent05b90f82014-08-27 15:32:29 -07003924
3925 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
3926 if (index >= 0) {
3927 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
3928 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
3929 mAudioPatches.removeItemsAt(index);
3930 mpClientInterface->onAudioPatchListUpdate();
3931 }
3932
3933 mpClientInterface->closeInput(input);
3934 mInputs.removeItem(input);
Eric Laurente552edb2014-03-10 17:42:56 -07003935}
3936
Eric Laurente0720872014-03-11 09:30:41 -07003937SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device,
Eric Laurent1f2f2232014-06-02 12:01:23 -07003938 DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs)
Eric Laurente552edb2014-03-10 17:42:56 -07003939{
3940 SortedVector<audio_io_handle_t> outputs;
3941
3942 ALOGVV("getOutputsForDevice() device %04x", device);
3943 for (size_t i = 0; i < openOutputs.size(); i++) {
3944 ALOGVV("output %d isDuplicated=%d device=%04x",
3945 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
3946 if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
3947 ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
3948 outputs.add(openOutputs.keyAt(i));
3949 }
3950 }
3951 return outputs;
3952}
3953
Eric Laurente0720872014-03-11 09:30:41 -07003954bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
Eric Laurente552edb2014-03-10 17:42:56 -07003955 SortedVector<audio_io_handle_t>& outputs2)
3956{
3957 if (outputs1.size() != outputs2.size()) {
3958 return false;
3959 }
3960 for (size_t i = 0; i < outputs1.size(); i++) {
3961 if (outputs1[i] != outputs2[i]) {
3962 return false;
3963 }
3964 }
3965 return true;
3966}
3967
Eric Laurente0720872014-03-11 09:30:41 -07003968void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy)
Eric Laurente552edb2014-03-10 17:42:56 -07003969{
3970 audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
3971 audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
3972 SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
3973 SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
3974
3975 if (!vectorsEqual(srcOutputs,dstOutputs)) {
3976 ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
3977 strategy, srcOutputs[0], dstOutputs[0]);
3978 // mute strategy while moving tracks from one output to another
3979 for (size_t i = 0; i < srcOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003980 sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07003981 if (desc->isStrategyActive(strategy)) {
3982 setStrategyMute(strategy, true, srcOutputs[i]);
3983 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
3984 }
3985 }
3986
3987 // Move effects associated to this strategy from previous output to new output
3988 if (strategy == STRATEGY_MEDIA) {
3989 audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
3990 SortedVector<audio_io_handle_t> moved;
3991 for (size_t i = 0; i < mEffects.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07003992 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i);
3993 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
3994 effectDesc->mIo != fxOutput) {
3995 if (moved.indexOf(effectDesc->mIo) < 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07003996 ALOGV("checkOutputForStrategy() moving effect %d to output %d",
3997 mEffects.keyAt(i), fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07003998 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo,
Eric Laurente552edb2014-03-10 17:42:56 -07003999 fxOutput);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004000 moved.add(effectDesc->mIo);
Eric Laurente552edb2014-03-10 17:42:56 -07004001 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07004002 effectDesc->mIo = fxOutput;
Eric Laurente552edb2014-03-10 17:42:56 -07004003 }
4004 }
4005 }
4006 // Move tracks associated to this strategy from previous output to new output
Eric Laurent3b73df72014-03-11 09:06:29 -07004007 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004008 if (i == AUDIO_STREAM_PATCH) {
4009 continue;
4010 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004011 if (getStrategy((audio_stream_type_t)i) == strategy) {
4012 mpClientInterface->invalidateStream((audio_stream_type_t)i);
Eric Laurente552edb2014-03-10 17:42:56 -07004013 }
4014 }
4015 }
4016}
4017
Eric Laurente0720872014-03-11 09:30:41 -07004018void AudioPolicyManager::checkOutputForAllStrategies()
Eric Laurente552edb2014-03-10 17:42:56 -07004019{
Jon Eklund966095e2014-09-09 15:39:49 -05004020 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4021 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004022 checkOutputForStrategy(STRATEGY_PHONE);
Jon Eklund966095e2014-09-09 15:39:49 -05004023 if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
4024 checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
Eric Laurente552edb2014-03-10 17:42:56 -07004025 checkOutputForStrategy(STRATEGY_SONIFICATION);
4026 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004027 checkOutputForStrategy(STRATEGY_ACCESSIBILITY);
Eric Laurente552edb2014-03-10 17:42:56 -07004028 checkOutputForStrategy(STRATEGY_MEDIA);
4029 checkOutputForStrategy(STRATEGY_DTMF);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004030 checkOutputForStrategy(STRATEGY_REROUTING);
Eric Laurente552edb2014-03-10 17:42:56 -07004031}
4032
Eric Laurente0720872014-03-11 09:30:41 -07004033audio_io_handle_t AudioPolicyManager::getA2dpOutput()
Eric Laurente552edb2014-03-10 17:42:56 -07004034{
Eric Laurente552edb2014-03-10 17:42:56 -07004035 for (size_t i = 0; i < mOutputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004036 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07004037 if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
4038 return mOutputs.keyAt(i);
4039 }
4040 }
4041
4042 return 0;
4043}
4044
Eric Laurente0720872014-03-11 09:30:41 -07004045void AudioPolicyManager::checkA2dpSuspend()
Eric Laurente552edb2014-03-10 17:42:56 -07004046{
Eric Laurente552edb2014-03-10 17:42:56 -07004047 audio_io_handle_t a2dpOutput = getA2dpOutput();
4048 if (a2dpOutput == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004049 mA2dpSuspended = false;
Eric Laurente552edb2014-03-10 17:42:56 -07004050 return;
4051 }
4052
Eric Laurent3a4311c2014-03-17 12:00:47 -07004053 bool isScoConnected =
Eric Laurentddbc6652014-11-13 15:13:44 -08004054 ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET &
4055 ~AUDIO_DEVICE_BIT_IN) != 0) ||
4056 ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0);
Eric Laurente552edb2014-03-10 17:42:56 -07004057 // suspend A2DP output if:
4058 // (NOT already suspended) &&
4059 // ((SCO device is connected &&
4060 // (forced usage for communication || for record is SCO))) ||
4061 // (phone state is ringing || in call)
4062 //
4063 // restore A2DP output if:
4064 // (Already suspended) &&
4065 // ((SCO device is NOT connected ||
4066 // (forced usage NOT for communication && NOT for record is SCO))) &&
4067 // (phone state is NOT ringing && NOT in call)
4068 //
4069 if (mA2dpSuspended) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004070 if ((!isScoConnected ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004071 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) &&
4072 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) &&
4073 ((mPhoneState != AUDIO_MODE_IN_CALL) &&
4074 (mPhoneState != AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004075
4076 mpClientInterface->restoreOutput(a2dpOutput);
4077 mA2dpSuspended = false;
4078 }
4079 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004080 if ((isScoConnected &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004081 ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
4082 (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) ||
4083 ((mPhoneState == AUDIO_MODE_IN_CALL) ||
4084 (mPhoneState == AUDIO_MODE_RINGTONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07004085
4086 mpClientInterface->suspendOutput(a2dpOutput);
4087 mA2dpSuspended = true;
4088 }
4089 }
4090}
4091
Eric Laurent1c333e22014-05-20 10:48:17 -07004092audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache)
Eric Laurente552edb2014-03-10 17:42:56 -07004093{
4094 audio_devices_t device = AUDIO_DEVICE_NONE;
4095
Eric Laurent1f2f2232014-06-02 12:01:23 -07004096 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004097
4098 ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4099 if (index >= 0) {
4100 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4101 if (patchDesc->mUid != mUidCached) {
4102 ALOGV("getNewOutputDevice() device %08x forced by patch %d",
4103 outputDesc->device(), outputDesc->mPatchHandle);
4104 return outputDesc->device();
4105 }
4106 }
4107
Eric Laurente552edb2014-03-10 17:42:56 -07004108 // check the following by order of priority to request a routing change if necessary:
Jon Eklund966095e2014-09-09 15:39:49 -05004109 // 1: the strategy enforced audible is active and enforced on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004110 // use device for strategy enforced audible
4111 // 2: we are in call or the strategy phone is active on the output:
4112 // use device for strategy phone
Jon Eklund966095e2014-09-09 15:39:49 -05004113 // 3: the strategy for enforced audible is active but not enforced on the output:
4114 // use the device for strategy enforced audible
4115 // 4: the strategy sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004116 // use device for strategy sonification
Jon Eklund966095e2014-09-09 15:39:49 -05004117 // 5: the strategy "respectful" sonification is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004118 // use device for strategy "respectful" sonification
Eric Laurent223fd5c2014-11-11 13:43:36 -08004119 // 6: the strategy accessibility is active on the output:
4120 // use device for strategy accessibility
4121 // 7: the strategy media is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004122 // use device for strategy media
Eric Laurent223fd5c2014-11-11 13:43:36 -08004123 // 8: the strategy DTMF is active on the output:
Eric Laurente552edb2014-03-10 17:42:56 -07004124 // use device for strategy DTMF
Eric Laurent223fd5c2014-11-11 13:43:36 -08004125 // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004126 // use device for strategy t-t-s
Jon Eklund966095e2014-09-09 15:39:49 -05004127 if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) &&
4128 mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Eric Laurente552edb2014-03-10 17:42:56 -07004129 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
4130 } else if (isInCall() ||
4131 outputDesc->isStrategyActive(STRATEGY_PHONE)) {
4132 device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
Jon Eklund966095e2014-09-09 15:39:49 -05004133 } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
4134 device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004135 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
4136 device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
4137 } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
4138 device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004139 } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) {
4140 device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004141 } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
4142 device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
4143 } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
4144 device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004145 } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
4146 device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
Eric Laurent223fd5c2014-11-11 13:43:36 -08004147 } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) {
4148 device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
Eric Laurente552edb2014-03-10 17:42:56 -07004149 }
4150
Eric Laurent1c333e22014-05-20 10:48:17 -07004151 ALOGV("getNewOutputDevice() selected device %x", device);
4152 return device;
4153}
4154
4155audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input)
4156{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004157 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004158
4159 ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4160 if (index >= 0) {
4161 sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4162 if (patchDesc->mUid != mUidCached) {
4163 ALOGV("getNewInputDevice() device %08x forced by patch %d",
4164 inputDesc->mDevice, inputDesc->mPatchHandle);
4165 return inputDesc->mDevice;
4166 }
4167 }
4168
Eric Laurent1c333e22014-05-20 10:48:17 -07004169 audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource);
4170
4171 ALOGV("getNewInputDevice() selected device %x", device);
Eric Laurente552edb2014-03-10 17:42:56 -07004172 return device;
4173}
4174
Eric Laurente0720872014-03-11 09:30:41 -07004175uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004176 return (uint32_t)getStrategy(stream);
4177}
4178
Eric Laurente0720872014-03-11 09:30:41 -07004179audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004180 // By checking the range of stream before calling getStrategy, we avoid
4181 // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE
4182 // and then return STRATEGY_MEDIA, but we want to return the empty set.
Eric Laurent223fd5c2014-11-11 13:43:36 -08004183 if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004184 return AUDIO_DEVICE_NONE;
4185 }
4186 audio_devices_t devices;
4187 AudioPolicyManager::routing_strategy strategy = getStrategy(stream);
4188 devices = getDeviceForStrategy(strategy, true /*fromCache*/);
4189 SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
4190 for (size_t i = 0; i < outputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004191 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
Eric Laurent6a94d692014-05-20 11:18:06 -07004192 if (outputDesc->isStrategyActive(strategy)) {
4193 devices = outputDesc->device();
4194 break;
4195 }
Eric Laurente552edb2014-03-10 17:42:56 -07004196 }
Jon Eklund11c9fb12014-06-23 14:47:03 -05004197
4198 /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it
4199 and doesn't really need to.*/
4200 if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
4201 devices |= AUDIO_DEVICE_OUT_SPEAKER;
4202 devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE;
4203 }
4204
Eric Laurente552edb2014-03-10 17:42:56 -07004205 return devices;
4206}
4207
Eric Laurente0720872014-03-11 09:30:41 -07004208AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy(
Eric Laurent3b73df72014-03-11 09:06:29 -07004209 audio_stream_type_t stream) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08004210
4211 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH");
4212
Eric Laurente552edb2014-03-10 17:42:56 -07004213 // stream to strategy mapping
4214 switch (stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004215 case AUDIO_STREAM_VOICE_CALL:
4216 case AUDIO_STREAM_BLUETOOTH_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004217 return STRATEGY_PHONE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004218 case AUDIO_STREAM_RING:
4219 case AUDIO_STREAM_ALARM:
Eric Laurente552edb2014-03-10 17:42:56 -07004220 return STRATEGY_SONIFICATION;
Eric Laurent3b73df72014-03-11 09:06:29 -07004221 case AUDIO_STREAM_NOTIFICATION:
Eric Laurente552edb2014-03-10 17:42:56 -07004222 return STRATEGY_SONIFICATION_RESPECTFUL;
Eric Laurent3b73df72014-03-11 09:06:29 -07004223 case AUDIO_STREAM_DTMF:
Eric Laurente552edb2014-03-10 17:42:56 -07004224 return STRATEGY_DTMF;
4225 default:
Eric Laurent223fd5c2014-11-11 13:43:36 -08004226 ALOGE("unknown stream type %d", stream);
Eric Laurent3b73df72014-03-11 09:06:29 -07004227 case AUDIO_STREAM_SYSTEM:
Eric Laurente552edb2014-03-10 17:42:56 -07004228 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
4229 // while key clicks are played produces a poor result
Eric Laurent3b73df72014-03-11 09:06:29 -07004230 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004231 return STRATEGY_MEDIA;
Eric Laurent3b73df72014-03-11 09:06:29 -07004232 case AUDIO_STREAM_ENFORCED_AUDIBLE:
Eric Laurente552edb2014-03-10 17:42:56 -07004233 return STRATEGY_ENFORCED_AUDIBLE;
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004234 case AUDIO_STREAM_TTS:
4235 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
Eric Laurent223fd5c2014-11-11 13:43:36 -08004236 case AUDIO_STREAM_ACCESSIBILITY:
4237 return STRATEGY_ACCESSIBILITY;
4238 case AUDIO_STREAM_REROUTING:
4239 return STRATEGY_REROUTING;
Eric Laurente552edb2014-03-10 17:42:56 -07004240 }
4241}
4242
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004243uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) {
4244 // flags to strategy mapping
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004245 if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) {
4246 return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
4247 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004248 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
4249 return (uint32_t) STRATEGY_ENFORCED_AUDIBLE;
4250 }
4251
4252 // usage to strategy mapping
4253 switch (attr->usage) {
Eric Laurent29e6cec2014-11-13 18:17:55 -08004254 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
4255 if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) {
4256 return (uint32_t) STRATEGY_SONIFICATION;
4257 }
4258 if (isInCall()) {
4259 return (uint32_t) STRATEGY_PHONE;
4260 }
Eric Laurent0f78eab2014-11-25 11:01:34 -08004261 return (uint32_t) STRATEGY_ACCESSIBILITY;
Eric Laurent29e6cec2014-11-13 18:17:55 -08004262
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004263 case AUDIO_USAGE_MEDIA:
4264 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07004265 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
4266 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
4267 return (uint32_t) STRATEGY_MEDIA;
4268
4269 case AUDIO_USAGE_VOICE_COMMUNICATION:
4270 return (uint32_t) STRATEGY_PHONE;
4271
4272 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
4273 return (uint32_t) STRATEGY_DTMF;
4274
4275 case AUDIO_USAGE_ALARM:
4276 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
4277 return (uint32_t) STRATEGY_SONIFICATION;
4278
4279 case AUDIO_USAGE_NOTIFICATION:
4280 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
4281 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
4282 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
4283 case AUDIO_USAGE_NOTIFICATION_EVENT:
4284 return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL;
4285
4286 case AUDIO_USAGE_UNKNOWN:
4287 default:
4288 return (uint32_t) STRATEGY_MEDIA;
4289 }
4290}
4291
Eric Laurente0720872014-03-11 09:30:41 -07004292void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) {
Eric Laurente552edb2014-03-10 17:42:56 -07004293 switch(stream) {
Eric Laurent3b73df72014-03-11 09:06:29 -07004294 case AUDIO_STREAM_MUSIC:
Eric Laurente552edb2014-03-10 17:42:56 -07004295 checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
4296 updateDevicesAndOutputs();
4297 break;
4298 default:
4299 break;
4300 }
4301}
4302
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004303bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) {
4304 for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) {
4305 if (s == (size_t) streamToIgnore) {
4306 continue;
4307 }
4308 for (size_t i = 0; i < mOutputs.size(); i++) {
4309 const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i);
4310 if (outputDesc->mRefCount[s] != 0) {
4311 return true;
4312 }
4313 }
4314 }
4315 return false;
4316}
4317
4318uint32_t AudioPolicyManager::handleEventForBeacon(int event) {
4319 switch(event) {
4320 case STARTING_OUTPUT:
4321 mBeaconMuteRefCount++;
4322 break;
4323 case STOPPING_OUTPUT:
4324 if (mBeaconMuteRefCount > 0) {
4325 mBeaconMuteRefCount--;
4326 }
4327 break;
4328 case STARTING_BEACON:
4329 mBeaconPlayingRefCount++;
4330 break;
4331 case STOPPING_BEACON:
4332 if (mBeaconPlayingRefCount > 0) {
4333 mBeaconPlayingRefCount--;
4334 }
4335 break;
4336 }
4337
4338 if (mBeaconMuteRefCount > 0) {
4339 // any playback causes beacon to be muted
4340 return setBeaconMute(true);
4341 } else {
4342 // no other playback: unmute when beacon starts playing, mute when it stops
4343 return setBeaconMute(mBeaconPlayingRefCount == 0);
4344 }
4345}
4346
4347uint32_t AudioPolicyManager::setBeaconMute(bool mute) {
4348 ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d",
4349 mute, mBeaconMuteRefCount, mBeaconPlayingRefCount);
4350 // keep track of muted state to avoid repeating mute/unmute operations
4351 if (mBeaconMuted != mute) {
4352 // mute/unmute AUDIO_STREAM_TTS on all outputs
4353 ALOGV("\t muting %d", mute);
4354 uint32_t maxLatency = 0;
4355 for (size_t i = 0; i < mOutputs.size(); i++) {
4356 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4357 setStreamMute(AUDIO_STREAM_TTS, mute/*on*/,
4358 desc->mIoHandle,
4359 0 /*delay*/, AUDIO_DEVICE_NONE);
4360 const uint32_t latency = desc->latency() * 2;
4361 if (latency > maxLatency) {
4362 maxLatency = latency;
4363 }
4364 }
4365 mBeaconMuted = mute;
4366 return maxLatency;
4367 }
4368 return 0;
4369}
4370
Eric Laurente0720872014-03-11 09:30:41 -07004371audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07004372 bool fromCache)
4373{
4374 uint32_t device = AUDIO_DEVICE_NONE;
4375
4376 if (fromCache) {
4377 ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
4378 strategy, mDeviceForStrategy[strategy]);
4379 return mDeviceForStrategy[strategy];
4380 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004381 audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004382 switch (strategy) {
4383
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07004384 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
4385 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4386 if (!device) {
4387 ALOGE("getDeviceForStrategy() no device found for "\
4388 "STRATEGY_TRANSMITTED_THROUGH_SPEAKER");
4389 }
4390 break;
4391
Eric Laurente552edb2014-03-10 17:42:56 -07004392 case STRATEGY_SONIFICATION_RESPECTFUL:
4393 if (isInCall()) {
4394 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07004395 } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
Eric Laurente552edb2014-03-10 17:42:56 -07004396 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
4397 // while media is playing on a remote device, use the the sonification behavior.
4398 // Note that we test this usecase before testing if media is playing because
4399 // the isStreamActive() method only informs about the activity of a stream, not
4400 // if it's for local playback. Note also that we use the same delay between both tests
4401 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004402 //user "safe" speaker if available instead of normal speaker to avoid triggering
4403 //other acoustic safety mechanisms for notification
4404 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4405 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurent3b73df72014-03-11 09:06:29 -07004406 } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004407 // while media is playing (or has recently played), use the same device
4408 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4409 } else {
4410 // when media is not playing anymore, fall back on the sonification behavior
4411 device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
Jon Eklund11c9fb12014-06-23 14:47:03 -05004412 //user "safe" speaker if available instead of normal speaker to avoid triggering
4413 //other acoustic safety mechanisms for notification
4414 if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE))
4415 device = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
Eric Laurente552edb2014-03-10 17:42:56 -07004416 }
4417
4418 break;
4419
4420 case STRATEGY_DTMF:
4421 if (!isInCall()) {
4422 // when off call, DTMF strategy follows the same rules as MEDIA strategy
4423 device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
4424 break;
4425 }
4426 // when in call, DTMF and PHONE strategies follow the same rules
4427 // FALL THROUGH
4428
4429 case STRATEGY_PHONE:
Eric Laurentc2730ba2014-07-20 15:47:07 -07004430 // Force use of only devices on primary output if:
4431 // - in call AND
4432 // - cannot route from voice call RX OR
4433 // - audio HAL version is < 3.0 and TX device is on the primary HW module
4434 if (mPhoneState == AUDIO_MODE_IN_CALL) {
4435 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
4436 sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
4437 if (((mAvailableInputDevices.types() &
4438 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
4439 (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Marco Nelissen961ec212014-08-25 15:58:39 -07004440 (hwOutputDesc->getAudioPort()->mModule->mHalVersion <
Eric Laurentc2730ba2014-07-20 15:47:07 -07004441 AUDIO_DEVICE_API_VERSION_3_0))) {
4442 availableOutputDeviceTypes = availablePrimaryOutputDevices();
4443 }
4444 }
Eric Laurente552edb2014-03-10 17:42:56 -07004445 // for phone strategy, we first consider the forced use and then the available devices by order
4446 // of priority
Eric Laurent3b73df72014-03-11 09:06:29 -07004447 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
4448 case AUDIO_POLICY_FORCE_BT_SCO:
Eric Laurente552edb2014-03-10 17:42:56 -07004449 if (!isInCall() || strategy != STRATEGY_DTMF) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004450 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
Eric Laurente552edb2014-03-10 17:42:56 -07004451 if (device) break;
4452 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004453 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004454 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004455 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
Eric Laurente552edb2014-03-10 17:42:56 -07004456 if (device) break;
4457 // if SCO device is requested but no SCO device is available, fall back to default case
4458 // FALL THROUGH
4459
4460 default: // FORCE_NONE
4461 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
Eric Laurent3a4311c2014-03-17 12:00:47 -07004462 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004463 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004464 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004465 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004466 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004467 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004468 if (device) break;
4469 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004470 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004471 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004472 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004473 if (device) break;
Eric Laurentc2730ba2014-07-20 15:47:07 -07004474 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
4475 if (device) break;
Eric Laurent3b73df72014-03-11 09:06:29 -07004476 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004477 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004478 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004479 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004480 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004481 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004482 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004483 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004484 if (device) break;
4485 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07004486 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE;
Eric Laurente552edb2014-03-10 17:42:56 -07004487 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004488 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004489 if (device == AUDIO_DEVICE_NONE) {
4490 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
4491 }
4492 break;
4493
Eric Laurent3b73df72014-03-11 09:06:29 -07004494 case AUDIO_POLICY_FORCE_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07004495 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
4496 // A2DP speaker when forcing to speaker output
Eric Laurent3a4311c2014-03-17 12:00:47 -07004497 if (!isInCall() &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004498 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004499 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004500 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004501 if (device) break;
4502 }
Eric Laurent3b73df72014-03-11 09:06:29 -07004503 if (mPhoneState != AUDIO_MODE_IN_CALL) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004504 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004505 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004506 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004507 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004508 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004509 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004510 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004511 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004512 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004513 if (device) break;
4514 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004515 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4516 if (device) break;
Eric Laurent3a4311c2014-03-17 12:00:47 -07004517 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004518 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004519 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004520 if (device == AUDIO_DEVICE_NONE) {
4521 ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
4522 }
4523 break;
4524 }
4525 break;
4526
4527 case STRATEGY_SONIFICATION:
4528
4529 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
4530 // handleIncallSonification().
4531 if (isInCall()) {
4532 device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
4533 break;
4534 }
4535 // FALL THROUGH
4536
4537 case STRATEGY_ENFORCED_AUDIBLE:
4538 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
4539 // except:
4540 // - when in call where it doesn't default to STRATEGY_PHONE behavior
4541 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
4542
4543 if ((strategy == STRATEGY_SONIFICATION) ||
Eric Laurent3b73df72014-03-11 09:06:29 -07004544 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004545 device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004546 if (device == AUDIO_DEVICE_NONE) {
4547 ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
4548 }
4549 }
4550 // The second device used for sonification is the same as the device used by media strategy
4551 // FALL THROUGH
4552
Eric Laurent223fd5c2014-11-11 13:43:36 -08004553 // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now
4554 case STRATEGY_ACCESSIBILITY:
Eric Laurent066ceec2014-11-25 12:35:01 -08004555 if (strategy == STRATEGY_ACCESSIBILITY) {
4556 // do not route accessibility prompts to a digital output currently configured with a
4557 // compressed format as they would likely not be mixed and dropped.
4558 for (size_t i = 0; i < mOutputs.size(); i++) {
4559 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i);
4560 audio_devices_t devices = desc->device() &
4561 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
4562 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
4563 devices != AUDIO_DEVICE_NONE) {
4564 availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices;
4565 }
4566 }
4567 }
4568 // FALL THROUGH
4569
Eric Laurent223fd5c2014-11-11 13:43:36 -08004570 case STRATEGY_REROUTING:
Eric Laurente552edb2014-03-10 17:42:56 -07004571 case STRATEGY_MEDIA: {
4572 uint32_t device2 = AUDIO_DEVICE_NONE;
4573 if (strategy != STRATEGY_SONIFICATION) {
4574 // no sonification on remote submix (e.g. WFD)
Eric Laurent275e8e92014-11-30 15:14:47 -08004575 if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
4576 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
4577 }
Eric Laurente552edb2014-03-10 17:42:56 -07004578 }
4579 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004580 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Eric Laurent29e6cec2014-11-13 18:17:55 -08004581 (getA2dpOutput() != 0)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004582 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
Eric Laurente552edb2014-03-10 17:42:56 -07004583 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004584 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
Eric Laurente552edb2014-03-10 17:42:56 -07004585 }
4586 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004587 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004588 }
4589 }
Hochi Huang327cb702014-09-21 09:47:31 +08004590 if ((device2 == AUDIO_DEVICE_NONE) &&
4591 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
4592 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
4593 }
Eric Laurente552edb2014-03-10 17:42:56 -07004594 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004595 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
Eric Laurente552edb2014-03-10 17:42:56 -07004596 }
Jon Eklundac29afa2014-07-28 16:06:06 -05004597 if ((device2 == AUDIO_DEVICE_NONE)) {
4598 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE;
4599 }
Eric Laurente552edb2014-03-10 17:42:56 -07004600 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004601 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004602 }
4603 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004604 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY;
Eric Laurente552edb2014-03-10 17:42:56 -07004605 }
4606 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004607 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE;
Eric Laurente552edb2014-03-10 17:42:56 -07004608 }
4609 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004610 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004611 }
4612 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
4613 // no sonification on aux digital (e.g. HDMI)
Eric Laurent3a4311c2014-03-17 12:00:47 -07004614 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL;
Eric Laurente552edb2014-03-10 17:42:56 -07004615 }
4616 if ((device2 == AUDIO_DEVICE_NONE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07004617 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004618 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
Eric Laurente552edb2014-03-10 17:42:56 -07004619 }
4620 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07004621 device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurente552edb2014-03-10 17:42:56 -07004622 }
Jungshik Jang839e4f32014-06-26 17:23:40 +09004623 int device3 = AUDIO_DEVICE_NONE;
4624 if (strategy == STRATEGY_MEDIA) {
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004625 // ARC, SPDIF and AUX_LINE can co-exist with others.
Jungshik Jang0c943092014-07-08 22:11:24 +09004626 device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC;
4627 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF);
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004628 device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE);
Jungshik Jang839e4f32014-06-26 17:23:40 +09004629 }
Eric Laurente552edb2014-03-10 17:42:56 -07004630
Jungshik Jang839e4f32014-06-26 17:23:40 +09004631 device2 |= device3;
Eric Laurente552edb2014-03-10 17:42:56 -07004632 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
4633 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
4634 device |= device2;
Jungshik Jang839e4f32014-06-26 17:23:40 +09004635
Jungshik Jang7b24ee32014-07-15 19:38:42 +09004636 // If hdmi system audio mode is on, remove speaker out of output list.
4637 if ((strategy == STRATEGY_MEDIA) &&
4638 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
4639 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
4640 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
4641 }
4642
Eric Laurente552edb2014-03-10 17:42:56 -07004643 if (device) break;
Eric Laurent1c333e22014-05-20 10:48:17 -07004644 device = mDefaultOutputDevice->mDeviceType;
Eric Laurente552edb2014-03-10 17:42:56 -07004645 if (device == AUDIO_DEVICE_NONE) {
4646 ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
4647 }
4648 } break;
4649
4650 default:
4651 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
4652 break;
4653 }
4654
4655 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
4656 return device;
4657}
4658
Eric Laurente0720872014-03-11 09:30:41 -07004659void AudioPolicyManager::updateDevicesAndOutputs()
Eric Laurente552edb2014-03-10 17:42:56 -07004660{
4661 for (int i = 0; i < NUM_STRATEGIES; i++) {
4662 mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
4663 }
4664 mPreviousOutputs = mOutputs;
4665}
4666
Eric Laurent1f2f2232014-06-02 12:01:23 -07004667uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07004668 audio_devices_t prevDevice,
4669 uint32_t delayMs)
4670{
4671 // mute/unmute strategies using an incompatible device combination
4672 // if muting, wait for the audio in pcm buffer to be drained before proceeding
4673 // if unmuting, unmute only after the specified delay
4674 if (outputDesc->isDuplicated()) {
4675 return 0;
4676 }
4677
4678 uint32_t muteWaitMs = 0;
4679 audio_devices_t device = outputDesc->device();
Eric Laurent3b73df72014-03-11 09:06:29 -07004680 bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2);
Eric Laurente552edb2014-03-10 17:42:56 -07004681
4682 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4683 audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
Eric Laurent31551f82014-10-10 18:21:56 -07004684 curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types();
Eric Laurente552edb2014-03-10 17:42:56 -07004685 bool mute = shouldMute && (curDevice & device) && (curDevice != device);
4686 bool doMute = false;
4687
4688 if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
4689 doMute = true;
4690 outputDesc->mStrategyMutedByDevice[i] = true;
4691 } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
4692 doMute = true;
4693 outputDesc->mStrategyMutedByDevice[i] = false;
4694 }
Eric Laurent99401132014-05-07 19:48:15 -07004695 if (doMute) {
Eric Laurente552edb2014-03-10 17:42:56 -07004696 for (size_t j = 0; j < mOutputs.size(); j++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07004697 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j);
Eric Laurente552edb2014-03-10 17:42:56 -07004698 // skip output if it does not share any device with current output
4699 if ((desc->supportedDevices() & outputDesc->supportedDevices())
4700 == AUDIO_DEVICE_NONE) {
4701 continue;
4702 }
4703 audio_io_handle_t curOutput = mOutputs.keyAt(j);
4704 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
4705 mute ? "muting" : "unmuting", i, curDevice, curOutput);
4706 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
4707 if (desc->isStrategyActive((routing_strategy)i)) {
Eric Laurent99401132014-05-07 19:48:15 -07004708 if (mute) {
4709 // FIXME: should not need to double latency if volume could be applied
4710 // immediately by the audioflinger mixer. We must account for the delay
4711 // between now and the next time the audioflinger thread for this output
4712 // will process a buffer (which corresponds to one buffer size,
4713 // usually 1/2 or 1/4 of the latency).
4714 if (muteWaitMs < desc->latency() * 2) {
4715 muteWaitMs = desc->latency() * 2;
Eric Laurente552edb2014-03-10 17:42:56 -07004716 }
4717 }
4718 }
4719 }
4720 }
4721 }
4722
Eric Laurent99401132014-05-07 19:48:15 -07004723 // temporary mute output if device selection changes to avoid volume bursts due to
4724 // different per device volumes
4725 if (outputDesc->isActive() && (device != prevDevice)) {
4726 if (muteWaitMs < outputDesc->latency() * 2) {
4727 muteWaitMs = outputDesc->latency() * 2;
4728 }
4729 for (size_t i = 0; i < NUM_STRATEGIES; i++) {
4730 if (outputDesc->isStrategyActive((routing_strategy)i)) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004731 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
Eric Laurent99401132014-05-07 19:48:15 -07004732 // do tempMute unmute after twice the mute wait time
Eric Laurent1c333e22014-05-20 10:48:17 -07004733 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
Eric Laurent99401132014-05-07 19:48:15 -07004734 muteWaitMs *2, device);
4735 }
4736 }
4737 }
4738
Eric Laurente552edb2014-03-10 17:42:56 -07004739 // wait for the PCM output buffers to empty before proceeding with the rest of the command
4740 if (muteWaitMs > delayMs) {
4741 muteWaitMs -= delayMs;
4742 usleep(muteWaitMs * 1000);
4743 return muteWaitMs;
4744 }
4745 return 0;
4746}
4747
Eric Laurente0720872014-03-11 09:30:41 -07004748uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07004749 audio_devices_t device,
4750 bool force,
Eric Laurent6a94d692014-05-20 11:18:06 -07004751 int delayMs,
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004752 audio_patch_handle_t *patchHandle,
4753 const char* address)
Eric Laurente552edb2014-03-10 17:42:56 -07004754{
4755 ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
Eric Laurent1f2f2232014-06-02 12:01:23 -07004756 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07004757 AudioParameter param;
4758 uint32_t muteWaitMs;
4759
4760 if (outputDesc->isDuplicated()) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004761 muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs);
4762 muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07004763 return muteWaitMs;
4764 }
4765 // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
4766 // output profile
4767 if ((device != AUDIO_DEVICE_NONE) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07004768 ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) {
Eric Laurente552edb2014-03-10 17:42:56 -07004769 return 0;
4770 }
4771
4772 // filter devices according to output selected
Eric Laurent3a4311c2014-03-17 12:00:47 -07004773 device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07004774
4775 audio_devices_t prevDevice = outputDesc->mDevice;
4776
4777 ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
4778
4779 if (device != AUDIO_DEVICE_NONE) {
4780 outputDesc->mDevice = device;
4781 }
4782 muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
4783
4784 // Do not change the routing if:
Eric Laurentb80a2a82014-10-27 16:07:59 -07004785 // the requested device is AUDIO_DEVICE_NONE
4786 // OR the requested device is the same as current device
4787 // AND force is not specified
4788 // AND the output is connected by a valid audio patch.
Eric Laurente552edb2014-03-10 17:42:56 -07004789 // Doing this check here allows the caller to call setOutputDevice() without conditions
Eric Laurentb80a2a82014-10-27 16:07:59 -07004790 if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force &&
4791 outputDesc->mPatchHandle != 0) {
4792 ALOGV("setOutputDevice() setting same device %04x or null device for output %d",
4793 device, output);
Eric Laurente552edb2014-03-10 17:42:56 -07004794 return muteWaitMs;
4795 }
4796
4797 ALOGV("setOutputDevice() changing device");
Eric Laurent1c333e22014-05-20 10:48:17 -07004798
Eric Laurente552edb2014-03-10 17:42:56 -07004799 // do the routing
Eric Laurent1c333e22014-05-20 10:48:17 -07004800 if (device == AUDIO_DEVICE_NONE) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004801 resetOutputDevice(output, delayMs, NULL);
Eric Laurent1c333e22014-05-20 10:48:17 -07004802 } else {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07004803 DeviceVector deviceList = (address == NULL) ?
4804 mAvailableOutputDevices.getDevicesFromType(device)
4805 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address));
Eric Laurent1c333e22014-05-20 10:48:17 -07004806 if (!deviceList.isEmpty()) {
4807 struct audio_patch patch;
4808 outputDesc->toAudioPortConfig(&patch.sources[0]);
4809 patch.num_sources = 1;
4810 patch.num_sinks = 0;
4811 for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) {
4812 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004813 patch.num_sinks++;
4814 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004815 ssize_t index;
4816 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4817 index = mAudioPatches.indexOfKey(*patchHandle);
4818 } else {
4819 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4820 }
4821 sp< AudioPatch> patchDesc;
4822 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4823 if (index >= 0) {
4824 patchDesc = mAudioPatches.valueAt(index);
4825 afPatchHandle = patchDesc->mAfPatchHandle;
4826 }
4827
Eric Laurent1c333e22014-05-20 10:48:17 -07004828 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004829 &afPatchHandle,
4830 delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004831 ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d"
4832 "num_sources %d num_sinks %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004833 status, afPatchHandle, patch.num_sources, patch.num_sinks);
Eric Laurent1c333e22014-05-20 10:48:17 -07004834 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004835 if (index < 0) {
4836 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
4837 &patch, mUidCached);
4838 addAudioPatch(patchDesc->mHandle, patchDesc);
4839 } else {
4840 patchDesc->mPatch = patch;
4841 }
4842 patchDesc->mAfPatchHandle = afPatchHandle;
4843 patchDesc->mUid = mUidCached;
4844 if (patchHandle) {
4845 *patchHandle = patchDesc->mHandle;
4846 }
4847 outputDesc->mPatchHandle = patchDesc->mHandle;
4848 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004849 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004850 }
4851 }
bryant_liuf5e7e792014-08-19 20:07:05 +08004852
4853 // inform all input as well
4854 for (size_t i = 0; i < mInputs.size(); i++) {
4855 const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i);
4856 if (!isVirtualInputDevice(inputDescriptor->mDevice)) {
4857 AudioParameter inputCmd = AudioParameter();
4858 ALOGV("%s: inform input %d of device:%d", __func__,
4859 inputDescriptor->mIoHandle, device);
4860 inputCmd.addInt(String8(AudioParameter::keyRouting),device);
4861 mpClientInterface->setParameters(inputDescriptor->mIoHandle,
4862 inputCmd.toString(),
4863 delayMs);
4864 }
4865 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004866 }
Eric Laurente552edb2014-03-10 17:42:56 -07004867
4868 // update stream volumes according to new device
4869 applyStreamVolumes(output, device, delayMs);
4870
4871 return muteWaitMs;
4872}
4873
Eric Laurent1c333e22014-05-20 10:48:17 -07004874status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output,
Eric Laurent6a94d692014-05-20 11:18:06 -07004875 int delayMs,
4876 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004877{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004878 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurent6a94d692014-05-20 11:18:06 -07004879 ssize_t index;
4880 if (patchHandle) {
4881 index = mAudioPatches.indexOfKey(*patchHandle);
4882 } else {
4883 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle);
4884 }
4885 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004886 return INVALID_OPERATION;
4887 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004888 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4889 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs);
Eric Laurent1c333e22014-05-20 10:48:17 -07004890 ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status);
4891 outputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004892 removeAudioPatch(patchDesc->mHandle);
4893 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004894 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004895 return status;
4896}
4897
4898status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input,
4899 audio_devices_t device,
Eric Laurent6a94d692014-05-20 11:18:06 -07004900 bool force,
4901 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004902{
4903 status_t status = NO_ERROR;
4904
Eric Laurent1f2f2232014-06-02 12:01:23 -07004905 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent1c333e22014-05-20 10:48:17 -07004906 if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) {
4907 inputDesc->mDevice = device;
4908
4909 DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device);
4910 if (!deviceList.isEmpty()) {
4911 struct audio_patch patch;
4912 inputDesc->toAudioPortConfig(&patch.sinks[0]);
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004913 // AUDIO_SOURCE_HOTWORD is for internal use only:
4914 // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07004915 if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD &&
4916 !inputDesc->mIsSoundTrigger) {
Eric Laurentdaf92cc2014-07-22 15:36:10 -07004917 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION;
4918 }
Eric Laurent1c333e22014-05-20 10:48:17 -07004919 patch.num_sinks = 1;
4920 //only one input device for now
4921 deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]);
Eric Laurent1c333e22014-05-20 10:48:17 -07004922 patch.num_sources = 1;
Eric Laurent6a94d692014-05-20 11:18:06 -07004923 ssize_t index;
4924 if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) {
4925 index = mAudioPatches.indexOfKey(*patchHandle);
4926 } else {
4927 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4928 }
4929 sp< AudioPatch> patchDesc;
4930 audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE;
4931 if (index >= 0) {
4932 patchDesc = mAudioPatches.valueAt(index);
4933 afPatchHandle = patchDesc->mAfPatchHandle;
4934 }
4935
Eric Laurent1c333e22014-05-20 10:48:17 -07004936 status_t status = mpClientInterface->createAudioPatch(&patch,
Eric Laurent6a94d692014-05-20 11:18:06 -07004937 &afPatchHandle,
Eric Laurent1c333e22014-05-20 10:48:17 -07004938 0);
4939 ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d",
Eric Laurent6a94d692014-05-20 11:18:06 -07004940 status, afPatchHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07004941 if (status == NO_ERROR) {
Eric Laurent6a94d692014-05-20 11:18:06 -07004942 if (index < 0) {
4943 patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(),
4944 &patch, mUidCached);
4945 addAudioPatch(patchDesc->mHandle, patchDesc);
4946 } else {
4947 patchDesc->mPatch = patch;
4948 }
4949 patchDesc->mAfPatchHandle = afPatchHandle;
4950 patchDesc->mUid = mUidCached;
4951 if (patchHandle) {
4952 *patchHandle = patchDesc->mHandle;
4953 }
4954 inputDesc->mPatchHandle = patchDesc->mHandle;
4955 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004956 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004957 }
4958 }
4959 }
4960 return status;
4961}
4962
Eric Laurent6a94d692014-05-20 11:18:06 -07004963status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input,
4964 audio_patch_handle_t *patchHandle)
Eric Laurent1c333e22014-05-20 10:48:17 -07004965{
Eric Laurent1f2f2232014-06-02 12:01:23 -07004966 sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input);
Eric Laurent6a94d692014-05-20 11:18:06 -07004967 ssize_t index;
4968 if (patchHandle) {
4969 index = mAudioPatches.indexOfKey(*patchHandle);
4970 } else {
4971 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle);
4972 }
4973 if (index < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07004974 return INVALID_OPERATION;
4975 }
Eric Laurent6a94d692014-05-20 11:18:06 -07004976 sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index);
4977 status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0);
Eric Laurent1c333e22014-05-20 10:48:17 -07004978 ALOGV("resetInputDevice() releaseAudioPatch returned %d", status);
4979 inputDesc->mPatchHandle = 0;
Eric Laurent6a94d692014-05-20 11:18:06 -07004980 removeAudioPatch(patchDesc->mHandle);
4981 nextAudioPortGeneration();
Eric Laurentb52c1522014-05-20 11:27:36 -07004982 mpClientInterface->onAudioPatchListUpdate();
Eric Laurent1c333e22014-05-20 10:48:17 -07004983 return status;
4984}
4985
4986sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -08004987 String8 address,
Glenn Kastencbd48022014-07-24 13:46:44 -07004988 uint32_t& samplingRate,
Eric Laurente552edb2014-03-10 17:42:56 -07004989 audio_format_t format,
Glenn Kasten6a8ab052014-07-24 14:08:35 -07004990 audio_channel_mask_t channelMask,
Glenn Kastencbd48022014-07-24 13:46:44 -07004991 audio_input_flags_t flags)
Eric Laurente552edb2014-03-10 17:42:56 -07004992{
4993 // Choose an input profile based on the requested capture parameters: select the first available
4994 // profile supporting all requested parameters.
4995
4996 for (size_t i = 0; i < mHwModules.size(); i++)
4997 {
4998 if (mHwModules[i]->mHandle == 0) {
4999 continue;
5000 }
5001 for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
5002 {
Eric Laurent1c333e22014-05-20 10:48:17 -07005003 sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j];
Eric Laurentd4692962014-05-05 18:13:44 -07005004 // profile->log();
Eric Laurent275e8e92014-11-30 15:14:47 -08005005 if (profile->isCompatibleProfile(device, address, samplingRate,
Glenn Kastencbd48022014-07-24 13:46:44 -07005006 &samplingRate /*updatedSamplingRate*/,
5007 format, channelMask, (audio_output_flags_t) flags)) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005008
Eric Laurente552edb2014-03-10 17:42:56 -07005009 return profile;
5010 }
5011 }
5012 }
5013 return NULL;
5014}
5015
Eric Laurent275e8e92014-11-30 15:14:47 -08005016audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource,
5017 String8 *address)
Eric Laurente552edb2014-03-10 17:42:56 -07005018{
5019 uint32_t device = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005020 audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() &
5021 ~AUDIO_DEVICE_BIT_IN;
Eric Laurent275e8e92014-11-30 15:14:47 -08005022
5023 for (size_t i = 0; i < mPolicyMixes.size(); i++) {
5024 if (mPolicyMixes[i]->mMix.mMixType != MIX_TYPE_RECORDERS) {
5025 continue;
5026 }
5027 for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) {
5028 if ((RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5029 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource == inputSource) ||
5030 (RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule &&
5031 mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource != inputSource)) {
5032 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
5033 if (address != NULL) {
5034 *address = mPolicyMixes[i]->mMix.mRegistrationId;
5035 }
5036 return AUDIO_DEVICE_IN_REMOTE_SUBMIX;
5037 }
5038 break;
5039 }
5040 }
5041 }
5042
Eric Laurente552edb2014-03-10 17:42:56 -07005043 switch (inputSource) {
5044 case AUDIO_SOURCE_VOICE_UPLINK:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005045 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005046 device = AUDIO_DEVICE_IN_VOICE_CALL;
5047 break;
5048 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005049 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005050
5051 case AUDIO_SOURCE_DEFAULT:
5052 case AUDIO_SOURCE_MIC:
Mike Lockwood41b0e242014-05-13 15:23:35 -07005053 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
5054 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
Eric Laurentc2730ba2014-07-20 15:47:07 -07005055 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5056 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5057 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5058 device = AUDIO_DEVICE_IN_USB_DEVICE;
5059 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5060 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005061 }
Eric Laurentc2730ba2014-07-20 15:47:07 -07005062 break;
5063
5064 case AUDIO_SOURCE_VOICE_COMMUNICATION:
5065 // Allow only use of devices on primary input if in call and HAL does not support routing
5066 // to voice call path.
5067 if ((mPhoneState == AUDIO_MODE_IN_CALL) &&
5068 (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
5069 availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN;
5070 }
5071
5072 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
5073 case AUDIO_POLICY_FORCE_BT_SCO:
5074 // if SCO device is requested but no SCO device is available, fall back to default case
5075 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
5076 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
5077 break;
5078 }
5079 // FALL THROUGH
5080
5081 default: // FORCE_NONE
5082 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
5083 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
5084 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5085 device = AUDIO_DEVICE_IN_USB_DEVICE;
5086 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5087 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5088 }
5089 break;
5090
5091 case AUDIO_POLICY_FORCE_SPEAKER:
5092 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
5093 device = AUDIO_DEVICE_IN_BACK_MIC;
5094 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
5095 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5096 }
5097 break;
5098 }
5099 break;
Mike Lockwood41b0e242014-05-13 15:23:35 -07005100
Eric Laurente552edb2014-03-10 17:42:56 -07005101 case AUDIO_SOURCE_VOICE_RECOGNITION:
5102 case AUDIO_SOURCE_HOTWORD:
Eric Laurent3b73df72014-03-11 09:06:29 -07005103 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07005104 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005105 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005106 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
Eric Laurente552edb2014-03-10 17:42:56 -07005107 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurentd4692962014-05-05 18:13:44 -07005108 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
5109 device = AUDIO_DEVICE_IN_USB_DEVICE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005110 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005111 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5112 }
5113 break;
5114 case AUDIO_SOURCE_CAMCORDER:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005115 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005116 device = AUDIO_DEVICE_IN_BACK_MIC;
Eric Laurent3a4311c2014-03-17 12:00:47 -07005117 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurente552edb2014-03-10 17:42:56 -07005118 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
5119 }
5120 break;
5121 case AUDIO_SOURCE_VOICE_DOWNLINK:
5122 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005123 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005124 device = AUDIO_DEVICE_IN_VOICE_CALL;
5125 }
5126 break;
5127 case AUDIO_SOURCE_REMOTE_SUBMIX:
Eric Laurent3a4311c2014-03-17 12:00:47 -07005128 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
Eric Laurente552edb2014-03-10 17:42:56 -07005129 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
Eric Laurent275e8e92014-11-30 15:14:47 -08005130 if (address != NULL) {
5131 *address = "0";
5132 }
Eric Laurente552edb2014-03-10 17:42:56 -07005133 }
5134 break;
Hochi Huang327cb702014-09-21 09:47:31 +08005135 case AUDIO_SOURCE_FM_TUNER:
5136 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
5137 device = AUDIO_DEVICE_IN_FM_TUNER;
5138 }
5139 break;
Eric Laurente552edb2014-03-10 17:42:56 -07005140 default:
5141 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
5142 break;
5143 }
5144 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
5145 return device;
5146}
5147
Eric Laurente0720872014-03-11 09:30:41 -07005148bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005149{
5150 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
5151 device &= ~AUDIO_DEVICE_BIT_IN;
5152 if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
5153 return true;
5154 }
5155 return false;
5156}
5157
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005158bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) {
Eric Laurent275e8e92014-11-30 15:14:47 -08005159 return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL & ~AUDIO_DEVICE_BIT_IN) != 0);
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07005160}
5161
Eric Laurente0720872014-03-11 09:30:41 -07005162audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs)
Eric Laurente552edb2014-03-10 17:42:56 -07005163{
5164 for (size_t i = 0; i < mInputs.size(); i++) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005165 const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i);
Eric Laurente552edb2014-03-10 17:42:56 -07005166 if ((input_descriptor->mRefCount > 0)
5167 && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
5168 return mInputs.keyAt(i);
5169 }
5170 }
5171 return 0;
5172}
5173
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005174uint32_t AudioPolicyManager::activeInputsCount() const
5175{
5176 uint32_t count = 0;
5177 for (size_t i = 0; i < mInputs.size(); i++) {
5178 const sp<AudioInputDescriptor> desc = mInputs.valueAt(i);
5179 if (desc->mRefCount > 0) {
5180 return count++;
5181 }
5182 }
5183 return count;
5184}
5185
Eric Laurente552edb2014-03-10 17:42:56 -07005186
Eric Laurente0720872014-03-11 09:30:41 -07005187audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005188{
5189 if (device == AUDIO_DEVICE_NONE) {
5190 // this happens when forcing a route update and no track is active on an output.
5191 // In this case the returned category is not important.
5192 device = AUDIO_DEVICE_OUT_SPEAKER;
Eric Laurent3b73df72014-03-11 09:06:29 -07005193 } else if (popcount(device) > 1) {
Eric Laurente552edb2014-03-10 17:42:56 -07005194 // Multiple device selection is either:
5195 // - speaker + one other device: give priority to speaker in this case.
5196 // - one A2DP device + another device: happens with duplicated output. In this case
5197 // retain the device on the A2DP output as the other must not correspond to an active
5198 // selection if not the speaker.
Jungshik Janga1f99172014-09-05 21:25:48 +09005199 // - HDMI-CEC system audio mode only output: give priority to available item in order.
Eric Laurente552edb2014-03-10 17:42:56 -07005200 if (device & AUDIO_DEVICE_OUT_SPEAKER) {
5201 device = AUDIO_DEVICE_OUT_SPEAKER;
Jungshik Janga1f99172014-09-05 21:25:48 +09005202 } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) {
5203 device = AUDIO_DEVICE_OUT_HDMI_ARC;
5204 } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) {
5205 device = AUDIO_DEVICE_OUT_AUX_LINE;
5206 } else if (device & AUDIO_DEVICE_OUT_SPDIF) {
5207 device = AUDIO_DEVICE_OUT_SPDIF;
Eric Laurente552edb2014-03-10 17:42:56 -07005208 } else {
5209 device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
5210 }
5211 }
5212
Jon Eklund11c9fb12014-06-23 14:47:03 -05005213 /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/
5214 if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE)
5215 device = AUDIO_DEVICE_OUT_SPEAKER;
5216
Eric Laurent3b73df72014-03-11 09:06:29 -07005217 ALOGW_IF(popcount(device) != 1,
Eric Laurente552edb2014-03-10 17:42:56 -07005218 "getDeviceForVolume() invalid device combination: %08x",
5219 device);
5220
5221 return device;
5222}
5223
Eric Laurente0720872014-03-11 09:30:41 -07005224AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07005225{
5226 switch(getDeviceForVolume(device)) {
5227 case AUDIO_DEVICE_OUT_EARPIECE:
5228 return DEVICE_CATEGORY_EARPIECE;
5229 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
5230 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
5231 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
5232 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
5233 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
5234 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
5235 return DEVICE_CATEGORY_HEADSET;
Jon Eklundac29afa2014-07-28 16:06:06 -05005236 case AUDIO_DEVICE_OUT_LINE:
5237 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
5238 /*USB? Remote submix?*/
5239 return DEVICE_CATEGORY_EXT_MEDIA;
Eric Laurente552edb2014-03-10 17:42:56 -07005240 case AUDIO_DEVICE_OUT_SPEAKER:
5241 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
5242 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
Eric Laurente552edb2014-03-10 17:42:56 -07005243 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
5244 case AUDIO_DEVICE_OUT_USB_DEVICE:
5245 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
5246 default:
5247 return DEVICE_CATEGORY_SPEAKER;
5248 }
5249}
5250
Eric Laurent223fd5c2014-11-11 13:43:36 -08005251/* static */
Eric Laurente0720872014-03-11 09:30:41 -07005252float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
Eric Laurente552edb2014-03-10 17:42:56 -07005253 int indexInUi)
5254{
5255 device_category deviceCategory = getDeviceCategory(device);
5256 const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
5257
5258 // the volume index in the UI is relative to the min and max volume indices for this stream type
5259 int nbSteps = 1 + curve[VOLMAX].mIndex -
5260 curve[VOLMIN].mIndex;
5261 int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
5262 (streamDesc.mIndexMax - streamDesc.mIndexMin);
5263
5264 // find what part of the curve this index volume belongs to, or if it's out of bounds
5265 int segment = 0;
5266 if (volIdx < curve[VOLMIN].mIndex) { // out of bounds
5267 return 0.0f;
5268 } else if (volIdx < curve[VOLKNEE1].mIndex) {
5269 segment = 0;
5270 } else if (volIdx < curve[VOLKNEE2].mIndex) {
5271 segment = 1;
5272 } else if (volIdx <= curve[VOLMAX].mIndex) {
5273 segment = 2;
5274 } else { // out of bounds
5275 return 1.0f;
5276 }
5277
5278 // linear interpolation in the attenuation table in dB
5279 float decibels = curve[segment].mDBAttenuation +
5280 ((float)(volIdx - curve[segment].mIndex)) *
5281 ( (curve[segment+1].mDBAttenuation -
5282 curve[segment].mDBAttenuation) /
5283 ((float)(curve[segment+1].mIndex -
5284 curve[segment].mIndex)) );
5285
5286 float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
5287
5288 ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
5289 curve[segment].mIndex, volIdx,
5290 curve[segment+1].mIndex,
5291 curve[segment].mDBAttenuation,
5292 decibels,
5293 curve[segment+1].mDBAttenuation,
5294 amplification);
5295
5296 return amplification;
5297}
5298
Eric Laurente0720872014-03-11 09:30:41 -07005299const AudioPolicyManager::VolumeCurvePoint
5300 AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005301 {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
5302};
5303
Eric Laurente0720872014-03-11 09:30:41 -07005304const AudioPolicyManager::VolumeCurvePoint
5305 AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005306 {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
5307};
5308
Eric Laurente0720872014-03-11 09:30:41 -07005309const AudioPolicyManager::VolumeCurvePoint
Jon Eklundac29afa2014-07-28 16:06:06 -05005310 AudioPolicyManager::sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
5311 {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f}
5312};
5313
5314const AudioPolicyManager::VolumeCurvePoint
Eric Laurente0720872014-03-11 09:30:41 -07005315 AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005316 {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
5317};
5318
Eric Laurente0720872014-03-11 09:30:41 -07005319const AudioPolicyManager::VolumeCurvePoint
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07005320 AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Jean-Michel Trivi98c60432014-07-09 08:51:34 -07005321 {1, -55.0f}, {20, -43.0f}, {86, -12.0f}, {100, 0.0f}
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07005322};
5323
5324const AudioPolicyManager::VolumeCurvePoint
Eric Laurente0720872014-03-11 09:30:41 -07005325 AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005326 {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
5327};
5328
Eric Laurente0720872014-03-11 09:30:41 -07005329const AudioPolicyManager::VolumeCurvePoint
5330 AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005331 {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
5332};
5333
5334// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
5335// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
5336// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
5337// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
5338
Eric Laurente0720872014-03-11 09:30:41 -07005339const AudioPolicyManager::VolumeCurvePoint
5340 AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005341 {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
5342};
5343
Eric Laurente0720872014-03-11 09:30:41 -07005344const AudioPolicyManager::VolumeCurvePoint
5345 AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005346 {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
5347};
5348
Eric Laurente0720872014-03-11 09:30:41 -07005349const AudioPolicyManager::VolumeCurvePoint
5350 AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005351 {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
5352};
5353
Eric Laurente0720872014-03-11 09:30:41 -07005354const AudioPolicyManager::VolumeCurvePoint
5355 AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005356 {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
5357};
5358
Eric Laurente0720872014-03-11 09:30:41 -07005359const AudioPolicyManager::VolumeCurvePoint
5360 AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005361 {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
5362};
5363
Eric Laurente0720872014-03-11 09:30:41 -07005364const AudioPolicyManager::VolumeCurvePoint
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07005365 AudioPolicyManager::sLinearVolumeCurve[AudioPolicyManager::VOLCNT] = {
5366 {0, -96.0f}, {33, -68.0f}, {66, -34.0f}, {100, 0.0f}
5367};
5368
5369const AudioPolicyManager::VolumeCurvePoint
5370 AudioPolicyManager::sSilentVolumeCurve[AudioPolicyManager::VOLCNT] = {
5371 {0, -96.0f}, {1, -96.0f}, {2, -96.0f}, {100, -96.0f}
5372};
5373
5374const AudioPolicyManager::VolumeCurvePoint
Eric Laurent223fd5c2014-11-11 13:43:36 -08005375 AudioPolicyManager::sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT] = {
5376 {0, 0.0f}, {1, 0.0f}, {2, 0.0f}, {100, 0.0f}
5377};
5378
5379const AudioPolicyManager::VolumeCurvePoint
Eric Laurente0720872014-03-11 09:30:41 -07005380 *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT]
5381 [AudioPolicyManager::DEVICE_CATEGORY_CNT] = {
Eric Laurente552edb2014-03-10 17:42:56 -07005382 { // AUDIO_STREAM_VOICE_CALL
5383 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
5384 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005385 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5386 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005387 },
5388 { // AUDIO_STREAM_SYSTEM
5389 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
5390 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005391 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5392 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005393 },
5394 { // AUDIO_STREAM_RING
5395 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
5396 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005397 sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5398 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005399 },
5400 { // AUDIO_STREAM_MUSIC
5401 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
5402 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005403 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5404 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005405 },
5406 { // AUDIO_STREAM_ALARM
5407 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
5408 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005409 sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5410 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005411 },
5412 { // AUDIO_STREAM_NOTIFICATION
5413 sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
5414 sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005415 sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5416 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005417 },
5418 { // AUDIO_STREAM_BLUETOOTH_SCO
5419 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
5420 sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005421 sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5422 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005423 },
5424 { // AUDIO_STREAM_ENFORCED_AUDIBLE
5425 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
5426 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005427 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5428 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005429 },
5430 { // AUDIO_STREAM_DTMF
5431 sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
5432 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
Jon Eklundac29afa2014-07-28 16:06:06 -05005433 sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5434 sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005435 },
5436 { // AUDIO_STREAM_TTS
Jean-Michel Trivid9cfeb42014-09-22 16:51:34 -07005437 // "Transmitted Through Speaker": always silent except on DEVICE_CATEGORY_SPEAKER
5438 sSilentVolumeCurve, // DEVICE_CATEGORY_HEADSET
5439 sLinearVolumeCurve, // DEVICE_CATEGORY_SPEAKER
5440 sSilentVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5441 sSilentVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
Eric Laurente552edb2014-03-10 17:42:56 -07005442 },
Eric Laurent223fd5c2014-11-11 13:43:36 -08005443 { // AUDIO_STREAM_ACCESSIBILITY
5444 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
5445 sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
5446 sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5447 sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
5448 },
5449 { // AUDIO_STREAM_REROUTING
5450 sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
5451 sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
5452 sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5453 sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
5454 },
5455 { // AUDIO_STREAM_PATCH
5456 sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET
5457 sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER
5458 sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE
5459 sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA
5460 },
Eric Laurente552edb2014-03-10 17:42:56 -07005461};
5462
Eric Laurente0720872014-03-11 09:30:41 -07005463void AudioPolicyManager::initializeVolumeCurves()
Eric Laurente552edb2014-03-10 17:42:56 -07005464{
5465 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
5466 for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
5467 mStreams[i].mVolumeCurve[j] =
5468 sVolumeProfiles[i][j];
5469 }
5470 }
5471
5472 // Check availability of DRC on speaker path: if available, override some of the speaker curves
5473 if (mSpeakerDrcEnabled) {
5474 mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5475 sDefaultSystemVolumeCurveDrc;
5476 mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5477 sSpeakerSonificationVolumeCurveDrc;
5478 mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5479 sSpeakerSonificationVolumeCurveDrc;
5480 mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5481 sSpeakerSonificationVolumeCurveDrc;
Jean-Michel Triviccd8e4a2014-06-05 15:33:20 -07005482 mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5483 sSpeakerMediaVolumeCurveDrc;
Eric Laurent223fd5c2014-11-11 13:43:36 -08005484 mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
5485 sSpeakerMediaVolumeCurveDrc;
Eric Laurente552edb2014-03-10 17:42:56 -07005486 }
5487}
5488
Eric Laurente0720872014-03-11 09:30:41 -07005489float AudioPolicyManager::computeVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005490 int index,
5491 audio_io_handle_t output,
5492 audio_devices_t device)
5493{
5494 float volume = 1.0;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005495 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005496 StreamDescriptor &streamDesc = mStreams[stream];
5497
5498 if (device == AUDIO_DEVICE_NONE) {
5499 device = outputDesc->device();
5500 }
5501
Eric Laurente552edb2014-03-10 17:42:56 -07005502 volume = volIndexToAmpl(device, streamDesc, index);
5503
5504 // if a headset is connected, apply the following rules to ring tones and notifications
5505 // to avoid sound level bursts in user's ears:
5506 // - always attenuate ring tones and notifications volume by 6dB
5507 // - if music is playing, always limit the volume to current music volume,
5508 // with a minimum threshold at -36dB so that notification is always perceived.
Eric Laurent3b73df72014-03-11 09:06:29 -07005509 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005510 if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
5511 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
5512 AUDIO_DEVICE_OUT_WIRED_HEADSET |
5513 AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
5514 ((stream_strategy == STRATEGY_SONIFICATION)
5515 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
Eric Laurent3b73df72014-03-11 09:06:29 -07005516 || (stream == AUDIO_STREAM_SYSTEM)
Eric Laurente552edb2014-03-10 17:42:56 -07005517 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005518 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) &&
Eric Laurente552edb2014-03-10 17:42:56 -07005519 streamDesc.mCanBeMuted) {
5520 volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
5521 // when the phone is ringing we must consider that music could have been paused just before
5522 // by the music application and behave as if music was active if the last music track was
5523 // just stopped
Eric Laurent3b73df72014-03-11 09:06:29 -07005524 if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005525 mLimitRingtoneVolume) {
5526 audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
Eric Laurent3b73df72014-03-11 09:06:29 -07005527 float musicVol = computeVolume(AUDIO_STREAM_MUSIC,
5528 mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice),
Eric Laurente552edb2014-03-10 17:42:56 -07005529 output,
5530 musicDevice);
5531 float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
5532 musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
5533 if (volume > minVol) {
5534 volume = minVol;
5535 ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
5536 }
5537 }
5538 }
5539
5540 return volume;
5541}
5542
Eric Laurente0720872014-03-11 09:30:41 -07005543status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005544 int index,
5545 audio_io_handle_t output,
5546 audio_devices_t device,
5547 int delayMs,
5548 bool force)
5549{
5550
5551 // do not change actual stream volume if the stream is muted
5552 if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
5553 ALOGVV("checkAndSetVolume() stream %d muted count %d",
5554 stream, mOutputs.valueFor(output)->mMuteCount[stream]);
5555 return NO_ERROR;
5556 }
5557
5558 // do not change in call volume if bluetooth is connected and vice versa
Eric Laurent3b73df72014-03-11 09:06:29 -07005559 if ((stream == AUDIO_STREAM_VOICE_CALL &&
5560 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) ||
5561 (stream == AUDIO_STREAM_BLUETOOTH_SCO &&
5562 mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005563 ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
Eric Laurent3b73df72014-03-11 09:06:29 -07005564 stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]);
Eric Laurente552edb2014-03-10 17:42:56 -07005565 return INVALID_OPERATION;
5566 }
5567
5568 float volume = computeVolume(stream, index, output, device);
Eric Laurent275e8e92014-11-30 15:14:47 -08005569 // unit gain if rerouting to external policy
5570 if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
5571 ssize_t index = mOutputs.indexOfKey(output);
5572 if (index >= 0) {
5573 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index);
5574 if (outputDesc->mPolicyMixAddress != String8("")
5575 && outputDesc->mPolicyMixAddress != String8("0")) {
5576 ALOGV("max gain when rerouting for output=%d", output);
5577 volume = 1.0f;
5578 }
5579 }
5580
5581 }
Eric Laurente552edb2014-03-10 17:42:56 -07005582 // We actually change the volume if:
5583 // - the float value returned by computeVolume() changed
5584 // - the force flag is set
5585 if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
5586 force) {
5587 mOutputs.valueFor(output)->mCurVolume[stream] = volume;
5588 ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
5589 // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
5590 // enabled
Eric Laurent3b73df72014-03-11 09:06:29 -07005591 if (stream == AUDIO_STREAM_BLUETOOTH_SCO) {
5592 mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005593 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005594 mpClientInterface->setStreamVolume(stream, volume, output, delayMs);
Eric Laurente552edb2014-03-10 17:42:56 -07005595 }
5596
Eric Laurent3b73df72014-03-11 09:06:29 -07005597 if (stream == AUDIO_STREAM_VOICE_CALL ||
5598 stream == AUDIO_STREAM_BLUETOOTH_SCO) {
Eric Laurente552edb2014-03-10 17:42:56 -07005599 float voiceVolume;
5600 // Force voice volume to max for bluetooth SCO as volume is managed by the headset
Eric Laurent3b73df72014-03-11 09:06:29 -07005601 if (stream == AUDIO_STREAM_VOICE_CALL) {
Eric Laurente552edb2014-03-10 17:42:56 -07005602 voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
5603 } else {
5604 voiceVolume = 1.0;
5605 }
5606
5607 if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
5608 mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
5609 mLastVoiceVolume = voiceVolume;
5610 }
5611 }
5612
5613 return NO_ERROR;
5614}
5615
Eric Laurente0720872014-03-11 09:30:41 -07005616void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output,
Eric Laurente552edb2014-03-10 17:42:56 -07005617 audio_devices_t device,
5618 int delayMs,
5619 bool force)
5620{
5621 ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
5622
Eric Laurent3b73df72014-03-11 09:06:29 -07005623 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005624 if (stream == AUDIO_STREAM_PATCH) {
5625 continue;
5626 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005627 checkAndSetVolume((audio_stream_type_t)stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005628 mStreams[stream].getVolumeIndex(device),
5629 output,
5630 device,
5631 delayMs,
5632 force);
5633 }
5634}
5635
Eric Laurente0720872014-03-11 09:30:41 -07005636void AudioPolicyManager::setStrategyMute(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07005637 bool on,
5638 audio_io_handle_t output,
5639 int delayMs,
5640 audio_devices_t device)
5641{
5642 ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
Eric Laurent3b73df72014-03-11 09:06:29 -07005643 for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005644 if (stream == AUDIO_STREAM_PATCH) {
5645 continue;
5646 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005647 if (getStrategy((audio_stream_type_t)stream) == strategy) {
5648 setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device);
Eric Laurente552edb2014-03-10 17:42:56 -07005649 }
5650 }
5651}
5652
Eric Laurente0720872014-03-11 09:30:41 -07005653void AudioPolicyManager::setStreamMute(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005654 bool on,
5655 audio_io_handle_t output,
5656 int delayMs,
5657 audio_devices_t device)
5658{
5659 StreamDescriptor &streamDesc = mStreams[stream];
Eric Laurent1f2f2232014-06-02 12:01:23 -07005660 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output);
Eric Laurente552edb2014-03-10 17:42:56 -07005661 if (device == AUDIO_DEVICE_NONE) {
5662 device = outputDesc->device();
5663 }
5664
5665 ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
5666 stream, on, output, outputDesc->mMuteCount[stream], device);
5667
5668 if (on) {
5669 if (outputDesc->mMuteCount[stream] == 0) {
5670 if (streamDesc.mCanBeMuted &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005671 ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) ||
5672 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) {
Eric Laurente552edb2014-03-10 17:42:56 -07005673 checkAndSetVolume(stream, 0, output, device, delayMs);
5674 }
5675 }
5676 // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
5677 outputDesc->mMuteCount[stream]++;
5678 } else {
5679 if (outputDesc->mMuteCount[stream] == 0) {
5680 ALOGV("setStreamMute() unmuting non muted stream!");
5681 return;
5682 }
5683 if (--outputDesc->mMuteCount[stream] == 0) {
5684 checkAndSetVolume(stream,
5685 streamDesc.getVolumeIndex(device),
5686 output,
5687 device,
5688 delayMs);
5689 }
5690 }
5691}
5692
Eric Laurente0720872014-03-11 09:30:41 -07005693void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005694 bool starting, bool stateChange)
Eric Laurente552edb2014-03-10 17:42:56 -07005695{
5696 // if the stream pertains to sonification strategy and we are in call we must
5697 // mute the stream if it is low visibility. If it is high visibility, we must play a tone
5698 // in the device used for phone strategy and play the tone if the selected device does not
5699 // interfere with the device used for phone strategy
5700 // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
5701 // many times as there are active tracks on the output
Eric Laurent3b73df72014-03-11 09:06:29 -07005702 const routing_strategy stream_strategy = getStrategy(stream);
Eric Laurente552edb2014-03-10 17:42:56 -07005703 if ((stream_strategy == STRATEGY_SONIFICATION) ||
5704 ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
Eric Laurent1f2f2232014-06-02 12:01:23 -07005705 sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput);
Eric Laurente552edb2014-03-10 17:42:56 -07005706 ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
5707 stream, starting, outputDesc->mDevice, stateChange);
5708 if (outputDesc->mRefCount[stream]) {
5709 int muteCount = 1;
5710 if (stateChange) {
5711 muteCount = outputDesc->mRefCount[stream];
5712 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005713 if (audio_is_low_visibility(stream)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005714 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
5715 for (int i = 0; i < muteCount; i++) {
5716 setStreamMute(stream, starting, mPrimaryOutput);
5717 }
5718 } else {
5719 ALOGV("handleIncallSonification() high visibility");
5720 if (outputDesc->device() &
5721 getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
5722 ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
5723 for (int i = 0; i < muteCount; i++) {
5724 setStreamMute(stream, starting, mPrimaryOutput);
5725 }
5726 }
5727 if (starting) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005728 mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION,
5729 AUDIO_STREAM_VOICE_CALL);
Eric Laurente552edb2014-03-10 17:42:56 -07005730 } else {
5731 mpClientInterface->stopTone();
5732 }
5733 }
5734 }
5735 }
5736}
5737
Eric Laurente0720872014-03-11 09:30:41 -07005738bool AudioPolicyManager::isInCall()
Eric Laurente552edb2014-03-10 17:42:56 -07005739{
5740 return isStateInCall(mPhoneState);
5741}
5742
Eric Laurente0720872014-03-11 09:30:41 -07005743bool AudioPolicyManager::isStateInCall(int state) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005744 return ((state == AUDIO_MODE_IN_CALL) ||
5745 (state == AUDIO_MODE_IN_COMMUNICATION));
Eric Laurente552edb2014-03-10 17:42:56 -07005746}
5747
Eric Laurente0720872014-03-11 09:30:41 -07005748uint32_t AudioPolicyManager::getMaxEffectsCpuLoad()
Eric Laurente552edb2014-03-10 17:42:56 -07005749{
5750 return MAX_EFFECTS_CPU_LOAD;
5751}
5752
Eric Laurente0720872014-03-11 09:30:41 -07005753uint32_t AudioPolicyManager::getMaxEffectsMemory()
Eric Laurente552edb2014-03-10 17:42:56 -07005754{
5755 return MAX_EFFECTS_MEMORY;
5756}
5757
Eric Laurent6a94d692014-05-20 11:18:06 -07005758
Eric Laurente552edb2014-03-10 17:42:56 -07005759// --- AudioOutputDescriptor class implementation
5760
Eric Laurente0720872014-03-11 09:30:41 -07005761AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor(
Eric Laurent1c333e22014-05-20 10:48:17 -07005762 const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07005763 : mId(0), mIoHandle(0), mLatency(0),
Eric Laurent275e8e92014-11-30 15:14:47 -08005764 mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPolicyMixAddress(String8("")),
5765 mPatchHandle(0),
Eric Laurente552edb2014-03-10 17:42:56 -07005766 mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
5767{
5768 // clear usage count for all stream types
Eric Laurent3b73df72014-03-11 09:06:29 -07005769 for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
Eric Laurente552edb2014-03-10 17:42:56 -07005770 mRefCount[i] = 0;
5771 mCurVolume[i] = -1.0;
5772 mMuteCount[i] = 0;
5773 mStopTime[i] = 0;
5774 }
5775 for (int i = 0; i < NUM_STRATEGIES; i++) {
5776 mStrategyMutedByDevice[i] = false;
5777 }
5778 if (profile != NULL) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07005779 mFlags = (audio_output_flags_t)profile->mFlags;
Eric Laurent1e693b52014-07-09 15:03:28 -07005780 mSamplingRate = profile->pickSamplingRate();
5781 mFormat = profile->pickFormat();
5782 mChannelMask = profile->pickChannelMask();
Eric Laurenta121f902014-06-03 13:32:54 -07005783 if (profile->mGains.size() > 0) {
5784 profile->mGains[0]->getDefaultConfig(&mGain);
5785 }
Eric Laurente552edb2014-03-10 17:42:56 -07005786 }
5787}
5788
Eric Laurente0720872014-03-11 09:30:41 -07005789audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const
Eric Laurente552edb2014-03-10 17:42:56 -07005790{
5791 if (isDuplicated()) {
5792 return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
5793 } else {
5794 return mDevice;
5795 }
5796}
5797
Eric Laurente0720872014-03-11 09:30:41 -07005798uint32_t AudioPolicyManager::AudioOutputDescriptor::latency()
Eric Laurente552edb2014-03-10 17:42:56 -07005799{
5800 if (isDuplicated()) {
5801 return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
5802 } else {
5803 return mLatency;
5804 }
5805}
5806
Eric Laurente0720872014-03-11 09:30:41 -07005807bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith(
Eric Laurent1f2f2232014-06-02 12:01:23 -07005808 const sp<AudioOutputDescriptor> outputDesc)
Eric Laurente552edb2014-03-10 17:42:56 -07005809{
5810 if (isDuplicated()) {
5811 return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
5812 } else if (outputDesc->isDuplicated()){
5813 return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
5814 } else {
5815 return (mProfile->mModule == outputDesc->mProfile->mModule);
5816 }
5817}
5818
Eric Laurente0720872014-03-11 09:30:41 -07005819void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream,
Eric Laurent3b73df72014-03-11 09:06:29 -07005820 int delta)
Eric Laurente552edb2014-03-10 17:42:56 -07005821{
5822 // forward usage count change to attached outputs
5823 if (isDuplicated()) {
5824 mOutput1->changeRefCount(stream, delta);
5825 mOutput2->changeRefCount(stream, delta);
5826 }
5827 if ((delta + (int)mRefCount[stream]) < 0) {
Eric Laurent3b73df72014-03-11 09:06:29 -07005828 ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d",
5829 delta, stream, mRefCount[stream]);
Eric Laurente552edb2014-03-10 17:42:56 -07005830 mRefCount[stream] = 0;
5831 return;
5832 }
5833 mRefCount[stream] += delta;
5834 ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
5835}
5836
Eric Laurente0720872014-03-11 09:30:41 -07005837audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices()
Eric Laurente552edb2014-03-10 17:42:56 -07005838{
5839 if (isDuplicated()) {
5840 return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
5841 } else {
Eric Laurent3a4311c2014-03-17 12:00:47 -07005842 return mProfile->mSupportedDevices.types() ;
Eric Laurente552edb2014-03-10 17:42:56 -07005843 }
5844}
5845
Eric Laurente0720872014-03-11 09:30:41 -07005846bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
Eric Laurente552edb2014-03-10 17:42:56 -07005847{
5848 return isStrategyActive(NUM_STRATEGIES, inPastMs);
5849}
5850
Eric Laurente0720872014-03-11 09:30:41 -07005851bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
Eric Laurente552edb2014-03-10 17:42:56 -07005852 uint32_t inPastMs,
5853 nsecs_t sysTime) const
5854{
5855 if ((sysTime == 0) && (inPastMs != 0)) {
5856 sysTime = systemTime();
5857 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005858 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
Eric Laurent223fd5c2014-11-11 13:43:36 -08005859 if (i == AUDIO_STREAM_PATCH) {
5860 continue;
5861 }
Eric Laurent3b73df72014-03-11 09:06:29 -07005862 if (((getStrategy((audio_stream_type_t)i) == strategy) ||
Eric Laurente552edb2014-03-10 17:42:56 -07005863 (NUM_STRATEGIES == strategy)) &&
Eric Laurent3b73df72014-03-11 09:06:29 -07005864 isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
Eric Laurente552edb2014-03-10 17:42:56 -07005865 return true;
5866 }
5867 }
5868 return false;
5869}
5870
Eric Laurente0720872014-03-11 09:30:41 -07005871bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream,
Eric Laurente552edb2014-03-10 17:42:56 -07005872 uint32_t inPastMs,
5873 nsecs_t sysTime) const
5874{
5875 if (mRefCount[stream] != 0) {
5876 return true;
5877 }
5878 if (inPastMs == 0) {
5879 return false;
5880 }
5881 if (sysTime == 0) {
5882 sysTime = systemTime();
5883 }
5884 if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
5885 return true;
5886 }
5887 return false;
5888}
5889
Eric Laurent1c333e22014-05-20 10:48:17 -07005890void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07005891 struct audio_port_config *dstConfig,
5892 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07005893{
Eric Laurent84c70242014-06-23 08:46:27 -07005894 ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle);
5895
Eric Laurent1f2f2232014-06-02 12:01:23 -07005896 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
5897 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
5898 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07005899 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005900 }
5901 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
5902
Eric Laurent6a94d692014-05-20 11:18:06 -07005903 dstConfig->id = mId;
5904 dstConfig->role = AUDIO_PORT_ROLE_SOURCE;
5905 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent6a94d692014-05-20 11:18:06 -07005906 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
5907 dstConfig->ext.mix.handle = mIoHandle;
5908 dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
Eric Laurent1c333e22014-05-20 10:48:17 -07005909}
5910
5911void AudioPolicyManager::AudioOutputDescriptor::toAudioPort(
5912 struct audio_port *port) const
5913{
Eric Laurent84c70242014-06-23 08:46:27 -07005914 ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle);
Eric Laurent1c333e22014-05-20 10:48:17 -07005915 mProfile->toAudioPort(port);
5916 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07005917 toAudioPortConfig(&port->active_config);
5918 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07005919 port->ext.mix.handle = mIoHandle;
5920 port->ext.mix.latency_class =
5921 mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL;
5922}
Eric Laurente552edb2014-03-10 17:42:56 -07005923
Eric Laurente0720872014-03-11 09:30:41 -07005924status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07005925{
5926 const size_t SIZE = 256;
5927 char buffer[SIZE];
5928 String8 result;
5929
Eric Laurent4d416952014-08-10 14:07:09 -07005930 snprintf(buffer, SIZE, " ID: %d\n", mId);
5931 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07005932 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
5933 result.append(buffer);
5934 snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
5935 result.append(buffer);
5936 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
5937 result.append(buffer);
5938 snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
5939 result.append(buffer);
5940 snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
5941 result.append(buffer);
5942 snprintf(buffer, SIZE, " Devices %08x\n", device());
5943 result.append(buffer);
5944 snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
5945 result.append(buffer);
Eric Laurent3b73df72014-03-11 09:06:29 -07005946 for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
5947 snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n",
5948 i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
Eric Laurente552edb2014-03-10 17:42:56 -07005949 result.append(buffer);
5950 }
5951 write(fd, result.string(), result.size());
5952
5953 return NO_ERROR;
5954}
5955
5956// --- AudioInputDescriptor class implementation
5957
Eric Laurent1c333e22014-05-20 10:48:17 -07005958AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
Eric Laurent1f2f2232014-06-02 12:01:23 -07005959 : mId(0), mIoHandle(0),
Eric Laurent1c333e22014-05-20 10:48:17 -07005960 mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0),
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07005961 mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false)
Eric Laurente552edb2014-03-10 17:42:56 -07005962{
Eric Laurent3a4311c2014-03-17 12:00:47 -07005963 if (profile != NULL) {
Eric Laurent1e693b52014-07-09 15:03:28 -07005964 mSamplingRate = profile->pickSamplingRate();
5965 mFormat = profile->pickFormat();
5966 mChannelMask = profile->pickChannelMask();
Eric Laurenta121f902014-06-03 13:32:54 -07005967 if (profile->mGains.size() > 0) {
5968 profile->mGains[0]->getDefaultConfig(&mGain);
5969 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07005970 }
Eric Laurente552edb2014-03-10 17:42:56 -07005971}
5972
Eric Laurent1c333e22014-05-20 10:48:17 -07005973void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig(
Eric Laurent6a94d692014-05-20 11:18:06 -07005974 struct audio_port_config *dstConfig,
5975 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07005976{
Eric Laurent84c70242014-06-23 08:46:27 -07005977 ALOG_ASSERT(mProfile != 0,
5978 "toAudioPortConfig() called on input with null profile %d", mIoHandle);
Eric Laurent1f2f2232014-06-02 12:01:23 -07005979 dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
5980 AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
5981 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07005982 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07005983 }
5984
5985 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
5986
Eric Laurent6a94d692014-05-20 11:18:06 -07005987 dstConfig->id = mId;
5988 dstConfig->role = AUDIO_PORT_ROLE_SINK;
5989 dstConfig->type = AUDIO_PORT_TYPE_MIX;
Eric Laurent62aaabb2014-06-02 10:40:54 -07005990 dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
5991 dstConfig->ext.mix.handle = mIoHandle;
5992 dstConfig->ext.mix.usecase.source = mInputSource;
Eric Laurent1c333e22014-05-20 10:48:17 -07005993}
5994
5995void AudioPolicyManager::AudioInputDescriptor::toAudioPort(
5996 struct audio_port *port) const
5997{
Eric Laurent84c70242014-06-23 08:46:27 -07005998 ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
5999
Eric Laurent1c333e22014-05-20 10:48:17 -07006000 mProfile->toAudioPort(port);
6001 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07006002 toAudioPortConfig(&port->active_config);
6003 port->ext.mix.hw_module = mProfile->mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07006004 port->ext.mix.handle = mIoHandle;
6005 port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
6006}
6007
Eric Laurente0720872014-03-11 09:30:41 -07006008status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07006009{
6010 const size_t SIZE = 256;
6011 char buffer[SIZE];
6012 String8 result;
6013
Eric Laurent4d416952014-08-10 14:07:09 -07006014 snprintf(buffer, SIZE, " ID: %d\n", mId);
6015 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07006016 snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
6017 result.append(buffer);
6018 snprintf(buffer, SIZE, " Format: %d\n", mFormat);
6019 result.append(buffer);
6020 snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
6021 result.append(buffer);
6022 snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
6023 result.append(buffer);
6024 snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
6025 result.append(buffer);
Glenn Kasten6a8ab052014-07-24 14:08:35 -07006026 snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount);
6027 result.append(buffer);
6028
Eric Laurente552edb2014-03-10 17:42:56 -07006029 write(fd, result.string(), result.size());
6030
6031 return NO_ERROR;
6032}
6033
6034// --- StreamDescriptor class implementation
6035
Eric Laurente0720872014-03-11 09:30:41 -07006036AudioPolicyManager::StreamDescriptor::StreamDescriptor()
Eric Laurente552edb2014-03-10 17:42:56 -07006037 : mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
6038{
6039 mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
6040}
6041
Eric Laurente0720872014-03-11 09:30:41 -07006042int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device)
Eric Laurente552edb2014-03-10 17:42:56 -07006043{
Eric Laurente0720872014-03-11 09:30:41 -07006044 device = AudioPolicyManager::getDeviceForVolume(device);
Eric Laurente552edb2014-03-10 17:42:56 -07006045 // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
6046 if (mIndexCur.indexOfKey(device) < 0) {
6047 device = AUDIO_DEVICE_OUT_DEFAULT;
6048 }
6049 return mIndexCur.valueFor(device);
6050}
6051
Eric Laurente0720872014-03-11 09:30:41 -07006052void AudioPolicyManager::StreamDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07006053{
6054 const size_t SIZE = 256;
6055 char buffer[SIZE];
6056 String8 result;
6057
6058 snprintf(buffer, SIZE, "%s %02d %02d ",
6059 mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
6060 result.append(buffer);
6061 for (size_t i = 0; i < mIndexCur.size(); i++) {
6062 snprintf(buffer, SIZE, "%04x : %02d, ",
6063 mIndexCur.keyAt(i),
6064 mIndexCur.valueAt(i));
6065 result.append(buffer);
6066 }
6067 result.append("\n");
6068
6069 write(fd, result.string(), result.size());
6070}
6071
6072// --- EffectDescriptor class implementation
6073
Eric Laurente0720872014-03-11 09:30:41 -07006074status_t AudioPolicyManager::EffectDescriptor::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07006075{
6076 const size_t SIZE = 256;
6077 char buffer[SIZE];
6078 String8 result;
6079
6080 snprintf(buffer, SIZE, " I/O: %d\n", mIo);
6081 result.append(buffer);
6082 snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
6083 result.append(buffer);
6084 snprintf(buffer, SIZE, " Session: %d\n", mSession);
6085 result.append(buffer);
6086 snprintf(buffer, SIZE, " Name: %s\n", mDesc.name);
6087 result.append(buffer);
6088 snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled");
6089 result.append(buffer);
6090 write(fd, result.string(), result.size());
6091
6092 return NO_ERROR;
6093}
6094
Eric Laurent1c333e22014-05-20 10:48:17 -07006095// --- HwModule class implementation
Eric Laurente552edb2014-03-10 17:42:56 -07006096
Eric Laurente0720872014-03-11 09:30:41 -07006097AudioPolicyManager::HwModule::HwModule(const char *name)
Eric Laurenteb108a42014-06-06 14:56:52 -07006098 : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)),
6099 mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0)
Eric Laurente552edb2014-03-10 17:42:56 -07006100{
6101}
6102
Eric Laurente0720872014-03-11 09:30:41 -07006103AudioPolicyManager::HwModule::~HwModule()
Eric Laurente552edb2014-03-10 17:42:56 -07006104{
6105 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07006106 mOutputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07006107 }
6108 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07006109 mInputProfiles[i]->mSupportedDevices.clear();
Eric Laurente552edb2014-03-10 17:42:56 -07006110 }
6111 free((void *)mName);
6112}
6113
Eric Laurent1afeecb2014-05-14 08:52:28 -07006114status_t AudioPolicyManager::HwModule::loadInput(cnode *root)
6115{
6116 cnode *node = root->first_child;
6117
6118 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this);
6119
6120 while (node) {
6121 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
6122 profile->loadSamplingRates((char *)node->value);
6123 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
6124 profile->loadFormats((char *)node->value);
6125 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
6126 profile->loadInChannels((char *)node->value);
6127 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
6128 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
6129 mDeclaredDevices);
Eric Laurent5dbe4712014-09-19 19:04:57 -07006130 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
6131 profile->mFlags = parseInputFlagNames((char *)node->value);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006132 } else if (strcmp(node->name, GAINS_TAG) == 0) {
6133 profile->loadGains(node);
6134 }
6135 node = node->next;
6136 }
6137 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
6138 "loadInput() invalid supported devices");
6139 ALOGW_IF(profile->mChannelMasks.size() == 0,
6140 "loadInput() invalid supported channel masks");
6141 ALOGW_IF(profile->mSamplingRates.size() == 0,
6142 "loadInput() invalid supported sampling rates");
6143 ALOGW_IF(profile->mFormats.size() == 0,
6144 "loadInput() invalid supported formats");
6145 if (!profile->mSupportedDevices.isEmpty() &&
6146 (profile->mChannelMasks.size() != 0) &&
6147 (profile->mSamplingRates.size() != 0) &&
6148 (profile->mFormats.size() != 0)) {
6149
6150 ALOGV("loadInput() adding input Supported Devices %04x",
6151 profile->mSupportedDevices.types());
6152
6153 mInputProfiles.add(profile);
6154 return NO_ERROR;
6155 } else {
6156 return BAD_VALUE;
6157 }
6158}
6159
6160status_t AudioPolicyManager::HwModule::loadOutput(cnode *root)
6161{
6162 cnode *node = root->first_child;
6163
6164 sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this);
6165
6166 while (node) {
6167 if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
6168 profile->loadSamplingRates((char *)node->value);
6169 } else if (strcmp(node->name, FORMATS_TAG) == 0) {
6170 profile->loadFormats((char *)node->value);
6171 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
6172 profile->loadOutChannels((char *)node->value);
6173 } else if (strcmp(node->name, DEVICES_TAG) == 0) {
6174 profile->mSupportedDevices.loadDevicesFromName((char *)node->value,
6175 mDeclaredDevices);
6176 } else if (strcmp(node->name, FLAGS_TAG) == 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07006177 profile->mFlags = parseOutputFlagNames((char *)node->value);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006178 } else if (strcmp(node->name, GAINS_TAG) == 0) {
6179 profile->loadGains(node);
6180 }
6181 node = node->next;
6182 }
6183 ALOGW_IF(profile->mSupportedDevices.isEmpty(),
6184 "loadOutput() invalid supported devices");
6185 ALOGW_IF(profile->mChannelMasks.size() == 0,
6186 "loadOutput() invalid supported channel masks");
6187 ALOGW_IF(profile->mSamplingRates.size() == 0,
6188 "loadOutput() invalid supported sampling rates");
6189 ALOGW_IF(profile->mFormats.size() == 0,
6190 "loadOutput() invalid supported formats");
6191 if (!profile->mSupportedDevices.isEmpty() &&
6192 (profile->mChannelMasks.size() != 0) &&
6193 (profile->mSamplingRates.size() != 0) &&
6194 (profile->mFormats.size() != 0)) {
6195
6196 ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x",
6197 profile->mSupportedDevices.types(), profile->mFlags);
6198
6199 mOutputProfiles.add(profile);
6200 return NO_ERROR;
6201 } else {
6202 return BAD_VALUE;
6203 }
6204}
6205
6206status_t AudioPolicyManager::HwModule::loadDevice(cnode *root)
6207{
6208 cnode *node = root->first_child;
6209
6210 audio_devices_t type = AUDIO_DEVICE_NONE;
6211 while (node) {
6212 if (strcmp(node->name, DEVICE_TYPE) == 0) {
6213 type = parseDeviceNames((char *)node->value);
6214 break;
6215 }
6216 node = node->next;
6217 }
6218 if (type == AUDIO_DEVICE_NONE ||
6219 (!audio_is_input_device(type) && !audio_is_output_device(type))) {
6220 ALOGW("loadDevice() bad type %08x", type);
6221 return BAD_VALUE;
6222 }
6223 sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type);
6224 deviceDesc->mModule = this;
6225
6226 node = root->first_child;
6227 while (node) {
6228 if (strcmp(node->name, DEVICE_ADDRESS) == 0) {
6229 deviceDesc->mAddress = String8((char *)node->value);
6230 } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
6231 if (audio_is_input_device(type)) {
6232 deviceDesc->loadInChannels((char *)node->value);
6233 } else {
6234 deviceDesc->loadOutChannels((char *)node->value);
6235 }
6236 } else if (strcmp(node->name, GAINS_TAG) == 0) {
6237 deviceDesc->loadGains(node);
6238 }
6239 node = node->next;
6240 }
6241
6242 ALOGV("loadDevice() adding device name %s type %08x address %s",
6243 deviceDesc->mName.string(), type, deviceDesc->mAddress.string());
6244
6245 mDeclaredDevices.add(deviceDesc);
6246
6247 return NO_ERROR;
6248}
6249
Eric Laurent275e8e92014-11-30 15:14:47 -08006250status_t AudioPolicyManager::HwModule::addOutputProfile(String8 name, const audio_config_t *config,
6251 audio_devices_t device, String8 address)
6252{
6253 sp<IOProfile> profile = new IOProfile(name, AUDIO_PORT_ROLE_SOURCE, this);
6254
6255 profile->mSamplingRates.add(config->sample_rate);
6256 profile->mChannelMasks.add(config->channel_mask);
6257 profile->mFormats.add(config->format);
6258
6259 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
6260 devDesc->mAddress = address;
6261 profile->mSupportedDevices.add(devDesc);
6262
6263 mOutputProfiles.add(profile);
6264
6265 return NO_ERROR;
6266}
6267
6268status_t AudioPolicyManager::HwModule::removeOutputProfile(String8 name)
6269{
6270 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
6271 if (mOutputProfiles[i]->mName == name) {
6272 mOutputProfiles.removeAt(i);
6273 break;
6274 }
6275 }
6276
6277 return NO_ERROR;
6278}
6279
6280status_t AudioPolicyManager::HwModule::addInputProfile(String8 name, const audio_config_t *config,
6281 audio_devices_t device, String8 address)
6282{
6283 sp<IOProfile> profile = new IOProfile(name, AUDIO_PORT_ROLE_SINK, this);
6284
6285 profile->mSamplingRates.add(config->sample_rate);
6286 profile->mChannelMasks.add(config->channel_mask);
6287 profile->mFormats.add(config->format);
6288
6289 sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device);
6290 devDesc->mAddress = address;
6291 profile->mSupportedDevices.add(devDesc);
6292
6293 ALOGV("addInputProfile() name %s rate %d mask 0x08", name.string(), config->sample_rate, config->channel_mask);
6294
6295 mInputProfiles.add(profile);
6296
6297 return NO_ERROR;
6298}
6299
6300status_t AudioPolicyManager::HwModule::removeInputProfile(String8 name)
6301{
6302 for (size_t i = 0; i < mInputProfiles.size(); i++) {
6303 if (mInputProfiles[i]->mName == name) {
6304 mInputProfiles.removeAt(i);
6305 break;
6306 }
6307 }
6308
6309 return NO_ERROR;
6310}
6311
6312
Eric Laurente0720872014-03-11 09:30:41 -07006313void AudioPolicyManager::HwModule::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07006314{
6315 const size_t SIZE = 256;
6316 char buffer[SIZE];
6317 String8 result;
6318
6319 snprintf(buffer, SIZE, " - name: %s\n", mName);
6320 result.append(buffer);
6321 snprintf(buffer, SIZE, " - handle: %d\n", mHandle);
6322 result.append(buffer);
Eric Laurenteb108a42014-06-06 14:56:52 -07006323 snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF);
6324 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07006325 write(fd, result.string(), result.size());
6326 if (mOutputProfiles.size()) {
6327 write(fd, " - outputs:\n", strlen(" - outputs:\n"));
6328 for (size_t i = 0; i < mOutputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07006329 snprintf(buffer, SIZE, " output %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07006330 write(fd, buffer, strlen(buffer));
6331 mOutputProfiles[i]->dump(fd);
6332 }
6333 }
6334 if (mInputProfiles.size()) {
6335 write(fd, " - inputs:\n", strlen(" - inputs:\n"));
6336 for (size_t i = 0; i < mInputProfiles.size(); i++) {
Eric Laurentd4692962014-05-05 18:13:44 -07006337 snprintf(buffer, SIZE, " input %zu:\n", i);
Eric Laurente552edb2014-03-10 17:42:56 -07006338 write(fd, buffer, strlen(buffer));
6339 mInputProfiles[i]->dump(fd);
6340 }
6341 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006342 if (mDeclaredDevices.size()) {
6343 write(fd, " - devices:\n", strlen(" - devices:\n"));
6344 for (size_t i = 0; i < mDeclaredDevices.size(); i++) {
6345 mDeclaredDevices[i]->dump(fd, 4, i);
6346 }
6347 }
Eric Laurente552edb2014-03-10 17:42:56 -07006348}
6349
Eric Laurent1c333e22014-05-20 10:48:17 -07006350// --- AudioPort class implementation
6351
Eric Laurenta121f902014-06-03 13:32:54 -07006352
6353AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type,
6354 audio_port_role_t role, const sp<HwModule>& module) :
Eric Laurent5dbe4712014-09-19 19:04:57 -07006355 mName(name), mType(type), mRole(role), mModule(module), mFlags(0)
Eric Laurenta121f902014-06-03 13:32:54 -07006356{
6357 mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) ||
6358 ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK));
6359}
6360
Eric Laurent1c333e22014-05-20 10:48:17 -07006361void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const
6362{
6363 port->role = mRole;
6364 port->type = mType;
6365 unsigned int i;
6366 for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006367 if (mSamplingRates[i] != 0) {
6368 port->sample_rates[i] = mSamplingRates[i];
6369 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006370 }
6371 port->num_sample_rates = i;
6372 for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006373 if (mChannelMasks[i] != 0) {
6374 port->channel_masks[i] = mChannelMasks[i];
6375 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006376 }
6377 port->num_channel_masks = i;
6378 for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) {
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006379 if (mFormats[i] != 0) {
6380 port->formats[i] = mFormats[i];
6381 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006382 }
6383 port->num_formats = i;
Eric Laurente1715a42014-05-20 11:30:42 -07006384
Mark Salyzynbeb9e302014-06-18 16:33:15 -07006385 ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size());
Eric Laurente1715a42014-05-20 11:30:42 -07006386
6387 for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) {
6388 port->gains[i] = mGains[i]->mGain;
6389 }
6390 port->num_gains = i;
Eric Laurent1c333e22014-05-20 10:48:17 -07006391}
6392
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006393void AudioPolicyManager::AudioPort::importAudioPort(const sp<AudioPort> port) {
6394 for (size_t k = 0 ; k < port->mSamplingRates.size() ; k++) {
6395 const uint32_t rate = port->mSamplingRates.itemAt(k);
6396 if (rate != 0) { // skip "dynamic" rates
6397 bool hasRate = false;
6398 for (size_t l = 0 ; l < mSamplingRates.size() ; l++) {
6399 if (rate == mSamplingRates.itemAt(l)) {
6400 hasRate = true;
6401 break;
6402 }
6403 }
6404 if (!hasRate) { // never import a sampling rate twice
6405 mSamplingRates.add(rate);
6406 }
6407 }
6408 }
6409 for (size_t k = 0 ; k < port->mChannelMasks.size() ; k++) {
6410 const audio_channel_mask_t mask = port->mChannelMasks.itemAt(k);
6411 if (mask != 0) { // skip "dynamic" masks
6412 bool hasMask = false;
6413 for (size_t l = 0 ; l < mChannelMasks.size() ; l++) {
6414 if (mask == mChannelMasks.itemAt(l)) {
6415 hasMask = true;
6416 break;
6417 }
6418 }
6419 if (!hasMask) { // never import a channel mask twice
6420 mChannelMasks.add(mask);
6421 }
6422 }
6423 }
6424 for (size_t k = 0 ; k < port->mFormats.size() ; k++) {
6425 const audio_format_t format = port->mFormats.itemAt(k);
6426 if (format != 0) { // skip "dynamic" formats
6427 bool hasFormat = false;
6428 for (size_t l = 0 ; l < mFormats.size() ; l++) {
6429 if (format == mFormats.itemAt(l)) {
6430 hasFormat = true;
6431 break;
6432 }
6433 }
6434 if (!hasFormat) { // never import a channel mask twice
6435 mFormats.add(format);
6436 }
6437 }
6438 }
Hochi Huang23fe3c02014-10-03 09:09:30 +08006439 for (size_t k = 0 ; k < port->mGains.size() ; k++) {
6440 sp<AudioGain> gain = port->mGains.itemAt(k);
6441 if (gain != 0) {
6442 bool hasGain = false;
6443 for (size_t l = 0 ; l < mGains.size() ; l++) {
6444 if (gain == mGains.itemAt(l)) {
6445 hasGain = true;
6446 break;
6447 }
6448 }
6449 if (!hasGain) { // never import a gain twice
6450 mGains.add(gain);
6451 }
6452 }
6453 }
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006454}
6455
6456void AudioPolicyManager::AudioPort::clearCapabilities() {
6457 mChannelMasks.clear();
6458 mFormats.clear();
6459 mSamplingRates.clear();
Hochi Huang23fe3c02014-10-03 09:09:30 +08006460 mGains.clear();
Jean-Michel Trivif17026d2014-08-10 14:30:48 -07006461}
Eric Laurent1c333e22014-05-20 10:48:17 -07006462
6463void AudioPolicyManager::AudioPort::loadSamplingRates(char *name)
6464{
6465 char *str = strtok(name, "|");
6466
6467 // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
6468 // rates should be read from the output stream after it is opened for the first time
6469 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
6470 mSamplingRates.add(0);
6471 return;
6472 }
6473
6474 while (str != NULL) {
6475 uint32_t rate = atoi(str);
6476 if (rate != 0) {
6477 ALOGV("loadSamplingRates() adding rate %d", rate);
6478 mSamplingRates.add(rate);
6479 }
6480 str = strtok(NULL, "|");
6481 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006482}
6483
6484void AudioPolicyManager::AudioPort::loadFormats(char *name)
6485{
6486 char *str = strtok(name, "|");
6487
6488 // by convention, "0' in the first entry in mFormats indicates the supported formats
6489 // should be read from the output stream after it is opened for the first time
6490 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
6491 mFormats.add(AUDIO_FORMAT_DEFAULT);
6492 return;
6493 }
6494
6495 while (str != NULL) {
6496 audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
6497 ARRAY_SIZE(sFormatNameToEnumTable),
6498 str);
6499 if (format != AUDIO_FORMAT_DEFAULT) {
6500 mFormats.add(format);
6501 }
6502 str = strtok(NULL, "|");
6503 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006504}
6505
6506void AudioPolicyManager::AudioPort::loadInChannels(char *name)
6507{
6508 const char *str = strtok(name, "|");
6509
6510 ALOGV("loadInChannels() %s", name);
6511
6512 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
6513 mChannelMasks.add(0);
6514 return;
6515 }
6516
6517 while (str != NULL) {
6518 audio_channel_mask_t channelMask =
6519 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
6520 ARRAY_SIZE(sInChannelsNameToEnumTable),
6521 str);
6522 if (channelMask != 0) {
6523 ALOGV("loadInChannels() adding channelMask %04x", channelMask);
6524 mChannelMasks.add(channelMask);
6525 }
6526 str = strtok(NULL, "|");
6527 }
Eric Laurent1c333e22014-05-20 10:48:17 -07006528}
6529
6530void AudioPolicyManager::AudioPort::loadOutChannels(char *name)
6531{
6532 const char *str = strtok(name, "|");
6533
6534 ALOGV("loadOutChannels() %s", name);
6535
6536 // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
6537 // masks should be read from the output stream after it is opened for the first time
6538 if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
6539 mChannelMasks.add(0);
6540 return;
6541 }
6542
6543 while (str != NULL) {
6544 audio_channel_mask_t channelMask =
6545 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
6546 ARRAY_SIZE(sOutChannelsNameToEnumTable),
6547 str);
6548 if (channelMask != 0) {
6549 mChannelMasks.add(channelMask);
6550 }
6551 str = strtok(NULL, "|");
6552 }
6553 return;
6554}
6555
Eric Laurent1afeecb2014-05-14 08:52:28 -07006556audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name)
6557{
6558 const char *str = strtok(name, "|");
6559
6560 ALOGV("loadGainMode() %s", name);
6561 audio_gain_mode_t mode = 0;
6562 while (str != NULL) {
6563 mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable,
6564 ARRAY_SIZE(sGainModeNameToEnumTable),
6565 str);
6566 str = strtok(NULL, "|");
6567 }
6568 return mode;
6569}
6570
Eric Laurenta121f902014-06-03 13:32:54 -07006571void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index)
Eric Laurent1afeecb2014-05-14 08:52:28 -07006572{
6573 cnode *node = root->first_child;
6574
Eric Laurenta121f902014-06-03 13:32:54 -07006575 sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006576
6577 while (node) {
6578 if (strcmp(node->name, GAIN_MODE) == 0) {
6579 gain->mGain.mode = loadGainMode((char *)node->value);
6580 } else if (strcmp(node->name, GAIN_CHANNELS) == 0) {
Eric Laurenta121f902014-06-03 13:32:54 -07006581 if (mUseInChannelMask) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07006582 gain->mGain.channel_mask =
6583 (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
6584 ARRAY_SIZE(sInChannelsNameToEnumTable),
6585 (char *)node->value);
6586 } else {
6587 gain->mGain.channel_mask =
6588 (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
6589 ARRAY_SIZE(sOutChannelsNameToEnumTable),
6590 (char *)node->value);
6591 }
6592 } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) {
6593 gain->mGain.min_value = atoi((char *)node->value);
6594 } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) {
6595 gain->mGain.max_value = atoi((char *)node->value);
6596 } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) {
6597 gain->mGain.default_value = atoi((char *)node->value);
6598 } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) {
6599 gain->mGain.step_value = atoi((char *)node->value);
6600 } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) {
6601 gain->mGain.min_ramp_ms = atoi((char *)node->value);
6602 } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) {
6603 gain->mGain.max_ramp_ms = atoi((char *)node->value);
6604 }
6605 node = node->next;
6606 }
6607
6608 ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d",
6609 gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value);
6610
6611 if (gain->mGain.mode == 0) {
6612 return;
6613 }
6614 mGains.add(gain);
6615}
6616
6617void AudioPolicyManager::AudioPort::loadGains(cnode *root)
6618{
6619 cnode *node = root->first_child;
Eric Laurenta121f902014-06-03 13:32:54 -07006620 int index = 0;
Eric Laurent1afeecb2014-05-14 08:52:28 -07006621 while (node) {
6622 ALOGV("loadGains() loading gain %s", node->name);
Eric Laurenta121f902014-06-03 13:32:54 -07006623 loadGain(node, index++);
Eric Laurent1afeecb2014-05-14 08:52:28 -07006624 node = node->next;
6625 }
6626}
6627
Glenn Kastencbd48022014-07-24 13:46:44 -07006628status_t AudioPolicyManager::AudioPort::checkExactSamplingRate(uint32_t samplingRate) const
Eric Laurenta121f902014-06-03 13:32:54 -07006629{
Eric Laurent0daea392014-12-04 19:14:54 -08006630 if (mSamplingRates.isEmpty()) {
6631 return NO_ERROR;
6632 }
6633
Eric Laurenta121f902014-06-03 13:32:54 -07006634 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
6635 if (mSamplingRates[i] == samplingRate) {
6636 return NO_ERROR;
6637 }
6638 }
6639 return BAD_VALUE;
6640}
6641
Glenn Kastencbd48022014-07-24 13:46:44 -07006642status_t AudioPolicyManager::AudioPort::checkCompatibleSamplingRate(uint32_t samplingRate,
6643 uint32_t *updatedSamplingRate) const
Eric Laurenta121f902014-06-03 13:32:54 -07006644{
Eric Laurent0daea392014-12-04 19:14:54 -08006645 if (mSamplingRates.isEmpty()) {
6646 return NO_ERROR;
6647 }
6648
Glenn Kastencbd48022014-07-24 13:46:44 -07006649 // Search for the closest supported sampling rate that is above (preferred)
6650 // or below (acceptable) the desired sampling rate, within a permitted ratio.
6651 // The sampling rates do not need to be sorted in ascending order.
6652 ssize_t maxBelow = -1;
6653 ssize_t minAbove = -1;
6654 uint32_t candidate;
6655 for (size_t i = 0; i < mSamplingRates.size(); i++) {
6656 candidate = mSamplingRates[i];
6657 if (candidate == samplingRate) {
6658 if (updatedSamplingRate != NULL) {
6659 *updatedSamplingRate = candidate;
6660 }
6661 return NO_ERROR;
6662 }
6663 // candidate < desired
6664 if (candidate < samplingRate) {
6665 if (maxBelow < 0 || candidate > mSamplingRates[maxBelow]) {
6666 maxBelow = i;
6667 }
6668 // candidate > desired
6669 } else {
6670 if (minAbove < 0 || candidate < mSamplingRates[minAbove]) {
6671 minAbove = i;
6672 }
6673 }
6674 }
6675 // This uses hard-coded knowledge about AudioFlinger resampling ratios.
6676 // TODO Move these assumptions out.
6677 static const uint32_t kMaxDownSampleRatio = 6; // beyond this aliasing occurs
6678 static const uint32_t kMaxUpSampleRatio = 256; // beyond this sample rate inaccuracies occur
6679 // due to approximation by an int32_t of the
6680 // phase increments
6681 // Prefer to down-sample from a higher sampling rate, as we get the desired frequency spectrum.
6682 if (minAbove >= 0) {
6683 candidate = mSamplingRates[minAbove];
6684 if (candidate / kMaxDownSampleRatio <= samplingRate) {
6685 if (updatedSamplingRate != NULL) {
6686 *updatedSamplingRate = candidate;
6687 }
6688 return NO_ERROR;
6689 }
6690 }
6691 // But if we have to up-sample from a lower sampling rate, that's OK.
6692 if (maxBelow >= 0) {
6693 candidate = mSamplingRates[maxBelow];
6694 if (candidate * kMaxUpSampleRatio >= samplingRate) {
6695 if (updatedSamplingRate != NULL) {
6696 *updatedSamplingRate = candidate;
6697 }
6698 return NO_ERROR;
6699 }
6700 }
6701 // leave updatedSamplingRate unmodified
6702 return BAD_VALUE;
6703}
6704
6705status_t AudioPolicyManager::AudioPort::checkExactChannelMask(audio_channel_mask_t channelMask) const
6706{
Eric Laurent0daea392014-12-04 19:14:54 -08006707 if (mChannelMasks.isEmpty()) {
6708 return NO_ERROR;
6709 }
6710
Glenn Kastencbd48022014-07-24 13:46:44 -07006711 for (size_t i = 0; i < mChannelMasks.size(); i++) {
Eric Laurenta121f902014-06-03 13:32:54 -07006712 if (mChannelMasks[i] == channelMask) {
6713 return NO_ERROR;
6714 }
6715 }
6716 return BAD_VALUE;
6717}
6718
Glenn Kastencbd48022014-07-24 13:46:44 -07006719status_t AudioPolicyManager::AudioPort::checkCompatibleChannelMask(audio_channel_mask_t channelMask)
6720 const
6721{
Eric Laurent0daea392014-12-04 19:14:54 -08006722 if (mChannelMasks.isEmpty()) {
6723 return NO_ERROR;
6724 }
6725
Glenn Kastencbd48022014-07-24 13:46:44 -07006726 const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK;
6727 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
6728 // FIXME Does not handle multi-channel automatic conversions yet
6729 audio_channel_mask_t supported = mChannelMasks[i];
6730 if (supported == channelMask) {
6731 return NO_ERROR;
6732 }
6733 if (isRecordThread) {
6734 // This uses hard-coded knowledge that AudioFlinger can silently down-mix and up-mix.
6735 // FIXME Abstract this out to a table.
6736 if (((supported == AUDIO_CHANNEL_IN_FRONT_BACK || supported == AUDIO_CHANNEL_IN_STEREO)
6737 && channelMask == AUDIO_CHANNEL_IN_MONO) ||
6738 (supported == AUDIO_CHANNEL_IN_MONO && (channelMask == AUDIO_CHANNEL_IN_FRONT_BACK
6739 || channelMask == AUDIO_CHANNEL_IN_STEREO))) {
6740 return NO_ERROR;
6741 }
6742 }
6743 }
6744 return BAD_VALUE;
6745}
6746
Eric Laurenta121f902014-06-03 13:32:54 -07006747status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const
6748{
Eric Laurent0daea392014-12-04 19:14:54 -08006749 if (mFormats.isEmpty()) {
6750 return NO_ERROR;
6751 }
6752
Eric Laurenta121f902014-06-03 13:32:54 -07006753 for (size_t i = 0; i < mFormats.size(); i ++) {
6754 if (mFormats[i] == format) {
6755 return NO_ERROR;
6756 }
6757 }
6758 return BAD_VALUE;
6759}
6760
Eric Laurent1e693b52014-07-09 15:03:28 -07006761
6762uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const
6763{
6764 // special case for uninitialized dynamic profile
6765 if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) {
6766 return 0;
6767 }
6768
Eric Laurent828bcff2014-09-07 12:26:06 -07006769 // For direct outputs, pick minimum sampling rate: this helps ensuring that the
6770 // channel count / sampling rate combination chosen will be supported by the connected
6771 // sink
6772 if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) &&
6773 (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) {
6774 uint32_t samplingRate = UINT_MAX;
6775 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
6776 if ((mSamplingRates[i] < samplingRate) && (mSamplingRates[i] > 0)) {
6777 samplingRate = mSamplingRates[i];
6778 }
6779 }
6780 return (samplingRate == UINT_MAX) ? 0 : samplingRate;
6781 }
6782
Eric Laurent1e693b52014-07-09 15:03:28 -07006783 uint32_t samplingRate = 0;
6784 uint32_t maxRate = MAX_MIXER_SAMPLING_RATE;
6785
6786 // For mixed output and inputs, use max mixer sampling rates. Do not
6787 // limit sampling rate otherwise
Eric Laurent828bcff2014-09-07 12:26:06 -07006788 if (mType != AUDIO_PORT_TYPE_MIX) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006789 maxRate = UINT_MAX;
6790 }
6791 for (size_t i = 0; i < mSamplingRates.size(); i ++) {
6792 if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) {
6793 samplingRate = mSamplingRates[i];
6794 }
6795 }
6796 return samplingRate;
6797}
6798
6799audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const
6800{
6801 // special case for uninitialized dynamic profile
6802 if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) {
6803 return AUDIO_CHANNEL_NONE;
6804 }
Eric Laurent1e693b52014-07-09 15:03:28 -07006805 audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE;
Eric Laurent828bcff2014-09-07 12:26:06 -07006806
6807 // For direct outputs, pick minimum channel count: this helps ensuring that the
6808 // channel count / sampling rate combination chosen will be supported by the connected
6809 // sink
6810 if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) &&
6811 (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) {
6812 uint32_t channelCount = UINT_MAX;
6813 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
6814 uint32_t cnlCount;
6815 if (mUseInChannelMask) {
6816 cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]);
6817 } else {
6818 cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]);
6819 }
6820 if ((cnlCount < channelCount) && (cnlCount > 0)) {
6821 channelMask = mChannelMasks[i];
6822 channelCount = cnlCount;
6823 }
6824 }
6825 return channelMask;
6826 }
6827
Eric Laurent1e693b52014-07-09 15:03:28 -07006828 uint32_t channelCount = 0;
6829 uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT;
6830
6831 // For mixed output and inputs, use max mixer channel count. Do not
6832 // limit channel count otherwise
Eric Laurent828bcff2014-09-07 12:26:06 -07006833 if (mType != AUDIO_PORT_TYPE_MIX) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006834 maxCount = UINT_MAX;
6835 }
6836 for (size_t i = 0; i < mChannelMasks.size(); i ++) {
6837 uint32_t cnlCount;
6838 if (mUseInChannelMask) {
6839 cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]);
6840 } else {
6841 cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]);
6842 }
6843 if ((cnlCount > channelCount) && (cnlCount <= maxCount)) {
6844 channelMask = mChannelMasks[i];
Eric Laurent828bcff2014-09-07 12:26:06 -07006845 channelCount = cnlCount;
Eric Laurent1e693b52014-07-09 15:03:28 -07006846 }
6847 }
6848 return channelMask;
6849}
6850
Andy Hung9a605382014-07-28 16:16:31 -07006851/* format in order of increasing preference */
Eric Laurent1e693b52014-07-09 15:03:28 -07006852const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = {
6853 AUDIO_FORMAT_DEFAULT,
6854 AUDIO_FORMAT_PCM_16_BIT,
Eric Laurenta2049942014-07-21 17:49:25 -07006855 AUDIO_FORMAT_PCM_8_24_BIT,
Eric Laurent1e693b52014-07-09 15:03:28 -07006856 AUDIO_FORMAT_PCM_24_BIT_PACKED,
Eric Laurenta2049942014-07-21 17:49:25 -07006857 AUDIO_FORMAT_PCM_32_BIT,
Andy Hung9a605382014-07-28 16:16:31 -07006858 AUDIO_FORMAT_PCM_FLOAT,
Eric Laurent1e693b52014-07-09 15:03:28 -07006859};
6860
6861int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1,
6862 audio_format_t format2)
6863{
6864 // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any
6865 // compressed format and better than any PCM format. This is by design of pickFormat()
6866 if (!audio_is_linear_pcm(format1)) {
6867 if (!audio_is_linear_pcm(format2)) {
6868 return 0;
6869 }
6870 return 1;
6871 }
6872 if (!audio_is_linear_pcm(format2)) {
6873 return -1;
6874 }
6875
6876 int index1 = -1, index2 = -1;
6877 for (size_t i = 0;
6878 (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1));
6879 i ++) {
6880 if (sPcmFormatCompareTable[i] == format1) {
6881 index1 = i;
6882 }
6883 if (sPcmFormatCompareTable[i] == format2) {
6884 index2 = i;
6885 }
6886 }
6887 // format1 not found => index1 < 0 => format2 > format1
6888 // format2 not found => index2 < 0 => format2 < format1
6889 return index1 - index2;
6890}
6891
6892audio_format_t AudioPolicyManager::AudioPort::pickFormat() const
6893{
6894 // special case for uninitialized dynamic profile
6895 if (mFormats.size() == 1 && mFormats[0] == 0) {
6896 return AUDIO_FORMAT_DEFAULT;
6897 }
6898
6899 audio_format_t format = AUDIO_FORMAT_DEFAULT;
Andy Hung9a605382014-07-28 16:16:31 -07006900 audio_format_t bestFormat =
6901 AudioPolicyManager::AudioPort::sPcmFormatCompareTable[
6902 ARRAY_SIZE(AudioPolicyManager::AudioPort::sPcmFormatCompareTable) - 1];
Eric Laurent1e693b52014-07-09 15:03:28 -07006903 // For mixed output and inputs, use best mixer output format. Do not
6904 // limit format otherwise
6905 if ((mType != AUDIO_PORT_TYPE_MIX) ||
6906 ((mRole == AUDIO_PORT_ROLE_SOURCE) &&
Eric Laurentd8622372014-07-27 13:47:31 -07006907 (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) != 0)))) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006908 bestFormat = AUDIO_FORMAT_INVALID;
6909 }
6910
6911 for (size_t i = 0; i < mFormats.size(); i ++) {
6912 if ((compareFormats(mFormats[i], format) > 0) &&
6913 (compareFormats(mFormats[i], bestFormat) <= 0)) {
6914 format = mFormats[i];
6915 }
6916 }
6917 return format;
6918}
6919
Eric Laurenta121f902014-06-03 13:32:54 -07006920status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig,
6921 int index) const
6922{
6923 if (index < 0 || (size_t)index >= mGains.size()) {
6924 return BAD_VALUE;
6925 }
6926 return mGains[index]->checkConfig(gainConfig);
6927}
6928
Eric Laurent1afeecb2014-05-14 08:52:28 -07006929void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const
6930{
6931 const size_t SIZE = 256;
6932 char buffer[SIZE];
6933 String8 result;
6934
6935 if (mName.size() != 0) {
6936 snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string());
6937 result.append(buffer);
6938 }
6939
6940 if (mSamplingRates.size() != 0) {
6941 snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, "");
6942 result.append(buffer);
6943 for (size_t i = 0; i < mSamplingRates.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006944 if (i == 0 && mSamplingRates[i] == 0) {
6945 snprintf(buffer, SIZE, "Dynamic");
6946 } else {
6947 snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
6948 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006949 result.append(buffer);
6950 result.append(i == (mSamplingRates.size() - 1) ? "" : ", ");
6951 }
6952 result.append("\n");
6953 }
6954
6955 if (mChannelMasks.size() != 0) {
6956 snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, "");
6957 result.append(buffer);
6958 for (size_t i = 0; i < mChannelMasks.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006959 ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]);
6960
6961 if (i == 0 && mChannelMasks[i] == 0) {
6962 snprintf(buffer, SIZE, "Dynamic");
6963 } else {
6964 snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
6965 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006966 result.append(buffer);
6967 result.append(i == (mChannelMasks.size() - 1) ? "" : ", ");
6968 }
6969 result.append("\n");
6970 }
6971
6972 if (mFormats.size() != 0) {
6973 snprintf(buffer, SIZE, "%*s- formats: ", spaces, "");
6974 result.append(buffer);
6975 for (size_t i = 0; i < mFormats.size(); i++) {
Eric Laurent1e693b52014-07-09 15:03:28 -07006976 const char *formatStr = enumToString(sFormatNameToEnumTable,
6977 ARRAY_SIZE(sFormatNameToEnumTable),
6978 mFormats[i]);
6979 if (i == 0 && strcmp(formatStr, "") == 0) {
6980 snprintf(buffer, SIZE, "Dynamic");
6981 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07006982 snprintf(buffer, SIZE, "%s", formatStr);
Eric Laurent1e693b52014-07-09 15:03:28 -07006983 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07006984 result.append(buffer);
6985 result.append(i == (mFormats.size() - 1) ? "" : ", ");
6986 }
6987 result.append("\n");
6988 }
6989 write(fd, result.string(), result.size());
6990 if (mGains.size() != 0) {
6991 snprintf(buffer, SIZE, "%*s- gains:\n", spaces, "");
6992 write(fd, buffer, strlen(buffer) + 1);
6993 result.append(buffer);
6994 for (size_t i = 0; i < mGains.size(); i++) {
6995 mGains[i]->dump(fd, spaces + 2, i);
6996 }
6997 }
6998}
6999
7000// --- AudioGain class implementation
7001
Eric Laurenta121f902014-06-03 13:32:54 -07007002AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask)
Eric Laurent1afeecb2014-05-14 08:52:28 -07007003{
Eric Laurenta121f902014-06-03 13:32:54 -07007004 mIndex = index;
7005 mUseInChannelMask = useInChannelMask;
Eric Laurent1afeecb2014-05-14 08:52:28 -07007006 memset(&mGain, 0, sizeof(struct audio_gain));
7007}
7008
Eric Laurenta121f902014-06-03 13:32:54 -07007009void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config)
7010{
7011 config->index = mIndex;
7012 config->mode = mGain.mode;
7013 config->channel_mask = mGain.channel_mask;
7014 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
7015 config->values[0] = mGain.default_value;
7016 } else {
7017 uint32_t numValues;
7018 if (mUseInChannelMask) {
7019 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
7020 } else {
7021 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
7022 }
7023 for (size_t i = 0; i < numValues; i++) {
7024 config->values[i] = mGain.default_value;
7025 }
7026 }
7027 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
7028 config->ramp_duration_ms = mGain.min_ramp_ms;
7029 }
7030}
7031
7032status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config)
7033{
7034 if ((config->mode & ~mGain.mode) != 0) {
7035 return BAD_VALUE;
7036 }
7037 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
7038 if ((config->values[0] < mGain.min_value) ||
7039 (config->values[0] > mGain.max_value)) {
7040 return BAD_VALUE;
7041 }
7042 } else {
7043 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
7044 return BAD_VALUE;
7045 }
7046 uint32_t numValues;
7047 if (mUseInChannelMask) {
7048 numValues = audio_channel_count_from_in_mask(config->channel_mask);
7049 } else {
7050 numValues = audio_channel_count_from_out_mask(config->channel_mask);
7051 }
7052 for (size_t i = 0; i < numValues; i++) {
7053 if ((config->values[i] < mGain.min_value) ||
7054 (config->values[i] > mGain.max_value)) {
7055 return BAD_VALUE;
7056 }
7057 }
7058 }
7059 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
7060 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
7061 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
7062 return BAD_VALUE;
7063 }
7064 }
7065 return NO_ERROR;
7066}
7067
Eric Laurent1afeecb2014-05-14 08:52:28 -07007068void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const
7069{
7070 const size_t SIZE = 256;
7071 char buffer[SIZE];
7072 String8 result;
7073
7074 snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1);
7075 result.append(buffer);
7076 snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode);
7077 result.append(buffer);
7078 snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask);
7079 result.append(buffer);
7080 snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value);
7081 result.append(buffer);
7082 snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value);
7083 result.append(buffer);
7084 snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value);
7085 result.append(buffer);
7086 snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value);
7087 result.append(buffer);
7088 snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms);
7089 result.append(buffer);
7090 snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms);
7091 result.append(buffer);
7092
7093 write(fd, result.string(), result.size());
7094}
7095
Eric Laurent1f2f2232014-06-02 12:01:23 -07007096// --- AudioPortConfig class implementation
7097
7098AudioPolicyManager::AudioPortConfig::AudioPortConfig()
7099{
7100 mSamplingRate = 0;
7101 mChannelMask = AUDIO_CHANNEL_NONE;
7102 mFormat = AUDIO_FORMAT_INVALID;
7103 mGain.index = -1;
7104}
7105
Eric Laurenta121f902014-06-03 13:32:54 -07007106status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig(
7107 const struct audio_port_config *config,
7108 struct audio_port_config *backupConfig)
7109{
7110 struct audio_port_config localBackupConfig;
7111 status_t status = NO_ERROR;
7112
7113 localBackupConfig.config_mask = config->config_mask;
7114 toAudioPortConfig(&localBackupConfig);
7115
Marco Nelissen961ec212014-08-25 15:58:39 -07007116 sp<AudioPort> audioport = getAudioPort();
7117 if (audioport == 0) {
Eric Laurenta121f902014-06-03 13:32:54 -07007118 status = NO_INIT;
7119 goto exit;
7120 }
7121 if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
Marco Nelissen961ec212014-08-25 15:58:39 -07007122 status = audioport->checkExactSamplingRate(config->sample_rate);
Eric Laurenta121f902014-06-03 13:32:54 -07007123 if (status != NO_ERROR) {
7124 goto exit;
7125 }
7126 mSamplingRate = config->sample_rate;
7127 }
7128 if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
Marco Nelissen961ec212014-08-25 15:58:39 -07007129 status = audioport->checkExactChannelMask(config->channel_mask);
Eric Laurenta121f902014-06-03 13:32:54 -07007130 if (status != NO_ERROR) {
7131 goto exit;
7132 }
7133 mChannelMask = config->channel_mask;
7134 }
7135 if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
Marco Nelissen961ec212014-08-25 15:58:39 -07007136 status = audioport->checkFormat(config->format);
Eric Laurenta121f902014-06-03 13:32:54 -07007137 if (status != NO_ERROR) {
7138 goto exit;
7139 }
7140 mFormat = config->format;
7141 }
7142 if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) {
Marco Nelissen961ec212014-08-25 15:58:39 -07007143 status = audioport->checkGain(&config->gain, config->gain.index);
Eric Laurenta121f902014-06-03 13:32:54 -07007144 if (status != NO_ERROR) {
7145 goto exit;
7146 }
7147 mGain = config->gain;
7148 }
7149
7150exit:
7151 if (status != NO_ERROR) {
7152 applyAudioPortConfig(&localBackupConfig);
7153 }
7154 if (backupConfig != NULL) {
7155 *backupConfig = localBackupConfig;
7156 }
7157 return status;
7158}
7159
Eric Laurent1f2f2232014-06-02 12:01:23 -07007160void AudioPolicyManager::AudioPortConfig::toAudioPortConfig(
7161 struct audio_port_config *dstConfig,
7162 const struct audio_port_config *srcConfig) const
7163{
7164 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
7165 dstConfig->sample_rate = mSamplingRate;
7166 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) {
7167 dstConfig->sample_rate = srcConfig->sample_rate;
7168 }
7169 } else {
7170 dstConfig->sample_rate = 0;
7171 }
7172 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
7173 dstConfig->channel_mask = mChannelMask;
7174 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) {
7175 dstConfig->channel_mask = srcConfig->channel_mask;
7176 }
7177 } else {
7178 dstConfig->channel_mask = AUDIO_CHANNEL_NONE;
7179 }
7180 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) {
7181 dstConfig->format = mFormat;
7182 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) {
7183 dstConfig->format = srcConfig->format;
7184 }
7185 } else {
7186 dstConfig->format = AUDIO_FORMAT_INVALID;
7187 }
7188 if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) {
7189 dstConfig->gain = mGain;
7190 if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) {
7191 dstConfig->gain = srcConfig->gain;
7192 }
7193 } else {
7194 dstConfig->gain.index = -1;
7195 }
7196 if (dstConfig->gain.index != -1) {
7197 dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
7198 } else {
7199 dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
7200 }
7201}
7202
Eric Laurent1c333e22014-05-20 10:48:17 -07007203// --- IOProfile class implementation
7204
Eric Laurent1afeecb2014-05-14 08:52:28 -07007205AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role,
Eric Laurent1f2f2232014-06-02 12:01:23 -07007206 const sp<HwModule>& module)
Eric Laurent1e693b52014-07-09 15:03:28 -07007207 : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module)
Eric Laurente552edb2014-03-10 17:42:56 -07007208{
7209}
7210
Eric Laurente0720872014-03-11 09:30:41 -07007211AudioPolicyManager::IOProfile::~IOProfile()
Eric Laurente552edb2014-03-10 17:42:56 -07007212{
7213}
7214
7215// checks if the IO profile is compatible with specified parameters.
7216// Sampling rate, format and channel mask must be specified in order to
7217// get a valid a match
Eric Laurente0720872014-03-11 09:30:41 -07007218bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device,
Eric Laurent275e8e92014-11-30 15:14:47 -08007219 String8 address,
7220 uint32_t samplingRate,
7221 uint32_t *updatedSamplingRate,
7222 audio_format_t format,
7223 audio_channel_mask_t channelMask,
7224 uint32_t flags) const
Eric Laurente552edb2014-03-10 17:42:56 -07007225{
Glenn Kastencbd48022014-07-24 13:46:44 -07007226 const bool isPlaybackThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SOURCE;
7227 const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK;
7228 ALOG_ASSERT(isPlaybackThread != isRecordThread);
Eric Laurente552edb2014-03-10 17:42:56 -07007229
Eric Laurent275e8e92014-11-30 15:14:47 -08007230 if (device != AUDIO_DEVICE_NONE && mSupportedDevices.getDevice(device, address) == 0) {
Glenn Kastencbd48022014-07-24 13:46:44 -07007231 return false;
7232 }
7233
7234 if (samplingRate == 0) {
Eric Laurente552edb2014-03-10 17:42:56 -07007235 return false;
Glenn Kastencbd48022014-07-24 13:46:44 -07007236 }
7237 uint32_t myUpdatedSamplingRate = samplingRate;
7238 if (isPlaybackThread && checkExactSamplingRate(samplingRate) != NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07007239 return false;
Glenn Kastencbd48022014-07-24 13:46:44 -07007240 }
7241 if (isRecordThread && checkCompatibleSamplingRate(samplingRate, &myUpdatedSamplingRate) !=
7242 NO_ERROR) {
Eric Laurente552edb2014-03-10 17:42:56 -07007243 return false;
Glenn Kastencbd48022014-07-24 13:46:44 -07007244 }
7245
7246 if (!audio_is_valid_format(format) || checkFormat(format) != NO_ERROR) {
7247 return false;
7248 }
7249
7250 if (isPlaybackThread && (!audio_is_output_channel(channelMask) ||
7251 checkExactChannelMask(channelMask) != NO_ERROR)) {
7252 return false;
7253 }
7254 if (isRecordThread && (!audio_is_input_channel(channelMask) ||
7255 checkCompatibleChannelMask(channelMask) != NO_ERROR)) {
7256 return false;
7257 }
7258
7259 if (isPlaybackThread && (mFlags & flags) != flags) {
7260 return false;
7261 }
7262 // The only input flag that is allowed to be different is the fast flag.
7263 // An existing fast stream is compatible with a normal track request.
7264 // An existing normal stream is compatible with a fast track request,
7265 // but the fast request will be denied by AudioFlinger and converted to normal track.
Eric Laurent5dbe4712014-09-19 19:04:57 -07007266 if (isRecordThread && ((mFlags ^ flags) &
Glenn Kastencbd48022014-07-24 13:46:44 -07007267 ~AUDIO_INPUT_FLAG_FAST)) {
7268 return false;
7269 }
7270
7271 if (updatedSamplingRate != NULL) {
7272 *updatedSamplingRate = myUpdatedSamplingRate;
7273 }
7274 return true;
Eric Laurente552edb2014-03-10 17:42:56 -07007275}
7276
Eric Laurente0720872014-03-11 09:30:41 -07007277void AudioPolicyManager::IOProfile::dump(int fd)
Eric Laurente552edb2014-03-10 17:42:56 -07007278{
7279 const size_t SIZE = 256;
7280 char buffer[SIZE];
7281 String8 result;
7282
Eric Laurent1afeecb2014-05-14 08:52:28 -07007283 AudioPort::dump(fd, 4);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007284
Eric Laurente552edb2014-03-10 17:42:56 -07007285 snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags);
7286 result.append(buffer);
Eric Laurent1afeecb2014-05-14 08:52:28 -07007287 snprintf(buffer, SIZE, " - devices:\n");
7288 result.append(buffer);
Eric Laurente552edb2014-03-10 17:42:56 -07007289 write(fd, result.string(), result.size());
Eric Laurent1afeecb2014-05-14 08:52:28 -07007290 for (size_t i = 0; i < mSupportedDevices.size(); i++) {
7291 mSupportedDevices[i]->dump(fd, 6, i);
7292 }
Eric Laurente552edb2014-03-10 17:42:56 -07007293}
7294
Eric Laurentd4692962014-05-05 18:13:44 -07007295void AudioPolicyManager::IOProfile::log()
7296{
7297 const size_t SIZE = 256;
7298 char buffer[SIZE];
7299 String8 result;
7300
7301 ALOGV(" - sampling rates: ");
7302 for (size_t i = 0; i < mSamplingRates.size(); i++) {
7303 ALOGV(" %d", mSamplingRates[i]);
7304 }
7305
7306 ALOGV(" - channel masks: ");
7307 for (size_t i = 0; i < mChannelMasks.size(); i++) {
7308 ALOGV(" 0x%04x", mChannelMasks[i]);
7309 }
7310
7311 ALOGV(" - formats: ");
7312 for (size_t i = 0; i < mFormats.size(); i++) {
7313 ALOGV(" 0x%08x", mFormats[i]);
7314 }
7315
7316 ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types());
7317 ALOGV(" - flags: 0x%04x\n", mFlags);
7318}
7319
7320
Eric Laurent3a4311c2014-03-17 12:00:47 -07007321// --- DeviceDescriptor implementation
Eric Laurente552edb2014-03-10 17:42:56 -07007322
Eric Laurent1f2f2232014-06-02 12:01:23 -07007323
7324AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) :
7325 AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
7326 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
7327 AUDIO_PORT_ROLE_SOURCE,
7328 NULL),
Eric Laurent1e693b52014-07-09 15:03:28 -07007329 mDeviceType(type), mAddress(""), mId(0)
Eric Laurent1f2f2232014-06-02 12:01:23 -07007330{
Eric Laurenta121f902014-06-03 13:32:54 -07007331 if (mGains.size() > 0) {
7332 mGains[0]->getDefaultConfig(&mGain);
7333 }
Eric Laurent1f2f2232014-06-02 12:01:23 -07007334}
7335
Eric Laurent3a4311c2014-03-17 12:00:47 -07007336bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
Eric Laurente552edb2014-03-10 17:42:56 -07007337{
Eric Laurent3a4311c2014-03-17 12:00:47 -07007338 // Devices are considered equal if they:
7339 // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
7340 // - have the same address or one device does not specify the address
7341 // - have the same channel mask or one device does not specify the channel mask
Eric Laurent1c333e22014-05-20 10:48:17 -07007342 return (mDeviceType == other->mDeviceType) &&
Eric Laurent3a4311c2014-03-17 12:00:47 -07007343 (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
Eric Laurent2f8a36f2014-03-26 19:05:55 -07007344 (mChannelMask == 0 || other->mChannelMask == 0 ||
Eric Laurent3a4311c2014-03-17 12:00:47 -07007345 mChannelMask == other->mChannelMask);
7346}
7347
7348void AudioPolicyManager::DeviceVector::refreshTypes()
7349{
Eric Laurent1c333e22014-05-20 10:48:17 -07007350 mDeviceTypes = AUDIO_DEVICE_NONE;
Eric Laurent3a4311c2014-03-17 12:00:47 -07007351 for(size_t i = 0; i < size(); i++) {
Eric Laurent1c333e22014-05-20 10:48:17 -07007352 mDeviceTypes |= itemAt(i)->mDeviceType;
Eric Laurent3a4311c2014-03-17 12:00:47 -07007353 }
Eric Laurent1c333e22014-05-20 10:48:17 -07007354 ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007355}
7356
7357ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
7358{
7359 for(size_t i = 0; i < size(); i++) {
7360 if (item->equals(itemAt(i))) {
7361 return i;
Eric Laurente552edb2014-03-10 17:42:56 -07007362 }
7363 }
Eric Laurent3a4311c2014-03-17 12:00:47 -07007364 return -1;
Eric Laurente552edb2014-03-10 17:42:56 -07007365}
7366
Eric Laurent3a4311c2014-03-17 12:00:47 -07007367ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item)
Eric Laurente552edb2014-03-10 17:42:56 -07007368{
Eric Laurent3a4311c2014-03-17 12:00:47 -07007369 ssize_t ret = indexOf(item);
7370
7371 if (ret < 0) {
7372 ret = SortedVector::add(item);
7373 if (ret >= 0) {
7374 refreshTypes();
7375 }
7376 } else {
Eric Laurent1c333e22014-05-20 10:48:17 -07007377 ALOGW("DeviceVector::add device %08x already in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007378 ret = -1;
7379 }
7380 return ret;
Eric Laurente552edb2014-03-10 17:42:56 -07007381}
7382
Eric Laurent3a4311c2014-03-17 12:00:47 -07007383ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item)
7384{
7385 size_t i;
7386 ssize_t ret = indexOf(item);
7387
7388 if (ret < 0) {
Eric Laurent1c333e22014-05-20 10:48:17 -07007389 ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007390 } else {
7391 ret = SortedVector::removeAt(ret);
7392 if (ret >= 0) {
7393 refreshTypes();
7394 }
7395 }
7396 return ret;
7397}
7398
7399void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types)
7400{
7401 DeviceVector deviceList;
7402
7403 uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
7404 types &= ~role_bit;
7405
7406 while (types) {
7407 uint32_t i = 31 - __builtin_clz(types);
7408 uint32_t type = 1 << i;
7409 types &= ~type;
Eric Laurent1afeecb2014-05-14 08:52:28 -07007410 add(new DeviceDescriptor(String8(""), type | role_bit));
Eric Laurent3a4311c2014-03-17 12:00:47 -07007411 }
7412}
7413
Eric Laurent1afeecb2014-05-14 08:52:28 -07007414void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name,
7415 const DeviceVector& declaredDevices)
7416{
7417 char *devName = strtok(name, "|");
7418 while (devName != NULL) {
7419 if (strlen(devName) != 0) {
7420 audio_devices_t type = stringToEnum(sDeviceNameToEnumTable,
7421 ARRAY_SIZE(sDeviceNameToEnumTable),
7422 devName);
7423 if (type != AUDIO_DEVICE_NONE) {
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07007424 sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(""), type);
Eric Laurent275e8e92014-11-30 15:14:47 -08007425 if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
7426 type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) {
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07007427 dev->mAddress = String8("0");
7428 }
7429 add(dev);
Eric Laurent1afeecb2014-05-14 08:52:28 -07007430 } else {
7431 sp<DeviceDescriptor> deviceDesc =
7432 declaredDevices.getDeviceFromName(String8(devName));
7433 if (deviceDesc != 0) {
7434 add(deviceDesc);
7435 }
7436 }
7437 }
Eric Laurent275e8e92014-11-30 15:14:47 -08007438 devName = strtok(NULL, "|");
Eric Laurent1afeecb2014-05-14 08:52:28 -07007439 }
7440}
7441
Eric Laurent1c333e22014-05-20 10:48:17 -07007442sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice(
7443 audio_devices_t type, String8 address) const
7444{
7445 sp<DeviceDescriptor> device;
7446 for (size_t i = 0; i < size(); i++) {
7447 if (itemAt(i)->mDeviceType == type) {
Eric Laurent275e8e92014-11-30 15:14:47 -08007448 if (address == "" || itemAt(i)->mAddress == address) {
7449 device = itemAt(i);
7450 if (itemAt(i)->mAddress == address) {
7451 break;
7452 }
Eric Laurent1c333e22014-05-20 10:48:17 -07007453 }
7454 }
7455 }
Eric Laurent275e8e92014-11-30 15:14:47 -08007456 ALOGV("DeviceVector::getDevice() for type %08x address %s found %p",
Eric Laurent1c333e22014-05-20 10:48:17 -07007457 type, address.string(), device.get());
7458 return device;
7459}
7460
Eric Laurent6a94d692014-05-20 11:18:06 -07007461sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId(
7462 audio_port_handle_t id) const
7463{
7464 sp<DeviceDescriptor> device;
7465 for (size_t i = 0; i < size(); i++) {
Mark Salyzynbeb9e302014-06-18 16:33:15 -07007466 ALOGV("DeviceVector::getDeviceFromId(%d) itemAt(%zu)->mId %d", id, i, itemAt(i)->mId);
Eric Laurent6a94d692014-05-20 11:18:06 -07007467 if (itemAt(i)->mId == id) {
7468 device = itemAt(i);
7469 break;
7470 }
7471 }
7472 return device;
7473}
7474
Eric Laurent1c333e22014-05-20 10:48:17 -07007475AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType(
7476 audio_devices_t type) const
7477{
7478 DeviceVector devices;
7479 for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
7480 if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) {
7481 devices.add(itemAt(i));
7482 type &= ~itemAt(i)->mDeviceType;
7483 ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
7484 itemAt(i)->mDeviceType, itemAt(i).get());
7485 }
7486 }
7487 return devices;
7488}
7489
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07007490AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromTypeAddr(
7491 audio_devices_t type, String8 address) const
7492{
7493 DeviceVector devices;
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07007494 for (size_t i = 0; i < size(); i++) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07007495 if (itemAt(i)->mDeviceType == type) {
7496 if (itemAt(i)->mAddress == address) {
Jean-Michel Trivi0fb47752014-07-22 16:19:14 -07007497 devices.add(itemAt(i));
7498 }
7499 }
7500 }
7501 return devices;
7502}
7503
Eric Laurent1afeecb2014-05-14 08:52:28 -07007504sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName(
7505 const String8& name) const
7506{
7507 sp<DeviceDescriptor> device;
7508 for (size_t i = 0; i < size(); i++) {
7509 if (itemAt(i)->mName == name) {
7510 device = itemAt(i);
7511 break;
7512 }
7513 }
7514 return device;
7515}
7516
Eric Laurent6a94d692014-05-20 11:18:06 -07007517void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig(
7518 struct audio_port_config *dstConfig,
7519 const struct audio_port_config *srcConfig) const
Eric Laurent1c333e22014-05-20 10:48:17 -07007520{
Eric Laurent1f2f2232014-06-02 12:01:23 -07007521 dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN;
7522 if (srcConfig != NULL) {
Eric Laurent84c70242014-06-23 08:46:27 -07007523 dstConfig->config_mask |= srcConfig->config_mask;
Eric Laurent1f2f2232014-06-02 12:01:23 -07007524 }
7525
7526 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
7527
Eric Laurent6a94d692014-05-20 11:18:06 -07007528 dstConfig->id = mId;
7529 dstConfig->role = audio_is_output_device(mDeviceType) ?
Eric Laurent1c333e22014-05-20 10:48:17 -07007530 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
Eric Laurent6a94d692014-05-20 11:18:06 -07007531 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
Eric Laurent6a94d692014-05-20 11:18:06 -07007532 dstConfig->ext.device.type = mDeviceType;
7533 dstConfig->ext.device.hw_module = mModule->mHandle;
7534 strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
Eric Laurent1c333e22014-05-20 10:48:17 -07007535}
7536
7537void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const
7538{
Eric Laurent83b88082014-06-20 18:31:16 -07007539 ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType);
Eric Laurent1c333e22014-05-20 10:48:17 -07007540 AudioPort::toAudioPort(port);
7541 port->id = mId;
Eric Laurent6a94d692014-05-20 11:18:06 -07007542 toAudioPortConfig(&port->active_config);
Eric Laurent1c333e22014-05-20 10:48:17 -07007543 port->ext.device.type = mDeviceType;
Eric Laurent6a94d692014-05-20 11:18:06 -07007544 port->ext.device.hw_module = mModule->mHandle;
Eric Laurent1c333e22014-05-20 10:48:17 -07007545 strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
7546}
7547
Eric Laurent1afeecb2014-05-14 08:52:28 -07007548status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const
Eric Laurent3a4311c2014-03-17 12:00:47 -07007549{
7550 const size_t SIZE = 256;
7551 char buffer[SIZE];
Eric Laurent1afeecb2014-05-14 08:52:28 -07007552 String8 result;
Eric Laurent3a4311c2014-03-17 12:00:47 -07007553
Eric Laurent1afeecb2014-05-14 08:52:28 -07007554 snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
7555 result.append(buffer);
7556 if (mId != 0) {
7557 snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
7558 result.append(buffer);
7559 }
7560 snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
7561 enumToString(sDeviceNameToEnumTable,
7562 ARRAY_SIZE(sDeviceNameToEnumTable),
7563 mDeviceType));
7564 result.append(buffer);
7565 if (mAddress.size() != 0) {
7566 snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
7567 result.append(buffer);
7568 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07007569 write(fd, result.string(), result.size());
7570 AudioPort::dump(fd, spaces);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007571
7572 return NO_ERROR;
7573}
7574
Eric Laurent4d416952014-08-10 14:07:09 -07007575status_t AudioPolicyManager::AudioPatch::dump(int fd, int spaces, int index) const
7576{
7577 const size_t SIZE = 256;
7578 char buffer[SIZE];
7579 String8 result;
7580
7581
7582 snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1);
7583 result.append(buffer);
7584 snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle);
7585 result.append(buffer);
7586 snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle);
7587 result.append(buffer);
7588 snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid);
7589 result.append(buffer);
7590 snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources);
7591 result.append(buffer);
7592 for (size_t i = 0; i < mPatch.num_sources; i++) {
7593 if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) {
7594 snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
7595 mPatch.sources[i].id, enumToString(sDeviceNameToEnumTable,
7596 ARRAY_SIZE(sDeviceNameToEnumTable),
7597 mPatch.sources[i].ext.device.type));
7598 } else {
7599 snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
7600 mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle);
7601 }
7602 result.append(buffer);
7603 }
7604 snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks);
7605 result.append(buffer);
7606 for (size_t i = 0; i < mPatch.num_sinks; i++) {
7607 if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) {
7608 snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
7609 mPatch.sinks[i].id, enumToString(sDeviceNameToEnumTable,
7610 ARRAY_SIZE(sDeviceNameToEnumTable),
7611 mPatch.sinks[i].ext.device.type));
7612 } else {
7613 snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
7614 mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle);
7615 }
7616 result.append(buffer);
7617 }
7618
7619 write(fd, result.string(), result.size());
7620 return NO_ERROR;
7621}
Eric Laurent3a4311c2014-03-17 12:00:47 -07007622
7623// --- audio_policy.conf file parsing
7624
Eric Laurent5dbe4712014-09-19 19:04:57 -07007625uint32_t AudioPolicyManager::parseOutputFlagNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07007626{
7627 uint32_t flag = 0;
7628
7629 // it is OK to cast name to non const here as we are not going to use it after
7630 // strtok() modifies it
7631 char *flagName = strtok(name, "|");
7632 while (flagName != NULL) {
7633 if (strlen(flagName) != 0) {
Eric Laurent5dbe4712014-09-19 19:04:57 -07007634 flag |= stringToEnum(sOutputFlagNameToEnumTable,
7635 ARRAY_SIZE(sOutputFlagNameToEnumTable),
Eric Laurente552edb2014-03-10 17:42:56 -07007636 flagName);
7637 }
7638 flagName = strtok(NULL, "|");
7639 }
7640 //force direct flag if offload flag is set: offloading implies a direct output stream
7641 // and all common behaviors are driven by checking only the direct flag
7642 // this should normally be set appropriately in the policy configuration file
7643 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
7644 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
7645 }
7646
Eric Laurent5dbe4712014-09-19 19:04:57 -07007647 return flag;
7648}
7649
7650uint32_t AudioPolicyManager::parseInputFlagNames(char *name)
7651{
7652 uint32_t flag = 0;
7653
7654 // it is OK to cast name to non const here as we are not going to use it after
7655 // strtok() modifies it
7656 char *flagName = strtok(name, "|");
7657 while (flagName != NULL) {
7658 if (strlen(flagName) != 0) {
7659 flag |= stringToEnum(sInputFlagNameToEnumTable,
7660 ARRAY_SIZE(sInputFlagNameToEnumTable),
7661 flagName);
7662 }
7663 flagName = strtok(NULL, "|");
7664 }
7665 return flag;
Eric Laurente552edb2014-03-10 17:42:56 -07007666}
7667
Eric Laurente0720872014-03-11 09:30:41 -07007668audio_devices_t AudioPolicyManager::parseDeviceNames(char *name)
Eric Laurente552edb2014-03-10 17:42:56 -07007669{
7670 uint32_t device = 0;
7671
7672 char *devName = strtok(name, "|");
7673 while (devName != NULL) {
7674 if (strlen(devName) != 0) {
7675 device |= stringToEnum(sDeviceNameToEnumTable,
7676 ARRAY_SIZE(sDeviceNameToEnumTable),
7677 devName);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007678 }
Eric Laurente552edb2014-03-10 17:42:56 -07007679 devName = strtok(NULL, "|");
Eric Laurent3a4311c2014-03-17 12:00:47 -07007680 }
Eric Laurente552edb2014-03-10 17:42:56 -07007681 return device;
7682}
7683
Eric Laurente0720872014-03-11 09:30:41 -07007684void AudioPolicyManager::loadHwModule(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07007685{
Eric Laurente552edb2014-03-10 17:42:56 -07007686 status_t status = NAME_NOT_FOUND;
Eric Laurent1afeecb2014-05-14 08:52:28 -07007687 cnode *node;
Eric Laurent1f2f2232014-06-02 12:01:23 -07007688 sp<HwModule> module = new HwModule(root->name);
Eric Laurente552edb2014-03-10 17:42:56 -07007689
Eric Laurent1afeecb2014-05-14 08:52:28 -07007690 node = config_find(root, DEVICES_TAG);
7691 if (node != NULL) {
7692 node = node->first_child;
7693 while (node) {
7694 ALOGV("loadHwModule() loading device %s", node->name);
7695 status_t tmpStatus = module->loadDevice(node);
7696 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
7697 status = tmpStatus;
7698 }
7699 node = node->next;
7700 }
7701 }
7702 node = config_find(root, OUTPUTS_TAG);
Eric Laurente552edb2014-03-10 17:42:56 -07007703 if (node != NULL) {
Eric Laurente552edb2014-03-10 17:42:56 -07007704 node = node->first_child;
7705 while (node) {
7706 ALOGV("loadHwModule() loading output %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07007707 status_t tmpStatus = module->loadOutput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07007708 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
7709 status = tmpStatus;
7710 }
7711 node = node->next;
7712 }
7713 }
7714 node = config_find(root, INPUTS_TAG);
7715 if (node != NULL) {
7716 node = node->first_child;
7717 while (node) {
7718 ALOGV("loadHwModule() loading input %s", node->name);
Eric Laurent1afeecb2014-05-14 08:52:28 -07007719 status_t tmpStatus = module->loadInput(node);
Eric Laurente552edb2014-03-10 17:42:56 -07007720 if (status == NAME_NOT_FOUND || status == NO_ERROR) {
7721 status = tmpStatus;
7722 }
7723 node = node->next;
7724 }
7725 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07007726 loadGlobalConfig(root, module);
7727
Eric Laurente552edb2014-03-10 17:42:56 -07007728 if (status == NO_ERROR) {
7729 mHwModules.add(module);
Eric Laurente552edb2014-03-10 17:42:56 -07007730 }
7731}
7732
Eric Laurente0720872014-03-11 09:30:41 -07007733void AudioPolicyManager::loadHwModules(cnode *root)
Eric Laurente552edb2014-03-10 17:42:56 -07007734{
7735 cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
7736 if (node == NULL) {
7737 return;
7738 }
7739
7740 node = node->first_child;
7741 while (node) {
7742 ALOGV("loadHwModules() loading module %s", node->name);
7743 loadHwModule(node);
7744 node = node->next;
7745 }
7746}
7747
Eric Laurent1f2f2232014-06-02 12:01:23 -07007748void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module)
Eric Laurente552edb2014-03-10 17:42:56 -07007749{
7750 cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
Eric Laurenteb108a42014-06-06 14:56:52 -07007751
Eric Laurente552edb2014-03-10 17:42:56 -07007752 if (node == NULL) {
7753 return;
7754 }
Eric Laurent1afeecb2014-05-14 08:52:28 -07007755 DeviceVector declaredDevices;
7756 if (module != NULL) {
7757 declaredDevices = module->mDeclaredDevices;
7758 }
7759
Eric Laurente552edb2014-03-10 17:42:56 -07007760 node = node->first_child;
7761 while (node) {
7762 if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07007763 mAvailableOutputDevices.loadDevicesFromName((char *)node->value,
7764 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007765 ALOGV("loadGlobalConfig() Attached Output Devices %08x",
7766 mAvailableOutputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07007767 } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
Eric Laurent3a4311c2014-03-17 12:00:47 -07007768 audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
Eric Laurente552edb2014-03-10 17:42:56 -07007769 ARRAY_SIZE(sDeviceNameToEnumTable),
7770 (char *)node->value);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007771 if (device != AUDIO_DEVICE_NONE) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07007772 mDefaultOutputDevice = new DeviceDescriptor(String8(""), device);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007773 } else {
7774 ALOGW("loadGlobalConfig() default device not specified");
7775 }
Eric Laurent1c333e22014-05-20 10:48:17 -07007776 ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType);
Eric Laurente552edb2014-03-10 17:42:56 -07007777 } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
Eric Laurent1afeecb2014-05-14 08:52:28 -07007778 mAvailableInputDevices.loadDevicesFromName((char *)node->value,
7779 declaredDevices);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007780 ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types());
Eric Laurente552edb2014-03-10 17:42:56 -07007781 } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
7782 mSpeakerDrcEnabled = stringToBool((char *)node->value);
7783 ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
Eric Laurenteb108a42014-06-06 14:56:52 -07007784 } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) {
7785 uint32_t major, minor;
7786 sscanf((char *)node->value, "%u.%u", &major, &minor);
7787 module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor);
7788 ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u",
7789 module->mHalVersion, major, minor);
Eric Laurente552edb2014-03-10 17:42:56 -07007790 }
7791 node = node->next;
7792 }
7793}
7794
Eric Laurente0720872014-03-11 09:30:41 -07007795status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path)
Eric Laurente552edb2014-03-10 17:42:56 -07007796{
7797 cnode *root;
7798 char *data;
7799
7800 data = (char *)load_file(path, NULL);
7801 if (data == NULL) {
7802 return -ENODEV;
7803 }
7804 root = config_node("", "");
7805 config_load(root, data);
7806
Eric Laurente552edb2014-03-10 17:42:56 -07007807 loadHwModules(root);
Eric Laurent1afeecb2014-05-14 08:52:28 -07007808 // legacy audio_policy.conf files have one global_configuration section
7809 loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY));
Eric Laurente552edb2014-03-10 17:42:56 -07007810 config_free(root);
7811 free(root);
7812 free(data);
7813
7814 ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
7815
7816 return NO_ERROR;
7817}
7818
Eric Laurente0720872014-03-11 09:30:41 -07007819void AudioPolicyManager::defaultAudioPolicyConfig(void)
Eric Laurente552edb2014-03-10 17:42:56 -07007820{
Eric Laurent1f2f2232014-06-02 12:01:23 -07007821 sp<HwModule> module;
Eric Laurent1c333e22014-05-20 10:48:17 -07007822 sp<IOProfile> profile;
Eric Laurent1f2f2232014-06-02 12:01:23 -07007823 sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""),
7824 AUDIO_DEVICE_IN_BUILTIN_MIC);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007825 mAvailableOutputDevices.add(mDefaultOutputDevice);
7826 mAvailableInputDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07007827
7828 module = new HwModule("primary");
7829
Eric Laurent1afeecb2014-05-14 08:52:28 -07007830 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module);
Eric Laurente552edb2014-03-10 17:42:56 -07007831 profile->mSamplingRates.add(44100);
7832 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
7833 profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007834 profile->mSupportedDevices.add(mDefaultOutputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07007835 profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
7836 module->mOutputProfiles.add(profile);
7837
Eric Laurent1afeecb2014-05-14 08:52:28 -07007838 profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module);
Eric Laurente552edb2014-03-10 17:42:56 -07007839 profile->mSamplingRates.add(8000);
7840 profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
7841 profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
Eric Laurent3a4311c2014-03-17 12:00:47 -07007842 profile->mSupportedDevices.add(defaultInputDevice);
Eric Laurente552edb2014-03-10 17:42:56 -07007843 module->mInputProfiles.add(profile);
7844
7845 mHwModules.add(module);
7846}
7847
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07007848audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr)
7849{
7850 // flags to stream type mapping
7851 if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) {
7852 return AUDIO_STREAM_ENFORCED_AUDIBLE;
7853 }
7854 if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) {
7855 return AUDIO_STREAM_BLUETOOTH_SCO;
7856 }
7857
7858 // usage to stream type mapping
7859 switch (attr->usage) {
7860 case AUDIO_USAGE_MEDIA:
7861 case AUDIO_USAGE_GAME:
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07007862 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
7863 return AUDIO_STREAM_MUSIC;
Eric Laurent223fd5c2014-11-11 13:43:36 -08007864 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
Eric Laurente83b55d2014-11-14 10:06:21 -08007865 if (isStreamActive(AUDIO_STREAM_ALARM)) {
7866 return AUDIO_STREAM_ALARM;
7867 }
7868 if (isStreamActive(AUDIO_STREAM_RING)) {
7869 return AUDIO_STREAM_RING;
7870 }
7871 if (isInCall()) {
7872 return AUDIO_STREAM_VOICE_CALL;
7873 }
Eric Laurent223fd5c2014-11-11 13:43:36 -08007874 return AUDIO_STREAM_ACCESSIBILITY;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07007875 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
7876 return AUDIO_STREAM_SYSTEM;
7877 case AUDIO_USAGE_VOICE_COMMUNICATION:
7878 return AUDIO_STREAM_VOICE_CALL;
7879
7880 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
7881 return AUDIO_STREAM_DTMF;
7882
7883 case AUDIO_USAGE_ALARM:
7884 return AUDIO_STREAM_ALARM;
7885 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
7886 return AUDIO_STREAM_RING;
7887
7888 case AUDIO_USAGE_NOTIFICATION:
7889 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
7890 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
7891 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
7892 case AUDIO_USAGE_NOTIFICATION_EVENT:
7893 return AUDIO_STREAM_NOTIFICATION;
7894
7895 case AUDIO_USAGE_UNKNOWN:
7896 default:
7897 return AUDIO_STREAM_MUSIC;
7898 }
7899}
Eric Laurente83b55d2014-11-14 10:06:21 -08007900
7901bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) {
7902 // has flags that map to a strategy?
7903 if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) {
7904 return true;
7905 }
7906
7907 // has known usage?
7908 switch (paa->usage) {
7909 case AUDIO_USAGE_UNKNOWN:
7910 case AUDIO_USAGE_MEDIA:
7911 case AUDIO_USAGE_VOICE_COMMUNICATION:
7912 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
7913 case AUDIO_USAGE_ALARM:
7914 case AUDIO_USAGE_NOTIFICATION:
7915 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
7916 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
7917 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
7918 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
7919 case AUDIO_USAGE_NOTIFICATION_EVENT:
7920 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
7921 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
7922 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
7923 case AUDIO_USAGE_GAME:
Eric Laurent275e8e92014-11-30 15:14:47 -08007924 case AUDIO_USAGE_VIRTUAL_SOURCE:
Eric Laurente83b55d2014-11-14 10:06:21 -08007925 break;
7926 default:
7927 return false;
7928 }
7929 return true;
7930}
7931
Eric Laurente552edb2014-03-10 17:42:56 -07007932}; // namespace android