| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioPolicyManager" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | //#define VERY_VERBOSE_LOGGING | 
|  | 21 | #ifdef VERY_VERBOSE_LOGGING | 
|  | 22 | #define ALOGVV ALOGV | 
|  | 23 | #else | 
|  | 24 | #define ALOGVV(a...) do { } while(0) | 
|  | 25 | #endif | 
|  | 26 |  | 
|  | 27 | // A device mask for all audio input devices that are considered "virtual" when evaluating | 
|  | 28 | // active inputs in getActiveInput() | 
|  | 29 | #define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL  AUDIO_DEVICE_IN_REMOTE_SUBMIX | 
|  | 30 | // A device mask for all audio output devices that are considered "remote" when evaluating | 
|  | 31 | // active output devices in isStreamActiveRemotely() | 
|  | 32 | #define APM_AUDIO_OUT_DEVICE_REMOTE_ALL  AUDIO_DEVICE_OUT_REMOTE_SUBMIX | 
|  | 33 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 34 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 35 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 36 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 38 | #include <utils/Log.h> | 
|  | 39 | #include <hardware/audio.h> | 
|  | 40 | #include <hardware/audio_effect.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 41 | #include <media/AudioParameter.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 42 | #include "AudioPolicyManager.h" | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 43 | #include "audio_policy_conf.h" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 48 | // Definitions for audio_policy.conf file parsing | 
|  | 49 | // ---------------------------------------------------------------------------- | 
|  | 50 |  | 
|  | 51 | struct StringToEnum { | 
|  | 52 | const char *name; | 
|  | 53 | uint32_t value; | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | #define STRING_TO_ENUM(string) { #string, string } | 
|  | 57 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | 
|  | 58 |  | 
|  | 59 | const StringToEnum sDeviceNameToEnumTable[] = { | 
|  | 60 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE), | 
|  | 61 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER), | 
|  | 62 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET), | 
|  | 63 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), | 
|  | 64 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO), | 
|  | 65 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET), | 
|  | 66 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT), | 
|  | 67 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO), | 
|  | 68 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP), | 
|  | 69 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES), | 
|  | 70 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), | 
|  | 71 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP), | 
|  | 72 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 73 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 74 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET), | 
|  | 75 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET), | 
|  | 76 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY), | 
|  | 77 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE), | 
|  | 78 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB), | 
|  | 79 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 80 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX), | 
|  | 81 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE), | 
|  | 82 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC), | 
|  | 83 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF), | 
|  | 84 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 85 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC), | 
|  | 86 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET), | 
|  | 87 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO), | 
|  | 88 | STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET), | 
|  | 89 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 90 | STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 91 | STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 92 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 93 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC), | 
|  | 94 | STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX), | 
|  | 95 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET), | 
|  | 96 | STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET), | 
|  | 97 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY), | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 98 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 99 | STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER), | 
|  | 100 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER), | 
|  | 101 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE), | 
|  | 102 | STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF), | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 103 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP), | 
| Terry Heo | 7999a22 | 2014-06-27 15:23:36 +0900 | [diff] [blame] | 104 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 105 | }; | 
|  | 106 |  | 
|  | 107 | const StringToEnum sFlagNameToEnumTable[] = { | 
|  | 108 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), | 
|  | 109 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), | 
|  | 110 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), | 
|  | 111 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), | 
|  | 112 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), | 
|  | 113 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | const StringToEnum sFormatNameToEnumTable[] = { | 
|  | 117 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), | 
|  | 118 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), | 
|  | 119 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT), | 
|  | 120 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT), | 
|  | 121 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT), | 
|  | 122 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED), | 
|  | 123 | STRING_TO_ENUM(AUDIO_FORMAT_MP3), | 
|  | 124 | STRING_TO_ENUM(AUDIO_FORMAT_AAC), | 
| aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 125 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN), | 
|  | 126 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC), | 
|  | 127 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR), | 
|  | 128 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP), | 
|  | 129 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1), | 
|  | 130 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE), | 
|  | 131 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC), | 
|  | 132 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD), | 
|  | 133 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2), | 
|  | 134 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 135 | STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 136 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1), | 
|  | 137 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2), | 
|  | 138 | STRING_TO_ENUM(AUDIO_FORMAT_OPUS), | 
|  | 139 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), | 
|  | 140 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 141 | }; | 
|  | 142 |  | 
|  | 143 | const StringToEnum sOutChannelsNameToEnumTable[] = { | 
|  | 144 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO), | 
|  | 145 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), | 
|  | 146 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), | 
|  | 147 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), | 
|  | 148 | }; | 
|  | 149 |  | 
|  | 150 | const StringToEnum sInChannelsNameToEnumTable[] = { | 
|  | 151 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), | 
|  | 152 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), | 
|  | 153 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), | 
|  | 154 | }; | 
|  | 155 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 156 | const StringToEnum sGainModeNameToEnumTable[] = { | 
|  | 157 | STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT), | 
|  | 158 | STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS), | 
|  | 159 | STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP), | 
|  | 160 | }; | 
|  | 161 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 |  | 
|  | 163 | uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table, | 
|  | 164 | size_t size, | 
|  | 165 | const char *name) | 
|  | 166 | { | 
|  | 167 | for (size_t i = 0; i < size; i++) { | 
|  | 168 | if (strcmp(table[i].name, name) == 0) { | 
|  | 169 | ALOGV("stringToEnum() found %s", table[i].name); | 
|  | 170 | return table[i].value; | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 | return 0; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | const char *AudioPolicyManager::enumToString(const struct StringToEnum *table, | 
|  | 177 | size_t size, | 
|  | 178 | uint32_t value) | 
|  | 179 | { | 
|  | 180 | for (size_t i = 0; i < size; i++) { | 
|  | 181 | if (table[i].value == value) { | 
|  | 182 | return table[i].name; | 
|  | 183 | } | 
|  | 184 | } | 
|  | 185 | return ""; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | bool AudioPolicyManager::stringToBool(const char *value) | 
|  | 189 | { | 
|  | 190 | return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0)); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 |  | 
|  | 194 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 195 | // AudioPolicyInterface implementation | 
|  | 196 | // ---------------------------------------------------------------------------- | 
|  | 197 |  | 
|  | 198 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 199 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 200 | audio_policy_dev_state_t state, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | const char *device_address) | 
|  | 202 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 203 | String8 address = String8(device_address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address); | 
|  | 206 |  | 
|  | 207 | // connect/disconnect only 1 device at a time | 
|  | 208 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 209 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | // handle output devices | 
|  | 211 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 212 | SortedVector <audio_io_handle_t> outputs; | 
|  | 213 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 214 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
|  | 215 | devDesc->mAddress = address; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 216 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 217 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 218 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 219 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 220 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 221 | switch (state) | 
|  | 222 | { | 
|  | 223 | // handle output device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 224 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 225 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 226 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 227 | return INVALID_OPERATION; | 
|  | 228 | } | 
|  | 229 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 230 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 231 | if (checkOutputsForDevice(device, state, outputs, address) != NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 232 | return INVALID_OPERATION; | 
|  | 233 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 234 | // outputs should never be empty here | 
|  | 235 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 236 | "checkOutputsForDevice() returned no outputs but status OK"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 237 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | outputs.size()); | 
|  | 239 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 240 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 241 | if (index >= 0) { | 
|  | 242 | mAvailableOutputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 243 | sp<HwModule> module = getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 244 | ALOG_ASSERT(module != NULL, "setDeviceConnectionState():" | 
|  | 245 | "could not find HW module for device %08x", device); | 
|  | 246 | mAvailableOutputDevices[index]->mModule = module; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 247 | } else { | 
|  | 248 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
|  | 251 | break; | 
|  | 252 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 253 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 254 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 255 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 256 | return INVALID_OPERATION; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | ALOGV("setDeviceConnectionState() disconnecting device %x", device); | 
|  | 260 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 261 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 262 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 263 | checkOutputsForDevice(device, state, outputs, address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 264 | // not currently handling multiple simultaneous submixes: ignoring remote submix | 
|  | 265 | //   case and address | 
|  | 266 | } break; | 
|  | 267 |  | 
|  | 268 | default: | 
|  | 269 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 270 | return BAD_VALUE; | 
|  | 271 | } | 
|  | 272 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 273 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 274 | // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 275 | checkA2dpSuspend(); | 
|  | 276 | checkOutputForAllStrategies(); | 
|  | 277 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 278 | if (!outputs.isEmpty()) { | 
|  | 279 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 280 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 281 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 282 | // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 283 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 285 | (desc->mDirectOpenCount == 0))) { | 
|  | 286 | closeOutput(outputs[i]); | 
|  | 287 | } | 
|  | 288 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 289 | // check again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 290 | checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 291 | } | 
|  | 292 |  | 
|  | 293 | updateDevicesAndOutputs(); | 
|  | 294 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 295 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 296 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 297 | // a valid device selection on those outputs. | 
|  | 298 | setOutputDevice(mOutputs.keyAt(i), | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 299 | getNewOutputDevice(mOutputs.keyAt(i), true /*fromCache*/), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | !mOutputs.valueAt(i)->isDuplicated(), | 
|  | 301 | 0); | 
|  | 302 | } | 
|  | 303 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 304 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 305 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 306 | }  // end if is output device | 
|  | 307 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 308 | // handle input devices | 
|  | 309 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 310 | SortedVector <audio_io_handle_t> inputs; | 
|  | 311 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 312 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
|  | 313 | devDesc->mAddress = address; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 314 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 315 | switch (state) | 
|  | 316 | { | 
|  | 317 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 318 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 319 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 320 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 321 | return INVALID_OPERATION; | 
|  | 322 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 323 | sp<HwModule> module = getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 324 | if (module == NULL) { | 
|  | 325 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 326 | device); | 
|  | 327 | return INVALID_OPERATION; | 
|  | 328 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 329 | if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) { | 
|  | 330 | return INVALID_OPERATION; | 
|  | 331 | } | 
|  | 332 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 333 | index = mAvailableInputDevices.add(devDesc); | 
|  | 334 | if (index >= 0) { | 
|  | 335 | mAvailableInputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 336 | mAvailableInputDevices[index]->mModule = module; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 337 | } else { | 
|  | 338 | return NO_MEMORY; | 
|  | 339 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 340 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 341 |  | 
|  | 342 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 343 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 344 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 345 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 346 | return INVALID_OPERATION; | 
|  | 347 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 348 | checkInputsForDevice(device, state, inputs, address); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 349 | mAvailableInputDevices.remove(devDesc); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 350 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 351 |  | 
|  | 352 | default: | 
|  | 353 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 354 | return BAD_VALUE; | 
|  | 355 | } | 
|  | 356 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 357 | closeAllInputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 358 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 359 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 361 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 362 |  | 
|  | 363 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 364 | return BAD_VALUE; | 
|  | 365 | } | 
|  | 366 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 367 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 368 | const char *device_address) | 
|  | 369 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 370 | audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 371 | String8 address = String8(device_address); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 372 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
|  | 373 | devDesc->mAddress = String8(device_address); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 374 | ssize_t index; | 
|  | 375 | DeviceVector *deviceVector; | 
|  | 376 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 377 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 378 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 379 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 380 | deviceVector = &mAvailableInputDevices; | 
|  | 381 | } else { | 
|  | 382 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 383 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 386 | index = deviceVector->indexOf(devDesc); | 
|  | 387 | if (index >= 0) { | 
|  | 388 | return AUDIO_POLICY_DEVICE_STATE_AVAILABLE; | 
|  | 389 | } else { | 
|  | 390 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 391 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 394 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 395 | { | 
|  | 396 | ALOGV("setPhoneState() state %d", state); | 
|  | 397 | audio_devices_t newDevice = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 398 | if (state < 0 || state >= AUDIO_MODE_CNT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 399 | ALOGW("setPhoneState() invalid state %d", state); | 
|  | 400 | return; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | if (state == mPhoneState ) { | 
|  | 404 | ALOGW("setPhoneState() setting same state %d", state); | 
|  | 405 | return; | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | // if leaving call state, handle special case of active streams | 
|  | 409 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 410 | if (isInCall()) { | 
|  | 411 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 412 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 413 | handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 414 | } | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | // store previous phone state for management of sonification strategy below | 
|  | 418 | int oldState = mPhoneState; | 
|  | 419 | mPhoneState = state; | 
|  | 420 | bool force = false; | 
|  | 421 |  | 
|  | 422 | // are we entering or starting a call | 
|  | 423 | if (!isStateInCall(oldState) && isStateInCall(state)) { | 
|  | 424 | ALOGV("  Entering call in setPhoneState()"); | 
|  | 425 | // force routing command to audio hardware when starting a call | 
|  | 426 | // even if no device change is needed | 
|  | 427 | force = true; | 
|  | 428 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 429 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = | 
|  | 430 | sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; | 
|  | 431 | } | 
|  | 432 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { | 
|  | 433 | ALOGV("  Exiting call in setPhoneState()"); | 
|  | 434 | // force routing command to audio hardware when exiting a call | 
|  | 435 | // even if no device change is needed | 
|  | 436 | force = true; | 
|  | 437 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 438 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = | 
|  | 439 | sVolumeProfiles[AUDIO_STREAM_DTMF][j]; | 
|  | 440 | } | 
|  | 441 | } else if (isStateInCall(state) && (state != oldState)) { | 
|  | 442 | ALOGV("  Switching between telephony and VoIP in setPhoneState()"); | 
|  | 443 | // force routing command to audio hardware when switching between telephony and VoIP | 
|  | 444 | // even if no device change is needed | 
|  | 445 | force = true; | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | // check for device and output changes triggered by new phone state | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 449 | newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 450 | checkA2dpSuspend(); | 
|  | 451 | checkOutputForAllStrategies(); | 
|  | 452 | updateDevicesAndOutputs(); | 
|  | 453 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 454 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 455 |  | 
|  | 456 | // force routing command to audio hardware when ending call | 
|  | 457 | // even if no device change is needed | 
|  | 458 | if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) { | 
|  | 459 | newDevice = hwOutputDesc->device(); | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | int delayMs = 0; | 
|  | 463 | if (isStateInCall(state)) { | 
|  | 464 | nsecs_t sysTime = systemTime(); | 
|  | 465 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 466 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 467 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 468 | // latency of any output where either strategy is active. | 
|  | 469 | // This avoid sending the ring tone or music tail into the earpiece or headset. | 
|  | 470 | if ((desc->isStrategyActive(STRATEGY_MEDIA, | 
|  | 471 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 472 | sysTime) || | 
|  | 473 | desc->isStrategyActive(STRATEGY_SONIFICATION, | 
|  | 474 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 475 | sysTime)) && | 
|  | 476 | (delayMs < (int)desc->mLatency*2)) { | 
|  | 477 | delayMs = desc->mLatency*2; | 
|  | 478 | } | 
|  | 479 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); | 
|  | 480 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, | 
|  | 481 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
|  | 482 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); | 
|  | 483 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, | 
|  | 484 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 485 | } | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | // change routing is necessary | 
|  | 489 | setOutputDevice(mPrimaryOutput, newDevice, force, delayMs); | 
|  | 490 |  | 
|  | 491 | // if entering in call state, handle special case of active streams | 
|  | 492 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 493 | if (isStateInCall(state)) { | 
|  | 494 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 495 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 496 | handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 497 | } | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 501 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 502 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 503 | mLimitRingtoneVolume = true; | 
|  | 504 | } else { | 
|  | 505 | mLimitRingtoneVolume = false; | 
|  | 506 | } | 
|  | 507 | } | 
|  | 508 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 509 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 510 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 511 | { | 
|  | 512 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); | 
|  | 513 |  | 
|  | 514 | bool forceVolumeReeval = false; | 
|  | 515 | switch(usage) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 516 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: | 
|  | 517 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && | 
|  | 518 | config != AUDIO_POLICY_FORCE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 519 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); | 
|  | 520 | return; | 
|  | 521 | } | 
|  | 522 | forceVolumeReeval = true; | 
|  | 523 | mForceUse[usage] = config; | 
|  | 524 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 525 | case AUDIO_POLICY_FORCE_FOR_MEDIA: | 
|  | 526 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && | 
|  | 527 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 528 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && | 
|  | 529 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && | 
| Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 530 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); | 
|  | 532 | return; | 
|  | 533 | } | 
|  | 534 | mForceUse[usage] = config; | 
|  | 535 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 536 | case AUDIO_POLICY_FORCE_FOR_RECORD: | 
|  | 537 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 538 | config != AUDIO_POLICY_FORCE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 539 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); | 
|  | 540 | return; | 
|  | 541 | } | 
|  | 542 | mForceUse[usage] = config; | 
|  | 543 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 544 | case AUDIO_POLICY_FORCE_FOR_DOCK: | 
|  | 545 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && | 
|  | 546 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && | 
|  | 547 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 548 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && | 
|  | 549 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 550 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); | 
|  | 551 | } | 
|  | 552 | forceVolumeReeval = true; | 
|  | 553 | mForceUse[usage] = config; | 
|  | 554 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 555 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: | 
|  | 556 | if (config != AUDIO_POLICY_FORCE_NONE && | 
|  | 557 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 558 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); | 
|  | 559 | } | 
|  | 560 | forceVolumeReeval = true; | 
|  | 561 | mForceUse[usage] = config; | 
|  | 562 | break; | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 563 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: | 
|  | 564 | if (config != AUDIO_POLICY_FORCE_NONE && | 
|  | 565 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { | 
|  | 566 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); | 
|  | 567 | } | 
|  | 568 | mForceUse[usage] = config; | 
|  | 569 | break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | default: | 
|  | 571 | ALOGW("setForceUse() invalid usage %d", usage); | 
|  | 572 | break; | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | // check for device and output changes triggered by new force usage | 
|  | 576 | checkA2dpSuspend(); | 
|  | 577 | checkOutputForAllStrategies(); | 
|  | 578 | updateDevicesAndOutputs(); | 
|  | 579 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 580 | audio_io_handle_t output = mOutputs.keyAt(i); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 581 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); | 
|  | 583 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
|  | 584 | applyStreamVolumes(output, newDevice, 0, true); | 
|  | 585 | } | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | audio_io_handle_t activeInput = getActiveInput(); | 
|  | 589 | if (activeInput != 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 590 | setInputDevice(activeInput, getNewInputDevice(activeInput)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | } | 
|  | 592 |  | 
|  | 593 | } | 
|  | 594 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 595 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 596 | { | 
|  | 597 | return mForceUse[usage]; | 
|  | 598 | } | 
|  | 599 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 600 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 601 | { | 
|  | 602 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 606 | // not explicitly request a direct output | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 607 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 608 | audio_devices_t device, | 
|  | 609 | uint32_t samplingRate, | 
|  | 610 | audio_format_t format, | 
|  | 611 | audio_channel_mask_t channelMask, | 
|  | 612 | audio_output_flags_t flags) | 
|  | 613 | { | 
|  | 614 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 615 | if (mHwModules[i]->mHandle == 0) { | 
|  | 616 | continue; | 
|  | 617 | } | 
|  | 618 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 619 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 620 | bool found = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 621 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 622 | if (profile->isCompatibleProfile(device, samplingRate, format, | 
|  | 623 | channelMask, | 
|  | 624 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 625 | found = true; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | } | 
|  | 627 | } else { | 
|  | 628 | if (profile->isCompatibleProfile(device, samplingRate, format, | 
|  | 629 | channelMask, | 
|  | 630 | AUDIO_OUTPUT_FLAG_DIRECT)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 631 | found = true; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | } | 
|  | 633 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 634 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { | 
|  | 635 | return profile; | 
|  | 636 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | } | 
|  | 638 | } | 
|  | 639 | return 0; | 
|  | 640 | } | 
|  | 641 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 642 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 643 | uint32_t samplingRate, | 
|  | 644 | audio_format_t format, | 
|  | 645 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 646 | audio_output_flags_t flags, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 647 | const audio_offload_info_t *offloadInfo) | 
|  | 648 | { | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 649 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 650 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 651 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 652 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 653 | device, stream, samplingRate, format, channelMask, flags); | 
|  | 654 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 655 | return getOutputForDevice(device, stream, samplingRate,format, channelMask, flags, | 
|  | 656 | offloadInfo); | 
|  | 657 | } | 
|  | 658 |  | 
|  | 659 | audio_io_handle_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 660 | uint32_t samplingRate, | 
|  | 661 | audio_format_t format, | 
|  | 662 | audio_channel_mask_t channelMask, | 
|  | 663 | audio_output_flags_t flags, | 
|  | 664 | const audio_offload_info_t *offloadInfo) | 
|  | 665 | { | 
|  | 666 | if (attr == NULL) { | 
|  | 667 | ALOGE("getOutputForAttr() called with NULL audio attributes"); | 
|  | 668 | return 0; | 
|  | 669 | } | 
|  | 670 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s", | 
|  | 671 | attr->usage, attr->content_type, attr->tags); | 
|  | 672 |  | 
|  | 673 | // TODO this is where filtering for custom policies (rerouting, dynamic sources) will go | 
|  | 674 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(attr); | 
|  | 675 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 676 | ALOGV("getOutputForAttr() device %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 677 | device, samplingRate, format, channelMask, flags); | 
|  | 678 |  | 
|  | 679 | audio_stream_type_t stream = streamTypefromAttributesInt(attr); | 
|  | 680 | return getOutputForDevice(device, stream, samplingRate, format, channelMask, flags, | 
|  | 681 | offloadInfo); | 
|  | 682 | } | 
|  | 683 |  | 
|  | 684 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 685 | audio_devices_t device, | 
|  | 686 | audio_stream_type_t stream, | 
|  | 687 | uint32_t samplingRate, | 
|  | 688 | audio_format_t format, | 
|  | 689 | audio_channel_mask_t channelMask, | 
|  | 690 | audio_output_flags_t flags, | 
|  | 691 | const audio_offload_info_t *offloadInfo) | 
|  | 692 | { | 
|  | 693 | audio_io_handle_t output = 0; | 
|  | 694 | uint32_t latency = 0; | 
|  | 695 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | #ifdef AUDIO_POLICY_TEST | 
|  | 697 | if (mCurOutput != 0) { | 
|  | 698 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
|  | 699 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
|  | 700 |  | 
|  | 701 | if (mTestOutputs[mCurOutput] == 0) { | 
|  | 702 | ALOGV("getOutput() opening test output"); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 703 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 704 | outputDesc->mDevice = mTestDevice; | 
|  | 705 | outputDesc->mSamplingRate = mTestSamplingRate; | 
|  | 706 | outputDesc->mFormat = mTestFormat; | 
|  | 707 | outputDesc->mChannelMask = mTestChannels; | 
|  | 708 | outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 709 | outputDesc->mFlags = | 
|  | 710 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 711 | outputDesc->mRefCount[stream] = 0; | 
|  | 712 | mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice, | 
|  | 713 | &outputDesc->mSamplingRate, | 
|  | 714 | &outputDesc->mFormat, | 
|  | 715 | &outputDesc->mChannelMask, | 
|  | 716 | &outputDesc->mLatency, | 
|  | 717 | outputDesc->mFlags, | 
|  | 718 | offloadInfo); | 
|  | 719 | if (mTestOutputs[mCurOutput]) { | 
|  | 720 | AudioParameter outputCmd = AudioParameter(); | 
|  | 721 | outputCmd.addInt(String8("set_id"),mCurOutput); | 
|  | 722 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
|  | 723 | addOutput(mTestOutputs[mCurOutput], outputDesc); | 
|  | 724 | } | 
|  | 725 | } | 
|  | 726 | return mTestOutputs[mCurOutput]; | 
|  | 727 | } | 
|  | 728 | #endif //AUDIO_POLICY_TEST | 
|  | 729 |  | 
|  | 730 | // open a direct output if required by specified parameters | 
|  | 731 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 732 | // and all common behaviors are driven by checking only the direct flag | 
|  | 733 | // this should normally be set appropriately in the policy configuration file | 
|  | 734 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 735 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 736 | } | 
|  | 737 |  | 
|  | 738 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 739 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 740 | // detects there is an active non offloadable effect. | 
|  | 741 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 742 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 743 | // in the background. | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 744 | sp<IOProfile> profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 745 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
|  | 746 | !isNonOffloadableEffectEnabled()) { | 
|  | 747 | profile = getProfileForDirectOutput(device, | 
|  | 748 | samplingRate, | 
|  | 749 | format, | 
|  | 750 | channelMask, | 
|  | 751 | (audio_output_flags_t)flags); | 
|  | 752 | } | 
|  | 753 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 754 | if (profile != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 755 | sp<AudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 756 |  | 
|  | 757 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 758 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 759 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 760 | outputDesc = desc; | 
|  | 761 | // reuse direct output if currently open and configured with same parameters | 
|  | 762 | if ((samplingRate == outputDesc->mSamplingRate) && | 
|  | 763 | (format == outputDesc->mFormat) && | 
|  | 764 | (channelMask == outputDesc->mChannelMask)) { | 
|  | 765 | outputDesc->mDirectOpenCount++; | 
|  | 766 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
|  | 767 | return mOutputs.keyAt(i); | 
|  | 768 | } | 
|  | 769 | } | 
|  | 770 | } | 
|  | 771 | // close direct output if currently open and configured with different parameters | 
|  | 772 | if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 773 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 774 | } | 
|  | 775 | outputDesc = new AudioOutputDescriptor(profile); | 
|  | 776 | outputDesc->mDevice = device; | 
|  | 777 | outputDesc->mSamplingRate = samplingRate; | 
|  | 778 | outputDesc->mFormat = format; | 
|  | 779 | outputDesc->mChannelMask = channelMask; | 
|  | 780 | outputDesc->mLatency = 0; | 
|  | 781 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); | 
|  | 782 | outputDesc->mRefCount[stream] = 0; | 
|  | 783 | outputDesc->mStopTime[stream] = 0; | 
|  | 784 | outputDesc->mDirectOpenCount = 1; | 
|  | 785 | output = mpClientInterface->openOutput(profile->mModule->mHandle, | 
|  | 786 | &outputDesc->mDevice, | 
|  | 787 | &outputDesc->mSamplingRate, | 
|  | 788 | &outputDesc->mFormat, | 
|  | 789 | &outputDesc->mChannelMask, | 
|  | 790 | &outputDesc->mLatency, | 
|  | 791 | outputDesc->mFlags, | 
|  | 792 | offloadInfo); | 
|  | 793 |  | 
|  | 794 | // only accept an output with the requested parameters | 
|  | 795 | if (output == 0 || | 
|  | 796 | (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) || | 
|  | 797 | (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) || | 
|  | 798 | (channelMask != 0 && channelMask != outputDesc->mChannelMask)) { | 
|  | 799 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
|  | 800 | "format %d %d, channelMask %04x %04x", output, samplingRate, | 
|  | 801 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
|  | 802 | outputDesc->mChannelMask); | 
|  | 803 | if (output != 0) { | 
|  | 804 | mpClientInterface->closeOutput(output); | 
|  | 805 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 806 | return 0; | 
|  | 807 | } | 
|  | 808 | audio_io_handle_t srcOutput = getOutputForEffect(); | 
|  | 809 | addOutput(output, outputDesc); | 
|  | 810 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 811 | if (dstOutput == output) { | 
|  | 812 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); | 
|  | 813 | } | 
|  | 814 | mPreviousOutputs = mOutputs; | 
|  | 815 | ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 816 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 817 | return output; | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | // ignoring channel mask due to downmix capability in mixer | 
|  | 821 |  | 
|  | 822 | // open a non direct output | 
|  | 823 |  | 
|  | 824 | // for non direct outputs, only PCM is supported | 
|  | 825 | if (audio_is_linear_pcm(format)) { | 
|  | 826 | // get which output is suitable for the specified stream. The actual | 
|  | 827 | // routing change will happen when startOutput() will be called | 
|  | 828 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 829 |  | 
|  | 830 | output = selectOutput(outputs, flags); | 
|  | 831 | } | 
|  | 832 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
|  | 833 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
|  | 834 |  | 
|  | 835 | ALOGV("getOutput() returns output %d", output); | 
|  | 836 |  | 
|  | 837 | return output; | 
|  | 838 | } | 
|  | 839 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 840 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 841 | audio_output_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 842 | { | 
|  | 843 | // select one output among several that provide a path to a particular device or set of | 
|  | 844 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 845 | // The priority is as follows: | 
|  | 846 | // 1: the output with the highest number of requested policy flags | 
|  | 847 | // 2: the primary output | 
|  | 848 | // 3: the first output in the list | 
|  | 849 |  | 
|  | 850 | if (outputs.size() == 0) { | 
|  | 851 | return 0; | 
|  | 852 | } | 
|  | 853 | if (outputs.size() == 1) { | 
|  | 854 | return outputs[0]; | 
|  | 855 | } | 
|  | 856 |  | 
|  | 857 | int maxCommonFlags = 0; | 
|  | 858 | audio_io_handle_t outputFlags = 0; | 
|  | 859 | audio_io_handle_t outputPrimary = 0; | 
|  | 860 |  | 
|  | 861 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 862 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 863 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 864 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 865 | if (commonFlags > maxCommonFlags) { | 
|  | 866 | outputFlags = outputs[i]; | 
|  | 867 | maxCommonFlags = commonFlags; | 
|  | 868 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
|  | 869 | } | 
|  | 870 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 871 | outputPrimary = outputs[i]; | 
|  | 872 | } | 
|  | 873 | } | 
|  | 874 | } | 
|  | 875 |  | 
|  | 876 | if (outputFlags != 0) { | 
|  | 877 | return outputFlags; | 
|  | 878 | } | 
|  | 879 | if (outputPrimary != 0) { | 
|  | 880 | return outputPrimary; | 
|  | 881 | } | 
|  | 882 |  | 
|  | 883 | return outputs[0]; | 
|  | 884 | } | 
|  | 885 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 886 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 887 | audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 888 | int session) | 
|  | 889 | { | 
|  | 890 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 891 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 892 | if (index < 0) { | 
|  | 893 | ALOGW("startOutput() unknown output %d", output); | 
|  | 894 | return BAD_VALUE; | 
|  | 895 | } | 
|  | 896 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 897 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 898 |  | 
|  | 899 | // increment usage count for this stream on the requested output: | 
|  | 900 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 901 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 902 | outputDesc->changeRefCount(stream, 1); | 
|  | 903 |  | 
|  | 904 | if (outputDesc->mRefCount[stream] == 1) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 905 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 906 | routing_strategy strategy = getStrategy(stream); | 
|  | 907 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
|  | 908 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 909 | uint32_t waitMs = 0; | 
|  | 910 | bool force = false; | 
|  | 911 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 912 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 913 | if (desc != outputDesc) { | 
|  | 914 | // force a device change if any other output is managed by the same hw | 
|  | 915 | // module and has a current device selection that differs from selected device. | 
|  | 916 | // In this case, the audio HAL must receive the new device selection so that it can | 
|  | 917 | // change the device currently selected by the other active output. | 
|  | 918 | if (outputDesc->sharesHwModuleWith(desc) && | 
|  | 919 | desc->device() != newDevice) { | 
|  | 920 | force = true; | 
|  | 921 | } | 
|  | 922 | // wait for audio on other active outputs to be presented when starting | 
|  | 923 | // a notification so that audio focus effect can propagate. | 
|  | 924 | uint32_t latency = desc->latency(); | 
|  | 925 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
|  | 926 | waitMs = latency; | 
|  | 927 | } | 
|  | 928 | } | 
|  | 929 | } | 
|  | 930 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); | 
|  | 931 |  | 
|  | 932 | // handle special case for sonification while in call | 
|  | 933 | if (isInCall()) { | 
|  | 934 | handleIncallSonification(stream, true, false); | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | // apply volume rules for current stream and device if necessary | 
|  | 938 | checkAndSetVolume(stream, | 
|  | 939 | mStreams[stream].getVolumeIndex(newDevice), | 
|  | 940 | output, | 
|  | 941 | newDevice); | 
|  | 942 |  | 
|  | 943 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 944 | // routing | 
|  | 945 | handleNotificationRoutingForStream(stream); | 
|  | 946 | if (waitMs > muteWaitMs) { | 
|  | 947 | usleep((waitMs - muteWaitMs) * 2 * 1000); | 
|  | 948 | } | 
|  | 949 | } | 
|  | 950 | return NO_ERROR; | 
|  | 951 | } | 
|  | 952 |  | 
|  | 953 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 954 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 955 | audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | int session) | 
|  | 957 | { | 
|  | 958 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 959 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 960 | if (index < 0) { | 
|  | 961 | ALOGW("stopOutput() unknown output %d", output); | 
|  | 962 | return BAD_VALUE; | 
|  | 963 | } | 
|  | 964 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 965 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 966 |  | 
|  | 967 | // handle special case for sonification while in call | 
|  | 968 | if (isInCall()) { | 
|  | 969 | handleIncallSonification(stream, false, false); | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 973 | // decrement usage count of this stream on the output | 
|  | 974 | outputDesc->changeRefCount(stream, -1); | 
|  | 975 | // store time at which the stream was stopped - see isStreamActive() | 
|  | 976 | if (outputDesc->mRefCount[stream] == 0) { | 
|  | 977 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 978 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 979 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 980 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 981 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 982 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 983 | // audio path (audio DSP, CODEC ...) | 
|  | 984 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); | 
|  | 985 |  | 
|  | 986 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 987 | // one being selected for this output | 
|  | 988 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 989 | audio_io_handle_t curOutput = mOutputs.keyAt(i); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 990 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 991 | if (curOutput != output && | 
|  | 992 | desc->isActive() && | 
|  | 993 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 994 | (newDevice != desc->device())) { | 
|  | 995 | setOutputDevice(curOutput, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 996 | getNewOutputDevice(curOutput, false /*fromCache*/), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 997 | true, | 
|  | 998 | outputDesc->mLatency*2); | 
|  | 999 | } | 
|  | 1000 | } | 
|  | 1001 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1002 | handleNotificationRoutingForStream(stream); | 
|  | 1003 | } | 
|  | 1004 | return NO_ERROR; | 
|  | 1005 | } else { | 
|  | 1006 | ALOGW("stopOutput() refcount is already 0 for output %d", output); | 
|  | 1007 | return INVALID_OPERATION; | 
|  | 1008 | } | 
|  | 1009 | } | 
|  | 1010 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1011 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1012 | { | 
|  | 1013 | ALOGV("releaseOutput() %d", output); | 
|  | 1014 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1015 | if (index < 0) { | 
|  | 1016 | ALOGW("releaseOutput() releasing unknown output %d", output); | 
|  | 1017 | return; | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | #ifdef AUDIO_POLICY_TEST | 
|  | 1021 | int testIndex = testOutputIndex(output); | 
|  | 1022 | if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1023 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1024 | if (outputDesc->isActive()) { | 
|  | 1025 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1026 | mOutputs.removeItem(output); | 
|  | 1027 | mTestOutputs[testIndex] = 0; | 
|  | 1028 | } | 
|  | 1029 | return; | 
|  | 1030 | } | 
|  | 1031 | #endif //AUDIO_POLICY_TEST | 
|  | 1032 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1033 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1034 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1035 | if (desc->mDirectOpenCount <= 0) { | 
|  | 1036 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
|  | 1037 | desc->mDirectOpenCount, output); | 
|  | 1038 | return; | 
|  | 1039 | } | 
|  | 1040 | if (--desc->mDirectOpenCount == 0) { | 
|  | 1041 | closeOutput(output); | 
|  | 1042 | // If effects where present on the output, audioflinger moved them to the primary | 
|  | 1043 | // output by default: move them back to the appropriate output. | 
|  | 1044 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 1045 | if (dstOutput != mPrimaryOutput) { | 
|  | 1046 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); | 
|  | 1047 | } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1048 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | } | 
|  | 1050 | } | 
|  | 1051 | } | 
|  | 1052 |  | 
|  | 1053 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1054 | audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1055 | uint32_t samplingRate, | 
|  | 1056 | audio_format_t format, | 
|  | 1057 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1058 | audio_in_acoustics_t acoustics) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1059 | { | 
|  | 1060 | audio_io_handle_t input = 0; | 
|  | 1061 | audio_devices_t device = getDeviceForInputSource(inputSource); | 
|  | 1062 |  | 
|  | 1063 | ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x", | 
|  | 1064 | inputSource, samplingRate, format, channelMask, acoustics); | 
|  | 1065 |  | 
|  | 1066 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1067 | ALOGW("getInput() could not find device for inputSource %d", inputSource); | 
|  | 1068 | return 0; | 
|  | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | // adapt channel selection to input source | 
|  | 1072 | switch(inputSource) { | 
|  | 1073 | case AUDIO_SOURCE_VOICE_UPLINK: | 
|  | 1074 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; | 
|  | 1075 | break; | 
|  | 1076 | case AUDIO_SOURCE_VOICE_DOWNLINK: | 
|  | 1077 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; | 
|  | 1078 | break; | 
|  | 1079 | case AUDIO_SOURCE_VOICE_CALL: | 
|  | 1080 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; | 
|  | 1081 | break; | 
|  | 1082 | default: | 
|  | 1083 | break; | 
|  | 1084 | } | 
|  | 1085 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1086 | sp<IOProfile> profile = getInputProfile(device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 | samplingRate, | 
|  | 1088 | format, | 
|  | 1089 | channelMask); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1090 | if (profile == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d, " | 
|  | 1092 | "channelMask %04x", | 
|  | 1093 | device, samplingRate, format, channelMask); | 
|  | 1094 | return 0; | 
|  | 1095 | } | 
|  | 1096 |  | 
|  | 1097 | if (profile->mModule->mHandle == 0) { | 
|  | 1098 | ALOGE("getInput(): HW module %s not opened", profile->mModule->mName); | 
|  | 1099 | return 0; | 
|  | 1100 | } | 
|  | 1101 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1102 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1103 |  | 
|  | 1104 | inputDesc->mInputSource = inputSource; | 
|  | 1105 | inputDesc->mDevice = device; | 
|  | 1106 | inputDesc->mSamplingRate = samplingRate; | 
|  | 1107 | inputDesc->mFormat = format; | 
|  | 1108 | inputDesc->mChannelMask = channelMask; | 
|  | 1109 | inputDesc->mRefCount = 0; | 
|  | 1110 | input = mpClientInterface->openInput(profile->mModule->mHandle, | 
|  | 1111 | &inputDesc->mDevice, | 
|  | 1112 | &inputDesc->mSamplingRate, | 
|  | 1113 | &inputDesc->mFormat, | 
| Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 1114 | &inputDesc->mChannelMask, | 
|  | 1115 | AUDIO_INPUT_FLAG_FAST /*FIXME*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1116 |  | 
|  | 1117 | // only accept input with the exact requested set of parameters | 
|  | 1118 | if (input == 0 || | 
|  | 1119 | (samplingRate != inputDesc->mSamplingRate) || | 
|  | 1120 | (format != inputDesc->mFormat) || | 
|  | 1121 | (channelMask != inputDesc->mChannelMask)) { | 
|  | 1122 | ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask %x", | 
|  | 1123 | samplingRate, format, channelMask); | 
|  | 1124 | if (input != 0) { | 
|  | 1125 | mpClientInterface->closeInput(input); | 
|  | 1126 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1127 | return 0; | 
|  | 1128 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1129 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1130 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1131 | return input; | 
|  | 1132 | } | 
|  | 1133 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1134 | status_t AudioPolicyManager::startInput(audio_io_handle_t input) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1135 | { | 
|  | 1136 | ALOGV("startInput() input %d", input); | 
|  | 1137 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1138 | if (index < 0) { | 
|  | 1139 | ALOGW("startInput() unknown input %d", input); | 
|  | 1140 | return BAD_VALUE; | 
|  | 1141 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1142 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1143 |  | 
|  | 1144 | #ifdef AUDIO_POLICY_TEST | 
|  | 1145 | if (mTestInput == 0) | 
|  | 1146 | #endif //AUDIO_POLICY_TEST | 
|  | 1147 | { | 
|  | 1148 | // refuse 2 active AudioRecord clients at the same time except if the active input | 
|  | 1149 | // uses AUDIO_SOURCE_HOTWORD in which case it is closed. | 
|  | 1150 | audio_io_handle_t activeInput = getActiveInput(); | 
|  | 1151 | if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1152 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1153 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1154 | ALOGW("startInput() preempting already started low-priority input %d", activeInput); | 
|  | 1155 | stopInput(activeInput); | 
|  | 1156 | releaseInput(activeInput); | 
|  | 1157 | } else { | 
|  | 1158 | ALOGW("startInput() input %d failed: other input already started", input); | 
|  | 1159 | return INVALID_OPERATION; | 
|  | 1160 | } | 
|  | 1161 | } | 
|  | 1162 | } | 
|  | 1163 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1164 | setInputDevice(input, getNewInputDevice(input), true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1165 |  | 
|  | 1166 | // automatically enable the remote submix output when input is started | 
|  | 1167 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1168 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1169 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1170 | } | 
|  | 1171 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1172 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); | 
|  | 1173 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1174 | inputDesc->mRefCount = 1; | 
|  | 1175 | return NO_ERROR; | 
|  | 1176 | } | 
|  | 1177 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1178 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1179 | { | 
|  | 1180 | ALOGV("stopInput() input %d", input); | 
|  | 1181 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1182 | if (index < 0) { | 
|  | 1183 | ALOGW("stopInput() unknown input %d", input); | 
|  | 1184 | return BAD_VALUE; | 
|  | 1185 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1186 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1187 |  | 
|  | 1188 | if (inputDesc->mRefCount == 0) { | 
|  | 1189 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 1190 | return INVALID_OPERATION; | 
|  | 1191 | } else { | 
|  | 1192 | // automatically disable the remote submix output when input is stopped | 
|  | 1193 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1194 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1195 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1196 | } | 
|  | 1197 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1198 | resetInputDevice(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1199 | inputDesc->mRefCount = 0; | 
|  | 1200 | return NO_ERROR; | 
|  | 1201 | } | 
|  | 1202 | } | 
|  | 1203 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1204 | void AudioPolicyManager::releaseInput(audio_io_handle_t input) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1205 | { | 
|  | 1206 | ALOGV("releaseInput() %d", input); | 
|  | 1207 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1208 | if (index < 0) { | 
|  | 1209 | ALOGW("releaseInput() releasing unknown input %d", input); | 
|  | 1210 | return; | 
|  | 1211 | } | 
|  | 1212 | mpClientInterface->closeInput(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1213 | mInputs.removeItem(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1214 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1215 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1216 | ALOGV("releaseInput() exit"); | 
|  | 1217 | } | 
|  | 1218 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1219 | void AudioPolicyManager::closeAllInputs() { | 
|  | 1220 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 1221 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
|  | 1222 | } | 
|  | 1223 | mInputs.clear(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1224 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1225 | } | 
|  | 1226 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1227 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1228 | int indexMin, | 
|  | 1229 | int indexMax) | 
|  | 1230 | { | 
|  | 1231 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
|  | 1232 | if (indexMin < 0 || indexMin >= indexMax) { | 
|  | 1233 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); | 
|  | 1234 | return; | 
|  | 1235 | } | 
|  | 1236 | mStreams[stream].mIndexMin = indexMin; | 
|  | 1237 | mStreams[stream].mIndexMax = indexMax; | 
|  | 1238 | } | 
|  | 1239 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1240 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1241 | int index, | 
|  | 1242 | audio_devices_t device) | 
|  | 1243 | { | 
|  | 1244 |  | 
|  | 1245 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { | 
|  | 1246 | return BAD_VALUE; | 
|  | 1247 | } | 
|  | 1248 | if (!audio_is_output_device(device)) { | 
|  | 1249 | return BAD_VALUE; | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | // Force max volume if stream cannot be muted | 
|  | 1253 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; | 
|  | 1254 |  | 
|  | 1255 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", | 
|  | 1256 | stream, device, index); | 
|  | 1257 |  | 
|  | 1258 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and | 
|  | 1259 | // clear all device specific values | 
|  | 1260 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
|  | 1261 | mStreams[stream].mIndexCur.clear(); | 
|  | 1262 | } | 
|  | 1263 | mStreams[stream].mIndexCur.add(device, index); | 
|  | 1264 |  | 
|  | 1265 | // compute and apply stream volume on all outputs according to connected device | 
|  | 1266 | status_t status = NO_ERROR; | 
|  | 1267 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1268 | audio_devices_t curDevice = | 
|  | 1269 | getDeviceForVolume(mOutputs.valueAt(i)->device()); | 
|  | 1270 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) { | 
|  | 1271 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); | 
|  | 1272 | if (volStatus != NO_ERROR) { | 
|  | 1273 | status = volStatus; | 
|  | 1274 | } | 
|  | 1275 | } | 
|  | 1276 | } | 
|  | 1277 | return status; | 
|  | 1278 | } | 
|  | 1279 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1280 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | int *index, | 
|  | 1282 | audio_devices_t device) | 
|  | 1283 | { | 
|  | 1284 | if (index == NULL) { | 
|  | 1285 | return BAD_VALUE; | 
|  | 1286 | } | 
|  | 1287 | if (!audio_is_output_device(device)) { | 
|  | 1288 | return BAD_VALUE; | 
|  | 1289 | } | 
|  | 1290 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to | 
|  | 1291 | // the strategy the stream belongs to. | 
|  | 1292 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
|  | 1293 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 1294 | } | 
|  | 1295 | device = getDeviceForVolume(device); | 
|  | 1296 |  | 
|  | 1297 | *index =  mStreams[stream].getVolumeIndex(device); | 
|  | 1298 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 1299 | return NO_ERROR; | 
|  | 1300 | } | 
|  | 1301 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1302 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1303 | const SortedVector<audio_io_handle_t>& outputs) | 
|  | 1304 | { | 
|  | 1305 | // select one output among several suitable for global effects. | 
|  | 1306 | // The priority is as follows: | 
|  | 1307 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 1308 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 1309 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 1310 | // 2: A deep buffer output | 
|  | 1311 | // 3: the first output in the list | 
|  | 1312 |  | 
|  | 1313 | if (outputs.size() == 0) { | 
|  | 1314 | return 0; | 
|  | 1315 | } | 
|  | 1316 |  | 
|  | 1317 | audio_io_handle_t outputOffloaded = 0; | 
|  | 1318 | audio_io_handle_t outputDeepBuffer = 0; | 
|  | 1319 |  | 
|  | 1320 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1321 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1322 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1324 | outputOffloaded = outputs[i]; | 
|  | 1325 | } | 
|  | 1326 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 1327 | outputDeepBuffer = outputs[i]; | 
|  | 1328 | } | 
|  | 1329 | } | 
|  | 1330 |  | 
|  | 1331 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", | 
|  | 1332 | outputOffloaded, outputDeepBuffer); | 
|  | 1333 | if (outputOffloaded != 0) { | 
|  | 1334 | return outputOffloaded; | 
|  | 1335 | } | 
|  | 1336 | if (outputDeepBuffer != 0) { | 
|  | 1337 | return outputDeepBuffer; | 
|  | 1338 | } | 
|  | 1339 |  | 
|  | 1340 | return outputs[0]; | 
|  | 1341 | } | 
|  | 1342 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1343 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1344 | { | 
|  | 1345 | // apply simple rule where global effects are attached to the same output as MUSIC streams | 
|  | 1346 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1347 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1348 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 1349 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); | 
|  | 1350 |  | 
|  | 1351 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); | 
|  | 1352 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", | 
|  | 1353 | output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags); | 
|  | 1354 |  | 
|  | 1355 | return output; | 
|  | 1356 | } | 
|  | 1357 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1358 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1359 | audio_io_handle_t io, | 
|  | 1360 | uint32_t strategy, | 
|  | 1361 | int session, | 
|  | 1362 | int id) | 
|  | 1363 | { | 
|  | 1364 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 1365 | if (index < 0) { | 
|  | 1366 | index = mInputs.indexOfKey(io); | 
|  | 1367 | if (index < 0) { | 
|  | 1368 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 1369 | return INVALID_OPERATION; | 
|  | 1370 | } | 
|  | 1371 | } | 
|  | 1372 |  | 
|  | 1373 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { | 
|  | 1374 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", | 
|  | 1375 | desc->name, desc->memoryUsage); | 
|  | 1376 | return INVALID_OPERATION; | 
|  | 1377 | } | 
|  | 1378 | mTotalEffectsMemory += desc->memoryUsage; | 
|  | 1379 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", | 
|  | 1380 | desc->name, io, strategy, session, id); | 
|  | 1381 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); | 
|  | 1382 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1383 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); | 
|  | 1384 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); | 
|  | 1385 | effectDesc->mIo = io; | 
|  | 1386 | effectDesc->mStrategy = (routing_strategy)strategy; | 
|  | 1387 | effectDesc->mSession = session; | 
|  | 1388 | effectDesc->mEnabled = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1389 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1390 | mEffects.add(id, effectDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 |  | 
|  | 1392 | return NO_ERROR; | 
|  | 1393 | } | 
|  | 1394 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1395 | status_t AudioPolicyManager::unregisterEffect(int id) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1396 | { | 
|  | 1397 | ssize_t index = mEffects.indexOfKey(id); | 
|  | 1398 | if (index < 0) { | 
|  | 1399 | ALOGW("unregisterEffect() unknown effect ID %d", id); | 
|  | 1400 | return INVALID_OPERATION; | 
|  | 1401 | } | 
|  | 1402 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1403 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1404 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1405 | setEffectEnabled(effectDesc, false); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1406 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1407 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1408 | ALOGW("unregisterEffect() memory %d too big for total %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1409 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); | 
|  | 1410 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1411 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1412 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1413 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1414 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1415 |  | 
|  | 1416 | mEffects.removeItem(id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1417 |  | 
|  | 1418 | return NO_ERROR; | 
|  | 1419 | } | 
|  | 1420 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1421 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1422 | { | 
|  | 1423 | ssize_t index = mEffects.indexOfKey(id); | 
|  | 1424 | if (index < 0) { | 
|  | 1425 | ALOGW("unregisterEffect() unknown effect ID %d", id); | 
|  | 1426 | return INVALID_OPERATION; | 
|  | 1427 | } | 
|  | 1428 |  | 
|  | 1429 | return setEffectEnabled(mEffects.valueAt(index), enabled); | 
|  | 1430 | } | 
|  | 1431 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1432 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1433 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1434 | if (enabled == effectDesc->mEnabled) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1435 | ALOGV("setEffectEnabled(%s) effect already %s", | 
|  | 1436 | enabled?"true":"false", enabled?"enabled":"disabled"); | 
|  | 1437 | return INVALID_OPERATION; | 
|  | 1438 | } | 
|  | 1439 |  | 
|  | 1440 | if (enabled) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1441 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1442 | ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1443 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | return INVALID_OPERATION; | 
|  | 1445 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1446 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1447 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); | 
|  | 1448 | } else { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1449 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1450 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1451 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); | 
|  | 1452 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1453 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1454 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1455 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); | 
|  | 1456 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1457 | effectDesc->mEnabled = enabled; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | return NO_ERROR; | 
|  | 1459 | } | 
|  | 1460 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1461 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1462 | { | 
|  | 1463 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1464 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 1465 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && | 
|  | 1466 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1468 | effectDesc->mDesc.name, effectDesc->mSession); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1469 | return true; | 
|  | 1470 | } | 
|  | 1471 | } | 
|  | 1472 | return false; | 
|  | 1473 | } | 
|  | 1474 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1475 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1476 | { | 
|  | 1477 | nsecs_t sysTime = systemTime(); | 
|  | 1478 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1479 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1480 | if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1481 | return true; | 
|  | 1482 | } | 
|  | 1483 | } | 
|  | 1484 | return false; | 
|  | 1485 | } | 
|  | 1486 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1487 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1488 | uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | { | 
|  | 1490 | nsecs_t sysTime = systemTime(); | 
|  | 1491 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1492 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1493 | if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1494 | outputDesc->isStreamActive(stream, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1495 | return true; | 
|  | 1496 | } | 
|  | 1497 | } | 
|  | 1498 | return false; | 
|  | 1499 | } | 
|  | 1500 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1501 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1502 | { | 
|  | 1503 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1504 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1505 | if ((inputDescriptor->mInputSource == (int)source || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1506 | (source == AUDIO_SOURCE_VOICE_RECOGNITION && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1507 | inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) | 
|  | 1508 | && (inputDescriptor->mRefCount > 0)) { | 
|  | 1509 | return true; | 
|  | 1510 | } | 
|  | 1511 | } | 
|  | 1512 | return false; | 
|  | 1513 | } | 
|  | 1514 |  | 
|  | 1515 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1516 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1517 | { | 
|  | 1518 | const size_t SIZE = 256; | 
|  | 1519 | char buffer[SIZE]; | 
|  | 1520 | String8 result; | 
|  | 1521 |  | 
|  | 1522 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
|  | 1523 | result.append(buffer); | 
|  | 1524 |  | 
|  | 1525 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); | 
|  | 1526 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); | 
|  | 1528 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1529 | snprintf(buffer, SIZE, " Force use for communications %d\n", | 
|  | 1530 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1531 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1532 | snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1533 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1534 | snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1535 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1536 | snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1538 | snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1539 | result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1540 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
|  | 1541 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); | 
|  | 1542 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1543 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1544 | snprintf(buffer, SIZE, " Available output devices:\n"); | 
|  | 1545 | result.append(buffer); | 
|  | 1546 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1547 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1548 | mAvailableOutputDevices[i]->dump(fd, 2, i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1549 | } | 
|  | 1550 | snprintf(buffer, SIZE, "\n Available input devices:\n"); | 
|  | 1551 | write(fd, buffer, strlen(buffer)); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1552 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1553 | mAvailableInputDevices[i]->dump(fd, 2, i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1554 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1555 |  | 
|  | 1556 | snprintf(buffer, SIZE, "\nHW Modules dump:\n"); | 
|  | 1557 | write(fd, buffer, strlen(buffer)); | 
|  | 1558 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1559 | snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1560 | write(fd, buffer, strlen(buffer)); | 
|  | 1561 | mHwModules[i]->dump(fd); | 
|  | 1562 | } | 
|  | 1563 |  | 
|  | 1564 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); | 
|  | 1565 | write(fd, buffer, strlen(buffer)); | 
|  | 1566 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1567 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); | 
|  | 1568 | write(fd, buffer, strlen(buffer)); | 
|  | 1569 | mOutputs.valueAt(i)->dump(fd); | 
|  | 1570 | } | 
|  | 1571 |  | 
|  | 1572 | snprintf(buffer, SIZE, "\nInputs dump:\n"); | 
|  | 1573 | write(fd, buffer, strlen(buffer)); | 
|  | 1574 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1575 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); | 
|  | 1576 | write(fd, buffer, strlen(buffer)); | 
|  | 1577 | mInputs.valueAt(i)->dump(fd); | 
|  | 1578 | } | 
|  | 1579 |  | 
|  | 1580 | snprintf(buffer, SIZE, "\nStreams dump:\n"); | 
|  | 1581 | write(fd, buffer, strlen(buffer)); | 
|  | 1582 | snprintf(buffer, SIZE, | 
|  | 1583 | " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n"); | 
|  | 1584 | write(fd, buffer, strlen(buffer)); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 1585 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1586 | snprintf(buffer, SIZE, " %02zu      ", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1587 | write(fd, buffer, strlen(buffer)); | 
|  | 1588 | mStreams[i].dump(fd); | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", | 
|  | 1592 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); | 
|  | 1593 | write(fd, buffer, strlen(buffer)); | 
|  | 1594 |  | 
|  | 1595 | snprintf(buffer, SIZE, "Registered effects:\n"); | 
|  | 1596 | write(fd, buffer, strlen(buffer)); | 
|  | 1597 | for (size_t i = 0; i < mEffects.size(); i++) { | 
|  | 1598 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); | 
|  | 1599 | write(fd, buffer, strlen(buffer)); | 
|  | 1600 | mEffects.valueAt(i)->dump(fd); | 
|  | 1601 | } | 
|  | 1602 |  | 
|  | 1603 |  | 
|  | 1604 | return NO_ERROR; | 
|  | 1605 | } | 
|  | 1606 |  | 
|  | 1607 | // This function checks for the parameters which can be offloaded. | 
|  | 1608 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 1609 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1610 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1611 | { | 
|  | 1612 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1613 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1614 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 1615 | offloadInfo.format, | 
|  | 1616 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 1617 | offloadInfo.has_video); | 
|  | 1618 |  | 
|  | 1619 | // Check if offload has been disabled | 
|  | 1620 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 1621 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 1622 | if (atoi(propValue) != 0) { | 
|  | 1623 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 1624 | return false; | 
|  | 1625 | } | 
|  | 1626 | } | 
|  | 1627 |  | 
|  | 1628 | // Check if stream type is music, then only allow offload as of now. | 
|  | 1629 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 1630 | { | 
|  | 1631 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 1632 | return false; | 
|  | 1633 | } | 
|  | 1634 |  | 
|  | 1635 | //TODO: enable audio offloading with video when ready | 
|  | 1636 | if (offloadInfo.has_video) | 
|  | 1637 | { | 
|  | 1638 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 1639 | return false; | 
|  | 1640 | } | 
|  | 1641 |  | 
|  | 1642 | //If duration is less than minimum value defined in property, return false | 
|  | 1643 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 1644 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 1645 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 1646 | return false; | 
|  | 1647 | } | 
|  | 1648 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 1649 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 1650 | return false; | 
|  | 1651 | } | 
|  | 1652 |  | 
|  | 1653 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 1654 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 1655 | // detects there is an active non offloadable effect. | 
|  | 1656 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 1657 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 1658 | // in the background. | 
|  | 1659 | if (isNonOffloadableEffectEnabled()) { | 
|  | 1660 | return false; | 
|  | 1661 | } | 
|  | 1662 |  | 
|  | 1663 | // See if there is a profile to support this. | 
|  | 1664 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1665 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1666 | offloadInfo.sample_rate, | 
|  | 1667 | offloadInfo.format, | 
|  | 1668 | offloadInfo.channel_mask, | 
|  | 1669 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1670 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 1671 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1672 | } | 
|  | 1673 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1674 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 1675 | audio_port_type_t type, | 
|  | 1676 | unsigned int *num_ports, | 
|  | 1677 | struct audio_port *ports, | 
|  | 1678 | unsigned int *generation) | 
|  | 1679 | { | 
|  | 1680 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 1681 | generation == NULL) { | 
|  | 1682 | return BAD_VALUE; | 
|  | 1683 | } | 
|  | 1684 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 1685 | if (ports == NULL) { | 
|  | 1686 | *num_ports = 0; | 
|  | 1687 | } | 
|  | 1688 |  | 
|  | 1689 | size_t portsWritten = 0; | 
|  | 1690 | size_t portsMax = *num_ports; | 
|  | 1691 | *num_ports = 0; | 
|  | 1692 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 1693 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1694 | for (size_t i = 0; | 
|  | 1695 | i  < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { | 
|  | 1696 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1697 | } | 
|  | 1698 | *num_ports += mAvailableOutputDevices.size(); | 
|  | 1699 | } | 
|  | 1700 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1701 | for (size_t i = 0; | 
|  | 1702 | i  < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { | 
|  | 1703 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1704 | } | 
|  | 1705 | *num_ports += mAvailableInputDevices.size(); | 
|  | 1706 | } | 
|  | 1707 | } | 
|  | 1708 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 1709 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1710 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 1711 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1712 | } | 
|  | 1713 | *num_ports += mInputs.size(); | 
|  | 1714 | } | 
|  | 1715 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1716 | size_t numOutputs = 0; | 
|  | 1717 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1718 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 1719 | numOutputs++; | 
|  | 1720 | if (portsWritten < portsMax) { | 
|  | 1721 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1722 | } | 
|  | 1723 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1724 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1725 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1726 | } | 
|  | 1727 | } | 
|  | 1728 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 1729 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1730 | return NO_ERROR; | 
|  | 1731 | } | 
|  | 1732 |  | 
|  | 1733 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
|  | 1734 | { | 
|  | 1735 | return NO_ERROR; | 
|  | 1736 | } | 
|  | 1737 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1738 | sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1739 | audio_port_handle_t id) const | 
|  | 1740 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1741 | sp<AudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1742 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1743 | outputDesc = mOutputs.valueAt(i); | 
|  | 1744 | if (outputDesc->mId == id) { | 
|  | 1745 | break; | 
|  | 1746 | } | 
|  | 1747 | } | 
|  | 1748 | return outputDesc; | 
|  | 1749 | } | 
|  | 1750 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1751 | sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1752 | audio_port_handle_t id) const | 
|  | 1753 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1754 | sp<AudioInputDescriptor> inputDesc = NULL; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1755 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1756 | inputDesc = mInputs.valueAt(i); | 
|  | 1757 | if (inputDesc->mId == id) { | 
|  | 1758 | break; | 
|  | 1759 | } | 
|  | 1760 | } | 
|  | 1761 | return inputDesc; | 
|  | 1762 | } | 
|  | 1763 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1764 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice( | 
|  | 1765 | audio_devices_t device) const | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1766 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1767 | sp <HwModule> module; | 
|  | 1768 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1769 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 1770 | if (mHwModules[i]->mHandle == 0) { | 
|  | 1771 | continue; | 
|  | 1772 | } | 
|  | 1773 | if (audio_is_output_device(device)) { | 
|  | 1774 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 1775 | { | 
|  | 1776 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { | 
|  | 1777 | return mHwModules[i]; | 
|  | 1778 | } | 
|  | 1779 | } | 
|  | 1780 | } else { | 
|  | 1781 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { | 
|  | 1782 | if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() & | 
|  | 1783 | device & ~AUDIO_DEVICE_BIT_IN) { | 
|  | 1784 | return mHwModules[i]; | 
|  | 1785 | } | 
|  | 1786 | } | 
|  | 1787 | } | 
|  | 1788 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1789 | return module; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1790 | } | 
|  | 1791 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1792 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1793 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1794 | sp <HwModule> module; | 
|  | 1795 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1796 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 1797 | { | 
|  | 1798 | if (strcmp(mHwModules[i]->mName, name) == 0) { | 
|  | 1799 | return mHwModules[i]; | 
|  | 1800 | } | 
|  | 1801 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1802 | return module; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1803 | } | 
|  | 1804 |  | 
|  | 1805 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1806 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 1807 | audio_patch_handle_t *handle, | 
|  | 1808 | uid_t uid) | 
|  | 1809 | { | 
|  | 1810 | ALOGV("createAudioPatch()"); | 
|  | 1811 |  | 
|  | 1812 | if (handle == NULL || patch == NULL) { | 
|  | 1813 | return BAD_VALUE; | 
|  | 1814 | } | 
|  | 1815 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 1816 |  | 
|  | 1817 | if (patch->num_sources > 1 || patch->num_sinks > 1) { | 
|  | 1818 | return INVALID_OPERATION; | 
|  | 1819 | } | 
|  | 1820 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 1821 | patch->sinks[0].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 1822 | return INVALID_OPERATION; | 
|  | 1823 | } | 
|  | 1824 |  | 
|  | 1825 | sp<AudioPatch> patchDesc; | 
|  | 1826 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 1827 |  | 
|  | 1828 | ALOGV("createAudioPatch sink id %d role %d type %d", patch->sinks[0].id, patch->sinks[0].role, | 
|  | 1829 | patch->sinks[0].type); | 
|  | 1830 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 1831 | patch->sources[0].role, | 
|  | 1832 | patch->sources[0].type); | 
|  | 1833 |  | 
|  | 1834 | if (index >= 0) { | 
|  | 1835 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 1836 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 1837 | mUidCached, patchDesc->mUid, uid); | 
|  | 1838 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 1839 | return INVALID_OPERATION; | 
|  | 1840 | } | 
|  | 1841 | } else { | 
|  | 1842 | *handle = 0; | 
|  | 1843 | } | 
|  | 1844 |  | 
|  | 1845 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 1846 | // TODO add support for mix to mix connection | 
|  | 1847 | if (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 1848 | ALOGV("createAudioPatch() source mix sink not device"); | 
|  | 1849 | return BAD_VALUE; | 
|  | 1850 | } | 
|  | 1851 | // output mix to output device connection | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1852 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1853 | if (outputDesc == NULL) { | 
|  | 1854 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 1855 | return BAD_VALUE; | 
|  | 1856 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1857 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 1858 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1859 | if (patchDesc != 0) { | 
|  | 1860 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 1861 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 1862 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 1863 | return BAD_VALUE; | 
|  | 1864 | } | 
|  | 1865 | } | 
|  | 1866 | sp<DeviceDescriptor> devDesc = | 
|  | 1867 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); | 
|  | 1868 | if (devDesc == 0) { | 
|  | 1869 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[0].id); | 
|  | 1870 | return BAD_VALUE; | 
|  | 1871 | } | 
|  | 1872 |  | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1873 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1874 | patch->sources[0].sample_rate, | 
|  | 1875 | patch->sources[0].format, | 
|  | 1876 | patch->sources[0].channel_mask, | 
|  | 1877 | AUDIO_OUTPUT_FLAG_NONE)) { | 
|  | 1878 | return INVALID_OPERATION; | 
|  | 1879 | } | 
|  | 1880 | // TODO: reconfigure output format and channels here | 
|  | 1881 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1882 | devDesc->mDeviceType, outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1883 | setOutputDevice(outputDesc->mIoHandle, | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1884 | devDesc->mDeviceType, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1885 | true, | 
|  | 1886 | 0, | 
|  | 1887 | handle); | 
|  | 1888 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 1889 | if (index >= 0) { | 
|  | 1890 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 1891 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 1892 | } | 
|  | 1893 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 1894 | patchDesc->mUid = uid; | 
|  | 1895 | ALOGV("createAudioPatch() success"); | 
|  | 1896 | } else { | 
|  | 1897 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 1898 | return INVALID_OPERATION; | 
|  | 1899 | } | 
|  | 1900 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 1901 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 1902 | // input device to input mix connection | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1903 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1904 | if (inputDesc == NULL) { | 
|  | 1905 | return BAD_VALUE; | 
|  | 1906 | } | 
|  | 1907 | if (patchDesc != 0) { | 
|  | 1908 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 1909 | return BAD_VALUE; | 
|  | 1910 | } | 
|  | 1911 | } | 
|  | 1912 | sp<DeviceDescriptor> devDesc = | 
|  | 1913 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 1914 | if (devDesc == 0) { | 
|  | 1915 | return BAD_VALUE; | 
|  | 1916 | } | 
|  | 1917 |  | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1918 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1919 | patch->sinks[0].sample_rate, | 
|  | 1920 | patch->sinks[0].format, | 
|  | 1921 | patch->sinks[0].channel_mask, | 
|  | 1922 | AUDIO_OUTPUT_FLAG_NONE)) { | 
|  | 1923 | return INVALID_OPERATION; | 
|  | 1924 | } | 
|  | 1925 | // TODO: reconfigure output format and channels here | 
|  | 1926 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1927 | devDesc->mDeviceType, inputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1928 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1929 | devDesc->mDeviceType, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1930 | true, | 
|  | 1931 | handle); | 
|  | 1932 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 1933 | if (index >= 0) { | 
|  | 1934 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 1935 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 1936 | } | 
|  | 1937 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 1938 | patchDesc->mUid = uid; | 
|  | 1939 | ALOGV("createAudioPatch() success"); | 
|  | 1940 | } else { | 
|  | 1941 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 1942 | return INVALID_OPERATION; | 
|  | 1943 | } | 
|  | 1944 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 1945 | // device to device connection | 
|  | 1946 | if (patchDesc != 0) { | 
|  | 1947 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id && | 
|  | 1948 | patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 1949 | return BAD_VALUE; | 
|  | 1950 | } | 
|  | 1951 | } | 
|  | 1952 |  | 
|  | 1953 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 1954 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 1955 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 1956 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); | 
|  | 1957 | if (srcDeviceDesc == 0 || sinkDeviceDesc == 0) { | 
|  | 1958 | return BAD_VALUE; | 
|  | 1959 | } | 
|  | 1960 | //update source and sink with our own data as the data passed in the patch may | 
|  | 1961 | // be incomplete. | 
|  | 1962 | struct audio_patch newPatch = *patch; | 
|  | 1963 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
|  | 1964 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[0], &patch->sinks[0]); | 
|  | 1965 |  | 
|  | 1966 | // TODO: add support for devices on different HW modules | 
|  | 1967 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { | 
|  | 1968 | return INVALID_OPERATION; | 
|  | 1969 | } | 
|  | 1970 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 1971 |  | 
|  | 1972 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 1973 | if (index >= 0) { | 
|  | 1974 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 1975 | } | 
|  | 1976 |  | 
|  | 1977 | status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
|  | 1978 | &afPatchHandle, | 
|  | 1979 | 0); | 
|  | 1980 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 1981 | status, afPatchHandle); | 
|  | 1982 | if (status == NO_ERROR) { | 
|  | 1983 | if (index < 0) { | 
|  | 1984 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 1985 | &newPatch, uid); | 
|  | 1986 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 1987 | } else { | 
|  | 1988 | patchDesc->mPatch = newPatch; | 
|  | 1989 | } | 
|  | 1990 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 1991 | *handle = patchDesc->mHandle; | 
|  | 1992 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1993 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1994 | } else { | 
|  | 1995 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 1996 | status); | 
|  | 1997 | return INVALID_OPERATION; | 
|  | 1998 | } | 
|  | 1999 | } else { | 
|  | 2000 | return BAD_VALUE; | 
|  | 2001 | } | 
|  | 2002 | } else { | 
|  | 2003 | return BAD_VALUE; | 
|  | 2004 | } | 
|  | 2005 | return NO_ERROR; | 
|  | 2006 | } | 
|  | 2007 |  | 
|  | 2008 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 2009 | uid_t uid) | 
|  | 2010 | { | 
|  | 2011 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 2012 |  | 
|  | 2013 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2014 |  | 
|  | 2015 | if (index < 0) { | 
|  | 2016 | return BAD_VALUE; | 
|  | 2017 | } | 
|  | 2018 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 2019 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2020 | mUidCached, patchDesc->mUid, uid); | 
|  | 2021 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2022 | return INVALID_OPERATION; | 
|  | 2023 | } | 
|  | 2024 |  | 
|  | 2025 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 2026 | patchDesc->mUid = mUidCached; | 
|  | 2027 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2028 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2029 | if (outputDesc == NULL) { | 
|  | 2030 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2031 | return BAD_VALUE; | 
|  | 2032 | } | 
|  | 2033 |  | 
|  | 2034 | setOutputDevice(outputDesc->mIoHandle, | 
|  | 2035 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), | 
|  | 2036 | true, | 
|  | 2037 | 0, | 
|  | 2038 | NULL); | 
|  | 2039 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2040 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2041 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2042 | if (inputDesc == NULL) { | 
|  | 2043 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 2044 | return BAD_VALUE; | 
|  | 2045 | } | 
|  | 2046 | setInputDevice(inputDesc->mIoHandle, | 
|  | 2047 | getNewInputDevice(inputDesc->mIoHandle), | 
|  | 2048 | true, | 
|  | 2049 | NULL); | 
|  | 2050 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2051 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2052 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 2053 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2054 | status, patchDesc->mAfPatchHandle); | 
|  | 2055 | removeAudioPatch(patchDesc->mHandle); | 
|  | 2056 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2057 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2058 | } else { | 
|  | 2059 | return BAD_VALUE; | 
|  | 2060 | } | 
|  | 2061 | } else { | 
|  | 2062 | return BAD_VALUE; | 
|  | 2063 | } | 
|  | 2064 | return NO_ERROR; | 
|  | 2065 | } | 
|  | 2066 |  | 
|  | 2067 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 2068 | struct audio_patch *patches, | 
|  | 2069 | unsigned int *generation) | 
|  | 2070 | { | 
|  | 2071 | if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || | 
|  | 2072 | generation == NULL) { | 
|  | 2073 | return BAD_VALUE; | 
|  | 2074 | } | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2075 | ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2076 | *num_patches, patches, mAudioPatches.size()); | 
|  | 2077 | if (patches == NULL) { | 
|  | 2078 | *num_patches = 0; | 
|  | 2079 | } | 
|  | 2080 |  | 
|  | 2081 | size_t patchesWritten = 0; | 
|  | 2082 | size_t patchesMax = *num_patches; | 
|  | 2083 | for (size_t i = 0; | 
|  | 2084 | i  < mAudioPatches.size() && patchesWritten < patchesMax; i++) { | 
|  | 2085 | patches[patchesWritten] = mAudioPatches[i]->mPatch; | 
|  | 2086 | patches[patchesWritten++].id = mAudioPatches[i]->mHandle; | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2087 | ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2088 | i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks); | 
|  | 2089 | } | 
|  | 2090 | *num_patches = mAudioPatches.size(); | 
|  | 2091 |  | 
|  | 2092 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2093 | ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2094 | return NO_ERROR; | 
|  | 2095 | } | 
|  | 2096 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2097 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2098 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2099 | ALOGV("setAudioPortConfig()"); | 
|  | 2100 |  | 
|  | 2101 | if (config == NULL) { | 
|  | 2102 | return BAD_VALUE; | 
|  | 2103 | } | 
|  | 2104 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 2105 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2106 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 2107 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2108 | } | 
|  | 2109 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2110 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2111 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2112 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2113 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2114 | if (outputDesc == NULL) { | 
|  | 2115 | return BAD_VALUE; | 
|  | 2116 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2117 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 2118 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 2119 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2120 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2121 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2122 | sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2123 | if (inputDesc == NULL) { | 
|  | 2124 | return BAD_VALUE; | 
|  | 2125 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2126 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2127 | } else { | 
|  | 2128 | return BAD_VALUE; | 
|  | 2129 | } | 
|  | 2130 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2131 | sp<DeviceDescriptor> deviceDesc; | 
|  | 2132 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 2133 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 2134 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 2135 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 2136 | } else { | 
|  | 2137 | return BAD_VALUE; | 
|  | 2138 | } | 
|  | 2139 | if (deviceDesc == NULL) { | 
|  | 2140 | return BAD_VALUE; | 
|  | 2141 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2142 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2143 | } else { | 
|  | 2144 | return BAD_VALUE; | 
|  | 2145 | } | 
|  | 2146 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2147 | struct audio_port_config backupConfig; | 
|  | 2148 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 2149 | if (status == NO_ERROR) { | 
|  | 2150 | struct audio_port_config newConfig; | 
|  | 2151 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 2152 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2153 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2154 | if (status != NO_ERROR) { | 
|  | 2155 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2156 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2157 |  | 
|  | 2158 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2159 | } | 
|  | 2160 |  | 
|  | 2161 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 2162 | { | 
|  | 2163 | for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++)  { | 
|  | 2164 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 2165 | if (patchDesc->mUid == uid) { | 
|  | 2166 | // releaseAudioPatch() removes the patch from mAudioPatches | 
|  | 2167 | if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) { | 
|  | 2168 | i--; | 
|  | 2169 | } | 
|  | 2170 | } | 
|  | 2171 | } | 
|  | 2172 | } | 
|  | 2173 |  | 
|  | 2174 | status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle, | 
|  | 2175 | const sp<AudioPatch>& patch) | 
|  | 2176 | { | 
|  | 2177 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2178 |  | 
|  | 2179 | if (index >= 0) { | 
|  | 2180 | ALOGW("addAudioPatch() patch %d already in", handle); | 
|  | 2181 | return ALREADY_EXISTS; | 
|  | 2182 | } | 
|  | 2183 | mAudioPatches.add(handle, patch); | 
|  | 2184 | ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d" | 
|  | 2185 | "sink handle %d", | 
|  | 2186 | handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks, | 
|  | 2187 | patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id); | 
|  | 2188 | return NO_ERROR; | 
|  | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle) | 
|  | 2192 | { | 
|  | 2193 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2194 |  | 
|  | 2195 | if (index < 0) { | 
|  | 2196 | ALOGW("removeAudioPatch() patch %d not in", handle); | 
|  | 2197 | return ALREADY_EXISTS; | 
|  | 2198 | } | 
|  | 2199 | ALOGV("removeAudioPatch() handle %d af handle %d", handle, | 
|  | 2200 | mAudioPatches.valueAt(index)->mAfPatchHandle); | 
|  | 2201 | mAudioPatches.removeItemsAt(index); | 
|  | 2202 | return NO_ERROR; | 
|  | 2203 | } | 
|  | 2204 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2205 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2206 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2207 | // ---------------------------------------------------------------------------- | 
|  | 2208 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2209 | uint32_t AudioPolicyManager::nextUniqueId() | 
|  | 2210 | { | 
|  | 2211 | return android_atomic_inc(&mNextUniqueId); | 
|  | 2212 | } | 
|  | 2213 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2214 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 2215 | { | 
|  | 2216 | return android_atomic_inc(&mAudioPortGeneration); | 
|  | 2217 | } | 
|  | 2218 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2219 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2220 | : | 
|  | 2221 | #ifdef AUDIO_POLICY_TEST | 
|  | 2222 | Thread(false), | 
|  | 2223 | #endif //AUDIO_POLICY_TEST | 
|  | 2224 | mPrimaryOutput((audio_io_handle_t)0), | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2225 | mPhoneState(AUDIO_MODE_NORMAL), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2226 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
|  | 2227 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2228 | mA2dpSuspended(false), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2229 | mSpeakerDrcEnabled(false), mNextUniqueId(1), | 
|  | 2230 | mAudioPortGeneration(1) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2231 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2232 | mUidCached = getuid(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2233 | mpClientInterface = clientInterface; | 
|  | 2234 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2235 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { | 
|  | 2236 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2237 | } | 
|  | 2238 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2239 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2240 | if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { | 
|  | 2241 | if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { | 
|  | 2242 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
|  | 2243 | defaultAudioPolicyConfig(); | 
|  | 2244 | } | 
|  | 2245 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2246 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2247 |  | 
|  | 2248 | // must be done after reading the policy | 
|  | 2249 | initializeVolumeCurves(); | 
|  | 2250 |  | 
|  | 2251 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2252 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 2253 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2254 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 2255 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); | 
|  | 2256 | if (mHwModules[i]->mHandle == 0) { | 
|  | 2257 | ALOGW("could not open HW module %s", mHwModules[i]->mName); | 
|  | 2258 | continue; | 
|  | 2259 | } | 
|  | 2260 | // open all output streams needed to access attached devices | 
|  | 2261 | // except for direct output streams that are only opened when they are actually | 
|  | 2262 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2263 | // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2264 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 2265 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2266 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2267 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2268 | if (outProfile->mSupportedDevices.isEmpty()) { | 
|  | 2269 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); | 
|  | 2270 | continue; | 
|  | 2271 | } | 
|  | 2272 |  | 
|  | 2273 | audio_devices_t profileTypes = outProfile->mSupportedDevices.types(); | 
|  | 2274 | if ((profileTypes & outputDeviceTypes) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2275 | ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2276 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2277 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2278 | outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice->mDeviceType & profileTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2279 | audio_io_handle_t output = mpClientInterface->openOutput( | 
|  | 2280 | outProfile->mModule->mHandle, | 
|  | 2281 | &outputDesc->mDevice, | 
|  | 2282 | &outputDesc->mSamplingRate, | 
|  | 2283 | &outputDesc->mFormat, | 
|  | 2284 | &outputDesc->mChannelMask, | 
|  | 2285 | &outputDesc->mLatency, | 
|  | 2286 | outputDesc->mFlags); | 
|  | 2287 | if (output == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2288 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 2289 | outputDesc->mDevice, | 
|  | 2290 | mHwModules[i]->mName); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2291 | } else { | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2292 | for (size_t k = 0; k  < outProfile->mSupportedDevices.size(); k++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2293 | audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2294 | ssize_t index = | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2295 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2296 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 2297 | if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) { | 
|  | 2298 | mAvailableOutputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2299 | mAvailableOutputDevices[index]->mModule = mHwModules[i]; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2300 | } | 
|  | 2301 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | if (mPrimaryOutput == 0 && | 
|  | 2303 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 2304 | mPrimaryOutput = output; | 
|  | 2305 | } | 
|  | 2306 | addOutput(output, outputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2307 | ALOGI("CSTOR setOutputDevice %08x", outputDesc->mDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2308 | setOutputDevice(output, | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2309 | outputDesc->mDevice, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2310 | true); | 
|  | 2311 | } | 
|  | 2312 | } | 
|  | 2313 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2314 | // open input streams needed to access attached devices to validate | 
|  | 2315 | // mAvailableInputDevices list | 
|  | 2316 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 2317 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2318 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2319 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2320 | if (inProfile->mSupportedDevices.isEmpty()) { | 
|  | 2321 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); | 
|  | 2322 | continue; | 
|  | 2323 | } | 
|  | 2324 |  | 
|  | 2325 | audio_devices_t profileTypes = inProfile->mSupportedDevices.types(); | 
|  | 2326 | if (profileTypes & inputDeviceTypes) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2327 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2328 |  | 
|  | 2329 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2330 | inputDesc->mDevice = inProfile->mSupportedDevices[0]->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2331 | audio_io_handle_t input = mpClientInterface->openInput( | 
|  | 2332 | inProfile->mModule->mHandle, | 
|  | 2333 | &inputDesc->mDevice, | 
|  | 2334 | &inputDesc->mSamplingRate, | 
|  | 2335 | &inputDesc->mFormat, | 
| Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2336 | &inputDesc->mChannelMask, | 
|  | 2337 | AUDIO_INPUT_FLAG_FAST /*FIXME*/); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2338 |  | 
|  | 2339 | if (input != 0) { | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2340 | for (size_t k = 0; k  < inProfile->mSupportedDevices.size(); k++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2341 | audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2342 | ssize_t index = | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2343 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2344 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 2345 | if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) { | 
|  | 2346 | mAvailableInputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2347 | mAvailableInputDevices[index]->mModule = mHwModules[i]; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2348 | } | 
|  | 2349 | } | 
|  | 2350 | mpClientInterface->closeInput(input); | 
|  | 2351 | } else { | 
|  | 2352 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
|  | 2353 | inputDesc->mDevice, | 
|  | 2354 | mHwModules[i]->mName); | 
|  | 2355 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2356 | } | 
|  | 2357 | } | 
|  | 2358 | } | 
|  | 2359 | // make sure all attached devices have been allocated a unique ID | 
|  | 2360 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
|  | 2361 | if (mAvailableOutputDevices[i]->mId == 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2362 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2363 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 2364 | continue; | 
|  | 2365 | } | 
|  | 2366 | i++; | 
|  | 2367 | } | 
|  | 2368 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
|  | 2369 | if (mAvailableInputDevices[i]->mId == 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2370 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2371 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 2372 | continue; | 
|  | 2373 | } | 
|  | 2374 | i++; | 
|  | 2375 | } | 
|  | 2376 | // make sure default device is reachable | 
|  | 2377 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2378 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2379 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2380 |  | 
|  | 2381 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
|  | 2382 |  | 
|  | 2383 | updateDevicesAndOutputs(); | 
|  | 2384 |  | 
|  | 2385 | #ifdef AUDIO_POLICY_TEST | 
|  | 2386 | if (mPrimaryOutput != 0) { | 
|  | 2387 | AudioParameter outputCmd = AudioParameter(); | 
|  | 2388 | outputCmd.addInt(String8("set_id"), 0); | 
|  | 2389 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); | 
|  | 2390 |  | 
|  | 2391 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 2392 | mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2393 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 2394 | mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2395 | mTestLatencyMs = 0; | 
|  | 2396 | mCurOutput = 0; | 
|  | 2397 | mDirectOutput = false; | 
|  | 2398 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 2399 | mTestOutputs[i] = 0; | 
|  | 2400 | } | 
|  | 2401 |  | 
|  | 2402 | const size_t SIZE = 256; | 
|  | 2403 | char buffer[SIZE]; | 
|  | 2404 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
|  | 2405 | run(buffer, ANDROID_PRIORITY_AUDIO); | 
|  | 2406 | } | 
|  | 2407 | #endif //AUDIO_POLICY_TEST | 
|  | 2408 | } | 
|  | 2409 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2410 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2411 | { | 
|  | 2412 | #ifdef AUDIO_POLICY_TEST | 
|  | 2413 | exit(); | 
|  | 2414 | #endif //AUDIO_POLICY_TEST | 
|  | 2415 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2416 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2417 | } | 
|  | 2418 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2419 | mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2420 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2421 | mAvailableOutputDevices.clear(); | 
|  | 2422 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2423 | mOutputs.clear(); | 
|  | 2424 | mInputs.clear(); | 
|  | 2425 | mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2426 | } | 
|  | 2427 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2428 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2429 | { | 
|  | 2430 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; | 
|  | 2431 | } | 
|  | 2432 |  | 
|  | 2433 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2434 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2435 | { | 
|  | 2436 | ALOGV("entering threadLoop()"); | 
|  | 2437 | while (!exitPending()) | 
|  | 2438 | { | 
|  | 2439 | String8 command; | 
|  | 2440 | int valueInt; | 
|  | 2441 | String8 value; | 
|  | 2442 |  | 
|  | 2443 | Mutex::Autolock _l(mLock); | 
|  | 2444 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
|  | 2445 |  | 
|  | 2446 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
|  | 2447 | AudioParameter param = AudioParameter(command); | 
|  | 2448 |  | 
|  | 2449 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
|  | 2450 | valueInt != 0) { | 
|  | 2451 | ALOGV("Test command %s received", command.string()); | 
|  | 2452 | String8 target; | 
|  | 2453 | if (param.get(String8("target"), target) != NO_ERROR) { | 
|  | 2454 | target = "Manager"; | 
|  | 2455 | } | 
|  | 2456 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
|  | 2457 | param.remove(String8("test_cmd_policy_output")); | 
|  | 2458 | mCurOutput = valueInt; | 
|  | 2459 | } | 
|  | 2460 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
|  | 2461 | param.remove(String8("test_cmd_policy_direct")); | 
|  | 2462 | if (value == "false") { | 
|  | 2463 | mDirectOutput = false; | 
|  | 2464 | } else if (value == "true") { | 
|  | 2465 | mDirectOutput = true; | 
|  | 2466 | } | 
|  | 2467 | } | 
|  | 2468 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
|  | 2469 | param.remove(String8("test_cmd_policy_input")); | 
|  | 2470 | mTestInput = valueInt; | 
|  | 2471 | } | 
|  | 2472 |  | 
|  | 2473 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
|  | 2474 | param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2475 | int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2476 | if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2477 | format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2478 | } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2479 | format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2480 | } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2481 | format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2482 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2483 | if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2484 | if (target == "Manager") { | 
|  | 2485 | mTestFormat = format; | 
|  | 2486 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2487 | AudioParameter outputParam = AudioParameter(); | 
|  | 2488 | outputParam.addInt(String8("format"), format); | 
|  | 2489 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2490 | } | 
|  | 2491 | } | 
|  | 2492 | } | 
|  | 2493 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
|  | 2494 | param.remove(String8("test_cmd_policy_channels")); | 
|  | 2495 | int channels = 0; | 
|  | 2496 |  | 
|  | 2497 | if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2498 | channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2499 | } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2500 | channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2501 | } | 
|  | 2502 | if (channels != 0) { | 
|  | 2503 | if (target == "Manager") { | 
|  | 2504 | mTestChannels = channels; | 
|  | 2505 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2506 | AudioParameter outputParam = AudioParameter(); | 
|  | 2507 | outputParam.addInt(String8("channels"), channels); | 
|  | 2508 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2509 | } | 
|  | 2510 | } | 
|  | 2511 | } | 
|  | 2512 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
|  | 2513 | param.remove(String8("test_cmd_policy_sampleRate")); | 
|  | 2514 | if (valueInt >= 0 && valueInt <= 96000) { | 
|  | 2515 | int samplingRate = valueInt; | 
|  | 2516 | if (target == "Manager") { | 
|  | 2517 | mTestSamplingRate = samplingRate; | 
|  | 2518 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2519 | AudioParameter outputParam = AudioParameter(); | 
|  | 2520 | outputParam.addInt(String8("sampling_rate"), samplingRate); | 
|  | 2521 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2522 | } | 
|  | 2523 | } | 
|  | 2524 | } | 
|  | 2525 |  | 
|  | 2526 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
|  | 2527 | param.remove(String8("test_cmd_policy_reopen")); | 
|  | 2528 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2529 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2530 | mpClientInterface->closeOutput(mPrimaryOutput); | 
|  | 2531 |  | 
|  | 2532 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; | 
|  | 2533 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2534 | mOutputs.removeItem(mPrimaryOutput); | 
|  | 2535 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2536 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2537 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 2538 | mPrimaryOutput = mpClientInterface->openOutput(moduleHandle, | 
|  | 2539 | &outputDesc->mDevice, | 
|  | 2540 | &outputDesc->mSamplingRate, | 
|  | 2541 | &outputDesc->mFormat, | 
|  | 2542 | &outputDesc->mChannelMask, | 
|  | 2543 | &outputDesc->mLatency, | 
|  | 2544 | outputDesc->mFlags); | 
|  | 2545 | if (mPrimaryOutput == 0) { | 
|  | 2546 | ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d", | 
|  | 2547 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
|  | 2548 | } else { | 
|  | 2549 | AudioParameter outputCmd = AudioParameter(); | 
|  | 2550 | outputCmd.addInt(String8("set_id"), 0); | 
|  | 2551 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); | 
|  | 2552 | addOutput(mPrimaryOutput, outputDesc); | 
|  | 2553 | } | 
|  | 2554 | } | 
|  | 2555 |  | 
|  | 2556 |  | 
|  | 2557 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
|  | 2558 | } | 
|  | 2559 | } | 
|  | 2560 | return false; | 
|  | 2561 | } | 
|  | 2562 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2563 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2564 | { | 
|  | 2565 | { | 
|  | 2566 | AutoMutex _l(mLock); | 
|  | 2567 | requestExit(); | 
|  | 2568 | mWaitWorkCV.signal(); | 
|  | 2569 | } | 
|  | 2570 | requestExitAndWait(); | 
|  | 2571 | } | 
|  | 2572 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2573 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2574 | { | 
|  | 2575 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 2576 | if (output == mTestOutputs[i]) return i; | 
|  | 2577 | } | 
|  | 2578 | return 0; | 
|  | 2579 | } | 
|  | 2580 | #endif //AUDIO_POLICY_TEST | 
|  | 2581 |  | 
|  | 2582 | // --- | 
|  | 2583 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2584 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2585 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2586 | outputDesc->mIoHandle = output; | 
|  | 2587 | outputDesc->mId = nextUniqueId(); | 
|  | 2588 | mOutputs.add(output, outputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2589 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2590 | } | 
|  | 2591 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2592 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2593 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2594 | inputDesc->mIoHandle = input; | 
|  | 2595 | inputDesc->mId = nextUniqueId(); | 
|  | 2596 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2597 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2598 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2599 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2600 | String8 AudioPolicyManager::addressToParameter(audio_devices_t device, const String8 address) | 
|  | 2601 | { | 
|  | 2602 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { | 
|  | 2603 | return String8("a2dp_sink_address=")+address; | 
|  | 2604 | } | 
|  | 2605 | return address; | 
|  | 2606 | } | 
|  | 2607 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2608 | status_t AudioPolicyManager::checkOutputsForDevice(audio_devices_t device, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2609 | audio_policy_dev_state_t state, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2610 | SortedVector<audio_io_handle_t>& outputs, | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2611 | const String8 address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2612 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2613 | sp<AudioOutputDescriptor> desc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2614 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2615 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2616 | // first list already open outputs that can be routed to this device | 
|  | 2617 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2618 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2619 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2620 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 2621 | outputs.add(mOutputs.keyAt(i)); | 
|  | 2622 | } | 
|  | 2623 | } | 
|  | 2624 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2625 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2626 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 2627 | { | 
|  | 2628 | if (mHwModules[i]->mHandle == 0) { | 
|  | 2629 | continue; | 
|  | 2630 | } | 
|  | 2631 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 2632 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2633 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2634 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2635 | profiles.add(mHwModules[i]->mOutputProfiles[j]); | 
|  | 2636 | } | 
|  | 2637 | } | 
|  | 2638 | } | 
|  | 2639 |  | 
|  | 2640 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 2641 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 2642 | return BAD_VALUE; | 
|  | 2643 | } | 
|  | 2644 |  | 
|  | 2645 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 2646 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 2647 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2648 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2649 |  | 
|  | 2650 | // nothing to do if one output is already opened for this profile | 
|  | 2651 | size_t j; | 
|  | 2652 | for (j = 0; j < mOutputs.size(); j++) { | 
|  | 2653 | desc = mOutputs.valueAt(j); | 
|  | 2654 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
|  | 2655 | break; | 
|  | 2656 | } | 
|  | 2657 | } | 
|  | 2658 | if (j != mOutputs.size()) { | 
|  | 2659 | continue; | 
|  | 2660 | } | 
|  | 2661 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2662 | ALOGV("opening output for device %08x with params %s", device, address.string()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2663 | desc = new AudioOutputDescriptor(profile); | 
|  | 2664 | desc->mDevice = device; | 
|  | 2665 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; | 
|  | 2666 | offloadInfo.sample_rate = desc->mSamplingRate; | 
|  | 2667 | offloadInfo.format = desc->mFormat; | 
|  | 2668 | offloadInfo.channel_mask = desc->mChannelMask; | 
|  | 2669 |  | 
|  | 2670 | audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle, | 
|  | 2671 | &desc->mDevice, | 
|  | 2672 | &desc->mSamplingRate, | 
|  | 2673 | &desc->mFormat, | 
|  | 2674 | &desc->mChannelMask, | 
|  | 2675 | &desc->mLatency, | 
|  | 2676 | desc->mFlags, | 
|  | 2677 | &offloadInfo); | 
|  | 2678 | if (output != 0) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2679 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2680 | if (!address.isEmpty()) { | 
|  | 2681 | mpClientInterface->setParameters(output, addressToParameter(device, address)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2682 | } | 
|  | 2683 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2684 | // Here is where we step through and resolve any "dynamic" fields | 
|  | 2685 | String8 reply; | 
|  | 2686 | char *value; | 
|  | 2687 | if (profile->mSamplingRates[0] == 0) { | 
|  | 2688 | reply = mpClientInterface->getParameters(output, | 
|  | 2689 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); | 
|  | 2690 | ALOGV("checkOutputsForDevice() direct output sup sampling rates %s", | 
|  | 2691 | reply.string()); | 
|  | 2692 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2693 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2694 | profile->loadSamplingRates(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2695 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2696 | } | 
|  | 2697 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 2698 | reply = mpClientInterface->getParameters(output, | 
|  | 2699 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
|  | 2700 | ALOGV("checkOutputsForDevice() direct output sup formats %s", | 
|  | 2701 | reply.string()); | 
|  | 2702 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2703 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2704 | profile->loadFormats(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2705 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2706 | } | 
|  | 2707 | if (profile->mChannelMasks[0] == 0) { | 
|  | 2708 | reply = mpClientInterface->getParameters(output, | 
|  | 2709 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); | 
|  | 2710 | ALOGV("checkOutputsForDevice() direct output sup channel masks %s", | 
|  | 2711 | reply.string()); | 
|  | 2712 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2713 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2714 | profile->loadOutChannels(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2715 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2716 | } | 
|  | 2717 | if (((profile->mSamplingRates[0] == 0) && | 
|  | 2718 | (profile->mSamplingRates.size() < 2)) || | 
|  | 2719 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && | 
|  | 2720 | (profile->mFormats.size() < 2)) || | 
|  | 2721 | ((profile->mChannelMasks[0] == 0) && | 
|  | 2722 | (profile->mChannelMasks.size() < 2))) { | 
|  | 2723 | ALOGW("checkOutputsForDevice() direct output missing param"); | 
|  | 2724 | mpClientInterface->closeOutput(output); | 
|  | 2725 | output = 0; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 2726 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || | 
|  | 2727 | profile->mChannelMasks[0] == 0) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2728 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 2729 | desc->mSamplingRate = profile->pickSamplingRate(); | 
|  | 2730 | desc->mFormat = profile->pickFormat(); | 
|  | 2731 | desc->mChannelMask = profile->pickChannelMask(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2732 | offloadInfo.sample_rate = desc->mSamplingRate; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 2733 | offloadInfo.format = desc->mFormat; | 
|  | 2734 | offloadInfo.channel_mask = desc->mChannelMask; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2735 | output = mpClientInterface->openOutput( | 
|  | 2736 | profile->mModule->mHandle, | 
|  | 2737 | &desc->mDevice, | 
|  | 2738 | &desc->mSamplingRate, | 
|  | 2739 | &desc->mFormat, | 
|  | 2740 | &desc->mChannelMask, | 
|  | 2741 | &desc->mLatency, | 
|  | 2742 | desc->mFlags, | 
|  | 2743 | &offloadInfo); | 
|  | 2744 | } | 
|  | 2745 |  | 
|  | 2746 | if (output != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2747 | addOutput(output, desc); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2748 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { | 
|  | 2749 | audio_io_handle_t duplicatedOutput = 0; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2750 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2751 | // set initial stream volume for device | 
|  | 2752 | applyStreamVolumes(output, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2753 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2754 | //TODO: configure audio effect output stage here | 
|  | 2755 |  | 
|  | 2756 | // open a duplicating output thread for the new output and the primary output | 
|  | 2757 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, | 
|  | 2758 | mPrimaryOutput); | 
|  | 2759 | if (duplicatedOutput != 0) { | 
|  | 2760 | // add duplicated output descriptor | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2761 | sp<AudioOutputDescriptor> dupOutputDesc = new AudioOutputDescriptor(NULL); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2762 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); | 
|  | 2763 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); | 
|  | 2764 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
|  | 2765 | dupOutputDesc->mFormat = desc->mFormat; | 
|  | 2766 | dupOutputDesc->mChannelMask = desc->mChannelMask; | 
|  | 2767 | dupOutputDesc->mLatency = desc->mLatency; | 
|  | 2768 | addOutput(duplicatedOutput, dupOutputDesc); | 
|  | 2769 | applyStreamVolumes(duplicatedOutput, device, 0, true); | 
|  | 2770 | } else { | 
|  | 2771 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
|  | 2772 | mPrimaryOutput, output); | 
|  | 2773 | mpClientInterface->closeOutput(output); | 
|  | 2774 | mOutputs.removeItem(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2775 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2776 | output = 0; | 
|  | 2777 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2778 | } | 
|  | 2779 | } | 
|  | 2780 | } | 
|  | 2781 | if (output == 0) { | 
|  | 2782 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2783 | profiles.removeAt(profile_index); | 
|  | 2784 | profile_index--; | 
|  | 2785 | } else { | 
|  | 2786 | outputs.add(output); | 
|  | 2787 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 2788 | } | 
|  | 2789 | } | 
|  | 2790 |  | 
|  | 2791 | if (profiles.isEmpty()) { | 
|  | 2792 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 2793 | return BAD_VALUE; | 
|  | 2794 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2795 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2796 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 2797 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2798 | desc = mOutputs.valueAt(i); | 
|  | 2799 | if (!desc->isDuplicated() && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2800 | !(desc->mProfile->mSupportedDevices.types() & | 
|  | 2801 | mAvailableOutputDevices.types())) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2802 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i)); | 
|  | 2803 | outputs.add(mOutputs.keyAt(i)); | 
|  | 2804 | } | 
|  | 2805 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2806 | // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2807 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 2808 | { | 
|  | 2809 | if (mHwModules[i]->mHandle == 0) { | 
|  | 2810 | continue; | 
|  | 2811 | } | 
|  | 2812 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 2813 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2814 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2815 | if (profile->mSupportedDevices.types() & device) { | 
|  | 2816 | ALOGV("checkOutputsForDevice(): " | 
|  | 2817 | "clearing direct output profile %zu on module %zu", j, i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2818 | if (profile->mSamplingRates[0] == 0) { | 
|  | 2819 | profile->mSamplingRates.clear(); | 
|  | 2820 | profile->mSamplingRates.add(0); | 
|  | 2821 | } | 
|  | 2822 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 2823 | profile->mFormats.clear(); | 
|  | 2824 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 2825 | } | 
|  | 2826 | if (profile->mChannelMasks[0] == 0) { | 
|  | 2827 | profile->mChannelMasks.clear(); | 
|  | 2828 | profile->mChannelMasks.add(0); | 
|  | 2829 | } | 
|  | 2830 | } | 
|  | 2831 | } | 
|  | 2832 | } | 
|  | 2833 | } | 
|  | 2834 | return NO_ERROR; | 
|  | 2835 | } | 
|  | 2836 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2837 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, | 
|  | 2838 | audio_policy_dev_state_t state, | 
|  | 2839 | SortedVector<audio_io_handle_t>& inputs, | 
|  | 2840 | const String8 address) | 
|  | 2841 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2842 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2843 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 2844 | // first list already open inputs that can be routed to this device | 
|  | 2845 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 2846 | desc = mInputs.valueAt(input_index); | 
|  | 2847 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 2848 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 2849 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 2850 | } | 
|  | 2851 | } | 
|  | 2852 |  | 
|  | 2853 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2854 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2855 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
|  | 2856 | { | 
|  | 2857 | if (mHwModules[module_idx]->mHandle == 0) { | 
|  | 2858 | continue; | 
|  | 2859 | } | 
|  | 2860 | for (size_t profile_index = 0; | 
|  | 2861 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
|  | 2862 | profile_index++) | 
|  | 2863 | { | 
|  | 2864 | if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types() | 
|  | 2865 | & (device & ~AUDIO_DEVICE_BIT_IN)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2866 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2867 | profile_index, module_idx); | 
|  | 2868 | profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]); | 
|  | 2869 | } | 
|  | 2870 | } | 
|  | 2871 | } | 
|  | 2872 |  | 
|  | 2873 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 2874 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 2875 | return BAD_VALUE; | 
|  | 2876 | } | 
|  | 2877 |  | 
|  | 2878 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 2879 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 2880 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 2881 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2882 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2883 | // nothing to do if one input is already opened for this profile | 
|  | 2884 | size_t input_index; | 
|  | 2885 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 2886 | desc = mInputs.valueAt(input_index); | 
|  | 2887 | if (desc->mProfile == profile) { | 
|  | 2888 | break; | 
|  | 2889 | } | 
|  | 2890 | } | 
|  | 2891 | if (input_index != mInputs.size()) { | 
|  | 2892 | continue; | 
|  | 2893 | } | 
|  | 2894 |  | 
|  | 2895 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
|  | 2896 | desc = new AudioInputDescriptor(profile); | 
|  | 2897 | desc->mDevice = device; | 
|  | 2898 |  | 
|  | 2899 | audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle, | 
|  | 2900 | &desc->mDevice, | 
|  | 2901 | &desc->mSamplingRate, | 
|  | 2902 | &desc->mFormat, | 
| Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2903 | &desc->mChannelMask, | 
|  | 2904 | AUDIO_INPUT_FLAG_FAST /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2905 |  | 
|  | 2906 | if (input != 0) { | 
|  | 2907 | if (!address.isEmpty()) { | 
|  | 2908 | mpClientInterface->setParameters(input, addressToParameter(device, address)); | 
|  | 2909 | } | 
|  | 2910 |  | 
|  | 2911 | // Here is where we step through and resolve any "dynamic" fields | 
|  | 2912 | String8 reply; | 
|  | 2913 | char *value; | 
|  | 2914 | if (profile->mSamplingRates[0] == 0) { | 
|  | 2915 | reply = mpClientInterface->getParameters(input, | 
|  | 2916 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); | 
|  | 2917 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", | 
|  | 2918 | reply.string()); | 
|  | 2919 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2920 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2921 | profile->loadSamplingRates(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2922 | } | 
|  | 2923 | } | 
|  | 2924 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 2925 | reply = mpClientInterface->getParameters(input, | 
|  | 2926 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
|  | 2927 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); | 
|  | 2928 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2929 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2930 | profile->loadFormats(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2931 | } | 
|  | 2932 | } | 
|  | 2933 | if (profile->mChannelMasks[0] == 0) { | 
|  | 2934 | reply = mpClientInterface->getParameters(input, | 
|  | 2935 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); | 
|  | 2936 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", | 
|  | 2937 | reply.string()); | 
|  | 2938 | value = strpbrk((char *)reply.string(), "="); | 
|  | 2939 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2940 | profile->loadInChannels(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2941 | } | 
|  | 2942 | } | 
|  | 2943 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || | 
|  | 2944 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || | 
|  | 2945 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { | 
|  | 2946 | ALOGW("checkInputsForDevice() direct input missing param"); | 
|  | 2947 | mpClientInterface->closeInput(input); | 
|  | 2948 | input = 0; | 
|  | 2949 | } | 
|  | 2950 |  | 
|  | 2951 | if (input != 0) { | 
|  | 2952 | addInput(input, desc); | 
|  | 2953 | } | 
|  | 2954 | } // endif input != 0 | 
|  | 2955 |  | 
|  | 2956 | if (input == 0) { | 
|  | 2957 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2958 | profiles.removeAt(profile_index); | 
|  | 2959 | profile_index--; | 
|  | 2960 | } else { | 
|  | 2961 | inputs.add(input); | 
|  | 2962 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 2963 | } | 
|  | 2964 | } // end scan profiles | 
|  | 2965 |  | 
|  | 2966 | if (profiles.isEmpty()) { | 
|  | 2967 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 2968 | return BAD_VALUE; | 
|  | 2969 | } | 
|  | 2970 | } else { | 
|  | 2971 | // Disconnect | 
|  | 2972 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 2973 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 2974 | desc = mInputs.valueAt(input_index); | 
|  | 2975 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) { | 
|  | 2976 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 2977 | mInputs.keyAt(input_index)); | 
|  | 2978 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 2979 | } | 
|  | 2980 | } | 
|  | 2981 | // Clear any profiles associated with the disconnected device. | 
|  | 2982 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
|  | 2983 | if (mHwModules[module_index]->mHandle == 0) { | 
|  | 2984 | continue; | 
|  | 2985 | } | 
|  | 2986 | for (size_t profile_index = 0; | 
|  | 2987 | profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
|  | 2988 | profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2989 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2990 | if (profile->mSupportedDevices.types() & device) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2991 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2992 | profile_index, module_index); | 
|  | 2993 | if (profile->mSamplingRates[0] == 0) { | 
|  | 2994 | profile->mSamplingRates.clear(); | 
|  | 2995 | profile->mSamplingRates.add(0); | 
|  | 2996 | } | 
|  | 2997 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 2998 | profile->mFormats.clear(); | 
|  | 2999 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 3000 | } | 
|  | 3001 | if (profile->mChannelMasks[0] == 0) { | 
|  | 3002 | profile->mChannelMasks.clear(); | 
|  | 3003 | profile->mChannelMasks.add(0); | 
|  | 3004 | } | 
|  | 3005 | } | 
|  | 3006 | } | 
|  | 3007 | } | 
|  | 3008 | } // end disconnect | 
|  | 3009 |  | 
|  | 3010 | return NO_ERROR; | 
|  | 3011 | } | 
|  | 3012 |  | 
|  | 3013 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3014 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3015 | { | 
|  | 3016 | ALOGV("closeOutput(%d)", output); | 
|  | 3017 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3018 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3019 | if (outputDesc == NULL) { | 
|  | 3020 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 3021 | return; | 
|  | 3022 | } | 
|  | 3023 |  | 
|  | 3024 | // look for duplicated outputs connected to the output being removed. | 
|  | 3025 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3026 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3027 | if (dupOutputDesc->isDuplicated() && | 
|  | 3028 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 3029 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3030 | sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3031 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 3032 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 3033 | } else { | 
|  | 3034 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 3035 | } | 
|  | 3036 | // As all active tracks on duplicated output will be deleted, | 
|  | 3037 | // and as they were also referenced on the other output, the reference | 
|  | 3038 | // count for their stream type must be adjusted accordingly on | 
|  | 3039 | // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3040 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3041 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3042 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3043 | } | 
|  | 3044 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 3045 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 3046 |  | 
|  | 3047 | mpClientInterface->closeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3048 | mOutputs.removeItem(duplicatedOutput); | 
|  | 3049 | } | 
|  | 3050 | } | 
|  | 3051 |  | 
|  | 3052 | AudioParameter param; | 
|  | 3053 | param.add(String8("closing"), String8("true")); | 
|  | 3054 | mpClientInterface->setParameters(output, param.toString()); | 
|  | 3055 |  | 
|  | 3056 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3057 | mOutputs.removeItem(output); | 
|  | 3058 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3059 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3060 | } | 
|  | 3061 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3062 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3063 | DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3064 | { | 
|  | 3065 | SortedVector<audio_io_handle_t> outputs; | 
|  | 3066 |  | 
|  | 3067 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 3068 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
|  | 3069 | ALOGVV("output %d isDuplicated=%d device=%04x", | 
|  | 3070 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); | 
|  | 3071 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 3072 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 3073 | outputs.add(openOutputs.keyAt(i)); | 
|  | 3074 | } | 
|  | 3075 | } | 
|  | 3076 | return outputs; | 
|  | 3077 | } | 
|  | 3078 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3079 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3080 | SortedVector<audio_io_handle_t>& outputs2) | 
|  | 3081 | { | 
|  | 3082 | if (outputs1.size() != outputs2.size()) { | 
|  | 3083 | return false; | 
|  | 3084 | } | 
|  | 3085 | for (size_t i = 0; i < outputs1.size(); i++) { | 
|  | 3086 | if (outputs1[i] != outputs2[i]) { | 
|  | 3087 | return false; | 
|  | 3088 | } | 
|  | 3089 | } | 
|  | 3090 | return true; | 
|  | 3091 | } | 
|  | 3092 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3093 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3094 | { | 
|  | 3095 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 3096 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 3097 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 3098 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 3099 |  | 
|  | 3100 | if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
|  | 3101 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 3102 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 3103 | // mute strategy while moving tracks from one output to another | 
|  | 3104 | for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3105 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3106 | if (desc->isStrategyActive(strategy)) { | 
|  | 3107 | setStrategyMute(strategy, true, srcOutputs[i]); | 
|  | 3108 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); | 
|  | 3109 | } | 
|  | 3110 | } | 
|  | 3111 |  | 
|  | 3112 | // Move effects associated to this strategy from previous output to new output | 
|  | 3113 | if (strategy == STRATEGY_MEDIA) { | 
|  | 3114 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); | 
|  | 3115 | SortedVector<audio_io_handle_t> moved; | 
|  | 3116 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3117 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 3118 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && | 
|  | 3119 | effectDesc->mIo != fxOutput) { | 
|  | 3120 | if (moved.indexOf(effectDesc->mIo) < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3121 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", | 
|  | 3122 | mEffects.keyAt(i), fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3123 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3124 | fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3125 | moved.add(effectDesc->mIo); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3126 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3127 | effectDesc->mIo = fxOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3128 | } | 
|  | 3129 | } | 
|  | 3130 | } | 
|  | 3131 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3132 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
|  | 3133 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 3134 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3135 | } | 
|  | 3136 | } | 
|  | 3137 | } | 
|  | 3138 | } | 
|  | 3139 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3140 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3141 | { | 
|  | 3142 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
|  | 3143 | checkOutputForStrategy(STRATEGY_PHONE); | 
|  | 3144 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 3145 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 3146 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 3147 | checkOutputForStrategy(STRATEGY_DTMF); | 
|  | 3148 | } | 
|  | 3149 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3150 | audio_io_handle_t AudioPolicyManager::getA2dpOutput() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3151 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3152 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3153 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3154 | if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { | 
|  | 3155 | return mOutputs.keyAt(i); | 
|  | 3156 | } | 
|  | 3157 | } | 
|  | 3158 |  | 
|  | 3159 | return 0; | 
|  | 3160 | } | 
|  | 3161 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3162 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3163 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3164 | audio_io_handle_t a2dpOutput = getA2dpOutput(); | 
|  | 3165 | if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3166 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3167 | return; | 
|  | 3168 | } | 
|  | 3169 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3170 | bool isScoConnected = | 
|  | 3171 | (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3172 | // suspend A2DP output if: | 
|  | 3173 | //      (NOT already suspended) && | 
|  | 3174 | //      ((SCO device is connected && | 
|  | 3175 | //       (forced usage for communication || for record is SCO))) || | 
|  | 3176 | //      (phone state is ringing || in call) | 
|  | 3177 | // | 
|  | 3178 | // restore A2DP output if: | 
|  | 3179 | //      (Already suspended) && | 
|  | 3180 | //      ((SCO device is NOT connected || | 
|  | 3181 | //       (forced usage NOT for communication && NOT for record is SCO))) && | 
|  | 3182 | //      (phone state is NOT ringing && NOT in call) | 
|  | 3183 | // | 
|  | 3184 | if (mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3185 | if ((!isScoConnected || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3186 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 3187 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && | 
|  | 3188 | ((mPhoneState != AUDIO_MODE_IN_CALL) && | 
|  | 3189 | (mPhoneState != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3190 |  | 
|  | 3191 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 3192 | mA2dpSuspended = false; | 
|  | 3193 | } | 
|  | 3194 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3195 | if ((isScoConnected && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3196 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 3197 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || | 
|  | 3198 | ((mPhoneState == AUDIO_MODE_IN_CALL) || | 
|  | 3199 | (mPhoneState == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3200 |  | 
|  | 3201 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 3202 | mA2dpSuspended = true; | 
|  | 3203 | } | 
|  | 3204 | } | 
|  | 3205 | } | 
|  | 3206 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3207 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3208 | { | 
|  | 3209 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 3210 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3211 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3212 |  | 
|  | 3213 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 3214 | if (index >= 0) { | 
|  | 3215 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3216 | if (patchDesc->mUid != mUidCached) { | 
|  | 3217 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
|  | 3218 | outputDesc->device(), outputDesc->mPatchHandle); | 
|  | 3219 | return outputDesc->device(); | 
|  | 3220 | } | 
|  | 3221 | } | 
|  | 3222 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3223 | // check the following by order of priority to request a routing change if necessary: | 
|  | 3224 | // 1: the strategy enforced audible is active on the output: | 
|  | 3225 | //      use device for strategy enforced audible | 
|  | 3226 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 3227 | //      use device for strategy phone | 
|  | 3228 | // 3: the strategy sonification is active on the output: | 
|  | 3229 | //      use device for strategy sonification | 
|  | 3230 | // 4: the strategy "respectful" sonification is active on the output: | 
|  | 3231 | //      use device for strategy "respectful" sonification | 
|  | 3232 | // 5: the strategy media is active on the output: | 
|  | 3233 | //      use device for strategy media | 
|  | 3234 | // 6: the strategy DTMF is active on the output: | 
|  | 3235 | //      use device for strategy DTMF | 
|  | 3236 | if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { | 
|  | 3237 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 3238 | } else if (isInCall() || | 
|  | 3239 | outputDesc->isStrategyActive(STRATEGY_PHONE)) { | 
|  | 3240 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
|  | 3241 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { | 
|  | 3242 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
|  | 3243 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { | 
|  | 3244 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
|  | 3245 | } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { | 
|  | 3246 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
|  | 3247 | } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { | 
|  | 3248 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
|  | 3249 | } | 
|  | 3250 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3251 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 3252 | return device; | 
|  | 3253 | } | 
|  | 3254 |  | 
|  | 3255 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) | 
|  | 3256 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3257 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3258 |  | 
|  | 3259 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 3260 | if (index >= 0) { | 
|  | 3261 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3262 | if (patchDesc->mUid != mUidCached) { | 
|  | 3263 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
|  | 3264 | inputDesc->mDevice, inputDesc->mPatchHandle); | 
|  | 3265 | return inputDesc->mDevice; | 
|  | 3266 | } | 
|  | 3267 | } | 
|  | 3268 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3269 | audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource); | 
|  | 3270 |  | 
|  | 3271 | ALOGV("getNewInputDevice() selected device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3272 | return device; | 
|  | 3273 | } | 
|  | 3274 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3275 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3276 | return (uint32_t)getStrategy(stream); | 
|  | 3277 | } | 
|  | 3278 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3279 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3280 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 3281 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 3282 | // and then return STRATEGY_MEDIA, but we want to return the empty set. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3283 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3284 | return AUDIO_DEVICE_NONE; | 
|  | 3285 | } | 
|  | 3286 | audio_devices_t devices; | 
|  | 3287 | AudioPolicyManager::routing_strategy strategy = getStrategy(stream); | 
|  | 3288 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 3289 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); | 
|  | 3290 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3291 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3292 | if (outputDesc->isStrategyActive(strategy)) { | 
|  | 3293 | devices = outputDesc->device(); | 
|  | 3294 | break; | 
|  | 3295 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3296 | } | 
|  | 3297 | return devices; | 
|  | 3298 | } | 
|  | 3299 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3300 | AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy( | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3301 | audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3302 | // stream to strategy mapping | 
|  | 3303 | switch (stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3304 | case AUDIO_STREAM_VOICE_CALL: | 
|  | 3305 | case AUDIO_STREAM_BLUETOOTH_SCO: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3306 | return STRATEGY_PHONE; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3307 | case AUDIO_STREAM_RING: | 
|  | 3308 | case AUDIO_STREAM_ALARM: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3309 | return STRATEGY_SONIFICATION; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3310 | case AUDIO_STREAM_NOTIFICATION: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3311 | return STRATEGY_SONIFICATION_RESPECTFUL; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3312 | case AUDIO_STREAM_DTMF: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3313 | return STRATEGY_DTMF; | 
|  | 3314 | default: | 
|  | 3315 | ALOGE("unknown stream type"); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3316 | case AUDIO_STREAM_SYSTEM: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3317 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs | 
|  | 3318 | // while key clicks are played produces a poor result | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3319 | case AUDIO_STREAM_TTS: | 
|  | 3320 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3321 | return STRATEGY_MEDIA; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3322 | case AUDIO_STREAM_ENFORCED_AUDIBLE: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3323 | return STRATEGY_ENFORCED_AUDIBLE; | 
|  | 3324 | } | 
|  | 3325 | } | 
|  | 3326 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3327 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 3328 | // flags to strategy mapping | 
|  | 3329 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 3330 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 3331 | } | 
|  | 3332 |  | 
|  | 3333 | // usage to strategy mapping | 
|  | 3334 | switch (attr->usage) { | 
|  | 3335 | case AUDIO_USAGE_MEDIA: | 
|  | 3336 | case AUDIO_USAGE_GAME: | 
|  | 3337 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 3338 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 3339 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 3340 | return (uint32_t) STRATEGY_MEDIA; | 
|  | 3341 |  | 
|  | 3342 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 3343 | return (uint32_t) STRATEGY_PHONE; | 
|  | 3344 |  | 
|  | 3345 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 3346 | return (uint32_t) STRATEGY_DTMF; | 
|  | 3347 |  | 
|  | 3348 | case AUDIO_USAGE_ALARM: | 
|  | 3349 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 3350 | return (uint32_t) STRATEGY_SONIFICATION; | 
|  | 3351 |  | 
|  | 3352 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 3353 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 3354 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 3355 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 3356 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 3357 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; | 
|  | 3358 |  | 
|  | 3359 | case AUDIO_USAGE_UNKNOWN: | 
|  | 3360 | default: | 
|  | 3361 | return (uint32_t) STRATEGY_MEDIA; | 
|  | 3362 | } | 
|  | 3363 | } | 
|  | 3364 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3365 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3366 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3367 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3368 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 3369 | updateDevicesAndOutputs(); | 
|  | 3370 | break; | 
|  | 3371 | default: | 
|  | 3372 | break; | 
|  | 3373 | } | 
|  | 3374 | } | 
|  | 3375 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3376 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3377 | bool fromCache) | 
|  | 3378 | { | 
|  | 3379 | uint32_t device = AUDIO_DEVICE_NONE; | 
|  | 3380 |  | 
|  | 3381 | if (fromCache) { | 
|  | 3382 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 3383 | strategy, mDeviceForStrategy[strategy]); | 
|  | 3384 | return mDeviceForStrategy[strategy]; | 
|  | 3385 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3386 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3387 | switch (strategy) { | 
|  | 3388 |  | 
|  | 3389 | case STRATEGY_SONIFICATION_RESPECTFUL: | 
|  | 3390 | if (isInCall()) { | 
|  | 3391 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3392 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3393 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { | 
|  | 3394 | // while media is playing on a remote device, use the the sonification behavior. | 
|  | 3395 | // Note that we test this usecase before testing if media is playing because | 
|  | 3396 | //   the isStreamActive() method only informs about the activity of a stream, not | 
|  | 3397 | //   if it's for local playback. Note also that we use the same delay between both tests | 
|  | 3398 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3399 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3400 | // while media is playing (or has recently played), use the same device | 
|  | 3401 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); | 
|  | 3402 | } else { | 
|  | 3403 | // when media is not playing anymore, fall back on the sonification behavior | 
|  | 3404 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
|  | 3405 | } | 
|  | 3406 |  | 
|  | 3407 | break; | 
|  | 3408 |  | 
|  | 3409 | case STRATEGY_DTMF: | 
|  | 3410 | if (!isInCall()) { | 
|  | 3411 | // when off call, DTMF strategy follows the same rules as MEDIA strategy | 
|  | 3412 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); | 
|  | 3413 | break; | 
|  | 3414 | } | 
|  | 3415 | // when in call, DTMF and PHONE strategies follow the same rules | 
|  | 3416 | // FALL THROUGH | 
|  | 3417 |  | 
|  | 3418 | case STRATEGY_PHONE: | 
|  | 3419 | // for phone strategy, we first consider the forced use and then the available devices by order | 
|  | 3420 | // of priority | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3421 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { | 
|  | 3422 | case AUDIO_POLICY_FORCE_BT_SCO: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3423 | if (!isInCall() || strategy != STRATEGY_DTMF) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3424 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3425 | if (device) break; | 
|  | 3426 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3427 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3428 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3429 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3430 | if (device) break; | 
|  | 3431 | // if SCO device is requested but no SCO device is available, fall back to default case | 
|  | 3432 | // FALL THROUGH | 
|  | 3433 |  | 
|  | 3434 | default:    // FORCE_NONE | 
|  | 3435 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3436 | if (!isInCall() && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3437 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3438 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3439 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3440 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3441 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3442 | if (device) break; | 
|  | 3443 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3444 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3445 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3446 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3447 | if (device) break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3448 | if (mPhoneState != AUDIO_MODE_IN_CALL) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3449 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3450 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3451 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3452 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3453 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3454 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3455 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3456 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3457 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3458 | if (device) break; | 
|  | 3459 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3460 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3461 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3462 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3463 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 3464 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); | 
|  | 3465 | } | 
|  | 3466 | break; | 
|  | 3467 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3468 | case AUDIO_POLICY_FORCE_SPEAKER: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3469 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to | 
|  | 3470 | // A2DP speaker when forcing to speaker output | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3471 | if (!isInCall() && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3472 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3473 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3474 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3475 | if (device) break; | 
|  | 3476 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3477 | if (mPhoneState != AUDIO_MODE_IN_CALL) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3478 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3479 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3480 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3481 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3482 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3483 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3484 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3485 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3486 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3487 | if (device) break; | 
|  | 3488 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3489 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3490 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3491 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3492 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 3493 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); | 
|  | 3494 | } | 
|  | 3495 | break; | 
|  | 3496 | } | 
|  | 3497 | break; | 
|  | 3498 |  | 
|  | 3499 | case STRATEGY_SONIFICATION: | 
|  | 3500 |  | 
|  | 3501 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by | 
|  | 3502 | // handleIncallSonification(). | 
|  | 3503 | if (isInCall()) { | 
|  | 3504 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); | 
|  | 3505 | break; | 
|  | 3506 | } | 
|  | 3507 | // FALL THROUGH | 
|  | 3508 |  | 
|  | 3509 | case STRATEGY_ENFORCED_AUDIBLE: | 
|  | 3510 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION | 
|  | 3511 | // except: | 
|  | 3512 | //   - when in call where it doesn't default to STRATEGY_PHONE behavior | 
|  | 3513 | //   - in countries where not enforced in which case it follows STRATEGY_MEDIA | 
|  | 3514 |  | 
|  | 3515 | if ((strategy == STRATEGY_SONIFICATION) || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3516 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3517 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3518 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 3519 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); | 
|  | 3520 | } | 
|  | 3521 | } | 
|  | 3522 | // The second device used for sonification is the same as the device used by media strategy | 
|  | 3523 | // FALL THROUGH | 
|  | 3524 |  | 
|  | 3525 | case STRATEGY_MEDIA: { | 
|  | 3526 | uint32_t device2 = AUDIO_DEVICE_NONE; | 
|  | 3527 | if (strategy != STRATEGY_SONIFICATION) { | 
|  | 3528 | // no sonification on remote submix (e.g. WFD) | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3529 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3530 | } | 
|  | 3531 | if ((device2 == AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3532 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3533 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3534 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3535 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3536 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3537 | } | 
|  | 3538 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3539 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3540 | } | 
|  | 3541 | } | 
|  | 3542 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3543 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | } | 
|  | 3545 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3546 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3547 | } | 
|  | 3548 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3549 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3550 | } | 
|  | 3551 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3552 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3553 | } | 
|  | 3554 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3555 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3556 | } | 
|  | 3557 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { | 
|  | 3558 | // no sonification on aux digital (e.g. HDMI) | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3559 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3560 | } | 
|  | 3561 | if ((device2 == AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3562 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3563 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3564 | } | 
|  | 3565 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3566 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3567 | } | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 3568 | int device3 = AUDIO_DEVICE_NONE; | 
|  | 3569 | if (strategy == STRATEGY_MEDIA) { | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 3570 | // ARC, SPDIF and AUX_LINE can co-exist with others. | 
| Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 3571 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; | 
|  | 3572 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 3573 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 3574 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3575 |  | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 3576 | device2 |= device3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3577 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or | 
|  | 3578 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise | 
|  | 3579 | device |= device2; | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 3580 |  | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 3581 | // If hdmi system audio mode is on, remove speaker out of output list. | 
|  | 3582 | if ((strategy == STRATEGY_MEDIA) && | 
|  | 3583 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == | 
|  | 3584 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { | 
|  | 3585 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3586 | } | 
|  | 3587 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3588 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3589 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3590 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 3591 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); | 
|  | 3592 | } | 
|  | 3593 | } break; | 
|  | 3594 |  | 
|  | 3595 | default: | 
|  | 3596 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); | 
|  | 3597 | break; | 
|  | 3598 | } | 
|  | 3599 |  | 
|  | 3600 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); | 
|  | 3601 | return device; | 
|  | 3602 | } | 
|  | 3603 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3604 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3605 | { | 
|  | 3606 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 3607 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 3608 | } | 
|  | 3609 | mPreviousOutputs = mOutputs; | 
|  | 3610 | } | 
|  | 3611 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3612 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3613 | audio_devices_t prevDevice, | 
|  | 3614 | uint32_t delayMs) | 
|  | 3615 | { | 
|  | 3616 | // mute/unmute strategies using an incompatible device combination | 
|  | 3617 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 3618 | // if unmuting, unmute only after the specified delay | 
|  | 3619 | if (outputDesc->isDuplicated()) { | 
|  | 3620 | return 0; | 
|  | 3621 | } | 
|  | 3622 |  | 
|  | 3623 | uint32_t muteWaitMs = 0; | 
|  | 3624 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3625 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3626 |  | 
|  | 3627 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 3628 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 3629 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 3630 | bool doMute = false; | 
|  | 3631 |  | 
|  | 3632 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 3633 | doMute = true; | 
|  | 3634 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 3635 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 3636 | doMute = true; | 
|  | 3637 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 3638 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 3639 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3640 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3641 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3642 | // skip output if it does not share any device with current output | 
|  | 3643 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 3644 | == AUDIO_DEVICE_NONE) { | 
|  | 3645 | continue; | 
|  | 3646 | } | 
|  | 3647 | audio_io_handle_t curOutput = mOutputs.keyAt(j); | 
|  | 3648 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", | 
|  | 3649 | mute ? "muting" : "unmuting", i, curDevice, curOutput); | 
|  | 3650 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); | 
|  | 3651 | if (desc->isStrategyActive((routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 3652 | if (mute) { | 
|  | 3653 | // FIXME: should not need to double latency if volume could be applied | 
|  | 3654 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 3655 | // between now and the next time the audioflinger thread for this output | 
|  | 3656 | // will process a buffer (which corresponds to one buffer size, | 
|  | 3657 | // usually 1/2 or 1/4 of the latency). | 
|  | 3658 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 3659 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3660 | } | 
|  | 3661 | } | 
|  | 3662 | } | 
|  | 3663 | } | 
|  | 3664 | } | 
|  | 3665 | } | 
|  | 3666 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 3667 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 3668 | // different per device volumes | 
|  | 3669 | if (outputDesc->isActive() && (device != prevDevice)) { | 
|  | 3670 | if (muteWaitMs < outputDesc->latency() * 2) { | 
|  | 3671 | muteWaitMs = outputDesc->latency() * 2; | 
|  | 3672 | } | 
|  | 3673 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 3674 | if (outputDesc->isStrategyActive((routing_strategy)i)) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3675 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 3676 | // do tempMute unmute after twice the mute wait time | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3677 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 3678 | muteWaitMs *2, device); | 
|  | 3679 | } | 
|  | 3680 | } | 
|  | 3681 | } | 
|  | 3682 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3683 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 3684 | if (muteWaitMs > delayMs) { | 
|  | 3685 | muteWaitMs -= delayMs; | 
|  | 3686 | usleep(muteWaitMs * 1000); | 
|  | 3687 | return muteWaitMs; | 
|  | 3688 | } | 
|  | 3689 | return 0; | 
|  | 3690 | } | 
|  | 3691 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3692 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3693 | audio_devices_t device, | 
|  | 3694 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3695 | int delayMs, | 
|  | 3696 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3697 | { | 
|  | 3698 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3699 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3700 | AudioParameter param; | 
|  | 3701 | uint32_t muteWaitMs; | 
|  | 3702 |  | 
|  | 3703 | if (outputDesc->isDuplicated()) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3704 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); | 
|  | 3705 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3706 | return muteWaitMs; | 
|  | 3707 | } | 
|  | 3708 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 3709 | // output profile | 
|  | 3710 | if ((device != AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3711 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3712 | return 0; | 
|  | 3713 | } | 
|  | 3714 |  | 
|  | 3715 | // filter devices according to output selected | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3716 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3717 |  | 
|  | 3718 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 3719 |  | 
|  | 3720 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); | 
|  | 3721 |  | 
|  | 3722 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 3723 | outputDesc->mDevice = device; | 
|  | 3724 | } | 
|  | 3725 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 3726 |  | 
|  | 3727 | // Do not change the routing if: | 
|  | 3728 | //  - the requested device is AUDIO_DEVICE_NONE | 
|  | 3729 | //  - the requested device is the same as current device and force is not specified. | 
|  | 3730 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
|  | 3731 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) { | 
|  | 3732 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output); | 
|  | 3733 | return muteWaitMs; | 
|  | 3734 | } | 
|  | 3735 |  | 
|  | 3736 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3737 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3738 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3739 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3740 | resetOutputDevice(output, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3741 | } else { | 
|  | 3742 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); | 
|  | 3743 | if (!deviceList.isEmpty()) { | 
|  | 3744 | struct audio_patch patch; | 
|  | 3745 | outputDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 3746 | patch.num_sources = 1; | 
|  | 3747 | patch.num_sinks = 0; | 
|  | 3748 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
|  | 3749 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3750 | patch.num_sinks++; | 
|  | 3751 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3752 | ssize_t index; | 
|  | 3753 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 3754 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 3755 | } else { | 
|  | 3756 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 3757 | } | 
|  | 3758 | sp< AudioPatch> patchDesc; | 
|  | 3759 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 3760 | if (index >= 0) { | 
|  | 3761 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 3762 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 3763 | } | 
|  | 3764 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3765 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3766 | &afPatchHandle, | 
|  | 3767 | delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3768 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
|  | 3769 | "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3770 | status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3771 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3772 | if (index < 0) { | 
|  | 3773 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 3774 | &patch, mUidCached); | 
|  | 3775 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 3776 | } else { | 
|  | 3777 | patchDesc->mPatch = patch; | 
|  | 3778 | } | 
|  | 3779 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3780 | patchDesc->mUid = mUidCached; | 
|  | 3781 | if (patchHandle) { | 
|  | 3782 | *patchHandle = patchDesc->mHandle; | 
|  | 3783 | } | 
|  | 3784 | outputDesc->mPatchHandle = patchDesc->mHandle; | 
|  | 3785 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3786 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3787 | } | 
|  | 3788 | } | 
|  | 3789 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3790 |  | 
|  | 3791 | // update stream volumes according to new device | 
|  | 3792 | applyStreamVolumes(output, device, delayMs); | 
|  | 3793 |  | 
|  | 3794 | return muteWaitMs; | 
|  | 3795 | } | 
|  | 3796 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3797 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3798 | int delayMs, | 
|  | 3799 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3800 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3801 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3802 | ssize_t index; | 
|  | 3803 | if (patchHandle) { | 
|  | 3804 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 3805 | } else { | 
|  | 3806 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 3807 | } | 
|  | 3808 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3809 | return INVALID_OPERATION; | 
|  | 3810 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3811 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3812 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3813 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
|  | 3814 | outputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3815 | removeAudioPatch(patchDesc->mHandle); | 
|  | 3816 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3817 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3818 | return status; | 
|  | 3819 | } | 
|  | 3820 |  | 
|  | 3821 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 3822 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3823 | bool force, | 
|  | 3824 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3825 | { | 
|  | 3826 | status_t status = NO_ERROR; | 
|  | 3827 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3828 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3829 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 3830 | inputDesc->mDevice = device; | 
|  | 3831 |  | 
|  | 3832 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 3833 | if (!deviceList.isEmpty()) { | 
|  | 3834 | struct audio_patch patch; | 
|  | 3835 | inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame^] | 3836 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 3837 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
|  | 3838 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD) { | 
|  | 3839 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 3840 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3841 | patch.num_sinks = 1; | 
|  | 3842 | //only one input device for now | 
|  | 3843 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3844 | patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3845 | ssize_t index; | 
|  | 3846 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 3847 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 3848 | } else { | 
|  | 3849 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 3850 | } | 
|  | 3851 | sp< AudioPatch> patchDesc; | 
|  | 3852 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 3853 | if (index >= 0) { | 
|  | 3854 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 3855 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 3856 | } | 
|  | 3857 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3858 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3859 | &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3860 | 0); | 
|  | 3861 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3862 | status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3863 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3864 | if (index < 0) { | 
|  | 3865 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 3866 | &patch, mUidCached); | 
|  | 3867 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 3868 | } else { | 
|  | 3869 | patchDesc->mPatch = patch; | 
|  | 3870 | } | 
|  | 3871 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3872 | patchDesc->mUid = mUidCached; | 
|  | 3873 | if (patchHandle) { | 
|  | 3874 | *patchHandle = patchDesc->mHandle; | 
|  | 3875 | } | 
|  | 3876 | inputDesc->mPatchHandle = patchDesc->mHandle; | 
|  | 3877 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3878 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3879 | } | 
|  | 3880 | } | 
|  | 3881 | } | 
|  | 3882 | return status; | 
|  | 3883 | } | 
|  | 3884 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3885 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 3886 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3887 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3888 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3889 | ssize_t index; | 
|  | 3890 | if (patchHandle) { | 
|  | 3891 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 3892 | } else { | 
|  | 3893 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 3894 | } | 
|  | 3895 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3896 | return INVALID_OPERATION; | 
|  | 3897 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3898 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3899 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3900 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
|  | 3901 | inputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3902 | removeAudioPatch(patchDesc->mHandle); | 
|  | 3903 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3904 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3905 | return status; | 
|  | 3906 | } | 
|  | 3907 |  | 
|  | 3908 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3909 | uint32_t samplingRate, | 
|  | 3910 | audio_format_t format, | 
|  | 3911 | audio_channel_mask_t channelMask) | 
|  | 3912 | { | 
|  | 3913 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 3914 | // profile supporting all requested parameters. | 
|  | 3915 |  | 
|  | 3916 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3917 | { | 
|  | 3918 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3919 | continue; | 
|  | 3920 | } | 
|  | 3921 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 3922 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3923 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3924 | // profile->log(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3925 | if (profile->isCompatibleProfile(device, samplingRate, format, | 
|  | 3926 | channelMask, AUDIO_OUTPUT_FLAG_NONE)) { | 
|  | 3927 | return profile; | 
|  | 3928 | } | 
|  | 3929 | } | 
|  | 3930 | } | 
|  | 3931 | return NULL; | 
|  | 3932 | } | 
|  | 3933 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3934 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3935 | { | 
|  | 3936 | uint32_t device = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3937 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & | 
|  | 3938 | ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3939 | switch (inputSource) { | 
|  | 3940 | case AUDIO_SOURCE_VOICE_UPLINK: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3941 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3942 | device = AUDIO_DEVICE_IN_VOICE_CALL; | 
|  | 3943 | break; | 
|  | 3944 | } | 
|  | 3945 | // FALL THROUGH | 
|  | 3946 |  | 
|  | 3947 | case AUDIO_SOURCE_DEFAULT: | 
|  | 3948 | case AUDIO_SOURCE_MIC: | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 3949 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { | 
|  | 3950 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; | 
|  | 3951 | break; | 
|  | 3952 | } | 
|  | 3953 | // FALL THROUGH | 
|  | 3954 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3955 | case AUDIO_SOURCE_VOICE_RECOGNITION: | 
|  | 3956 | case AUDIO_SOURCE_HOTWORD: | 
|  | 3957 | case AUDIO_SOURCE_VOICE_COMMUNICATION: | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3958 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3959 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3960 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3961 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3962 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3963 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { | 
|  | 3964 | device = AUDIO_DEVICE_IN_USB_DEVICE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3965 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3966 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 3967 | } | 
|  | 3968 | break; | 
|  | 3969 | case AUDIO_SOURCE_CAMCORDER: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3970 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3971 | device = AUDIO_DEVICE_IN_BACK_MIC; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3972 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3973 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 3974 | } | 
|  | 3975 | break; | 
|  | 3976 | case AUDIO_SOURCE_VOICE_DOWNLINK: | 
|  | 3977 | case AUDIO_SOURCE_VOICE_CALL: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3978 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3979 | device = AUDIO_DEVICE_IN_VOICE_CALL; | 
|  | 3980 | } | 
|  | 3981 | break; | 
|  | 3982 | case AUDIO_SOURCE_REMOTE_SUBMIX: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3983 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3984 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 3985 | } | 
|  | 3986 | break; | 
|  | 3987 | default: | 
|  | 3988 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); | 
|  | 3989 | break; | 
|  | 3990 | } | 
|  | 3991 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); | 
|  | 3992 | return device; | 
|  | 3993 | } | 
|  | 3994 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3995 | bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3996 | { | 
|  | 3997 | if ((device & AUDIO_DEVICE_BIT_IN) != 0) { | 
|  | 3998 | device &= ~AUDIO_DEVICE_BIT_IN; | 
|  | 3999 | if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) | 
|  | 4000 | return true; | 
|  | 4001 | } | 
|  | 4002 | return false; | 
|  | 4003 | } | 
|  | 4004 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4005 | audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | { | 
|  | 4007 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4008 | const sp<AudioInputDescriptor>  input_descriptor = mInputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4009 | if ((input_descriptor->mRefCount > 0) | 
|  | 4010 | && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { | 
|  | 4011 | return mInputs.keyAt(i); | 
|  | 4012 | } | 
|  | 4013 | } | 
|  | 4014 | return 0; | 
|  | 4015 | } | 
|  | 4016 |  | 
|  | 4017 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4018 | audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4019 | { | 
|  | 4020 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4021 | // this happens when forcing a route update and no track is active on an output. | 
|  | 4022 | // In this case the returned category is not important. | 
|  | 4023 | device =  AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4024 | } else if (popcount(device) > 1) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4025 | // Multiple device selection is either: | 
|  | 4026 | //  - speaker + one other device: give priority to speaker in this case. | 
|  | 4027 | //  - one A2DP device + another device: happens with duplicated output. In this case | 
|  | 4028 | // retain the device on the A2DP output as the other must not correspond to an active | 
|  | 4029 | // selection if not the speaker. | 
|  | 4030 | if (device & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 4031 | device = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4032 | } else { | 
|  | 4033 | device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); | 
|  | 4034 | } | 
|  | 4035 | } | 
|  | 4036 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4037 | ALOGW_IF(popcount(device) != 1, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4038 | "getDeviceForVolume() invalid device combination: %08x", | 
|  | 4039 | device); | 
|  | 4040 |  | 
|  | 4041 | return device; | 
|  | 4042 | } | 
|  | 4043 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4044 | AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4045 | { | 
|  | 4046 | switch(getDeviceForVolume(device)) { | 
|  | 4047 | case AUDIO_DEVICE_OUT_EARPIECE: | 
|  | 4048 | return DEVICE_CATEGORY_EARPIECE; | 
|  | 4049 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: | 
|  | 4050 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: | 
|  | 4051 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: | 
|  | 4052 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: | 
|  | 4053 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: | 
|  | 4054 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: | 
|  | 4055 | return DEVICE_CATEGORY_HEADSET; | 
|  | 4056 | case AUDIO_DEVICE_OUT_SPEAKER: | 
|  | 4057 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: | 
|  | 4058 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: | 
|  | 4059 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: | 
|  | 4060 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: | 
|  | 4061 | case AUDIO_DEVICE_OUT_USB_DEVICE: | 
|  | 4062 | case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: | 
|  | 4063 | default: | 
|  | 4064 | return DEVICE_CATEGORY_SPEAKER; | 
|  | 4065 | } | 
|  | 4066 | } | 
|  | 4067 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4068 | float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4069 | int indexInUi) | 
|  | 4070 | { | 
|  | 4071 | device_category deviceCategory = getDeviceCategory(device); | 
|  | 4072 | const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory]; | 
|  | 4073 |  | 
|  | 4074 | // the volume index in the UI is relative to the min and max volume indices for this stream type | 
|  | 4075 | int nbSteps = 1 + curve[VOLMAX].mIndex - | 
|  | 4076 | curve[VOLMIN].mIndex; | 
|  | 4077 | int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) / | 
|  | 4078 | (streamDesc.mIndexMax - streamDesc.mIndexMin); | 
|  | 4079 |  | 
|  | 4080 | // find what part of the curve this index volume belongs to, or if it's out of bounds | 
|  | 4081 | int segment = 0; | 
|  | 4082 | if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds | 
|  | 4083 | return 0.0f; | 
|  | 4084 | } else if (volIdx < curve[VOLKNEE1].mIndex) { | 
|  | 4085 | segment = 0; | 
|  | 4086 | } else if (volIdx < curve[VOLKNEE2].mIndex) { | 
|  | 4087 | segment = 1; | 
|  | 4088 | } else if (volIdx <= curve[VOLMAX].mIndex) { | 
|  | 4089 | segment = 2; | 
|  | 4090 | } else {                                                               // out of bounds | 
|  | 4091 | return 1.0f; | 
|  | 4092 | } | 
|  | 4093 |  | 
|  | 4094 | // linear interpolation in the attenuation table in dB | 
|  | 4095 | float decibels = curve[segment].mDBAttenuation + | 
|  | 4096 | ((float)(volIdx - curve[segment].mIndex)) * | 
|  | 4097 | ( (curve[segment+1].mDBAttenuation - | 
|  | 4098 | curve[segment].mDBAttenuation) / | 
|  | 4099 | ((float)(curve[segment+1].mIndex - | 
|  | 4100 | curve[segment].mIndex)) ); | 
|  | 4101 |  | 
|  | 4102 | float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) | 
|  | 4103 |  | 
|  | 4104 | ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f", | 
|  | 4105 | curve[segment].mIndex, volIdx, | 
|  | 4106 | curve[segment+1].mIndex, | 
|  | 4107 | curve[segment].mDBAttenuation, | 
|  | 4108 | decibels, | 
|  | 4109 | curve[segment+1].mDBAttenuation, | 
|  | 4110 | amplification); | 
|  | 4111 |  | 
|  | 4112 | return amplification; | 
|  | 4113 | } | 
|  | 4114 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4115 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4116 | AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 | {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f} | 
|  | 4118 | }; | 
|  | 4119 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4120 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4121 | AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4122 | {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f} | 
|  | 4123 | }; | 
|  | 4124 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4125 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4126 | AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4127 | {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f} | 
|  | 4128 | }; | 
|  | 4129 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4130 | const AudioPolicyManager::VolumeCurvePoint | 
| Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 4131 | AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Jean-Michel Trivi | 98c6043 | 2014-07-09 08:51:34 -0700 | [diff] [blame] | 4132 | {1, -55.0f}, {20, -43.0f}, {86, -12.0f}, {100, 0.0f} | 
| Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 4133 | }; | 
|  | 4134 |  | 
|  | 4135 | const AudioPolicyManager::VolumeCurvePoint | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4136 | AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4137 | {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f} | 
|  | 4138 | }; | 
|  | 4139 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4140 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4141 | AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4142 | {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f} | 
|  | 4143 | }; | 
|  | 4144 |  | 
|  | 4145 | // AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks | 
|  | 4146 | // AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets. | 
|  | 4147 | // AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java). | 
|  | 4148 | // The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset. | 
|  | 4149 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4150 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4151 | AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 | {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f} | 
|  | 4153 | }; | 
|  | 4154 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4155 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4156 | AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4157 | {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f} | 
|  | 4158 | }; | 
|  | 4159 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4160 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4161 | AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f} | 
|  | 4163 | }; | 
|  | 4164 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4165 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4166 | AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f} | 
|  | 4168 | }; | 
|  | 4169 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4170 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4171 | AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4172 | {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f} | 
|  | 4173 | }; | 
|  | 4174 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4175 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4176 | *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT] | 
|  | 4177 | [AudioPolicyManager::DEVICE_CATEGORY_CNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 | { // AUDIO_STREAM_VOICE_CALL | 
|  | 4179 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4180 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4181 | sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4182 | }, | 
|  | 4183 | { // AUDIO_STREAM_SYSTEM | 
|  | 4184 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4185 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4186 | sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4187 | }, | 
|  | 4188 | { // AUDIO_STREAM_RING | 
|  | 4189 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4190 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4191 | sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4192 | }, | 
|  | 4193 | { // AUDIO_STREAM_MUSIC | 
|  | 4194 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4195 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4196 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4197 | }, | 
|  | 4198 | { // AUDIO_STREAM_ALARM | 
|  | 4199 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4200 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4201 | sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4202 | }, | 
|  | 4203 | { // AUDIO_STREAM_NOTIFICATION | 
|  | 4204 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4205 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4206 | sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4207 | }, | 
|  | 4208 | { // AUDIO_STREAM_BLUETOOTH_SCO | 
|  | 4209 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4210 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4211 | sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4212 | }, | 
|  | 4213 | { // AUDIO_STREAM_ENFORCED_AUDIBLE | 
|  | 4214 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4215 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4216 | sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4217 | }, | 
|  | 4218 | {  // AUDIO_STREAM_DTMF | 
|  | 4219 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4220 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4221 | sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4222 | }, | 
|  | 4223 | { // AUDIO_STREAM_TTS | 
|  | 4224 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4225 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
|  | 4226 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE | 
|  | 4227 | }, | 
|  | 4228 | }; | 
|  | 4229 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4230 | void AudioPolicyManager::initializeVolumeCurves() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | { | 
|  | 4232 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
|  | 4233 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 4234 | mStreams[i].mVolumeCurve[j] = | 
|  | 4235 | sVolumeProfiles[i][j]; | 
|  | 4236 | } | 
|  | 4237 | } | 
|  | 4238 |  | 
|  | 4239 | // Check availability of DRC on speaker path: if available, override some of the speaker curves | 
|  | 4240 | if (mSpeakerDrcEnabled) { | 
|  | 4241 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4242 | sDefaultSystemVolumeCurveDrc; | 
|  | 4243 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4244 | sSpeakerSonificationVolumeCurveDrc; | 
|  | 4245 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4246 | sSpeakerSonificationVolumeCurveDrc; | 
|  | 4247 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4248 | sSpeakerSonificationVolumeCurveDrc; | 
| Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 4249 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4250 | sSpeakerMediaVolumeCurveDrc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4251 | } | 
|  | 4252 | } | 
|  | 4253 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4254 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4255 | int index, | 
|  | 4256 | audio_io_handle_t output, | 
|  | 4257 | audio_devices_t device) | 
|  | 4258 | { | 
|  | 4259 | float volume = 1.0; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4260 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4261 | StreamDescriptor &streamDesc = mStreams[stream]; | 
|  | 4262 |  | 
|  | 4263 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4264 | device = outputDesc->device(); | 
|  | 4265 | } | 
|  | 4266 |  | 
|  | 4267 | // if volume is not 0 (not muted), force media volume to max on digital output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4268 | if (stream == AUDIO_STREAM_MUSIC && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4269 | index != mStreams[stream].mIndexMin && | 
|  | 4270 | (device == AUDIO_DEVICE_OUT_AUX_DIGITAL || | 
| Paul McLean | b919dd3 | 2014-06-26 13:16:44 -0700 | [diff] [blame] | 4271 | device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4272 | return 1.0; | 
|  | 4273 | } | 
|  | 4274 |  | 
|  | 4275 | volume = volIndexToAmpl(device, streamDesc, index); | 
|  | 4276 |  | 
|  | 4277 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 4278 | // to avoid sound level bursts in user's ears: | 
|  | 4279 | // - always attenuate ring tones and notifications volume by 6dB | 
|  | 4280 | // - if music is playing, always limit the volume to current music volume, | 
|  | 4281 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4282 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4283 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 4284 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 4285 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
|  | 4286 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && | 
|  | 4287 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 4288 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4289 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4290 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4291 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4292 | streamDesc.mCanBeMuted) { | 
|  | 4293 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; | 
|  | 4294 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 4295 | // by the music application and behave as if music was active if the last music track was | 
|  | 4296 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4297 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 | mLimitRingtoneVolume) { | 
|  | 4299 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4300 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, | 
|  | 4301 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4302 | output, | 
|  | 4303 | musicDevice); | 
|  | 4304 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? | 
|  | 4305 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; | 
|  | 4306 | if (volume > minVol) { | 
|  | 4307 | volume = minVol; | 
|  | 4308 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); | 
|  | 4309 | } | 
|  | 4310 | } | 
|  | 4311 | } | 
|  | 4312 |  | 
|  | 4313 | return volume; | 
|  | 4314 | } | 
|  | 4315 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4316 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4317 | int index, | 
|  | 4318 | audio_io_handle_t output, | 
|  | 4319 | audio_devices_t device, | 
|  | 4320 | int delayMs, | 
|  | 4321 | bool force) | 
|  | 4322 | { | 
|  | 4323 |  | 
|  | 4324 | // do not change actual stream volume if the stream is muted | 
|  | 4325 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { | 
|  | 4326 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
|  | 4327 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); | 
|  | 4328 | return NO_ERROR; | 
|  | 4329 | } | 
|  | 4330 |  | 
|  | 4331 | // do not change in call volume if bluetooth is connected and vice versa | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4332 | if ((stream == AUDIO_STREAM_VOICE_CALL && | 
|  | 4333 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4334 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && | 
|  | 4335 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm", | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4337 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4338 | return INVALID_OPERATION; | 
|  | 4339 | } | 
|  | 4340 |  | 
|  | 4341 | float volume = computeVolume(stream, index, output, device); | 
|  | 4342 | // We actually change the volume if: | 
|  | 4343 | // - the float value returned by computeVolume() changed | 
|  | 4344 | // - the force flag is set | 
|  | 4345 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || | 
|  | 4346 | force) { | 
|  | 4347 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; | 
|  | 4348 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); | 
|  | 4349 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is | 
|  | 4350 | // enabled | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4351 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
|  | 4352 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4353 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4354 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4355 | } | 
|  | 4356 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4357 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 4358 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4359 | float voiceVolume; | 
|  | 4360 | // Force voice volume to max for bluetooth SCO as volume is managed by the headset | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4361 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4362 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; | 
|  | 4363 | } else { | 
|  | 4364 | voiceVolume = 1.0; | 
|  | 4365 | } | 
|  | 4366 |  | 
|  | 4367 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { | 
|  | 4368 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 4369 | mLastVoiceVolume = voiceVolume; | 
|  | 4370 | } | 
|  | 4371 | } | 
|  | 4372 |  | 
|  | 4373 | return NO_ERROR; | 
|  | 4374 | } | 
|  | 4375 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4376 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4377 | audio_devices_t device, | 
|  | 4378 | int delayMs, | 
|  | 4379 | bool force) | 
|  | 4380 | { | 
|  | 4381 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); | 
|  | 4382 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4383 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 4384 | checkAndSetVolume((audio_stream_type_t)stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | mStreams[stream].getVolumeIndex(device), | 
|  | 4386 | output, | 
|  | 4387 | device, | 
|  | 4388 | delayMs, | 
|  | 4389 | force); | 
|  | 4390 | } | 
|  | 4391 | } | 
|  | 4392 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4393 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4394 | bool on, | 
|  | 4395 | audio_io_handle_t output, | 
|  | 4396 | int delayMs, | 
|  | 4397 | audio_devices_t device) | 
|  | 4398 | { | 
|  | 4399 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4400 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 4401 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 4402 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4403 | } | 
|  | 4404 | } | 
|  | 4405 | } | 
|  | 4406 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4407 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | bool on, | 
|  | 4409 | audio_io_handle_t output, | 
|  | 4410 | int delayMs, | 
|  | 4411 | audio_devices_t device) | 
|  | 4412 | { | 
|  | 4413 | StreamDescriptor &streamDesc = mStreams[stream]; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4414 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4415 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4416 | device = outputDesc->device(); | 
|  | 4417 | } | 
|  | 4418 |  | 
|  | 4419 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", | 
|  | 4420 | stream, on, output, outputDesc->mMuteCount[stream], device); | 
|  | 4421 |  | 
|  | 4422 | if (on) { | 
|  | 4423 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 4424 | if (streamDesc.mCanBeMuted && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4425 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
|  | 4426 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4427 | checkAndSetVolume(stream, 0, output, device, delayMs); | 
|  | 4428 | } | 
|  | 4429 | } | 
|  | 4430 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 4431 | outputDesc->mMuteCount[stream]++; | 
|  | 4432 | } else { | 
|  | 4433 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 4434 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 4435 | return; | 
|  | 4436 | } | 
|  | 4437 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 4438 | checkAndSetVolume(stream, | 
|  | 4439 | streamDesc.getVolumeIndex(device), | 
|  | 4440 | output, | 
|  | 4441 | device, | 
|  | 4442 | delayMs); | 
|  | 4443 | } | 
|  | 4444 | } | 
|  | 4445 | } | 
|  | 4446 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4447 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4448 | bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4449 | { | 
|  | 4450 | // if the stream pertains to sonification strategy and we are in call we must | 
|  | 4451 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
|  | 4452 | // in the device used for phone strategy and play the tone if the selected device does not | 
|  | 4453 | // interfere with the device used for phone strategy | 
|  | 4454 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
|  | 4455 | // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4456 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4457 | if ((stream_strategy == STRATEGY_SONIFICATION) || | 
|  | 4458 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4459 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4460 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
|  | 4461 | stream, starting, outputDesc->mDevice, stateChange); | 
|  | 4462 | if (outputDesc->mRefCount[stream]) { | 
|  | 4463 | int muteCount = 1; | 
|  | 4464 | if (stateChange) { | 
|  | 4465 | muteCount = outputDesc->mRefCount[stream]; | 
|  | 4466 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4467 | if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4468 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
|  | 4469 | for (int i = 0; i < muteCount; i++) { | 
|  | 4470 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 4471 | } | 
|  | 4472 | } else { | 
|  | 4473 | ALOGV("handleIncallSonification() high visibility"); | 
|  | 4474 | if (outputDesc->device() & | 
|  | 4475 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
|  | 4476 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
|  | 4477 | for (int i = 0; i < muteCount; i++) { | 
|  | 4478 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 4479 | } | 
|  | 4480 | } | 
|  | 4481 | if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4482 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
|  | 4483 | AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4484 | } else { | 
|  | 4485 | mpClientInterface->stopTone(); | 
|  | 4486 | } | 
|  | 4487 | } | 
|  | 4488 | } | 
|  | 4489 | } | 
|  | 4490 | } | 
|  | 4491 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4492 | bool AudioPolicyManager::isInCall() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4493 | { | 
|  | 4494 | return isStateInCall(mPhoneState); | 
|  | 4495 | } | 
|  | 4496 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4497 | bool AudioPolicyManager::isStateInCall(int state) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4498 | return ((state == AUDIO_MODE_IN_CALL) || | 
|  | 4499 | (state == AUDIO_MODE_IN_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4500 | } | 
|  | 4501 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4502 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4503 | { | 
|  | 4504 | return MAX_EFFECTS_CPU_LOAD; | 
|  | 4505 | } | 
|  | 4506 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4507 | uint32_t AudioPolicyManager::getMaxEffectsMemory() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4508 | { | 
|  | 4509 | return MAX_EFFECTS_MEMORY; | 
|  | 4510 | } | 
|  | 4511 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4512 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4513 | // --- AudioOutputDescriptor class implementation | 
|  | 4514 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4515 | AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor( | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4516 | const sp<IOProfile>& profile) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4517 | : mId(0), mIoHandle(0), mLatency(0), | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4518 | mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4519 | mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0) | 
|  | 4520 | { | 
|  | 4521 | // clear usage count for all stream types | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4522 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4523 | mRefCount[i] = 0; | 
|  | 4524 | mCurVolume[i] = -1.0; | 
|  | 4525 | mMuteCount[i] = 0; | 
|  | 4526 | mStopTime[i] = 0; | 
|  | 4527 | } | 
|  | 4528 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4529 | mStrategyMutedByDevice[i] = false; | 
|  | 4530 | } | 
|  | 4531 | if (profile != NULL) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4532 | mAudioPort = profile; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 4533 | mSamplingRate = profile->pickSamplingRate(); | 
|  | 4534 | mFormat = profile->pickFormat(); | 
|  | 4535 | mChannelMask = profile->pickChannelMask(); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4536 | if (profile->mGains.size() > 0) { | 
|  | 4537 | profile->mGains[0]->getDefaultConfig(&mGain); | 
|  | 4538 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | mFlags = profile->mFlags; | 
|  | 4540 | } | 
|  | 4541 | } | 
|  | 4542 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4543 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4544 | { | 
|  | 4545 | if (isDuplicated()) { | 
|  | 4546 | return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); | 
|  | 4547 | } else { | 
|  | 4548 | return mDevice; | 
|  | 4549 | } | 
|  | 4550 | } | 
|  | 4551 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4552 | uint32_t AudioPolicyManager::AudioOutputDescriptor::latency() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4553 | { | 
|  | 4554 | if (isDuplicated()) { | 
|  | 4555 | return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; | 
|  | 4556 | } else { | 
|  | 4557 | return mLatency; | 
|  | 4558 | } | 
|  | 4559 | } | 
|  | 4560 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4561 | bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith( | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4562 | const sp<AudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4563 | { | 
|  | 4564 | if (isDuplicated()) { | 
|  | 4565 | return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); | 
|  | 4566 | } else if (outputDesc->isDuplicated()){ | 
|  | 4567 | return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2); | 
|  | 4568 | } else { | 
|  | 4569 | return (mProfile->mModule == outputDesc->mProfile->mModule); | 
|  | 4570 | } | 
|  | 4571 | } | 
|  | 4572 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4573 | void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4574 | int delta) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4575 | { | 
|  | 4576 | // forward usage count change to attached outputs | 
|  | 4577 | if (isDuplicated()) { | 
|  | 4578 | mOutput1->changeRefCount(stream, delta); | 
|  | 4579 | mOutput2->changeRefCount(stream, delta); | 
|  | 4580 | } | 
|  | 4581 | if ((delta + (int)mRefCount[stream]) < 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4582 | ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", | 
|  | 4583 | delta, stream, mRefCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4584 | mRefCount[stream] = 0; | 
|  | 4585 | return; | 
|  | 4586 | } | 
|  | 4587 | mRefCount[stream] += delta; | 
|  | 4588 | ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); | 
|  | 4589 | } | 
|  | 4590 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4591 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4592 | { | 
|  | 4593 | if (isDuplicated()) { | 
|  | 4594 | return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); | 
|  | 4595 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4596 | return mProfile->mSupportedDevices.types() ; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4597 | } | 
|  | 4598 | } | 
|  | 4599 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4600 | bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | { | 
|  | 4602 | return isStrategyActive(NUM_STRATEGIES, inPastMs); | 
|  | 4603 | } | 
|  | 4604 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4605 | bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4606 | uint32_t inPastMs, | 
|  | 4607 | nsecs_t sysTime) const | 
|  | 4608 | { | 
|  | 4609 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 4610 | sysTime = systemTime(); | 
|  | 4611 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4612 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { | 
|  | 4613 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4614 | (NUM_STRATEGIES == strategy)) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4615 | isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4616 | return true; | 
|  | 4617 | } | 
|  | 4618 | } | 
|  | 4619 | return false; | 
|  | 4620 | } | 
|  | 4621 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4622 | bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4623 | uint32_t inPastMs, | 
|  | 4624 | nsecs_t sysTime) const | 
|  | 4625 | { | 
|  | 4626 | if (mRefCount[stream] != 0) { | 
|  | 4627 | return true; | 
|  | 4628 | } | 
|  | 4629 | if (inPastMs == 0) { | 
|  | 4630 | return false; | 
|  | 4631 | } | 
|  | 4632 | if (sysTime == 0) { | 
|  | 4633 | sysTime = systemTime(); | 
|  | 4634 | } | 
|  | 4635 | if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { | 
|  | 4636 | return true; | 
|  | 4637 | } | 
|  | 4638 | return false; | 
|  | 4639 | } | 
|  | 4640 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4641 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4642 | struct audio_port_config *dstConfig, | 
|  | 4643 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4644 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4645 | ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle); | 
|  | 4646 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4647 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 4648 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; | 
|  | 4649 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4650 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4651 | } | 
|  | 4652 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 4653 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4654 | dstConfig->id = mId; | 
|  | 4655 | dstConfig->role = AUDIO_PORT_ROLE_SOURCE; | 
|  | 4656 | dstConfig->type = AUDIO_PORT_TYPE_MIX; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4657 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; | 
|  | 4658 | dstConfig->ext.mix.handle = mIoHandle; | 
|  | 4659 | dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4660 | } | 
|  | 4661 |  | 
|  | 4662 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPort( | 
|  | 4663 | struct audio_port *port) const | 
|  | 4664 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4665 | ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4666 | mProfile->toAudioPort(port); | 
|  | 4667 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4668 | toAudioPortConfig(&port->active_config); | 
|  | 4669 | port->ext.mix.hw_module = mProfile->mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4670 | port->ext.mix.handle = mIoHandle; | 
|  | 4671 | port->ext.mix.latency_class = | 
|  | 4672 | mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL; | 
|  | 4673 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4674 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4675 | status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4676 | { | 
|  | 4677 | const size_t SIZE = 256; | 
|  | 4678 | char buffer[SIZE]; | 
|  | 4679 | String8 result; | 
|  | 4680 |  | 
|  | 4681 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); | 
|  | 4682 | result.append(buffer); | 
|  | 4683 | snprintf(buffer, SIZE, " Format: %08x\n", mFormat); | 
|  | 4684 | result.append(buffer); | 
|  | 4685 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); | 
|  | 4686 | result.append(buffer); | 
|  | 4687 | snprintf(buffer, SIZE, " Latency: %d\n", mLatency); | 
|  | 4688 | result.append(buffer); | 
|  | 4689 | snprintf(buffer, SIZE, " Flags %08x\n", mFlags); | 
|  | 4690 | result.append(buffer); | 
|  | 4691 | snprintf(buffer, SIZE, " Devices %08x\n", device()); | 
|  | 4692 | result.append(buffer); | 
|  | 4693 | snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); | 
|  | 4694 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4695 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { | 
|  | 4696 | snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", | 
|  | 4697 | i, mCurVolume[i], mRefCount[i], mMuteCount[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4698 | result.append(buffer); | 
|  | 4699 | } | 
|  | 4700 | write(fd, result.string(), result.size()); | 
|  | 4701 |  | 
|  | 4702 | return NO_ERROR; | 
|  | 4703 | } | 
|  | 4704 |  | 
|  | 4705 | // --- AudioInputDescriptor class implementation | 
|  | 4706 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4707 | AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4708 | : mId(0), mIoHandle(0), | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4709 | mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0), | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4710 | mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4711 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4712 | if (profile != NULL) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4713 | mAudioPort = profile; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 4714 | mSamplingRate = profile->pickSamplingRate(); | 
|  | 4715 | mFormat = profile->pickFormat(); | 
|  | 4716 | mChannelMask = profile->pickChannelMask(); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4717 | if (profile->mGains.size() > 0) { | 
|  | 4718 | profile->mGains[0]->getDefaultConfig(&mGain); | 
|  | 4719 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4720 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4721 | } | 
|  | 4722 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4723 | void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4724 | struct audio_port_config *dstConfig, | 
|  | 4725 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4726 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4727 | ALOG_ASSERT(mProfile != 0, | 
|  | 4728 | "toAudioPortConfig() called on input with null profile %d", mIoHandle); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4729 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 4730 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; | 
|  | 4731 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4732 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4733 | } | 
|  | 4734 |  | 
|  | 4735 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 4736 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4737 | dstConfig->id = mId; | 
|  | 4738 | dstConfig->role = AUDIO_PORT_ROLE_SINK; | 
|  | 4739 | dstConfig->type = AUDIO_PORT_TYPE_MIX; | 
| Eric Laurent | 62aaabb | 2014-06-02 10:40:54 -0700 | [diff] [blame] | 4740 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; | 
|  | 4741 | dstConfig->ext.mix.handle = mIoHandle; | 
|  | 4742 | dstConfig->ext.mix.usecase.source = mInputSource; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4743 | } | 
|  | 4744 |  | 
|  | 4745 | void AudioPolicyManager::AudioInputDescriptor::toAudioPort( | 
|  | 4746 | struct audio_port *port) const | 
|  | 4747 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4748 | ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle); | 
|  | 4749 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4750 | mProfile->toAudioPort(port); | 
|  | 4751 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4752 | toAudioPortConfig(&port->active_config); | 
|  | 4753 | port->ext.mix.hw_module = mProfile->mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4754 | port->ext.mix.handle = mIoHandle; | 
|  | 4755 | port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL; | 
|  | 4756 | } | 
|  | 4757 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4758 | status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4759 | { | 
|  | 4760 | const size_t SIZE = 256; | 
|  | 4761 | char buffer[SIZE]; | 
|  | 4762 | String8 result; | 
|  | 4763 |  | 
|  | 4764 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); | 
|  | 4765 | result.append(buffer); | 
|  | 4766 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); | 
|  | 4767 | result.append(buffer); | 
|  | 4768 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); | 
|  | 4769 | result.append(buffer); | 
|  | 4770 | snprintf(buffer, SIZE, " Devices %08x\n", mDevice); | 
|  | 4771 | result.append(buffer); | 
|  | 4772 | snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); | 
|  | 4773 | result.append(buffer); | 
|  | 4774 | write(fd, result.string(), result.size()); | 
|  | 4775 |  | 
|  | 4776 | return NO_ERROR; | 
|  | 4777 | } | 
|  | 4778 |  | 
|  | 4779 | // --- StreamDescriptor class implementation | 
|  | 4780 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4781 | AudioPolicyManager::StreamDescriptor::StreamDescriptor() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4782 | :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true) | 
|  | 4783 | { | 
|  | 4784 | mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0); | 
|  | 4785 | } | 
|  | 4786 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4787 | int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4788 | { | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4789 | device = AudioPolicyManager::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4790 | // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT | 
|  | 4791 | if (mIndexCur.indexOfKey(device) < 0) { | 
|  | 4792 | device = AUDIO_DEVICE_OUT_DEFAULT; | 
|  | 4793 | } | 
|  | 4794 | return mIndexCur.valueFor(device); | 
|  | 4795 | } | 
|  | 4796 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4797 | void AudioPolicyManager::StreamDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4798 | { | 
|  | 4799 | const size_t SIZE = 256; | 
|  | 4800 | char buffer[SIZE]; | 
|  | 4801 | String8 result; | 
|  | 4802 |  | 
|  | 4803 | snprintf(buffer, SIZE, "%s         %02d         %02d         ", | 
|  | 4804 | mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax); | 
|  | 4805 | result.append(buffer); | 
|  | 4806 | for (size_t i = 0; i < mIndexCur.size(); i++) { | 
|  | 4807 | snprintf(buffer, SIZE, "%04x : %02d, ", | 
|  | 4808 | mIndexCur.keyAt(i), | 
|  | 4809 | mIndexCur.valueAt(i)); | 
|  | 4810 | result.append(buffer); | 
|  | 4811 | } | 
|  | 4812 | result.append("\n"); | 
|  | 4813 |  | 
|  | 4814 | write(fd, result.string(), result.size()); | 
|  | 4815 | } | 
|  | 4816 |  | 
|  | 4817 | // --- EffectDescriptor class implementation | 
|  | 4818 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4819 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4820 | { | 
|  | 4821 | const size_t SIZE = 256; | 
|  | 4822 | char buffer[SIZE]; | 
|  | 4823 | String8 result; | 
|  | 4824 |  | 
|  | 4825 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); | 
|  | 4826 | result.append(buffer); | 
|  | 4827 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); | 
|  | 4828 | result.append(buffer); | 
|  | 4829 | snprintf(buffer, SIZE, " Session: %d\n", mSession); | 
|  | 4830 | result.append(buffer); | 
|  | 4831 | snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name); | 
|  | 4832 | result.append(buffer); | 
|  | 4833 | snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled"); | 
|  | 4834 | result.append(buffer); | 
|  | 4835 | write(fd, result.string(), result.size()); | 
|  | 4836 |  | 
|  | 4837 | return NO_ERROR; | 
|  | 4838 | } | 
|  | 4839 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4840 | // --- HwModule class implementation | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4842 | AudioPolicyManager::HwModule::HwModule(const char *name) | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 4843 | : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), | 
|  | 4844 | mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4845 | { | 
|  | 4846 | } | 
|  | 4847 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4848 | AudioPolicyManager::HwModule::~HwModule() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4849 | { | 
|  | 4850 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4851 | mOutputProfiles[i]->mSupportedDevices.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4852 | } | 
|  | 4853 | for (size_t i = 0; i < mInputProfiles.size(); i++) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4854 | mInputProfiles[i]->mSupportedDevices.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4855 | } | 
|  | 4856 | free((void *)mName); | 
|  | 4857 | } | 
|  | 4858 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 4859 | status_t AudioPolicyManager::HwModule::loadInput(cnode *root) | 
|  | 4860 | { | 
|  | 4861 | cnode *node = root->first_child; | 
|  | 4862 |  | 
|  | 4863 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this); | 
|  | 4864 |  | 
|  | 4865 | while (node) { | 
|  | 4866 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { | 
|  | 4867 | profile->loadSamplingRates((char *)node->value); | 
|  | 4868 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { | 
|  | 4869 | profile->loadFormats((char *)node->value); | 
|  | 4870 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 4871 | profile->loadInChannels((char *)node->value); | 
|  | 4872 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { | 
|  | 4873 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, | 
|  | 4874 | mDeclaredDevices); | 
|  | 4875 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 4876 | profile->loadGains(node); | 
|  | 4877 | } | 
|  | 4878 | node = node->next; | 
|  | 4879 | } | 
|  | 4880 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), | 
|  | 4881 | "loadInput() invalid supported devices"); | 
|  | 4882 | ALOGW_IF(profile->mChannelMasks.size() == 0, | 
|  | 4883 | "loadInput() invalid supported channel masks"); | 
|  | 4884 | ALOGW_IF(profile->mSamplingRates.size() == 0, | 
|  | 4885 | "loadInput() invalid supported sampling rates"); | 
|  | 4886 | ALOGW_IF(profile->mFormats.size() == 0, | 
|  | 4887 | "loadInput() invalid supported formats"); | 
|  | 4888 | if (!profile->mSupportedDevices.isEmpty() && | 
|  | 4889 | (profile->mChannelMasks.size() != 0) && | 
|  | 4890 | (profile->mSamplingRates.size() != 0) && | 
|  | 4891 | (profile->mFormats.size() != 0)) { | 
|  | 4892 |  | 
|  | 4893 | ALOGV("loadInput() adding input Supported Devices %04x", | 
|  | 4894 | profile->mSupportedDevices.types()); | 
|  | 4895 |  | 
|  | 4896 | mInputProfiles.add(profile); | 
|  | 4897 | return NO_ERROR; | 
|  | 4898 | } else { | 
|  | 4899 | return BAD_VALUE; | 
|  | 4900 | } | 
|  | 4901 | } | 
|  | 4902 |  | 
|  | 4903 | status_t AudioPolicyManager::HwModule::loadOutput(cnode *root) | 
|  | 4904 | { | 
|  | 4905 | cnode *node = root->first_child; | 
|  | 4906 |  | 
|  | 4907 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this); | 
|  | 4908 |  | 
|  | 4909 | while (node) { | 
|  | 4910 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { | 
|  | 4911 | profile->loadSamplingRates((char *)node->value); | 
|  | 4912 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { | 
|  | 4913 | profile->loadFormats((char *)node->value); | 
|  | 4914 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 4915 | profile->loadOutChannels((char *)node->value); | 
|  | 4916 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { | 
|  | 4917 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, | 
|  | 4918 | mDeclaredDevices); | 
|  | 4919 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { | 
|  | 4920 | profile->mFlags = parseFlagNames((char *)node->value); | 
|  | 4921 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 4922 | profile->loadGains(node); | 
|  | 4923 | } | 
|  | 4924 | node = node->next; | 
|  | 4925 | } | 
|  | 4926 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), | 
|  | 4927 | "loadOutput() invalid supported devices"); | 
|  | 4928 | ALOGW_IF(profile->mChannelMasks.size() == 0, | 
|  | 4929 | "loadOutput() invalid supported channel masks"); | 
|  | 4930 | ALOGW_IF(profile->mSamplingRates.size() == 0, | 
|  | 4931 | "loadOutput() invalid supported sampling rates"); | 
|  | 4932 | ALOGW_IF(profile->mFormats.size() == 0, | 
|  | 4933 | "loadOutput() invalid supported formats"); | 
|  | 4934 | if (!profile->mSupportedDevices.isEmpty() && | 
|  | 4935 | (profile->mChannelMasks.size() != 0) && | 
|  | 4936 | (profile->mSamplingRates.size() != 0) && | 
|  | 4937 | (profile->mFormats.size() != 0)) { | 
|  | 4938 |  | 
|  | 4939 | ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x", | 
|  | 4940 | profile->mSupportedDevices.types(), profile->mFlags); | 
|  | 4941 |  | 
|  | 4942 | mOutputProfiles.add(profile); | 
|  | 4943 | return NO_ERROR; | 
|  | 4944 | } else { | 
|  | 4945 | return BAD_VALUE; | 
|  | 4946 | } | 
|  | 4947 | } | 
|  | 4948 |  | 
|  | 4949 | status_t AudioPolicyManager::HwModule::loadDevice(cnode *root) | 
|  | 4950 | { | 
|  | 4951 | cnode *node = root->first_child; | 
|  | 4952 |  | 
|  | 4953 | audio_devices_t type = AUDIO_DEVICE_NONE; | 
|  | 4954 | while (node) { | 
|  | 4955 | if (strcmp(node->name, DEVICE_TYPE) == 0) { | 
|  | 4956 | type = parseDeviceNames((char *)node->value); | 
|  | 4957 | break; | 
|  | 4958 | } | 
|  | 4959 | node = node->next; | 
|  | 4960 | } | 
|  | 4961 | if (type == AUDIO_DEVICE_NONE || | 
|  | 4962 | (!audio_is_input_device(type) && !audio_is_output_device(type))) { | 
|  | 4963 | ALOGW("loadDevice() bad type %08x", type); | 
|  | 4964 | return BAD_VALUE; | 
|  | 4965 | } | 
|  | 4966 | sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type); | 
|  | 4967 | deviceDesc->mModule = this; | 
|  | 4968 |  | 
|  | 4969 | node = root->first_child; | 
|  | 4970 | while (node) { | 
|  | 4971 | if (strcmp(node->name, DEVICE_ADDRESS) == 0) { | 
|  | 4972 | deviceDesc->mAddress = String8((char *)node->value); | 
|  | 4973 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 4974 | if (audio_is_input_device(type)) { | 
|  | 4975 | deviceDesc->loadInChannels((char *)node->value); | 
|  | 4976 | } else { | 
|  | 4977 | deviceDesc->loadOutChannels((char *)node->value); | 
|  | 4978 | } | 
|  | 4979 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 4980 | deviceDesc->loadGains(node); | 
|  | 4981 | } | 
|  | 4982 | node = node->next; | 
|  | 4983 | } | 
|  | 4984 |  | 
|  | 4985 | ALOGV("loadDevice() adding device name %s type %08x address %s", | 
|  | 4986 | deviceDesc->mName.string(), type, deviceDesc->mAddress.string()); | 
|  | 4987 |  | 
|  | 4988 | mDeclaredDevices.add(deviceDesc); | 
|  | 4989 |  | 
|  | 4990 | return NO_ERROR; | 
|  | 4991 | } | 
|  | 4992 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4993 | void AudioPolicyManager::HwModule::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4994 | { | 
|  | 4995 | const size_t SIZE = 256; | 
|  | 4996 | char buffer[SIZE]; | 
|  | 4997 | String8 result; | 
|  | 4998 |  | 
|  | 4999 | snprintf(buffer, SIZE, "  - name: %s\n", mName); | 
|  | 5000 | result.append(buffer); | 
|  | 5001 | snprintf(buffer, SIZE, "  - handle: %d\n", mHandle); | 
|  | 5002 | result.append(buffer); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5003 | snprintf(buffer, SIZE, "  - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF); | 
|  | 5004 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5005 | write(fd, result.string(), result.size()); | 
|  | 5006 | if (mOutputProfiles.size()) { | 
|  | 5007 | write(fd, "  - outputs:\n", strlen("  - outputs:\n")); | 
|  | 5008 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5009 | snprintf(buffer, SIZE, "    output %zu:\n", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5010 | write(fd, buffer, strlen(buffer)); | 
|  | 5011 | mOutputProfiles[i]->dump(fd); | 
|  | 5012 | } | 
|  | 5013 | } | 
|  | 5014 | if (mInputProfiles.size()) { | 
|  | 5015 | write(fd, "  - inputs:\n", strlen("  - inputs:\n")); | 
|  | 5016 | for (size_t i = 0; i < mInputProfiles.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5017 | snprintf(buffer, SIZE, "    input %zu:\n", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5018 | write(fd, buffer, strlen(buffer)); | 
|  | 5019 | mInputProfiles[i]->dump(fd); | 
|  | 5020 | } | 
|  | 5021 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5022 | if (mDeclaredDevices.size()) { | 
|  | 5023 | write(fd, "  - devices:\n", strlen("  - devices:\n")); | 
|  | 5024 | for (size_t i = 0; i < mDeclaredDevices.size(); i++) { | 
|  | 5025 | mDeclaredDevices[i]->dump(fd, 4, i); | 
|  | 5026 | } | 
|  | 5027 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5028 | } | 
|  | 5029 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5030 | // --- AudioPort class implementation | 
|  | 5031 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5032 |  | 
|  | 5033 | AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type, | 
|  | 5034 | audio_port_role_t role, const sp<HwModule>& module) : | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5035 | mName(name), mType(type), mRole(role), mModule(module), mFlags((audio_output_flags_t)0) | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5036 | { | 
|  | 5037 | mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || | 
|  | 5038 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); | 
|  | 5039 | } | 
|  | 5040 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5041 | void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const | 
|  | 5042 | { | 
|  | 5043 | port->role = mRole; | 
|  | 5044 | port->type = mType; | 
|  | 5045 | unsigned int i; | 
|  | 5046 | for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) { | 
|  | 5047 | port->sample_rates[i] = mSamplingRates[i]; | 
|  | 5048 | } | 
|  | 5049 | port->num_sample_rates = i; | 
|  | 5050 | for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) { | 
|  | 5051 | port->channel_masks[i] = mChannelMasks[i]; | 
|  | 5052 | } | 
|  | 5053 | port->num_channel_masks = i; | 
|  | 5054 | for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) { | 
|  | 5055 | port->formats[i] = mFormats[i]; | 
|  | 5056 | } | 
|  | 5057 | port->num_formats = i; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5058 |  | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 5059 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5060 |  | 
|  | 5061 | for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) { | 
|  | 5062 | port->gains[i] = mGains[i]->mGain; | 
|  | 5063 | } | 
|  | 5064 | port->num_gains = i; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5065 | } | 
|  | 5066 |  | 
|  | 5067 |  | 
|  | 5068 | void AudioPolicyManager::AudioPort::loadSamplingRates(char *name) | 
|  | 5069 | { | 
|  | 5070 | char *str = strtok(name, "|"); | 
|  | 5071 |  | 
|  | 5072 | // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling | 
|  | 5073 | // rates should be read from the output stream after it is opened for the first time | 
|  | 5074 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5075 | mSamplingRates.add(0); | 
|  | 5076 | return; | 
|  | 5077 | } | 
|  | 5078 |  | 
|  | 5079 | while (str != NULL) { | 
|  | 5080 | uint32_t rate = atoi(str); | 
|  | 5081 | if (rate != 0) { | 
|  | 5082 | ALOGV("loadSamplingRates() adding rate %d", rate); | 
|  | 5083 | mSamplingRates.add(rate); | 
|  | 5084 | } | 
|  | 5085 | str = strtok(NULL, "|"); | 
|  | 5086 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5087 | } | 
|  | 5088 |  | 
|  | 5089 | void AudioPolicyManager::AudioPort::loadFormats(char *name) | 
|  | 5090 | { | 
|  | 5091 | char *str = strtok(name, "|"); | 
|  | 5092 |  | 
|  | 5093 | // by convention, "0' in the first entry in mFormats indicates the supported formats | 
|  | 5094 | // should be read from the output stream after it is opened for the first time | 
|  | 5095 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5096 | mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 5097 | return; | 
|  | 5098 | } | 
|  | 5099 |  | 
|  | 5100 | while (str != NULL) { | 
|  | 5101 | audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable, | 
|  | 5102 | ARRAY_SIZE(sFormatNameToEnumTable), | 
|  | 5103 | str); | 
|  | 5104 | if (format != AUDIO_FORMAT_DEFAULT) { | 
|  | 5105 | mFormats.add(format); | 
|  | 5106 | } | 
|  | 5107 | str = strtok(NULL, "|"); | 
|  | 5108 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5109 | } | 
|  | 5110 |  | 
|  | 5111 | void AudioPolicyManager::AudioPort::loadInChannels(char *name) | 
|  | 5112 | { | 
|  | 5113 | const char *str = strtok(name, "|"); | 
|  | 5114 |  | 
|  | 5115 | ALOGV("loadInChannels() %s", name); | 
|  | 5116 |  | 
|  | 5117 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5118 | mChannelMasks.add(0); | 
|  | 5119 | return; | 
|  | 5120 | } | 
|  | 5121 |  | 
|  | 5122 | while (str != NULL) { | 
|  | 5123 | audio_channel_mask_t channelMask = | 
|  | 5124 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, | 
|  | 5125 | ARRAY_SIZE(sInChannelsNameToEnumTable), | 
|  | 5126 | str); | 
|  | 5127 | if (channelMask != 0) { | 
|  | 5128 | ALOGV("loadInChannels() adding channelMask %04x", channelMask); | 
|  | 5129 | mChannelMasks.add(channelMask); | 
|  | 5130 | } | 
|  | 5131 | str = strtok(NULL, "|"); | 
|  | 5132 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5133 | } | 
|  | 5134 |  | 
|  | 5135 | void AudioPolicyManager::AudioPort::loadOutChannels(char *name) | 
|  | 5136 | { | 
|  | 5137 | const char *str = strtok(name, "|"); | 
|  | 5138 |  | 
|  | 5139 | ALOGV("loadOutChannels() %s", name); | 
|  | 5140 |  | 
|  | 5141 | // by convention, "0' in the first entry in mChannelMasks indicates the supported channel | 
|  | 5142 | // masks should be read from the output stream after it is opened for the first time | 
|  | 5143 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5144 | mChannelMasks.add(0); | 
|  | 5145 | return; | 
|  | 5146 | } | 
|  | 5147 |  | 
|  | 5148 | while (str != NULL) { | 
|  | 5149 | audio_channel_mask_t channelMask = | 
|  | 5150 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, | 
|  | 5151 | ARRAY_SIZE(sOutChannelsNameToEnumTable), | 
|  | 5152 | str); | 
|  | 5153 | if (channelMask != 0) { | 
|  | 5154 | mChannelMasks.add(channelMask); | 
|  | 5155 | } | 
|  | 5156 | str = strtok(NULL, "|"); | 
|  | 5157 | } | 
|  | 5158 | return; | 
|  | 5159 | } | 
|  | 5160 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5161 | audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name) | 
|  | 5162 | { | 
|  | 5163 | const char *str = strtok(name, "|"); | 
|  | 5164 |  | 
|  | 5165 | ALOGV("loadGainMode() %s", name); | 
|  | 5166 | audio_gain_mode_t mode = 0; | 
|  | 5167 | while (str != NULL) { | 
|  | 5168 | mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable, | 
|  | 5169 | ARRAY_SIZE(sGainModeNameToEnumTable), | 
|  | 5170 | str); | 
|  | 5171 | str = strtok(NULL, "|"); | 
|  | 5172 | } | 
|  | 5173 | return mode; | 
|  | 5174 | } | 
|  | 5175 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5176 | void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index) | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5177 | { | 
|  | 5178 | cnode *node = root->first_child; | 
|  | 5179 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5180 | sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5181 |  | 
|  | 5182 | while (node) { | 
|  | 5183 | if (strcmp(node->name, GAIN_MODE) == 0) { | 
|  | 5184 | gain->mGain.mode = loadGainMode((char *)node->value); | 
|  | 5185 | } else if (strcmp(node->name, GAIN_CHANNELS) == 0) { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5186 | if (mUseInChannelMask) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5187 | gain->mGain.channel_mask = | 
|  | 5188 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, | 
|  | 5189 | ARRAY_SIZE(sInChannelsNameToEnumTable), | 
|  | 5190 | (char *)node->value); | 
|  | 5191 | } else { | 
|  | 5192 | gain->mGain.channel_mask = | 
|  | 5193 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, | 
|  | 5194 | ARRAY_SIZE(sOutChannelsNameToEnumTable), | 
|  | 5195 | (char *)node->value); | 
|  | 5196 | } | 
|  | 5197 | } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) { | 
|  | 5198 | gain->mGain.min_value = atoi((char *)node->value); | 
|  | 5199 | } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) { | 
|  | 5200 | gain->mGain.max_value = atoi((char *)node->value); | 
|  | 5201 | } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) { | 
|  | 5202 | gain->mGain.default_value = atoi((char *)node->value); | 
|  | 5203 | } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) { | 
|  | 5204 | gain->mGain.step_value = atoi((char *)node->value); | 
|  | 5205 | } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) { | 
|  | 5206 | gain->mGain.min_ramp_ms = atoi((char *)node->value); | 
|  | 5207 | } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) { | 
|  | 5208 | gain->mGain.max_ramp_ms = atoi((char *)node->value); | 
|  | 5209 | } | 
|  | 5210 | node = node->next; | 
|  | 5211 | } | 
|  | 5212 |  | 
|  | 5213 | ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d", | 
|  | 5214 | gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value); | 
|  | 5215 |  | 
|  | 5216 | if (gain->mGain.mode == 0) { | 
|  | 5217 | return; | 
|  | 5218 | } | 
|  | 5219 | mGains.add(gain); | 
|  | 5220 | } | 
|  | 5221 |  | 
|  | 5222 | void AudioPolicyManager::AudioPort::loadGains(cnode *root) | 
|  | 5223 | { | 
|  | 5224 | cnode *node = root->first_child; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5225 | int index = 0; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5226 | while (node) { | 
|  | 5227 | ALOGV("loadGains() loading gain %s", node->name); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5228 | loadGain(node, index++); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5229 | node = node->next; | 
|  | 5230 | } | 
|  | 5231 | } | 
|  | 5232 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5233 | status_t AudioPolicyManager::AudioPort::checkSamplingRate(uint32_t samplingRate) const | 
|  | 5234 | { | 
|  | 5235 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { | 
|  | 5236 | if (mSamplingRates[i] == samplingRate) { | 
|  | 5237 | return NO_ERROR; | 
|  | 5238 | } | 
|  | 5239 | } | 
|  | 5240 | return BAD_VALUE; | 
|  | 5241 | } | 
|  | 5242 |  | 
|  | 5243 | status_t AudioPolicyManager::AudioPort::checkChannelMask(audio_channel_mask_t channelMask) const | 
|  | 5244 | { | 
|  | 5245 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { | 
|  | 5246 | if (mChannelMasks[i] == channelMask) { | 
|  | 5247 | return NO_ERROR; | 
|  | 5248 | } | 
|  | 5249 | } | 
|  | 5250 | return BAD_VALUE; | 
|  | 5251 | } | 
|  | 5252 |  | 
|  | 5253 | status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const | 
|  | 5254 | { | 
|  | 5255 | for (size_t i = 0; i < mFormats.size(); i ++) { | 
|  | 5256 | if (mFormats[i] == format) { | 
|  | 5257 | return NO_ERROR; | 
|  | 5258 | } | 
|  | 5259 | } | 
|  | 5260 | return BAD_VALUE; | 
|  | 5261 | } | 
|  | 5262 |  | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5263 |  | 
|  | 5264 | uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const | 
|  | 5265 | { | 
|  | 5266 | // special case for uninitialized dynamic profile | 
|  | 5267 | if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) { | 
|  | 5268 | return 0; | 
|  | 5269 | } | 
|  | 5270 |  | 
|  | 5271 | uint32_t samplingRate = 0; | 
|  | 5272 | uint32_t maxRate = MAX_MIXER_SAMPLING_RATE; | 
|  | 5273 |  | 
|  | 5274 | // For mixed output and inputs, use max mixer sampling rates. Do not | 
|  | 5275 | // limit sampling rate otherwise | 
|  | 5276 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 5277 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
|  | 5278 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) { | 
|  | 5279 | maxRate = UINT_MAX; | 
|  | 5280 | } | 
|  | 5281 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { | 
|  | 5282 | if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) { | 
|  | 5283 | samplingRate = mSamplingRates[i]; | 
|  | 5284 | } | 
|  | 5285 | } | 
|  | 5286 | return samplingRate; | 
|  | 5287 | } | 
|  | 5288 |  | 
|  | 5289 | audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const | 
|  | 5290 | { | 
|  | 5291 | // special case for uninitialized dynamic profile | 
|  | 5292 | if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) { | 
|  | 5293 | return AUDIO_CHANNEL_NONE; | 
|  | 5294 | } | 
|  | 5295 |  | 
|  | 5296 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE; | 
|  | 5297 | uint32_t channelCount = 0; | 
|  | 5298 | uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; | 
|  | 5299 |  | 
|  | 5300 | // For mixed output and inputs, use max mixer channel count. Do not | 
|  | 5301 | // limit channel count otherwise | 
|  | 5302 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 5303 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
|  | 5304 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) { | 
|  | 5305 | maxCount = UINT_MAX; | 
|  | 5306 | } | 
|  | 5307 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { | 
|  | 5308 | uint32_t cnlCount; | 
|  | 5309 | if (mUseInChannelMask) { | 
|  | 5310 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); | 
|  | 5311 | } else { | 
|  | 5312 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); | 
|  | 5313 | } | 
|  | 5314 | if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { | 
|  | 5315 | channelMask = mChannelMasks[i]; | 
|  | 5316 | } | 
|  | 5317 | } | 
|  | 5318 | return channelMask; | 
|  | 5319 | } | 
|  | 5320 |  | 
|  | 5321 | const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = { | 
|  | 5322 | AUDIO_FORMAT_DEFAULT, | 
|  | 5323 | AUDIO_FORMAT_PCM_16_BIT, | 
| Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 5324 | AUDIO_FORMAT_PCM_8_24_BIT, | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5325 | AUDIO_FORMAT_PCM_24_BIT_PACKED, | 
| Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 5326 | AUDIO_FORMAT_PCM_32_BIT, | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5327 | }; | 
|  | 5328 |  | 
|  | 5329 | int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1, | 
|  | 5330 | audio_format_t format2) | 
|  | 5331 | { | 
|  | 5332 | // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any | 
|  | 5333 | // compressed format and better than any PCM format. This is by design of pickFormat() | 
|  | 5334 | if (!audio_is_linear_pcm(format1)) { | 
|  | 5335 | if (!audio_is_linear_pcm(format2)) { | 
|  | 5336 | return 0; | 
|  | 5337 | } | 
|  | 5338 | return 1; | 
|  | 5339 | } | 
|  | 5340 | if (!audio_is_linear_pcm(format2)) { | 
|  | 5341 | return -1; | 
|  | 5342 | } | 
|  | 5343 |  | 
|  | 5344 | int index1 = -1, index2 = -1; | 
|  | 5345 | for (size_t i = 0; | 
|  | 5346 | (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1)); | 
|  | 5347 | i ++) { | 
|  | 5348 | if (sPcmFormatCompareTable[i] == format1) { | 
|  | 5349 | index1 = i; | 
|  | 5350 | } | 
|  | 5351 | if (sPcmFormatCompareTable[i] == format2) { | 
|  | 5352 | index2 = i; | 
|  | 5353 | } | 
|  | 5354 | } | 
|  | 5355 | // format1 not found => index1 < 0 => format2 > format1 | 
|  | 5356 | // format2 not found => index2 < 0 => format2 < format1 | 
|  | 5357 | return index1 - index2; | 
|  | 5358 | } | 
|  | 5359 |  | 
|  | 5360 | audio_format_t AudioPolicyManager::AudioPort::pickFormat() const | 
|  | 5361 | { | 
|  | 5362 | // special case for uninitialized dynamic profile | 
|  | 5363 | if (mFormats.size() == 1 && mFormats[0] == 0) { | 
|  | 5364 | return AUDIO_FORMAT_DEFAULT; | 
|  | 5365 | } | 
|  | 5366 |  | 
|  | 5367 | audio_format_t format = AUDIO_FORMAT_DEFAULT; | 
|  | 5368 | audio_format_t bestFormat = BEST_MIXER_FORMAT; | 
|  | 5369 | // For mixed output and inputs, use best mixer output format. Do not | 
|  | 5370 | // limit format otherwise | 
|  | 5371 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 5372 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
|  | 5373 | (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) == 0)))) { | 
|  | 5374 | bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 5375 | } | 
|  | 5376 |  | 
|  | 5377 | for (size_t i = 0; i < mFormats.size(); i ++) { | 
|  | 5378 | if ((compareFormats(mFormats[i], format) > 0) && | 
|  | 5379 | (compareFormats(mFormats[i], bestFormat) <= 0)) { | 
|  | 5380 | format = mFormats[i]; | 
|  | 5381 | } | 
|  | 5382 | } | 
|  | 5383 | return format; | 
|  | 5384 | } | 
|  | 5385 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5386 | status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig, | 
|  | 5387 | int index) const | 
|  | 5388 | { | 
|  | 5389 | if (index < 0 || (size_t)index >= mGains.size()) { | 
|  | 5390 | return BAD_VALUE; | 
|  | 5391 | } | 
|  | 5392 | return mGains[index]->checkConfig(gainConfig); | 
|  | 5393 | } | 
|  | 5394 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5395 | void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const | 
|  | 5396 | { | 
|  | 5397 | const size_t SIZE = 256; | 
|  | 5398 | char buffer[SIZE]; | 
|  | 5399 | String8 result; | 
|  | 5400 |  | 
|  | 5401 | if (mName.size() != 0) { | 
|  | 5402 | snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string()); | 
|  | 5403 | result.append(buffer); | 
|  | 5404 | } | 
|  | 5405 |  | 
|  | 5406 | if (mSamplingRates.size() != 0) { | 
|  | 5407 | snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, ""); | 
|  | 5408 | result.append(buffer); | 
|  | 5409 | for (size_t i = 0; i < mSamplingRates.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5410 | if (i == 0 && mSamplingRates[i] == 0) { | 
|  | 5411 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 5412 | } else { | 
|  | 5413 | snprintf(buffer, SIZE, "%d", mSamplingRates[i]); | 
|  | 5414 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5415 | result.append(buffer); | 
|  | 5416 | result.append(i == (mSamplingRates.size() - 1) ? "" : ", "); | 
|  | 5417 | } | 
|  | 5418 | result.append("\n"); | 
|  | 5419 | } | 
|  | 5420 |  | 
|  | 5421 | if (mChannelMasks.size() != 0) { | 
|  | 5422 | snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, ""); | 
|  | 5423 | result.append(buffer); | 
|  | 5424 | for (size_t i = 0; i < mChannelMasks.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5425 | ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]); | 
|  | 5426 |  | 
|  | 5427 | if (i == 0 && mChannelMasks[i] == 0) { | 
|  | 5428 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 5429 | } else { | 
|  | 5430 | snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]); | 
|  | 5431 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5432 | result.append(buffer); | 
|  | 5433 | result.append(i == (mChannelMasks.size() - 1) ? "" : ", "); | 
|  | 5434 | } | 
|  | 5435 | result.append("\n"); | 
|  | 5436 | } | 
|  | 5437 |  | 
|  | 5438 | if (mFormats.size() != 0) { | 
|  | 5439 | snprintf(buffer, SIZE, "%*s- formats: ", spaces, ""); | 
|  | 5440 | result.append(buffer); | 
|  | 5441 | for (size_t i = 0; i < mFormats.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5442 | const char *formatStr = enumToString(sFormatNameToEnumTable, | 
|  | 5443 | ARRAY_SIZE(sFormatNameToEnumTable), | 
|  | 5444 | mFormats[i]); | 
|  | 5445 | if (i == 0 && strcmp(formatStr, "") == 0) { | 
|  | 5446 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 5447 | } else { | 
|  | 5448 | snprintf(buffer, SIZE, "%-48s", formatStr); | 
|  | 5449 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5450 | result.append(buffer); | 
|  | 5451 | result.append(i == (mFormats.size() - 1) ? "" : ", "); | 
|  | 5452 | } | 
|  | 5453 | result.append("\n"); | 
|  | 5454 | } | 
|  | 5455 | write(fd, result.string(), result.size()); | 
|  | 5456 | if (mGains.size() != 0) { | 
|  | 5457 | snprintf(buffer, SIZE, "%*s- gains:\n", spaces, ""); | 
|  | 5458 | write(fd, buffer, strlen(buffer) + 1); | 
|  | 5459 | result.append(buffer); | 
|  | 5460 | for (size_t i = 0; i < mGains.size(); i++) { | 
|  | 5461 | mGains[i]->dump(fd, spaces + 2, i); | 
|  | 5462 | } | 
|  | 5463 | } | 
|  | 5464 | } | 
|  | 5465 |  | 
|  | 5466 | // --- AudioGain class implementation | 
|  | 5467 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5468 | AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask) | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5469 | { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5470 | mIndex = index; | 
|  | 5471 | mUseInChannelMask = useInChannelMask; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5472 | memset(&mGain, 0, sizeof(struct audio_gain)); | 
|  | 5473 | } | 
|  | 5474 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5475 | void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config) | 
|  | 5476 | { | 
|  | 5477 | config->index = mIndex; | 
|  | 5478 | config->mode = mGain.mode; | 
|  | 5479 | config->channel_mask = mGain.channel_mask; | 
|  | 5480 | if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { | 
|  | 5481 | config->values[0] = mGain.default_value; | 
|  | 5482 | } else { | 
|  | 5483 | uint32_t numValues; | 
|  | 5484 | if (mUseInChannelMask) { | 
|  | 5485 | numValues = audio_channel_count_from_in_mask(mGain.channel_mask); | 
|  | 5486 | } else { | 
|  | 5487 | numValues = audio_channel_count_from_out_mask(mGain.channel_mask); | 
|  | 5488 | } | 
|  | 5489 | for (size_t i = 0; i < numValues; i++) { | 
|  | 5490 | config->values[i] = mGain.default_value; | 
|  | 5491 | } | 
|  | 5492 | } | 
|  | 5493 | if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { | 
|  | 5494 | config->ramp_duration_ms = mGain.min_ramp_ms; | 
|  | 5495 | } | 
|  | 5496 | } | 
|  | 5497 |  | 
|  | 5498 | status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config) | 
|  | 5499 | { | 
|  | 5500 | if ((config->mode & ~mGain.mode) != 0) { | 
|  | 5501 | return BAD_VALUE; | 
|  | 5502 | } | 
|  | 5503 | if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { | 
|  | 5504 | if ((config->values[0] < mGain.min_value) || | 
|  | 5505 | (config->values[0] > mGain.max_value)) { | 
|  | 5506 | return BAD_VALUE; | 
|  | 5507 | } | 
|  | 5508 | } else { | 
|  | 5509 | if ((config->channel_mask & ~mGain.channel_mask) != 0) { | 
|  | 5510 | return BAD_VALUE; | 
|  | 5511 | } | 
|  | 5512 | uint32_t numValues; | 
|  | 5513 | if (mUseInChannelMask) { | 
|  | 5514 | numValues = audio_channel_count_from_in_mask(config->channel_mask); | 
|  | 5515 | } else { | 
|  | 5516 | numValues = audio_channel_count_from_out_mask(config->channel_mask); | 
|  | 5517 | } | 
|  | 5518 | for (size_t i = 0; i < numValues; i++) { | 
|  | 5519 | if ((config->values[i] < mGain.min_value) || | 
|  | 5520 | (config->values[i] > mGain.max_value)) { | 
|  | 5521 | return BAD_VALUE; | 
|  | 5522 | } | 
|  | 5523 | } | 
|  | 5524 | } | 
|  | 5525 | if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { | 
|  | 5526 | if ((config->ramp_duration_ms < mGain.min_ramp_ms) || | 
|  | 5527 | (config->ramp_duration_ms > mGain.max_ramp_ms)) { | 
|  | 5528 | return BAD_VALUE; | 
|  | 5529 | } | 
|  | 5530 | } | 
|  | 5531 | return NO_ERROR; | 
|  | 5532 | } | 
|  | 5533 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5534 | void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const | 
|  | 5535 | { | 
|  | 5536 | const size_t SIZE = 256; | 
|  | 5537 | char buffer[SIZE]; | 
|  | 5538 | String8 result; | 
|  | 5539 |  | 
|  | 5540 | snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1); | 
|  | 5541 | result.append(buffer); | 
|  | 5542 | snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode); | 
|  | 5543 | result.append(buffer); | 
|  | 5544 | snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask); | 
|  | 5545 | result.append(buffer); | 
|  | 5546 | snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value); | 
|  | 5547 | result.append(buffer); | 
|  | 5548 | snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value); | 
|  | 5549 | result.append(buffer); | 
|  | 5550 | snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value); | 
|  | 5551 | result.append(buffer); | 
|  | 5552 | snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value); | 
|  | 5553 | result.append(buffer); | 
|  | 5554 | snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms); | 
|  | 5555 | result.append(buffer); | 
|  | 5556 | snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms); | 
|  | 5557 | result.append(buffer); | 
|  | 5558 |  | 
|  | 5559 | write(fd, result.string(), result.size()); | 
|  | 5560 | } | 
|  | 5561 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5562 | // --- AudioPortConfig class implementation | 
|  | 5563 |  | 
|  | 5564 | AudioPolicyManager::AudioPortConfig::AudioPortConfig() | 
|  | 5565 | { | 
|  | 5566 | mSamplingRate = 0; | 
|  | 5567 | mChannelMask = AUDIO_CHANNEL_NONE; | 
|  | 5568 | mFormat = AUDIO_FORMAT_INVALID; | 
|  | 5569 | mGain.index = -1; | 
|  | 5570 | } | 
|  | 5571 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5572 | status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig( | 
|  | 5573 | const struct audio_port_config *config, | 
|  | 5574 | struct audio_port_config *backupConfig) | 
|  | 5575 | { | 
|  | 5576 | struct audio_port_config localBackupConfig; | 
|  | 5577 | status_t status = NO_ERROR; | 
|  | 5578 |  | 
|  | 5579 | localBackupConfig.config_mask = config->config_mask; | 
|  | 5580 | toAudioPortConfig(&localBackupConfig); | 
|  | 5581 |  | 
|  | 5582 | if (mAudioPort == 0) { | 
|  | 5583 | status = NO_INIT; | 
|  | 5584 | goto exit; | 
|  | 5585 | } | 
|  | 5586 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { | 
|  | 5587 | status = mAudioPort->checkSamplingRate(config->sample_rate); | 
|  | 5588 | if (status != NO_ERROR) { | 
|  | 5589 | goto exit; | 
|  | 5590 | } | 
|  | 5591 | mSamplingRate = config->sample_rate; | 
|  | 5592 | } | 
|  | 5593 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { | 
|  | 5594 | status = mAudioPort->checkChannelMask(config->channel_mask); | 
|  | 5595 | if (status != NO_ERROR) { | 
|  | 5596 | goto exit; | 
|  | 5597 | } | 
|  | 5598 | mChannelMask = config->channel_mask; | 
|  | 5599 | } | 
|  | 5600 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { | 
|  | 5601 | status = mAudioPort->checkFormat(config->format); | 
|  | 5602 | if (status != NO_ERROR) { | 
|  | 5603 | goto exit; | 
|  | 5604 | } | 
|  | 5605 | mFormat = config->format; | 
|  | 5606 | } | 
|  | 5607 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { | 
|  | 5608 | status = mAudioPort->checkGain(&config->gain, config->gain.index); | 
|  | 5609 | if (status != NO_ERROR) { | 
|  | 5610 | goto exit; | 
|  | 5611 | } | 
|  | 5612 | mGain = config->gain; | 
|  | 5613 | } | 
|  | 5614 |  | 
|  | 5615 | exit: | 
|  | 5616 | if (status != NO_ERROR) { | 
|  | 5617 | applyAudioPortConfig(&localBackupConfig); | 
|  | 5618 | } | 
|  | 5619 | if (backupConfig != NULL) { | 
|  | 5620 | *backupConfig = localBackupConfig; | 
|  | 5621 | } | 
|  | 5622 | return status; | 
|  | 5623 | } | 
|  | 5624 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5625 | void AudioPolicyManager::AudioPortConfig::toAudioPortConfig( | 
|  | 5626 | struct audio_port_config *dstConfig, | 
|  | 5627 | const struct audio_port_config *srcConfig) const | 
|  | 5628 | { | 
|  | 5629 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { | 
|  | 5630 | dstConfig->sample_rate = mSamplingRate; | 
|  | 5631 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) { | 
|  | 5632 | dstConfig->sample_rate = srcConfig->sample_rate; | 
|  | 5633 | } | 
|  | 5634 | } else { | 
|  | 5635 | dstConfig->sample_rate = 0; | 
|  | 5636 | } | 
|  | 5637 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { | 
|  | 5638 | dstConfig->channel_mask = mChannelMask; | 
|  | 5639 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) { | 
|  | 5640 | dstConfig->channel_mask = srcConfig->channel_mask; | 
|  | 5641 | } | 
|  | 5642 | } else { | 
|  | 5643 | dstConfig->channel_mask = AUDIO_CHANNEL_NONE; | 
|  | 5644 | } | 
|  | 5645 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) { | 
|  | 5646 | dstConfig->format = mFormat; | 
|  | 5647 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) { | 
|  | 5648 | dstConfig->format = srcConfig->format; | 
|  | 5649 | } | 
|  | 5650 | } else { | 
|  | 5651 | dstConfig->format = AUDIO_FORMAT_INVALID; | 
|  | 5652 | } | 
|  | 5653 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) { | 
|  | 5654 | dstConfig->gain = mGain; | 
|  | 5655 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) { | 
|  | 5656 | dstConfig->gain = srcConfig->gain; | 
|  | 5657 | } | 
|  | 5658 | } else { | 
|  | 5659 | dstConfig->gain.index = -1; | 
|  | 5660 | } | 
|  | 5661 | if (dstConfig->gain.index != -1) { | 
|  | 5662 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; | 
|  | 5663 | } else { | 
|  | 5664 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; | 
|  | 5665 | } | 
|  | 5666 | } | 
|  | 5667 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5668 | // --- IOProfile class implementation | 
|  | 5669 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5670 | AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role, | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5671 | const sp<HwModule>& module) | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5672 | : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5673 | { | 
|  | 5674 | } | 
|  | 5675 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5676 | AudioPolicyManager::IOProfile::~IOProfile() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5677 | { | 
|  | 5678 | } | 
|  | 5679 |  | 
|  | 5680 | // checks if the IO profile is compatible with specified parameters. | 
|  | 5681 | // Sampling rate, format and channel mask must be specified in order to | 
|  | 5682 | // get a valid a match | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5683 | bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5684 | uint32_t samplingRate, | 
|  | 5685 | audio_format_t format, | 
|  | 5686 | audio_channel_mask_t channelMask, | 
|  | 5687 | audio_output_flags_t flags) const | 
|  | 5688 | { | 
|  | 5689 | if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) { | 
|  | 5690 | return false; | 
|  | 5691 | } | 
|  | 5692 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5693 | if ((mSupportedDevices.types() & device) != device) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5694 | return false; | 
|  | 5695 | } | 
|  | 5696 | if ((mFlags & flags) != flags) { | 
|  | 5697 | return false; | 
|  | 5698 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5699 | if (checkSamplingRate(samplingRate) != NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5700 | return false; | 
|  | 5701 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5702 | if (checkChannelMask(channelMask) != NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5703 | return false; | 
|  | 5704 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5705 | if (checkFormat(format) != NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5706 | return false; | 
|  | 5707 | } | 
|  | 5708 | return true; | 
|  | 5709 | } | 
|  | 5710 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5711 | void AudioPolicyManager::IOProfile::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5712 | { | 
|  | 5713 | const size_t SIZE = 256; | 
|  | 5714 | char buffer[SIZE]; | 
|  | 5715 | String8 result; | 
|  | 5716 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5717 | AudioPort::dump(fd, 4); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5718 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5719 | snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags); | 
|  | 5720 | result.append(buffer); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5721 | snprintf(buffer, SIZE, "    - devices:\n"); | 
|  | 5722 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5723 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5724 | for (size_t i = 0; i < mSupportedDevices.size(); i++) { | 
|  | 5725 | mSupportedDevices[i]->dump(fd, 6, i); | 
|  | 5726 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5727 | } | 
|  | 5728 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5729 | void AudioPolicyManager::IOProfile::log() | 
|  | 5730 | { | 
|  | 5731 | const size_t SIZE = 256; | 
|  | 5732 | char buffer[SIZE]; | 
|  | 5733 | String8 result; | 
|  | 5734 |  | 
|  | 5735 | ALOGV("    - sampling rates: "); | 
|  | 5736 | for (size_t i = 0; i < mSamplingRates.size(); i++) { | 
|  | 5737 | ALOGV("  %d", mSamplingRates[i]); | 
|  | 5738 | } | 
|  | 5739 |  | 
|  | 5740 | ALOGV("    - channel masks: "); | 
|  | 5741 | for (size_t i = 0; i < mChannelMasks.size(); i++) { | 
|  | 5742 | ALOGV("  0x%04x", mChannelMasks[i]); | 
|  | 5743 | } | 
|  | 5744 |  | 
|  | 5745 | ALOGV("    - formats: "); | 
|  | 5746 | for (size_t i = 0; i < mFormats.size(); i++) { | 
|  | 5747 | ALOGV("  0x%08x", mFormats[i]); | 
|  | 5748 | } | 
|  | 5749 |  | 
|  | 5750 | ALOGV("    - devices: 0x%04x\n", mSupportedDevices.types()); | 
|  | 5751 | ALOGV("    - flags: 0x%04x\n", mFlags); | 
|  | 5752 | } | 
|  | 5753 |  | 
|  | 5754 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5755 | // --- DeviceDescriptor implementation | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5756 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5757 |  | 
|  | 5758 | AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) : | 
|  | 5759 | AudioPort(name, AUDIO_PORT_TYPE_DEVICE, | 
|  | 5760 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : | 
|  | 5761 | AUDIO_PORT_ROLE_SOURCE, | 
|  | 5762 | NULL), | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5763 | mDeviceType(type), mAddress(""), mId(0) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5764 | { | 
|  | 5765 | mAudioPort = this; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5766 | if (mGains.size() > 0) { | 
|  | 5767 | mGains[0]->getDefaultConfig(&mGain); | 
|  | 5768 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5769 | } | 
|  | 5770 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5771 | bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5772 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5773 | // Devices are considered equal if they: | 
|  | 5774 | // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) | 
|  | 5775 | // - have the same address or one device does not specify the address | 
|  | 5776 | // - have the same channel mask or one device does not specify the channel mask | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5777 | return (mDeviceType == other->mDeviceType) && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5778 | (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && | 
| Eric Laurent | 2f8a36f | 2014-03-26 19:05:55 -0700 | [diff] [blame] | 5779 | (mChannelMask == 0 || other->mChannelMask == 0 || | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5780 | mChannelMask == other->mChannelMask); | 
|  | 5781 | } | 
|  | 5782 |  | 
|  | 5783 | void AudioPolicyManager::DeviceVector::refreshTypes() | 
|  | 5784 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5785 | mDeviceTypes = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5786 | for(size_t i = 0; i < size(); i++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5787 | mDeviceTypes |= itemAt(i)->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5788 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5789 | ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5790 | } | 
|  | 5791 |  | 
|  | 5792 | ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const | 
|  | 5793 | { | 
|  | 5794 | for(size_t i = 0; i < size(); i++) { | 
|  | 5795 | if (item->equals(itemAt(i))) { | 
|  | 5796 | return i; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5797 | } | 
|  | 5798 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5799 | return -1; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5800 | } | 
|  | 5801 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5802 | ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5803 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5804 | ssize_t ret = indexOf(item); | 
|  | 5805 |  | 
|  | 5806 | if (ret < 0) { | 
|  | 5807 | ret = SortedVector::add(item); | 
|  | 5808 | if (ret >= 0) { | 
|  | 5809 | refreshTypes(); | 
|  | 5810 | } | 
|  | 5811 | } else { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5812 | ALOGW("DeviceVector::add device %08x already in", item->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5813 | ret = -1; | 
|  | 5814 | } | 
|  | 5815 | return ret; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5816 | } | 
|  | 5817 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5818 | ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item) | 
|  | 5819 | { | 
|  | 5820 | size_t i; | 
|  | 5821 | ssize_t ret = indexOf(item); | 
|  | 5822 |  | 
|  | 5823 | if (ret < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5824 | ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5825 | } else { | 
|  | 5826 | ret = SortedVector::removeAt(ret); | 
|  | 5827 | if (ret >= 0) { | 
|  | 5828 | refreshTypes(); | 
|  | 5829 | } | 
|  | 5830 | } | 
|  | 5831 | return ret; | 
|  | 5832 | } | 
|  | 5833 |  | 
|  | 5834 | void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types) | 
|  | 5835 | { | 
|  | 5836 | DeviceVector deviceList; | 
|  | 5837 |  | 
|  | 5838 | uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; | 
|  | 5839 | types &= ~role_bit; | 
|  | 5840 |  | 
|  | 5841 | while (types) { | 
|  | 5842 | uint32_t i = 31 - __builtin_clz(types); | 
|  | 5843 | uint32_t type = 1 << i; | 
|  | 5844 | types &= ~type; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5845 | add(new DeviceDescriptor(String8(""), type | role_bit)); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5846 | } | 
|  | 5847 | } | 
|  | 5848 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5849 | void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name, | 
|  | 5850 | const DeviceVector& declaredDevices) | 
|  | 5851 | { | 
|  | 5852 | char *devName = strtok(name, "|"); | 
|  | 5853 | while (devName != NULL) { | 
|  | 5854 | if (strlen(devName) != 0) { | 
|  | 5855 | audio_devices_t type = stringToEnum(sDeviceNameToEnumTable, | 
|  | 5856 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 5857 | devName); | 
|  | 5858 | if (type != AUDIO_DEVICE_NONE) { | 
|  | 5859 | add(new DeviceDescriptor(String8(""), type)); | 
|  | 5860 | } else { | 
|  | 5861 | sp<DeviceDescriptor> deviceDesc = | 
|  | 5862 | declaredDevices.getDeviceFromName(String8(devName)); | 
|  | 5863 | if (deviceDesc != 0) { | 
|  | 5864 | add(deviceDesc); | 
|  | 5865 | } | 
|  | 5866 | } | 
|  | 5867 | } | 
|  | 5868 | devName = strtok(NULL, "|"); | 
|  | 5869 | } | 
|  | 5870 | } | 
|  | 5871 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5872 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice( | 
|  | 5873 | audio_devices_t type, String8 address) const | 
|  | 5874 | { | 
|  | 5875 | sp<DeviceDescriptor> device; | 
|  | 5876 | for (size_t i = 0; i < size(); i++) { | 
|  | 5877 | if (itemAt(i)->mDeviceType == type) { | 
|  | 5878 | device = itemAt(i); | 
|  | 5879 | if (itemAt(i)->mAddress = address) { | 
|  | 5880 | break; | 
|  | 5881 | } | 
|  | 5882 | } | 
|  | 5883 | } | 
|  | 5884 | ALOGV("DeviceVector::getDevice() for type %d address %s found %p", | 
|  | 5885 | type, address.string(), device.get()); | 
|  | 5886 | return device; | 
|  | 5887 | } | 
|  | 5888 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5889 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId( | 
|  | 5890 | audio_port_handle_t id) const | 
|  | 5891 | { | 
|  | 5892 | sp<DeviceDescriptor> device; | 
|  | 5893 | for (size_t i = 0; i < size(); i++) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 5894 | ALOGV("DeviceVector::getDeviceFromId(%d) itemAt(%zu)->mId %d", id, i, itemAt(i)->mId); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5895 | if (itemAt(i)->mId == id) { | 
|  | 5896 | device = itemAt(i); | 
|  | 5897 | break; | 
|  | 5898 | } | 
|  | 5899 | } | 
|  | 5900 | return device; | 
|  | 5901 | } | 
|  | 5902 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5903 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType( | 
|  | 5904 | audio_devices_t type) const | 
|  | 5905 | { | 
|  | 5906 | DeviceVector devices; | 
|  | 5907 | for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { | 
|  | 5908 | if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) { | 
|  | 5909 | devices.add(itemAt(i)); | 
|  | 5910 | type &= ~itemAt(i)->mDeviceType; | 
|  | 5911 | ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", | 
|  | 5912 | itemAt(i)->mDeviceType, itemAt(i).get()); | 
|  | 5913 | } | 
|  | 5914 | } | 
|  | 5915 | return devices; | 
|  | 5916 | } | 
|  | 5917 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5918 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName( | 
|  | 5919 | const String8& name) const | 
|  | 5920 | { | 
|  | 5921 | sp<DeviceDescriptor> device; | 
|  | 5922 | for (size_t i = 0; i < size(); i++) { | 
|  | 5923 | if (itemAt(i)->mName == name) { | 
|  | 5924 | device = itemAt(i); | 
|  | 5925 | break; | 
|  | 5926 | } | 
|  | 5927 | } | 
|  | 5928 | return device; | 
|  | 5929 | } | 
|  | 5930 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5931 | void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig( | 
|  | 5932 | struct audio_port_config *dstConfig, | 
|  | 5933 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5934 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5935 | dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; | 
|  | 5936 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5937 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5938 | } | 
|  | 5939 |  | 
|  | 5940 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 5941 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5942 | dstConfig->id = mId; | 
|  | 5943 | dstConfig->role = audio_is_output_device(mDeviceType) ? | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5944 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5945 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5946 | dstConfig->ext.device.type = mDeviceType; | 
|  | 5947 | dstConfig->ext.device.hw_module = mModule->mHandle; | 
|  | 5948 | strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5949 | } | 
|  | 5950 |  | 
|  | 5951 | void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const | 
|  | 5952 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5953 | ALOGV("DeviceVector::toAudioPort() handle %d type %x", mId, mDeviceType); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5954 | AudioPort::toAudioPort(port); | 
|  | 5955 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5956 | toAudioPortConfig(&port->active_config); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5957 | port->ext.device.type = mDeviceType; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5958 | port->ext.device.hw_module = mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5959 | strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); | 
|  | 5960 | } | 
|  | 5961 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5962 | status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5963 | { | 
|  | 5964 | const size_t SIZE = 256; | 
|  | 5965 | char buffer[SIZE]; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5966 | String8 result; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5967 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5968 | snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); | 
|  | 5969 | result.append(buffer); | 
|  | 5970 | if (mId != 0) { | 
|  | 5971 | snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); | 
|  | 5972 | result.append(buffer); | 
|  | 5973 | } | 
|  | 5974 | snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", | 
|  | 5975 | enumToString(sDeviceNameToEnumTable, | 
|  | 5976 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 5977 | mDeviceType)); | 
|  | 5978 | result.append(buffer); | 
|  | 5979 | if (mAddress.size() != 0) { | 
|  | 5980 | snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); | 
|  | 5981 | result.append(buffer); | 
|  | 5982 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5983 | write(fd, result.string(), result.size()); | 
|  | 5984 | AudioPort::dump(fd, spaces); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5985 |  | 
|  | 5986 | return NO_ERROR; | 
|  | 5987 | } | 
|  | 5988 |  | 
|  | 5989 |  | 
|  | 5990 | // --- audio_policy.conf file parsing | 
|  | 5991 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5992 | audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5993 | { | 
|  | 5994 | uint32_t flag = 0; | 
|  | 5995 |  | 
|  | 5996 | // it is OK to cast name to non const here as we are not going to use it after | 
|  | 5997 | // strtok() modifies it | 
|  | 5998 | char *flagName = strtok(name, "|"); | 
|  | 5999 | while (flagName != NULL) { | 
|  | 6000 | if (strlen(flagName) != 0) { | 
|  | 6001 | flag |= stringToEnum(sFlagNameToEnumTable, | 
|  | 6002 | ARRAY_SIZE(sFlagNameToEnumTable), | 
|  | 6003 | flagName); | 
|  | 6004 | } | 
|  | 6005 | flagName = strtok(NULL, "|"); | 
|  | 6006 | } | 
|  | 6007 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 6008 | // and all common behaviors are driven by checking only the direct flag | 
|  | 6009 | // this should normally be set appropriately in the policy configuration file | 
|  | 6010 | if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 6011 | flag |= AUDIO_OUTPUT_FLAG_DIRECT; | 
|  | 6012 | } | 
|  | 6013 |  | 
|  | 6014 | return (audio_output_flags_t)flag; | 
|  | 6015 | } | 
|  | 6016 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6017 | audio_devices_t AudioPolicyManager::parseDeviceNames(char *name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6018 | { | 
|  | 6019 | uint32_t device = 0; | 
|  | 6020 |  | 
|  | 6021 | char *devName = strtok(name, "|"); | 
|  | 6022 | while (devName != NULL) { | 
|  | 6023 | if (strlen(devName) != 0) { | 
|  | 6024 | device |= stringToEnum(sDeviceNameToEnumTable, | 
|  | 6025 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6026 | devName); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6027 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6028 | devName = strtok(NULL, "|"); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6029 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6030 | return device; | 
|  | 6031 | } | 
|  | 6032 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6033 | void AudioPolicyManager::loadHwModule(cnode *root) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6034 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6035 | status_t status = NAME_NOT_FOUND; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6036 | cnode *node; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6037 | sp<HwModule> module = new HwModule(root->name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6038 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6039 | node = config_find(root, DEVICES_TAG); | 
|  | 6040 | if (node != NULL) { | 
|  | 6041 | node = node->first_child; | 
|  | 6042 | while (node) { | 
|  | 6043 | ALOGV("loadHwModule() loading device %s", node->name); | 
|  | 6044 | status_t tmpStatus = module->loadDevice(node); | 
|  | 6045 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6046 | status = tmpStatus; | 
|  | 6047 | } | 
|  | 6048 | node = node->next; | 
|  | 6049 | } | 
|  | 6050 | } | 
|  | 6051 | node = config_find(root, OUTPUTS_TAG); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6052 | if (node != NULL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6053 | node = node->first_child; | 
|  | 6054 | while (node) { | 
|  | 6055 | ALOGV("loadHwModule() loading output %s", node->name); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6056 | status_t tmpStatus = module->loadOutput(node); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6057 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6058 | status = tmpStatus; | 
|  | 6059 | } | 
|  | 6060 | node = node->next; | 
|  | 6061 | } | 
|  | 6062 | } | 
|  | 6063 | node = config_find(root, INPUTS_TAG); | 
|  | 6064 | if (node != NULL) { | 
|  | 6065 | node = node->first_child; | 
|  | 6066 | while (node) { | 
|  | 6067 | ALOGV("loadHwModule() loading input %s", node->name); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6068 | status_t tmpStatus = module->loadInput(node); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6069 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6070 | status = tmpStatus; | 
|  | 6071 | } | 
|  | 6072 | node = node->next; | 
|  | 6073 | } | 
|  | 6074 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6075 | loadGlobalConfig(root, module); | 
|  | 6076 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6077 | if (status == NO_ERROR) { | 
|  | 6078 | mHwModules.add(module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6079 | } | 
|  | 6080 | } | 
|  | 6081 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6082 | void AudioPolicyManager::loadHwModules(cnode *root) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6083 | { | 
|  | 6084 | cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); | 
|  | 6085 | if (node == NULL) { | 
|  | 6086 | return; | 
|  | 6087 | } | 
|  | 6088 |  | 
|  | 6089 | node = node->first_child; | 
|  | 6090 | while (node) { | 
|  | 6091 | ALOGV("loadHwModules() loading module %s", node->name); | 
|  | 6092 | loadHwModule(node); | 
|  | 6093 | node = node->next; | 
|  | 6094 | } | 
|  | 6095 | } | 
|  | 6096 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6097 | void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6098 | { | 
|  | 6099 | cnode *node = config_find(root, GLOBAL_CONFIG_TAG); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 6100 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6101 | if (node == NULL) { | 
|  | 6102 | return; | 
|  | 6103 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6104 | DeviceVector declaredDevices; | 
|  | 6105 | if (module != NULL) { | 
|  | 6106 | declaredDevices = module->mDeclaredDevices; | 
|  | 6107 | } | 
|  | 6108 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6109 | node = node->first_child; | 
|  | 6110 | while (node) { | 
|  | 6111 | if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6112 | mAvailableOutputDevices.loadDevicesFromName((char *)node->value, | 
|  | 6113 | declaredDevices); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6114 | ALOGV("loadGlobalConfig() Attached Output Devices %08x", | 
|  | 6115 | mAvailableOutputDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6116 | } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6117 | audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6118 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6119 | (char *)node->value); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6120 | if (device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6121 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), device); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6122 | } else { | 
|  | 6123 | ALOGW("loadGlobalConfig() default device not specified"); | 
|  | 6124 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6125 | ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6126 | } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6127 | mAvailableInputDevices.loadDevicesFromName((char *)node->value, | 
|  | 6128 | declaredDevices); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6129 | ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6130 | } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) { | 
|  | 6131 | mSpeakerDrcEnabled = stringToBool((char *)node->value); | 
|  | 6132 | ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 6133 | } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) { | 
|  | 6134 | uint32_t major, minor; | 
|  | 6135 | sscanf((char *)node->value, "%u.%u", &major, &minor); | 
|  | 6136 | module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor); | 
|  | 6137 | ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u", | 
|  | 6138 | module->mHalVersion, major, minor); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6139 | } | 
|  | 6140 | node = node->next; | 
|  | 6141 | } | 
|  | 6142 | } | 
|  | 6143 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6144 | status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6145 | { | 
|  | 6146 | cnode *root; | 
|  | 6147 | char *data; | 
|  | 6148 |  | 
|  | 6149 | data = (char *)load_file(path, NULL); | 
|  | 6150 | if (data == NULL) { | 
|  | 6151 | return -ENODEV; | 
|  | 6152 | } | 
|  | 6153 | root = config_node("", ""); | 
|  | 6154 | config_load(root, data); | 
|  | 6155 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6156 | loadHwModules(root); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6157 | // legacy audio_policy.conf files have one global_configuration section | 
|  | 6158 | loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6159 | config_free(root); | 
|  | 6160 | free(root); | 
|  | 6161 | free(data); | 
|  | 6162 |  | 
|  | 6163 | ALOGI("loadAudioPolicyConfig() loaded %s\n", path); | 
|  | 6164 |  | 
|  | 6165 | return NO_ERROR; | 
|  | 6166 | } | 
|  | 6167 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6168 | void AudioPolicyManager::defaultAudioPolicyConfig(void) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6169 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6170 | sp<HwModule> module; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6171 | sp<IOProfile> profile; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6172 | sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""), | 
|  | 6173 | AUDIO_DEVICE_IN_BUILTIN_MIC); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6174 | mAvailableOutputDevices.add(mDefaultOutputDevice); | 
|  | 6175 | mAvailableInputDevices.add(defaultInputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6176 |  | 
|  | 6177 | module = new HwModule("primary"); | 
|  | 6178 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6179 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6180 | profile->mSamplingRates.add(44100); | 
|  | 6181 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); | 
|  | 6182 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6183 | profile->mSupportedDevices.add(mDefaultOutputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6184 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; | 
|  | 6185 | module->mOutputProfiles.add(profile); | 
|  | 6186 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6187 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6188 | profile->mSamplingRates.add(8000); | 
|  | 6189 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); | 
|  | 6190 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6191 | profile->mSupportedDevices.add(defaultInputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6192 | module->mInputProfiles.add(profile); | 
|  | 6193 |  | 
|  | 6194 | mHwModules.add(module); | 
|  | 6195 | } | 
|  | 6196 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 6197 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 6198 | { | 
|  | 6199 | // flags to stream type mapping | 
|  | 6200 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 6201 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 6202 | } | 
|  | 6203 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 6204 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 6205 | } | 
|  | 6206 |  | 
|  | 6207 | // usage to stream type mapping | 
|  | 6208 | switch (attr->usage) { | 
|  | 6209 | case AUDIO_USAGE_MEDIA: | 
|  | 6210 | case AUDIO_USAGE_GAME: | 
|  | 6211 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 6212 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 6213 | return AUDIO_STREAM_MUSIC; | 
|  | 6214 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 6215 | return AUDIO_STREAM_SYSTEM; | 
|  | 6216 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 6217 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 6218 |  | 
|  | 6219 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 6220 | return AUDIO_STREAM_DTMF; | 
|  | 6221 |  | 
|  | 6222 | case AUDIO_USAGE_ALARM: | 
|  | 6223 | return AUDIO_STREAM_ALARM; | 
|  | 6224 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 6225 | return AUDIO_STREAM_RING; | 
|  | 6226 |  | 
|  | 6227 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 6228 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 6229 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 6230 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 6231 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 6232 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 6233 |  | 
|  | 6234 | case AUDIO_USAGE_UNKNOWN: | 
|  | 6235 | default: | 
|  | 6236 | return AUDIO_STREAM_MUSIC; | 
|  | 6237 | } | 
|  | 6238 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6239 | }; // namespace android |