| 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 | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 33 | // A device mask for all audio input and output devices where matching inputs/outputs on device | 
|  | 34 | // type alone is not enough: the address must match too | 
|  | 35 | #define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \ | 
|  | 36 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 37 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 38 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 39 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 40 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 41 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 42 | #include <utils/Log.h> | 
|  | 43 | #include <hardware/audio.h> | 
|  | 44 | #include <hardware/audio_effect.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | #include <media/AudioParameter.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 46 | #include <soundtrigger/SoundTrigger.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 47 | #include "AudioPolicyManager.h" | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 48 | #include "audio_policy_conf.h" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 49 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 50 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 53 | // Definitions for audio_policy.conf file parsing | 
|  | 54 | // ---------------------------------------------------------------------------- | 
|  | 55 |  | 
|  | 56 | struct StringToEnum { | 
|  | 57 | const char *name; | 
|  | 58 | uint32_t value; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | #define STRING_TO_ENUM(string) { #string, string } | 
|  | 62 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) | 
|  | 63 |  | 
|  | 64 | const StringToEnum sDeviceNameToEnumTable[] = { | 
|  | 65 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE), | 
|  | 66 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER), | 
|  | 67 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET), | 
|  | 68 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), | 
|  | 69 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO), | 
|  | 70 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET), | 
|  | 71 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT), | 
|  | 72 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO), | 
|  | 73 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP), | 
|  | 74 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES), | 
|  | 75 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), | 
|  | 76 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP), | 
|  | 77 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 78 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 79 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET), | 
|  | 80 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET), | 
|  | 81 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY), | 
|  | 82 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE), | 
|  | 83 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB), | 
|  | 84 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 85 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX), | 
|  | 86 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE), | 
|  | 87 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC), | 
|  | 88 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF), | 
|  | 89 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM), | 
| Eric Laurent | e1d37b7 | 2014-07-29 10:26:26 -0700 | [diff] [blame] | 90 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_LINE), | 
| Eric Laurent | a57ab8d | 2014-07-30 10:01:42 -0500 | [diff] [blame] | 91 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AMBIENT), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 92 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC), | 
|  | 93 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET), | 
|  | 94 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO), | 
|  | 95 | STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET), | 
|  | 96 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 97 | STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 98 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX), | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 99 | STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 100 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC), | 
|  | 101 | STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX), | 
|  | 102 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET), | 
|  | 103 | STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET), | 
|  | 104 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY), | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 105 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE), | 
| Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 106 | STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER), | 
|  | 107 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER), | 
|  | 108 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE), | 
|  | 109 | STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF), | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 110 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP), | 
| Terry Heo | 7999a22 | 2014-06-27 15:23:36 +0900 | [diff] [blame] | 111 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 112 | }; | 
|  | 113 |  | 
|  | 114 | const StringToEnum sFlagNameToEnumTable[] = { | 
|  | 115 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), | 
|  | 116 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), | 
|  | 117 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), | 
|  | 118 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), | 
|  | 119 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), | 
|  | 120 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 121 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | const StringToEnum sFormatNameToEnumTable[] = { | 
|  | 125 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), | 
|  | 126 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), | 
|  | 127 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT), | 
|  | 128 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT), | 
|  | 129 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT), | 
|  | 130 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED), | 
|  | 131 | STRING_TO_ENUM(AUDIO_FORMAT_MP3), | 
|  | 132 | STRING_TO_ENUM(AUDIO_FORMAT_AAC), | 
| aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 133 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN), | 
|  | 134 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC), | 
|  | 135 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR), | 
|  | 136 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP), | 
|  | 137 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1), | 
|  | 138 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE), | 
|  | 139 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC), | 
|  | 140 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD), | 
|  | 141 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2), | 
|  | 142 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 143 | STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), | 
| Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 144 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1), | 
|  | 145 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2), | 
|  | 146 | STRING_TO_ENUM(AUDIO_FORMAT_OPUS), | 
|  | 147 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), | 
|  | 148 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | }; | 
|  | 150 |  | 
|  | 151 | const StringToEnum sOutChannelsNameToEnumTable[] = { | 
|  | 152 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO), | 
|  | 153 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), | 
| Andy Hung | 3a0fe12 | 2014-07-29 17:56:46 -0700 | [diff] [blame] | 154 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 155 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), | 
|  | 156 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), | 
|  | 157 | }; | 
|  | 158 |  | 
|  | 159 | const StringToEnum sInChannelsNameToEnumTable[] = { | 
|  | 160 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), | 
|  | 161 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), | 
|  | 162 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), | 
|  | 163 | }; | 
|  | 164 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 165 | const StringToEnum sGainModeNameToEnumTable[] = { | 
|  | 166 | STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT), | 
|  | 167 | STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS), | 
|  | 168 | STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP), | 
|  | 169 | }; | 
|  | 170 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 171 |  | 
|  | 172 | uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table, | 
|  | 173 | size_t size, | 
|  | 174 | const char *name) | 
|  | 175 | { | 
|  | 176 | for (size_t i = 0; i < size; i++) { | 
|  | 177 | if (strcmp(table[i].name, name) == 0) { | 
|  | 178 | ALOGV("stringToEnum() found %s", table[i].name); | 
|  | 179 | return table[i].value; | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 | return 0; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | const char *AudioPolicyManager::enumToString(const struct StringToEnum *table, | 
|  | 186 | size_t size, | 
|  | 187 | uint32_t value) | 
|  | 188 | { | 
|  | 189 | for (size_t i = 0; i < size; i++) { | 
|  | 190 | if (table[i].value == value) { | 
|  | 191 | return table[i].name; | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 | return ""; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | bool AudioPolicyManager::stringToBool(const char *value) | 
|  | 198 | { | 
|  | 199 | return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0)); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 |  | 
|  | 203 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 | // AudioPolicyInterface implementation | 
|  | 205 | // ---------------------------------------------------------------------------- | 
|  | 206 |  | 
|  | 207 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 208 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 209 | audio_policy_dev_state_t state, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | const char *device_address) | 
|  | 211 | { | 
| Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 212 | String8 address = (device_address == NULL) ? String8("") : String8(device_address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 |  | 
| Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 214 | ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", | 
|  | 215 | device, state, address.string()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | // connect/disconnect only 1 device at a time | 
|  | 218 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 219 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | // handle output devices | 
|  | 221 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 222 | SortedVector <audio_io_handle_t> outputs; | 
|  | 223 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 224 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
|  | 225 | devDesc->mAddress = address; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 226 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 227 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 229 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 230 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 231 | switch (state) | 
|  | 232 | { | 
|  | 233 | // handle output device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 234 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 235 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 236 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 237 | return INVALID_OPERATION; | 
|  | 238 | } | 
|  | 239 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 240 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 241 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 242 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 243 | if (index >= 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 244 | sp<HwModule> module = getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 245 | if (module == 0) { | 
|  | 246 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
|  | 247 | device); | 
|  | 248 | mAvailableOutputDevices.remove(devDesc); | 
|  | 249 | return INVALID_OPERATION; | 
|  | 250 | } | 
|  | 251 | mAvailableOutputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 252 | mAvailableOutputDevices[index]->mModule = module; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | } else { | 
|  | 254 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 257 | if (checkOutputsForDevice(devDesc, state, outputs, address) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 258 | mAvailableOutputDevices.remove(devDesc); | 
|  | 259 | return INVALID_OPERATION; | 
|  | 260 | } | 
|  | 261 | // outputs should never be empty here | 
|  | 262 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 263 | "checkOutputsForDevice() returned no outputs but status OK"); | 
|  | 264 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
|  | 265 | outputs.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | break; | 
|  | 267 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 268 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 269 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 270 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 271 | return INVALID_OPERATION; | 
|  | 272 | } | 
|  | 273 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 274 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
|  | 275 |  | 
|  | 276 | // Set Disconnect to HALs | 
|  | 277 | AudioParameter param = AudioParameter(address); | 
|  | 278 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 279 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 280 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 281 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 282 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 283 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 284 | checkOutputsForDevice(devDesc, state, outputs, address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 285 | } break; | 
|  | 286 |  | 
|  | 287 | default: | 
|  | 288 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 289 | return BAD_VALUE; | 
|  | 290 | } | 
|  | 291 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 292 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 293 | // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 294 | checkA2dpSuspend(); | 
|  | 295 | checkOutputForAllStrategies(); | 
|  | 296 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 297 | if (!outputs.isEmpty()) { | 
|  | 298 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 299 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 301 | // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 302 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 304 | (desc->mDirectOpenCount == 0))) { | 
|  | 305 | closeOutput(outputs[i]); | 
|  | 306 | } | 
|  | 307 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 308 | // check again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 309 | checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
|  | 312 | updateDevicesAndOutputs(); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 313 | if (mPhoneState == AUDIO_MODE_IN_CALL) { | 
|  | 314 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 315 | updateCallRouting(newDevice); | 
|  | 316 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 317 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 318 | audio_io_handle_t output = mOutputs.keyAt(i); | 
|  | 319 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { | 
|  | 320 | audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i), | 
|  | 321 | true /*fromCache*/); | 
|  | 322 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 323 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 324 | // a valid device selection on those outputs. | 
|  | 325 | bool force = !mOutputs.valueAt(i)->isDuplicated() | 
|  | 326 | && (!deviceDistinguishesOnAddress(device) | 
|  | 327 | // always force when disconnecting (a non-duplicated device) | 
|  | 328 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
|  | 329 | setOutputDevice(output, newDevice, force, 0); | 
|  | 330 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 333 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 334 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 335 | }  // end if is output device | 
|  | 336 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 337 | // handle input devices | 
|  | 338 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 339 | SortedVector <audio_io_handle_t> inputs; | 
|  | 340 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 341 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
|  | 342 | devDesc->mAddress = address; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 343 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 344 | switch (state) | 
|  | 345 | { | 
|  | 346 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 347 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 348 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 349 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 350 | return INVALID_OPERATION; | 
|  | 351 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 352 | sp<HwModule> module = getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 353 | if (module == NULL) { | 
|  | 354 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 355 | device); | 
|  | 356 | return INVALID_OPERATION; | 
|  | 357 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 358 | if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) { | 
|  | 359 | return INVALID_OPERATION; | 
|  | 360 | } | 
|  | 361 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 362 | index = mAvailableInputDevices.add(devDesc); | 
|  | 363 | if (index >= 0) { | 
|  | 364 | mAvailableInputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 365 | mAvailableInputDevices[index]->mModule = module; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 366 | } else { | 
|  | 367 | return NO_MEMORY; | 
|  | 368 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 369 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 370 |  | 
|  | 371 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 372 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 373 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 374 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 375 | return INVALID_OPERATION; | 
|  | 376 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 377 |  | 
|  | 378 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
|  | 379 |  | 
|  | 380 | // Set Disconnect to HALs | 
|  | 381 | AudioParameter param = AudioParameter(address); | 
|  | 382 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
|  | 383 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 384 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 385 | checkInputsForDevice(device, state, inputs, address); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 386 | mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 387 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 388 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 389 |  | 
|  | 390 | default: | 
|  | 391 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 392 | return BAD_VALUE; | 
|  | 393 | } | 
|  | 394 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 395 | closeAllInputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 396 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 397 | if (mPhoneState == AUDIO_MODE_IN_CALL) { | 
|  | 398 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 399 | updateCallRouting(newDevice); | 
|  | 400 | } | 
|  | 401 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 402 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 403 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 404 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 405 |  | 
|  | 406 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 407 | return BAD_VALUE; | 
|  | 408 | } | 
|  | 409 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 410 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 411 | const char *device_address) | 
|  | 412 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 413 | audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 414 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); | 
| Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 415 | devDesc->mAddress = (device_address == NULL) ? String8("") : String8(device_address); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 416 | ssize_t index; | 
|  | 417 | DeviceVector *deviceVector; | 
|  | 418 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 419 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 420 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 421 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 422 | deviceVector = &mAvailableInputDevices; | 
|  | 423 | } else { | 
|  | 424 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 425 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 426 | } | 
|  | 427 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 428 | index = deviceVector->indexOf(devDesc); | 
|  | 429 | if (index >= 0) { | 
|  | 430 | return AUDIO_POLICY_DEVICE_STATE_AVAILABLE; | 
|  | 431 | } else { | 
|  | 432 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 433 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 436 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) | 
|  | 437 | { | 
|  | 438 | bool createTxPatch = false; | 
|  | 439 | struct audio_patch patch; | 
|  | 440 | patch.num_sources = 1; | 
|  | 441 | patch.num_sinks = 1; | 
|  | 442 | status_t status; | 
|  | 443 | audio_patch_handle_t afPatchHandle; | 
|  | 444 | DeviceVector deviceList; | 
|  | 445 |  | 
|  | 446 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
|  | 447 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
|  | 448 |  | 
|  | 449 | // release existing RX patch if any | 
|  | 450 | if (mCallRxPatch != 0) { | 
|  | 451 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 452 | mCallRxPatch.clear(); | 
|  | 453 | } | 
|  | 454 | // release TX patch if any | 
|  | 455 | if (mCallTxPatch != 0) { | 
|  | 456 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 457 | mCallTxPatch.clear(); | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
|  | 461 | // via setOutputDevice() on primary output. | 
|  | 462 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 463 | if (availablePrimaryOutputDevices() & rxDevice) { | 
|  | 464 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
|  | 465 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 466 | // of it due to legacy implementation. If not, create a patch. | 
|  | 467 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
|  | 468 | == AUDIO_DEVICE_NONE) { | 
|  | 469 | createTxPatch = true; | 
|  | 470 | } | 
|  | 471 | } else { | 
|  | 472 | // create RX path audio patch | 
|  | 473 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); | 
|  | 474 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 475 | "updateCallRouting() selected device not in output device list"); | 
|  | 476 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 477 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); | 
|  | 478 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 479 | "updateCallRouting() no telephony RX device"); | 
|  | 480 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 481 |  | 
|  | 482 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 483 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 484 |  | 
|  | 485 | // request to reuse existing output stream if one is already opened to reach the RX device | 
|  | 486 | SortedVector<audio_io_handle_t> outputs = | 
|  | 487 | getOutputsForDevice(rxDevice, mOutputs); | 
|  | 488 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE); | 
|  | 489 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 490 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 491 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 492 | "updateCallRouting() RX device output is duplicated"); | 
|  | 493 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
|  | 494 | patch.num_sources = 2; | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 498 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); | 
|  | 499 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", | 
|  | 500 | status); | 
|  | 501 | if (status == NO_ERROR) { | 
|  | 502 | mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 503 | &patch, mUidCached); | 
|  | 504 | mCallRxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 505 | mCallRxPatch->mUid = mUidCached; | 
|  | 506 | } | 
|  | 507 | createTxPatch = true; | 
|  | 508 | } | 
|  | 509 | if (createTxPatch) { | 
|  | 510 |  | 
|  | 511 | struct audio_patch patch; | 
|  | 512 | patch.num_sources = 1; | 
|  | 513 | patch.num_sinks = 1; | 
|  | 514 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); | 
|  | 515 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 516 | "updateCallRouting() selected device not in input device list"); | 
|  | 517 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 518 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 519 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); | 
|  | 520 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 521 | "updateCallRouting() no telephony TX device"); | 
|  | 522 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 523 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 524 |  | 
|  | 525 | SortedVector<audio_io_handle_t> outputs = | 
|  | 526 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); | 
|  | 527 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE); | 
|  | 528 | // request to reuse existing output stream if one is already opened to reach the TX | 
|  | 529 | // path output device | 
|  | 530 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 531 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 532 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 533 | "updateCallRouting() RX device output is duplicated"); | 
|  | 534 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
|  | 535 | patch.num_sources = 2; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 539 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); | 
|  | 540 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", | 
|  | 541 | status); | 
|  | 542 | if (status == NO_ERROR) { | 
|  | 543 | mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 544 | &patch, mUidCached); | 
|  | 545 | mCallTxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 546 | mCallTxPatch->mUid = mUidCached; | 
|  | 547 | } | 
|  | 548 | } | 
|  | 549 | } | 
|  | 550 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 551 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 552 | { | 
|  | 553 | ALOGV("setPhoneState() state %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 554 | if (state < 0 || state >= AUDIO_MODE_CNT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 555 | ALOGW("setPhoneState() invalid state %d", state); | 
|  | 556 | return; | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | if (state == mPhoneState ) { | 
|  | 560 | ALOGW("setPhoneState() setting same state %d", state); | 
|  | 561 | return; | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | // if leaving call state, handle special case of active streams | 
|  | 565 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 566 | if (isInCall()) { | 
|  | 567 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 568 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 569 | handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | } | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | // store previous phone state for management of sonification strategy below | 
|  | 574 | int oldState = mPhoneState; | 
|  | 575 | mPhoneState = state; | 
|  | 576 | bool force = false; | 
|  | 577 |  | 
|  | 578 | // are we entering or starting a call | 
|  | 579 | if (!isStateInCall(oldState) && isStateInCall(state)) { | 
|  | 580 | ALOGV("  Entering call in setPhoneState()"); | 
|  | 581 | // force routing command to audio hardware when starting a call | 
|  | 582 | // even if no device change is needed | 
|  | 583 | force = true; | 
|  | 584 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 585 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = | 
|  | 586 | sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; | 
|  | 587 | } | 
|  | 588 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { | 
|  | 589 | ALOGV("  Exiting call in setPhoneState()"); | 
|  | 590 | // force routing command to audio hardware when exiting a call | 
|  | 591 | // even if no device change is needed | 
|  | 592 | force = true; | 
|  | 593 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 594 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = | 
|  | 595 | sVolumeProfiles[AUDIO_STREAM_DTMF][j]; | 
|  | 596 | } | 
|  | 597 | } else if (isStateInCall(state) && (state != oldState)) { | 
|  | 598 | ALOGV("  Switching between telephony and VoIP in setPhoneState()"); | 
|  | 599 | // force routing command to audio hardware when switching between telephony and VoIP | 
|  | 600 | // even if no device change is needed | 
|  | 601 | force = true; | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | // check for device and output changes triggered by new phone state | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 605 | checkA2dpSuspend(); | 
|  | 606 | checkOutputForAllStrategies(); | 
|  | 607 | updateDevicesAndOutputs(); | 
|  | 608 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 609 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 610 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 611 | int delayMs = 0; | 
|  | 612 | if (isStateInCall(state)) { | 
|  | 613 | nsecs_t sysTime = systemTime(); | 
|  | 614 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 615 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 617 | // latency of any output where either strategy is active. | 
|  | 618 | // This avoid sending the ring tone or music tail into the earpiece or headset. | 
|  | 619 | if ((desc->isStrategyActive(STRATEGY_MEDIA, | 
|  | 620 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 621 | sysTime) || | 
|  | 622 | desc->isStrategyActive(STRATEGY_SONIFICATION, | 
|  | 623 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 624 | sysTime)) && | 
|  | 625 | (delayMs < (int)desc->mLatency*2)) { | 
|  | 626 | delayMs = desc->mLatency*2; | 
|  | 627 | } | 
|  | 628 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); | 
|  | 629 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, | 
|  | 630 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
|  | 631 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); | 
|  | 632 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, | 
|  | 633 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 634 | } | 
|  | 635 | } | 
|  | 636 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 637 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
|  | 638 | // the device returned is not necessarily reachable via this output | 
|  | 639 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 640 | // force routing command to audio hardware when ending call | 
|  | 641 | // even if no device change is needed | 
|  | 642 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
|  | 643 | rxDevice = hwOutputDesc->device(); | 
|  | 644 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 645 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 646 | if (state == AUDIO_MODE_IN_CALL) { | 
|  | 647 | updateCallRouting(rxDevice, delayMs); | 
|  | 648 | } else if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 649 | if (mCallRxPatch != 0) { | 
|  | 650 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 651 | mCallRxPatch.clear(); | 
|  | 652 | } | 
|  | 653 | if (mCallTxPatch != 0) { | 
|  | 654 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 655 | mCallTxPatch.clear(); | 
|  | 656 | } | 
|  | 657 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 658 | } else { | 
|  | 659 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 660 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 661 | // if entering in call state, handle special case of active streams | 
|  | 662 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 663 | if (isStateInCall(state)) { | 
|  | 664 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 665 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 666 | handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 667 | } | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | // 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] | 671 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 672 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | mLimitRingtoneVolume = true; | 
|  | 674 | } else { | 
|  | 675 | mLimitRingtoneVolume = false; | 
|  | 676 | } | 
|  | 677 | } | 
|  | 678 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 679 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 680 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 681 | { | 
|  | 682 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); | 
|  | 683 |  | 
|  | 684 | bool forceVolumeReeval = false; | 
|  | 685 | switch(usage) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 686 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: | 
|  | 687 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && | 
|  | 688 | config != AUDIO_POLICY_FORCE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 689 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); | 
|  | 690 | return; | 
|  | 691 | } | 
|  | 692 | forceVolumeReeval = true; | 
|  | 693 | mForceUse[usage] = config; | 
|  | 694 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 695 | case AUDIO_POLICY_FORCE_FOR_MEDIA: | 
|  | 696 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && | 
|  | 697 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 698 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && | 
|  | 699 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && | 
| Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 700 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 701 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); | 
|  | 702 | return; | 
|  | 703 | } | 
|  | 704 | mForceUse[usage] = config; | 
|  | 705 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 706 | case AUDIO_POLICY_FORCE_FOR_RECORD: | 
|  | 707 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 708 | config != AUDIO_POLICY_FORCE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 709 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); | 
|  | 710 | return; | 
|  | 711 | } | 
|  | 712 | mForceUse[usage] = config; | 
|  | 713 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 714 | case AUDIO_POLICY_FORCE_FOR_DOCK: | 
|  | 715 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && | 
|  | 716 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && | 
|  | 717 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && | 
|  | 718 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && | 
|  | 719 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 720 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); | 
|  | 721 | } | 
|  | 722 | forceVolumeReeval = true; | 
|  | 723 | mForceUse[usage] = config; | 
|  | 724 | break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 725 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: | 
|  | 726 | if (config != AUDIO_POLICY_FORCE_NONE && | 
|  | 727 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 728 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); | 
|  | 729 | } | 
|  | 730 | forceVolumeReeval = true; | 
|  | 731 | mForceUse[usage] = config; | 
|  | 732 | break; | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 733 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: | 
|  | 734 | if (config != AUDIO_POLICY_FORCE_NONE && | 
|  | 735 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { | 
|  | 736 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); | 
|  | 737 | } | 
|  | 738 | mForceUse[usage] = config; | 
|  | 739 | break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 740 | default: | 
|  | 741 | ALOGW("setForceUse() invalid usage %d", usage); | 
|  | 742 | break; | 
|  | 743 | } | 
|  | 744 |  | 
|  | 745 | // check for device and output changes triggered by new force usage | 
|  | 746 | checkA2dpSuspend(); | 
|  | 747 | checkOutputForAllStrategies(); | 
|  | 748 | updateDevicesAndOutputs(); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 749 | if (mPhoneState == AUDIO_MODE_IN_CALL) { | 
|  | 750 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
|  | 751 | updateCallRouting(newDevice); | 
|  | 752 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 753 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 754 | audio_io_handle_t output = mOutputs.keyAt(i); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 755 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 756 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { | 
|  | 757 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); | 
|  | 758 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 759 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
|  | 760 | applyStreamVolumes(output, newDevice, 0, true); | 
|  | 761 | } | 
|  | 762 | } | 
|  | 763 |  | 
|  | 764 | audio_io_handle_t activeInput = getActiveInput(); | 
|  | 765 | if (activeInput != 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 766 | setInputDevice(activeInput, getNewInputDevice(activeInput)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 767 | } | 
|  | 768 |  | 
|  | 769 | } | 
|  | 770 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 771 | 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] | 772 | { | 
|  | 773 | return mForceUse[usage]; | 
|  | 774 | } | 
|  | 775 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 776 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 777 | { | 
|  | 778 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 779 | } | 
|  | 780 |  | 
|  | 781 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 782 | // not explicitly request a direct output | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 783 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 784 | audio_devices_t device, | 
|  | 785 | uint32_t samplingRate, | 
|  | 786 | audio_format_t format, | 
|  | 787 | audio_channel_mask_t channelMask, | 
|  | 788 | audio_output_flags_t flags) | 
|  | 789 | { | 
|  | 790 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 791 | if (mHwModules[i]->mHandle == 0) { | 
|  | 792 | continue; | 
|  | 793 | } | 
|  | 794 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 795 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 796 | bool found = profile->isCompatibleProfile(device, samplingRate, | 
|  | 797 | NULL /*updatedSamplingRate*/, format, channelMask, | 
|  | 798 | flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ? | 
|  | 799 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 800 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { | 
|  | 801 | return profile; | 
|  | 802 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 803 | } | 
|  | 804 | } | 
|  | 805 | return 0; | 
|  | 806 | } | 
|  | 807 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 808 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 809 | uint32_t samplingRate, | 
|  | 810 | audio_format_t format, | 
|  | 811 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 812 | audio_output_flags_t flags, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 813 | const audio_offload_info_t *offloadInfo) | 
|  | 814 | { | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 815 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 816 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 817 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 818 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 819 | device, stream, samplingRate, format, channelMask, flags); | 
|  | 820 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 821 | return getOutputForDevice(device, stream, samplingRate,format, channelMask, flags, | 
|  | 822 | offloadInfo); | 
|  | 823 | } | 
|  | 824 |  | 
|  | 825 | audio_io_handle_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 826 | uint32_t samplingRate, | 
|  | 827 | audio_format_t format, | 
|  | 828 | audio_channel_mask_t channelMask, | 
|  | 829 | audio_output_flags_t flags, | 
|  | 830 | const audio_offload_info_t *offloadInfo) | 
|  | 831 | { | 
|  | 832 | if (attr == NULL) { | 
|  | 833 | ALOGE("getOutputForAttr() called with NULL audio attributes"); | 
|  | 834 | return 0; | 
|  | 835 | } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 836 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x", | 
|  | 837 | attr->usage, attr->content_type, attr->tags, attr->flags); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 838 |  | 
|  | 839 | // TODO this is where filtering for custom policies (rerouting, dynamic sources) will go | 
|  | 840 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(attr); | 
|  | 841 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 842 |  | 
|  | 843 | if ((attr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
|  | 844 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
|  | 845 | } | 
|  | 846 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 847 | ALOGV("getOutputForAttr() device %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 848 | device, samplingRate, format, channelMask, flags); | 
|  | 849 |  | 
|  | 850 | audio_stream_type_t stream = streamTypefromAttributesInt(attr); | 
|  | 851 | return getOutputForDevice(device, stream, samplingRate, format, channelMask, flags, | 
|  | 852 | offloadInfo); | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 856 | audio_devices_t device, | 
|  | 857 | audio_stream_type_t stream, | 
|  | 858 | uint32_t samplingRate, | 
|  | 859 | audio_format_t format, | 
|  | 860 | audio_channel_mask_t channelMask, | 
|  | 861 | audio_output_flags_t flags, | 
|  | 862 | const audio_offload_info_t *offloadInfo) | 
|  | 863 | { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 864 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 865 | uint32_t latency = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 866 | status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 867 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 868 | #ifdef AUDIO_POLICY_TEST | 
|  | 869 | if (mCurOutput != 0) { | 
|  | 870 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
|  | 871 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
|  | 872 |  | 
|  | 873 | if (mTestOutputs[mCurOutput] == 0) { | 
|  | 874 | ALOGV("getOutput() opening test output"); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 875 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 876 | outputDesc->mDevice = mTestDevice; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 877 | outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 878 | outputDesc->mFlags = | 
|  | 879 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 880 | outputDesc->mRefCount[stream] = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 881 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 882 | config.sample_rate = mTestSamplingRate; | 
|  | 883 | config.channel_mask = mTestChannels; | 
|  | 884 | config.format = mTestFormat; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 885 | if (offloadInfo != NULL) { | 
|  | 886 | config.offload_info = *offloadInfo; | 
|  | 887 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 888 | status = mpClientInterface->openOutput(0, | 
|  | 889 | &mTestOutputs[mCurOutput], | 
|  | 890 | &config, | 
|  | 891 | &outputDesc->mDevice, | 
|  | 892 | String8(""), | 
|  | 893 | &outputDesc->mLatency, | 
|  | 894 | outputDesc->mFlags); | 
|  | 895 | if (status == NO_ERROR) { | 
|  | 896 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 897 | outputDesc->mFormat = config.format; | 
|  | 898 | outputDesc->mChannelMask = config.channel_mask; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 899 | AudioParameter outputCmd = AudioParameter(); | 
|  | 900 | outputCmd.addInt(String8("set_id"),mCurOutput); | 
|  | 901 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
|  | 902 | addOutput(mTestOutputs[mCurOutput], outputDesc); | 
|  | 903 | } | 
|  | 904 | } | 
|  | 905 | return mTestOutputs[mCurOutput]; | 
|  | 906 | } | 
|  | 907 | #endif //AUDIO_POLICY_TEST | 
|  | 908 |  | 
|  | 909 | // open a direct output if required by specified parameters | 
|  | 910 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 911 | // and all common behaviors are driven by checking only the direct flag | 
|  | 912 | // this should normally be set appropriately in the policy configuration file | 
|  | 913 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 914 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 915 | } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 916 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 917 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 918 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 919 |  | 
|  | 920 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 921 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 922 | // detects there is an active non offloadable effect. | 
|  | 923 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 924 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 925 | // in the background. | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 926 | sp<IOProfile> profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
|  | 928 | !isNonOffloadableEffectEnabled()) { | 
|  | 929 | profile = getProfileForDirectOutput(device, | 
|  | 930 | samplingRate, | 
|  | 931 | format, | 
|  | 932 | channelMask, | 
|  | 933 | (audio_output_flags_t)flags); | 
|  | 934 | } | 
|  | 935 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 936 | if (profile != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 937 | sp<AudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 938 |  | 
|  | 939 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 940 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 942 | outputDesc = desc; | 
|  | 943 | // reuse direct output if currently open and configured with same parameters | 
|  | 944 | if ((samplingRate == outputDesc->mSamplingRate) && | 
|  | 945 | (format == outputDesc->mFormat) && | 
|  | 946 | (channelMask == outputDesc->mChannelMask)) { | 
|  | 947 | outputDesc->mDirectOpenCount++; | 
|  | 948 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
|  | 949 | return mOutputs.keyAt(i); | 
|  | 950 | } | 
|  | 951 | } | 
|  | 952 | } | 
|  | 953 | // close direct output if currently open and configured with different parameters | 
|  | 954 | if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 955 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | } | 
|  | 957 | outputDesc = new AudioOutputDescriptor(profile); | 
|  | 958 | outputDesc->mDevice = device; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 959 | outputDesc->mLatency = 0; | 
|  | 960 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 961 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 962 | config.sample_rate = samplingRate; | 
|  | 963 | config.channel_mask = channelMask; | 
|  | 964 | config.format = format; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 965 | if (offloadInfo != NULL) { | 
|  | 966 | config.offload_info = *offloadInfo; | 
|  | 967 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 968 | status = mpClientInterface->openOutput(profile->mModule->mHandle, | 
|  | 969 | &output, | 
|  | 970 | &config, | 
|  | 971 | &outputDesc->mDevice, | 
|  | 972 | String8(""), | 
|  | 973 | &outputDesc->mLatency, | 
|  | 974 | outputDesc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 975 |  | 
|  | 976 | // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 977 | if (status != NO_ERROR || | 
|  | 978 | (samplingRate != 0 && samplingRate != config.sample_rate) || | 
|  | 979 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || | 
|  | 980 | (channelMask != 0 && channelMask != config.channel_mask)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
|  | 982 | "format %d %d, channelMask %04x %04x", output, samplingRate, | 
|  | 983 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
|  | 984 | outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 985 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 986 | mpClientInterface->closeOutput(output); | 
|  | 987 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 988 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 990 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 991 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 992 | outputDesc->mFormat = config.format; | 
|  | 993 | outputDesc->mRefCount[stream] = 0; | 
|  | 994 | outputDesc->mStopTime[stream] = 0; | 
|  | 995 | outputDesc->mDirectOpenCount = 1; | 
|  | 996 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 997 | audio_io_handle_t srcOutput = getOutputForEffect(); | 
|  | 998 | addOutput(output, outputDesc); | 
|  | 999 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 1000 | if (dstOutput == output) { | 
|  | 1001 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); | 
|  | 1002 | } | 
|  | 1003 | mPreviousOutputs = mOutputs; | 
|  | 1004 | ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1005 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1006 | return output; | 
|  | 1007 | } | 
|  | 1008 |  | 
|  | 1009 | // ignoring channel mask due to downmix capability in mixer | 
|  | 1010 |  | 
|  | 1011 | // open a non direct output | 
|  | 1012 |  | 
|  | 1013 | // for non direct outputs, only PCM is supported | 
|  | 1014 | if (audio_is_linear_pcm(format)) { | 
|  | 1015 | // get which output is suitable for the specified stream. The actual | 
|  | 1016 | // routing change will happen when startOutput() will be called | 
|  | 1017 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 1018 |  | 
|  | 1019 | output = selectOutput(outputs, flags); | 
|  | 1020 | } | 
|  | 1021 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
|  | 1022 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
|  | 1023 |  | 
|  | 1024 | ALOGV("getOutput() returns output %d", output); | 
|  | 1025 |  | 
|  | 1026 | return output; | 
|  | 1027 | } | 
|  | 1028 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1029 | 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] | 1030 | audio_output_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1031 | { | 
|  | 1032 | // select one output among several that provide a path to a particular device or set of | 
|  | 1033 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 1034 | // The priority is as follows: | 
|  | 1035 | // 1: the output with the highest number of requested policy flags | 
|  | 1036 | // 2: the primary output | 
|  | 1037 | // 3: the first output in the list | 
|  | 1038 |  | 
|  | 1039 | if (outputs.size() == 0) { | 
|  | 1040 | return 0; | 
|  | 1041 | } | 
|  | 1042 | if (outputs.size() == 1) { | 
|  | 1043 | return outputs[0]; | 
|  | 1044 | } | 
|  | 1045 |  | 
|  | 1046 | int maxCommonFlags = 0; | 
|  | 1047 | audio_io_handle_t outputFlags = 0; | 
|  | 1048 | audio_io_handle_t outputPrimary = 0; | 
|  | 1049 |  | 
|  | 1050 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1051 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1052 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1053 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1054 | if (commonFlags > maxCommonFlags) { | 
|  | 1055 | outputFlags = outputs[i]; | 
|  | 1056 | maxCommonFlags = commonFlags; | 
|  | 1057 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
|  | 1058 | } | 
|  | 1059 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 1060 | outputPrimary = outputs[i]; | 
|  | 1061 | } | 
|  | 1062 | } | 
|  | 1063 | } | 
|  | 1064 |  | 
|  | 1065 | if (outputFlags != 0) { | 
|  | 1066 | return outputFlags; | 
|  | 1067 | } | 
|  | 1068 | if (outputPrimary != 0) { | 
|  | 1069 | return outputPrimary; | 
|  | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | return outputs[0]; | 
|  | 1073 | } | 
|  | 1074 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1075 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1076 | audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | int session) | 
|  | 1078 | { | 
|  | 1079 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 1080 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1081 | if (index < 0) { | 
|  | 1082 | ALOGW("startOutput() unknown output %d", output); | 
|  | 1083 | return BAD_VALUE; | 
|  | 1084 | } | 
|  | 1085 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1086 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 |  | 
|  | 1088 | // increment usage count for this stream on the requested output: | 
|  | 1089 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1090 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 1091 | outputDesc->changeRefCount(stream, 1); | 
|  | 1092 |  | 
|  | 1093 | if (outputDesc->mRefCount[stream] == 1) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1094 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1095 | routing_strategy strategy = getStrategy(stream); | 
|  | 1096 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
|  | 1097 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 1098 | uint32_t waitMs = 0; | 
|  | 1099 | bool force = false; | 
|  | 1100 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1101 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | if (desc != outputDesc) { | 
|  | 1103 | // force a device change if any other output is managed by the same hw | 
|  | 1104 | // module and has a current device selection that differs from selected device. | 
|  | 1105 | // In this case, the audio HAL must receive the new device selection so that it can | 
|  | 1106 | // change the device currently selected by the other active output. | 
|  | 1107 | if (outputDesc->sharesHwModuleWith(desc) && | 
|  | 1108 | desc->device() != newDevice) { | 
|  | 1109 | force = true; | 
|  | 1110 | } | 
|  | 1111 | // wait for audio on other active outputs to be presented when starting | 
|  | 1112 | // a notification so that audio focus effect can propagate. | 
|  | 1113 | uint32_t latency = desc->latency(); | 
|  | 1114 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
|  | 1115 | waitMs = latency; | 
|  | 1116 | } | 
|  | 1117 | } | 
|  | 1118 | } | 
|  | 1119 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); | 
|  | 1120 |  | 
|  | 1121 | // handle special case for sonification while in call | 
|  | 1122 | if (isInCall()) { | 
|  | 1123 | handleIncallSonification(stream, true, false); | 
|  | 1124 | } | 
|  | 1125 |  | 
|  | 1126 | // apply volume rules for current stream and device if necessary | 
|  | 1127 | checkAndSetVolume(stream, | 
|  | 1128 | mStreams[stream].getVolumeIndex(newDevice), | 
|  | 1129 | output, | 
|  | 1130 | newDevice); | 
|  | 1131 |  | 
|  | 1132 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1133 | // routing | 
|  | 1134 | handleNotificationRoutingForStream(stream); | 
|  | 1135 | if (waitMs > muteWaitMs) { | 
|  | 1136 | usleep((waitMs - muteWaitMs) * 2 * 1000); | 
|  | 1137 | } | 
|  | 1138 | } | 
|  | 1139 | return NO_ERROR; | 
|  | 1140 | } | 
|  | 1141 |  | 
|  | 1142 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1143 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1144 | audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1145 | int session) | 
|  | 1146 | { | 
|  | 1147 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 1148 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1149 | if (index < 0) { | 
|  | 1150 | ALOGW("stopOutput() unknown output %d", output); | 
|  | 1151 | return BAD_VALUE; | 
|  | 1152 | } | 
|  | 1153 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1154 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1155 |  | 
|  | 1156 | // handle special case for sonification while in call | 
|  | 1157 | if (isInCall()) { | 
|  | 1158 | handleIncallSonification(stream, false, false); | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 1162 | // decrement usage count of this stream on the output | 
|  | 1163 | outputDesc->changeRefCount(stream, -1); | 
|  | 1164 | // store time at which the stream was stopped - see isStreamActive() | 
|  | 1165 | if (outputDesc->mRefCount[stream] == 0) { | 
|  | 1166 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1167 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1168 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 1169 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 1170 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 1171 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 1172 | // audio path (audio DSP, CODEC ...) | 
|  | 1173 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); | 
|  | 1174 |  | 
|  | 1175 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 1176 | // one being selected for this output | 
|  | 1177 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1178 | audio_io_handle_t curOutput = mOutputs.keyAt(i); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1179 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1180 | if (curOutput != output && | 
|  | 1181 | desc->isActive() && | 
|  | 1182 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 1183 | (newDevice != desc->device())) { | 
|  | 1184 | setOutputDevice(curOutput, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1185 | getNewOutputDevice(curOutput, false /*fromCache*/), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1186 | true, | 
|  | 1187 | outputDesc->mLatency*2); | 
|  | 1188 | } | 
|  | 1189 | } | 
|  | 1190 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1191 | handleNotificationRoutingForStream(stream); | 
|  | 1192 | } | 
|  | 1193 | return NO_ERROR; | 
|  | 1194 | } else { | 
|  | 1195 | ALOGW("stopOutput() refcount is already 0 for output %d", output); | 
|  | 1196 | return INVALID_OPERATION; | 
|  | 1197 | } | 
|  | 1198 | } | 
|  | 1199 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1200 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1201 | { | 
|  | 1202 | ALOGV("releaseOutput() %d", output); | 
|  | 1203 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1204 | if (index < 0) { | 
|  | 1205 | ALOGW("releaseOutput() releasing unknown output %d", output); | 
|  | 1206 | return; | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | #ifdef AUDIO_POLICY_TEST | 
|  | 1210 | int testIndex = testOutputIndex(output); | 
|  | 1211 | if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1212 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1213 | if (outputDesc->isActive()) { | 
|  | 1214 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1215 | mOutputs.removeItem(output); | 
|  | 1216 | mTestOutputs[testIndex] = 0; | 
|  | 1217 | } | 
|  | 1218 | return; | 
|  | 1219 | } | 
|  | 1220 | #endif //AUDIO_POLICY_TEST | 
|  | 1221 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1222 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1223 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1224 | if (desc->mDirectOpenCount <= 0) { | 
|  | 1225 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
|  | 1226 | desc->mDirectOpenCount, output); | 
|  | 1227 | return; | 
|  | 1228 | } | 
|  | 1229 | if (--desc->mDirectOpenCount == 0) { | 
|  | 1230 | closeOutput(output); | 
|  | 1231 | // If effects where present on the output, audioflinger moved them to the primary | 
|  | 1232 | // output by default: move them back to the appropriate output. | 
|  | 1233 | audio_io_handle_t dstOutput = getOutputForEffect(); | 
|  | 1234 | if (dstOutput != mPrimaryOutput) { | 
|  | 1235 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); | 
|  | 1236 | } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1237 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1238 | } | 
|  | 1239 | } | 
|  | 1240 | } | 
|  | 1241 |  | 
|  | 1242 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1243 | audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1244 | uint32_t samplingRate, | 
|  | 1245 | audio_format_t format, | 
|  | 1246 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1247 | audio_session_t session, | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1248 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1249 | { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1250 | ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, session %d, " | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1251 | "flags %#x", | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1252 | inputSource, samplingRate, format, channelMask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1253 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1254 | audio_devices_t device = getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1255 |  | 
|  | 1256 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1257 | ALOGW("getInput() could not find device for inputSource %d", inputSource); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1258 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1259 | } | 
|  | 1260 |  | 
|  | 1261 | // adapt channel selection to input source | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1262 | switch (inputSource) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1263 | case AUDIO_SOURCE_VOICE_UPLINK: | 
|  | 1264 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; | 
|  | 1265 | break; | 
|  | 1266 | case AUDIO_SOURCE_VOICE_DOWNLINK: | 
|  | 1267 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; | 
|  | 1268 | break; | 
|  | 1269 | case AUDIO_SOURCE_VOICE_CALL: | 
|  | 1270 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; | 
|  | 1271 | break; | 
|  | 1272 | default: | 
|  | 1273 | break; | 
|  | 1274 | } | 
|  | 1275 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1276 | sp<IOProfile> profile = getInputProfile(device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1277 | samplingRate, | 
|  | 1278 | format, | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1279 | channelMask, | 
|  | 1280 | flags); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1281 | if (profile == 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1282 | ALOGW("getInput() could not find profile for device 0x%X, samplingRate %u, format %#x, " | 
|  | 1283 | "channelMask 0x%X, flags %#x", | 
|  | 1284 | device, samplingRate, format, channelMask, flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1285 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | if (profile->mModule->mHandle == 0) { | 
|  | 1289 | ALOGE("getInput(): HW module %s not opened", profile->mModule->mName); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1290 | return AUDIO_IO_HANDLE_NONE; | 
|  | 1291 | } | 
|  | 1292 |  | 
|  | 1293 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 1294 | config.sample_rate = samplingRate; | 
|  | 1295 | config.channel_mask = channelMask; | 
|  | 1296 | config.format = format; | 
|  | 1297 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1298 |  | 
|  | 1299 | bool isSoundTrigger = false; | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1300 | audio_source_t halInputSource = inputSource; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1301 | if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1302 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 1303 | if (index >= 0) { | 
|  | 1304 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 1305 | isSoundTrigger = true; | 
|  | 1306 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1307 | } else { | 
|  | 1308 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1309 | } | 
|  | 1310 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1311 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, | 
|  | 1312 | &input, | 
|  | 1313 | &config, | 
|  | 1314 | &device, | 
|  | 1315 | String8(""), | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1316 | halInputSource, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1317 | flags); | 
|  | 1318 |  | 
|  | 1319 | // only accept input with the exact requested set of parameters | 
|  | 1320 | if (status != NO_ERROR || | 
|  | 1321 | (samplingRate != config.sample_rate) || | 
|  | 1322 | (format != config.format) || | 
|  | 1323 | (channelMask != config.channel_mask)) { | 
|  | 1324 | ALOGW("getInput() failed opening input: samplingRate %d, format %d, channelMask %x", | 
|  | 1325 | samplingRate, format, channelMask); | 
|  | 1326 | if (input != AUDIO_IO_HANDLE_NONE) { | 
|  | 1327 | mpClientInterface->closeInput(input); | 
|  | 1328 | } | 
|  | 1329 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1330 | } | 
|  | 1331 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1332 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1333 | inputDesc->mInputSource = inputSource; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1334 | inputDesc->mRefCount = 0; | 
|  | 1335 | inputDesc->mOpenRefCount = 1; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1336 | inputDesc->mSamplingRate = samplingRate; | 
|  | 1337 | inputDesc->mFormat = format; | 
|  | 1338 | inputDesc->mChannelMask = channelMask; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1339 | inputDesc->mDevice = device; | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1340 | inputDesc->mSessions.add(session); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1341 | inputDesc->mIsSoundTrigger = isSoundTrigger; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1342 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1343 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1344 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1345 | return input; | 
|  | 1346 | } | 
|  | 1347 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1348 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, | 
|  | 1349 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1350 | { | 
|  | 1351 | ALOGV("startInput() input %d", input); | 
|  | 1352 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1353 | if (index < 0) { | 
|  | 1354 | ALOGW("startInput() unknown input %d", input); | 
|  | 1355 | return BAD_VALUE; | 
|  | 1356 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1357 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1358 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1359 | index = inputDesc->mSessions.indexOf(session); | 
|  | 1360 | if (index < 0) { | 
|  | 1361 | ALOGW("startInput() unknown session %d on input %d", session, input); | 
|  | 1362 | return BAD_VALUE; | 
|  | 1363 | } | 
|  | 1364 |  | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1365 | // virtual input devices are compatible with other input devices | 
|  | 1366 | if (!isVirtualInputDevice(inputDesc->mDevice)) { | 
|  | 1367 |  | 
|  | 1368 | // for a non-virtual input device, check if there is another (non-virtual) active input | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1369 | audio_io_handle_t activeInput = getActiveInput(); | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1370 | if (activeInput != 0 && activeInput != input) { | 
|  | 1371 |  | 
|  | 1372 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, | 
|  | 1373 | // otherwise the active input continues and the new input cannot be started. | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1374 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1375 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1376 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1377 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); | 
|  | 1378 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1379 | } else { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1380 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1381 | return INVALID_OPERATION; | 
|  | 1382 | } | 
|  | 1383 | } | 
|  | 1384 | } | 
|  | 1385 |  | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1386 | if (inputDesc->mRefCount == 0) { | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1387 | if (activeInputsCount() == 0) { | 
|  | 1388 | SoundTrigger::setCaptureState(true); | 
|  | 1389 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1390 | setInputDevice(input, getNewInputDevice(input), true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 |  | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1392 | // Automatically enable the remote submix output when input is started. | 
|  | 1393 | // For remote submix (a virtual device), we open only one input per capture request. | 
|  | 1394 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1395 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1396 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); | 
|  | 1397 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1398 | } | 
|  | 1399 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1400 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); | 
|  | 1401 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1402 | inputDesc->mRefCount++; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1403 | return NO_ERROR; | 
|  | 1404 | } | 
|  | 1405 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1406 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, | 
|  | 1407 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1408 | { | 
|  | 1409 | ALOGV("stopInput() input %d", input); | 
|  | 1410 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1411 | if (index < 0) { | 
|  | 1412 | ALOGW("stopInput() unknown input %d", input); | 
|  | 1413 | return BAD_VALUE; | 
|  | 1414 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1415 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1416 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1417 | index = inputDesc->mSessions.indexOf(session); | 
|  | 1418 | if (index < 0) { | 
|  | 1419 | ALOGW("stopInput() unknown session %d on input %d", session, input); | 
|  | 1420 | return BAD_VALUE; | 
|  | 1421 | } | 
|  | 1422 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1423 | if (inputDesc->mRefCount == 0) { | 
|  | 1424 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 1425 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1426 | } | 
|  | 1427 |  | 
|  | 1428 | inputDesc->mRefCount--; | 
|  | 1429 | if (inputDesc->mRefCount == 0) { | 
|  | 1430 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1431 | // automatically disable the remote submix output when input is stopped | 
|  | 1432 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1433 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1434 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1435 | } | 
|  | 1436 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1437 | resetInputDevice(input); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1438 |  | 
|  | 1439 | if (activeInputsCount() == 0) { | 
|  | 1440 | SoundTrigger::setCaptureState(false); | 
|  | 1441 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1442 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1443 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | } | 
|  | 1445 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1446 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, | 
|  | 1447 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1448 | { | 
|  | 1449 | ALOGV("releaseInput() %d", input); | 
|  | 1450 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1451 | if (index < 0) { | 
|  | 1452 | ALOGW("releaseInput() releasing unknown input %d", input); | 
|  | 1453 | return; | 
|  | 1454 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1455 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
|  | 1456 | ALOG_ASSERT(inputDesc != 0); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1457 |  | 
|  | 1458 | index = inputDesc->mSessions.indexOf(session); | 
|  | 1459 | if (index < 0) { | 
|  | 1460 | ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
|  | 1461 | return; | 
|  | 1462 | } | 
|  | 1463 | inputDesc->mSessions.remove(session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1464 | if (inputDesc->mOpenRefCount == 0) { | 
|  | 1465 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); | 
|  | 1466 | return; | 
|  | 1467 | } | 
|  | 1468 | inputDesc->mOpenRefCount--; | 
|  | 1469 | if (inputDesc->mOpenRefCount > 0) { | 
|  | 1470 | ALOGV("releaseInput() exit > 0"); | 
|  | 1471 | return; | 
|  | 1472 | } | 
|  | 1473 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1474 | closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1475 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1476 | ALOGV("releaseInput() exit"); | 
|  | 1477 | } | 
|  | 1478 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1479 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1480 | bool patchRemoved = false; | 
|  | 1481 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1482 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1483 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
|  | 1484 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 1485 | if (patch_index >= 0) { | 
|  | 1486 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
|  | 1487 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 1488 | mAudioPatches.removeItemsAt(patch_index); | 
|  | 1489 | patchRemoved = true; | 
|  | 1490 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1491 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
|  | 1492 | } | 
|  | 1493 | mInputs.clear(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1494 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1495 |  | 
|  | 1496 | if (patchRemoved) { | 
|  | 1497 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 1498 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1499 | } | 
|  | 1500 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1501 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1502 | int indexMin, | 
|  | 1503 | int indexMax) | 
|  | 1504 | { | 
|  | 1505 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
|  | 1506 | if (indexMin < 0 || indexMin >= indexMax) { | 
|  | 1507 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); | 
|  | 1508 | return; | 
|  | 1509 | } | 
|  | 1510 | mStreams[stream].mIndexMin = indexMin; | 
|  | 1511 | mStreams[stream].mIndexMax = indexMax; | 
|  | 1512 | } | 
|  | 1513 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1514 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1515 | int index, | 
|  | 1516 | audio_devices_t device) | 
|  | 1517 | { | 
|  | 1518 |  | 
|  | 1519 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { | 
|  | 1520 | return BAD_VALUE; | 
|  | 1521 | } | 
|  | 1522 | if (!audio_is_output_device(device)) { | 
|  | 1523 | return BAD_VALUE; | 
|  | 1524 | } | 
|  | 1525 |  | 
|  | 1526 | // Force max volume if stream cannot be muted | 
|  | 1527 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; | 
|  | 1528 |  | 
|  | 1529 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", | 
|  | 1530 | stream, device, index); | 
|  | 1531 |  | 
|  | 1532 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and | 
|  | 1533 | // clear all device specific values | 
|  | 1534 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
|  | 1535 | mStreams[stream].mIndexCur.clear(); | 
|  | 1536 | } | 
|  | 1537 | mStreams[stream].mIndexCur.add(device, index); | 
|  | 1538 |  | 
|  | 1539 | // compute and apply stream volume on all outputs according to connected device | 
|  | 1540 | status_t status = NO_ERROR; | 
|  | 1541 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1542 | audio_devices_t curDevice = | 
|  | 1543 | getDeviceForVolume(mOutputs.valueAt(i)->device()); | 
|  | 1544 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) { | 
|  | 1545 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); | 
|  | 1546 | if (volStatus != NO_ERROR) { | 
|  | 1547 | status = volStatus; | 
|  | 1548 | } | 
|  | 1549 | } | 
|  | 1550 | } | 
|  | 1551 | return status; | 
|  | 1552 | } | 
|  | 1553 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1554 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1555 | int *index, | 
|  | 1556 | audio_devices_t device) | 
|  | 1557 | { | 
|  | 1558 | if (index == NULL) { | 
|  | 1559 | return BAD_VALUE; | 
|  | 1560 | } | 
|  | 1561 | if (!audio_is_output_device(device)) { | 
|  | 1562 | return BAD_VALUE; | 
|  | 1563 | } | 
|  | 1564 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to | 
|  | 1565 | // the strategy the stream belongs to. | 
|  | 1566 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
|  | 1567 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 1568 | } | 
|  | 1569 | device = getDeviceForVolume(device); | 
|  | 1570 |  | 
|  | 1571 | *index =  mStreams[stream].getVolumeIndex(device); | 
|  | 1572 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 1573 | return NO_ERROR; | 
|  | 1574 | } | 
|  | 1575 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1576 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1577 | const SortedVector<audio_io_handle_t>& outputs) | 
|  | 1578 | { | 
|  | 1579 | // select one output among several suitable for global effects. | 
|  | 1580 | // The priority is as follows: | 
|  | 1581 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 1582 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 1583 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 1584 | // 2: A deep buffer output | 
|  | 1585 | // 3: the first output in the list | 
|  | 1586 |  | 
|  | 1587 | if (outputs.size() == 0) { | 
|  | 1588 | return 0; | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | audio_io_handle_t outputOffloaded = 0; | 
|  | 1592 | audio_io_handle_t outputDeepBuffer = 0; | 
|  | 1593 |  | 
|  | 1594 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1595 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1596 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1597 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1598 | outputOffloaded = outputs[i]; | 
|  | 1599 | } | 
|  | 1600 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 1601 | outputDeepBuffer = outputs[i]; | 
|  | 1602 | } | 
|  | 1603 | } | 
|  | 1604 |  | 
|  | 1605 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", | 
|  | 1606 | outputOffloaded, outputDeepBuffer); | 
|  | 1607 | if (outputOffloaded != 0) { | 
|  | 1608 | return outputOffloaded; | 
|  | 1609 | } | 
|  | 1610 | if (outputDeepBuffer != 0) { | 
|  | 1611 | return outputDeepBuffer; | 
|  | 1612 | } | 
|  | 1613 |  | 
|  | 1614 | return outputs[0]; | 
|  | 1615 | } | 
|  | 1616 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1617 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1618 | { | 
|  | 1619 | // apply simple rule where global effects are attached to the same output as MUSIC streams | 
|  | 1620 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1621 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1622 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 1623 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); | 
|  | 1624 |  | 
|  | 1625 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); | 
|  | 1626 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", | 
|  | 1627 | output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags); | 
|  | 1628 |  | 
|  | 1629 | return output; | 
|  | 1630 | } | 
|  | 1631 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1632 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1633 | audio_io_handle_t io, | 
|  | 1634 | uint32_t strategy, | 
|  | 1635 | int session, | 
|  | 1636 | int id) | 
|  | 1637 | { | 
|  | 1638 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 1639 | if (index < 0) { | 
|  | 1640 | index = mInputs.indexOfKey(io); | 
|  | 1641 | if (index < 0) { | 
|  | 1642 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 1643 | return INVALID_OPERATION; | 
|  | 1644 | } | 
|  | 1645 | } | 
|  | 1646 |  | 
|  | 1647 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { | 
|  | 1648 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", | 
|  | 1649 | desc->name, desc->memoryUsage); | 
|  | 1650 | return INVALID_OPERATION; | 
|  | 1651 | } | 
|  | 1652 | mTotalEffectsMemory += desc->memoryUsage; | 
|  | 1653 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", | 
|  | 1654 | desc->name, io, strategy, session, id); | 
|  | 1655 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); | 
|  | 1656 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1657 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); | 
|  | 1658 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); | 
|  | 1659 | effectDesc->mIo = io; | 
|  | 1660 | effectDesc->mStrategy = (routing_strategy)strategy; | 
|  | 1661 | effectDesc->mSession = session; | 
|  | 1662 | effectDesc->mEnabled = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1663 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1664 | mEffects.add(id, effectDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1665 |  | 
|  | 1666 | return NO_ERROR; | 
|  | 1667 | } | 
|  | 1668 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1669 | status_t AudioPolicyManager::unregisterEffect(int id) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1670 | { | 
|  | 1671 | ssize_t index = mEffects.indexOfKey(id); | 
|  | 1672 | if (index < 0) { | 
|  | 1673 | ALOGW("unregisterEffect() unknown effect ID %d", id); | 
|  | 1674 | return INVALID_OPERATION; | 
|  | 1675 | } | 
|  | 1676 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1677 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1679 | setEffectEnabled(effectDesc, false); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1680 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1681 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1682 | ALOGW("unregisterEffect() memory %d too big for total %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1683 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); | 
|  | 1684 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1685 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1686 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1687 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1688 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1689 |  | 
|  | 1690 | mEffects.removeItem(id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1691 |  | 
|  | 1692 | return NO_ERROR; | 
|  | 1693 | } | 
|  | 1694 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1695 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1696 | { | 
|  | 1697 | ssize_t index = mEffects.indexOfKey(id); | 
|  | 1698 | if (index < 0) { | 
|  | 1699 | ALOGW("unregisterEffect() unknown effect ID %d", id); | 
|  | 1700 | return INVALID_OPERATION; | 
|  | 1701 | } | 
|  | 1702 |  | 
|  | 1703 | return setEffectEnabled(mEffects.valueAt(index), enabled); | 
|  | 1704 | } | 
|  | 1705 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1706 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1707 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1708 | if (enabled == effectDesc->mEnabled) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1709 | ALOGV("setEffectEnabled(%s) effect already %s", | 
|  | 1710 | enabled?"true":"false", enabled?"enabled":"disabled"); | 
|  | 1711 | return INVALID_OPERATION; | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 | if (enabled) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1715 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1716 | 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] | 1717 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1718 | return INVALID_OPERATION; | 
|  | 1719 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1720 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1721 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); | 
|  | 1722 | } else { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1723 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1724 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1725 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); | 
|  | 1726 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1727 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1728 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1729 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); | 
|  | 1730 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1731 | effectDesc->mEnabled = enabled; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1732 | return NO_ERROR; | 
|  | 1733 | } | 
|  | 1734 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1735 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1736 | { | 
|  | 1737 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1738 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 1739 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && | 
|  | 1740 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1741 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1742 | effectDesc->mDesc.name, effectDesc->mSession); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1743 | return true; | 
|  | 1744 | } | 
|  | 1745 | } | 
|  | 1746 | return false; | 
|  | 1747 | } | 
|  | 1748 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1749 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1750 | { | 
|  | 1751 | nsecs_t sysTime = systemTime(); | 
|  | 1752 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1753 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1754 | if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1755 | return true; | 
|  | 1756 | } | 
|  | 1757 | } | 
|  | 1758 | return false; | 
|  | 1759 | } | 
|  | 1760 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1761 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1762 | uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1763 | { | 
|  | 1764 | nsecs_t sysTime = systemTime(); | 
|  | 1765 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1766 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1767 | if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1768 | outputDesc->isStreamActive(stream, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1769 | return true; | 
|  | 1770 | } | 
|  | 1771 | } | 
|  | 1772 | return false; | 
|  | 1773 | } | 
|  | 1774 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1775 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1776 | { | 
|  | 1777 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1778 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1779 | if ((inputDescriptor->mInputSource == (int)source || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1780 | (source == AUDIO_SOURCE_VOICE_RECOGNITION && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1781 | inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) | 
|  | 1782 | && (inputDescriptor->mRefCount > 0)) { | 
|  | 1783 | return true; | 
|  | 1784 | } | 
|  | 1785 | } | 
|  | 1786 | return false; | 
|  | 1787 | } | 
|  | 1788 |  | 
|  | 1789 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1790 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 | { | 
|  | 1792 | const size_t SIZE = 256; | 
|  | 1793 | char buffer[SIZE]; | 
|  | 1794 | String8 result; | 
|  | 1795 |  | 
|  | 1796 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
|  | 1797 | result.append(buffer); | 
|  | 1798 |  | 
|  | 1799 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); | 
|  | 1800 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1801 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); | 
|  | 1802 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1803 | snprintf(buffer, SIZE, " Force use for communications %d\n", | 
|  | 1804 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1805 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1806 | 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] | 1807 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1808 | 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] | 1809 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1810 | 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] | 1811 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1812 | 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] | 1813 | result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1814 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
|  | 1815 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); | 
|  | 1816 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1817 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1818 | snprintf(buffer, SIZE, " Available output devices:\n"); | 
|  | 1819 | result.append(buffer); | 
|  | 1820 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1821 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1822 | mAvailableOutputDevices[i]->dump(fd, 2, i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1823 | } | 
|  | 1824 | snprintf(buffer, SIZE, "\n Available input devices:\n"); | 
|  | 1825 | write(fd, buffer, strlen(buffer)); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1826 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1827 | mAvailableInputDevices[i]->dump(fd, 2, i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1828 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1829 |  | 
|  | 1830 | snprintf(buffer, SIZE, "\nHW Modules dump:\n"); | 
|  | 1831 | write(fd, buffer, strlen(buffer)); | 
|  | 1832 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1833 | snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1834 | write(fd, buffer, strlen(buffer)); | 
|  | 1835 | mHwModules[i]->dump(fd); | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); | 
|  | 1839 | write(fd, buffer, strlen(buffer)); | 
|  | 1840 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1841 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); | 
|  | 1842 | write(fd, buffer, strlen(buffer)); | 
|  | 1843 | mOutputs.valueAt(i)->dump(fd); | 
|  | 1844 | } | 
|  | 1845 |  | 
|  | 1846 | snprintf(buffer, SIZE, "\nInputs dump:\n"); | 
|  | 1847 | write(fd, buffer, strlen(buffer)); | 
|  | 1848 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1849 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); | 
|  | 1850 | write(fd, buffer, strlen(buffer)); | 
|  | 1851 | mInputs.valueAt(i)->dump(fd); | 
|  | 1852 | } | 
|  | 1853 |  | 
|  | 1854 | snprintf(buffer, SIZE, "\nStreams dump:\n"); | 
|  | 1855 | write(fd, buffer, strlen(buffer)); | 
|  | 1856 | snprintf(buffer, SIZE, | 
|  | 1857 | " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n"); | 
|  | 1858 | write(fd, buffer, strlen(buffer)); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 1859 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1860 | snprintf(buffer, SIZE, " %02zu      ", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | write(fd, buffer, strlen(buffer)); | 
|  | 1862 | mStreams[i].dump(fd); | 
|  | 1863 | } | 
|  | 1864 |  | 
|  | 1865 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", | 
|  | 1866 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); | 
|  | 1867 | write(fd, buffer, strlen(buffer)); | 
|  | 1868 |  | 
|  | 1869 | snprintf(buffer, SIZE, "Registered effects:\n"); | 
|  | 1870 | write(fd, buffer, strlen(buffer)); | 
|  | 1871 | for (size_t i = 0; i < mEffects.size(); i++) { | 
|  | 1872 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); | 
|  | 1873 | write(fd, buffer, strlen(buffer)); | 
|  | 1874 | mEffects.valueAt(i)->dump(fd); | 
|  | 1875 | } | 
|  | 1876 |  | 
| Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 1877 | snprintf(buffer, SIZE, "\nAudio Patches:\n"); | 
|  | 1878 | write(fd, buffer, strlen(buffer)); | 
|  | 1879 | for (size_t i = 0; i < mAudioPatches.size(); i++) { | 
|  | 1880 | mAudioPatches[i]->dump(fd, 2, i); | 
|  | 1881 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1882 |  | 
|  | 1883 | return NO_ERROR; | 
|  | 1884 | } | 
|  | 1885 |  | 
|  | 1886 | // This function checks for the parameters which can be offloaded. | 
|  | 1887 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 1888 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1889 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 | { | 
|  | 1891 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1892 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1893 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 1894 | offloadInfo.format, | 
|  | 1895 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 1896 | offloadInfo.has_video); | 
|  | 1897 |  | 
|  | 1898 | // Check if offload has been disabled | 
|  | 1899 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 1900 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 1901 | if (atoi(propValue) != 0) { | 
|  | 1902 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 1903 | return false; | 
|  | 1904 | } | 
|  | 1905 | } | 
|  | 1906 |  | 
|  | 1907 | // Check if stream type is music, then only allow offload as of now. | 
|  | 1908 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 1909 | { | 
|  | 1910 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 1911 | return false; | 
|  | 1912 | } | 
|  | 1913 |  | 
|  | 1914 | //TODO: enable audio offloading with video when ready | 
|  | 1915 | if (offloadInfo.has_video) | 
|  | 1916 | { | 
|  | 1917 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 1918 | return false; | 
|  | 1919 | } | 
|  | 1920 |  | 
|  | 1921 | //If duration is less than minimum value defined in property, return false | 
|  | 1922 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 1923 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 1924 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 1925 | return false; | 
|  | 1926 | } | 
|  | 1927 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 1928 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 1929 | return false; | 
|  | 1930 | } | 
|  | 1931 |  | 
|  | 1932 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 1933 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 1934 | // detects there is an active non offloadable effect. | 
|  | 1935 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 1936 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 1937 | // in the background. | 
|  | 1938 | if (isNonOffloadableEffectEnabled()) { | 
|  | 1939 | return false; | 
|  | 1940 | } | 
|  | 1941 |  | 
|  | 1942 | // See if there is a profile to support this. | 
|  | 1943 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1944 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1945 | offloadInfo.sample_rate, | 
|  | 1946 | offloadInfo.format, | 
|  | 1947 | offloadInfo.channel_mask, | 
|  | 1948 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1949 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 1950 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1951 | } | 
|  | 1952 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1953 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 1954 | audio_port_type_t type, | 
|  | 1955 | unsigned int *num_ports, | 
|  | 1956 | struct audio_port *ports, | 
|  | 1957 | unsigned int *generation) | 
|  | 1958 | { | 
|  | 1959 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 1960 | generation == NULL) { | 
|  | 1961 | return BAD_VALUE; | 
|  | 1962 | } | 
|  | 1963 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 1964 | if (ports == NULL) { | 
|  | 1965 | *num_ports = 0; | 
|  | 1966 | } | 
|  | 1967 |  | 
|  | 1968 | size_t portsWritten = 0; | 
|  | 1969 | size_t portsMax = *num_ports; | 
|  | 1970 | *num_ports = 0; | 
|  | 1971 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 1972 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1973 | for (size_t i = 0; | 
|  | 1974 | i  < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { | 
|  | 1975 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1976 | } | 
|  | 1977 | *num_ports += mAvailableOutputDevices.size(); | 
|  | 1978 | } | 
|  | 1979 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1980 | for (size_t i = 0; | 
|  | 1981 | i  < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { | 
|  | 1982 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1983 | } | 
|  | 1984 | *num_ports += mAvailableInputDevices.size(); | 
|  | 1985 | } | 
|  | 1986 | } | 
|  | 1987 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 1988 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 1989 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 1990 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 1991 | } | 
|  | 1992 | *num_ports += mInputs.size(); | 
|  | 1993 | } | 
|  | 1994 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 1995 | size_t numOutputs = 0; | 
|  | 1996 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1997 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 1998 | numOutputs++; | 
|  | 1999 | if (portsWritten < portsMax) { | 
|  | 2000 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2001 | } | 
|  | 2002 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2003 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2004 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2005 | } | 
|  | 2006 | } | 
|  | 2007 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2008 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2009 | return NO_ERROR; | 
|  | 2010 | } | 
|  | 2011 |  | 
|  | 2012 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
|  | 2013 | { | 
|  | 2014 | return NO_ERROR; | 
|  | 2015 | } | 
|  | 2016 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2017 | sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2018 | audio_port_handle_t id) const | 
|  | 2019 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2020 | sp<AudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2021 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2022 | outputDesc = mOutputs.valueAt(i); | 
|  | 2023 | if (outputDesc->mId == id) { | 
|  | 2024 | break; | 
|  | 2025 | } | 
|  | 2026 | } | 
|  | 2027 | return outputDesc; | 
|  | 2028 | } | 
|  | 2029 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2030 | sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2031 | audio_port_handle_t id) const | 
|  | 2032 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2033 | sp<AudioInputDescriptor> inputDesc = NULL; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2034 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2035 | inputDesc = mInputs.valueAt(i); | 
|  | 2036 | if (inputDesc->mId == id) { | 
|  | 2037 | break; | 
|  | 2038 | } | 
|  | 2039 | } | 
|  | 2040 | return inputDesc; | 
|  | 2041 | } | 
|  | 2042 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2043 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice( | 
|  | 2044 | audio_devices_t device) const | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2045 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2046 | sp <HwModule> module; | 
|  | 2047 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2048 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 2049 | if (mHwModules[i]->mHandle == 0) { | 
|  | 2050 | continue; | 
|  | 2051 | } | 
|  | 2052 | if (audio_is_output_device(device)) { | 
|  | 2053 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 2054 | { | 
|  | 2055 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { | 
|  | 2056 | return mHwModules[i]; | 
|  | 2057 | } | 
|  | 2058 | } | 
|  | 2059 | } else { | 
|  | 2060 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { | 
|  | 2061 | if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() & | 
|  | 2062 | device & ~AUDIO_DEVICE_BIT_IN) { | 
|  | 2063 | return mHwModules[i]; | 
|  | 2064 | } | 
|  | 2065 | } | 
|  | 2066 | } | 
|  | 2067 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2068 | return module; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2069 | } | 
|  | 2070 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2071 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2072 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2073 | sp <HwModule> module; | 
|  | 2074 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2075 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 2076 | { | 
|  | 2077 | if (strcmp(mHwModules[i]->mName, name) == 0) { | 
|  | 2078 | return mHwModules[i]; | 
|  | 2079 | } | 
|  | 2080 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2081 | return module; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2082 | } | 
|  | 2083 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2084 | audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices() | 
|  | 2085 | { | 
|  | 2086 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); | 
|  | 2087 | audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types(); | 
|  | 2088 | return devices & mAvailableOutputDevices.types(); | 
|  | 2089 | } | 
|  | 2090 |  | 
|  | 2091 | audio_devices_t AudioPolicyManager::availablePrimaryInputDevices() | 
|  | 2092 | { | 
|  | 2093 | audio_module_handle_t primaryHandle = | 
|  | 2094 | mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle; | 
|  | 2095 | audio_devices_t devices = AUDIO_DEVICE_NONE; | 
|  | 2096 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 2097 | if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) { | 
|  | 2098 | devices |= mAvailableInputDevices[i]->mDeviceType; | 
|  | 2099 | } | 
|  | 2100 | } | 
|  | 2101 | return devices; | 
|  | 2102 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2103 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2104 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 2105 | audio_patch_handle_t *handle, | 
|  | 2106 | uid_t uid) | 
|  | 2107 | { | 
|  | 2108 | ALOGV("createAudioPatch()"); | 
|  | 2109 |  | 
|  | 2110 | if (handle == NULL || patch == NULL) { | 
|  | 2111 | return BAD_VALUE; | 
|  | 2112 | } | 
|  | 2113 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 2114 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2115 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || | 
|  | 2116 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { | 
|  | 2117 | return BAD_VALUE; | 
|  | 2118 | } | 
|  | 2119 | // only one source per audio patch supported for now | 
|  | 2120 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2121 | return INVALID_OPERATION; | 
|  | 2122 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2123 |  | 
|  | 2124 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2125 | return INVALID_OPERATION; | 
|  | 2126 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2127 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2128 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 2129 | return INVALID_OPERATION; | 
|  | 2130 | } | 
|  | 2131 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2132 |  | 
|  | 2133 | sp<AudioPatch> patchDesc; | 
|  | 2134 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 2135 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2136 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 2137 | patch->sources[0].role, | 
|  | 2138 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2139 | #if LOG_NDEBUG == 0 | 
|  | 2140 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2141 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, | 
|  | 2142 | patch->sinks[i].role, | 
|  | 2143 | patch->sinks[i].type); | 
|  | 2144 | } | 
|  | 2145 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2146 |  | 
|  | 2147 | if (index >= 0) { | 
|  | 2148 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2149 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2150 | mUidCached, patchDesc->mUid, uid); | 
|  | 2151 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2152 | return INVALID_OPERATION; | 
|  | 2153 | } | 
|  | 2154 | } else { | 
|  | 2155 | *handle = 0; | 
|  | 2156 | } | 
|  | 2157 |  | 
|  | 2158 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2159 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2160 | if (outputDesc == NULL) { | 
|  | 2161 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2162 | return BAD_VALUE; | 
|  | 2163 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2164 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 2165 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2166 | if (patchDesc != 0) { | 
|  | 2167 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 2168 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 2169 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 2170 | return BAD_VALUE; | 
|  | 2171 | } | 
|  | 2172 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2173 | DeviceVector devices; | 
|  | 2174 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2175 | // Only support mix to devices connection | 
|  | 2176 | // TODO add support for mix to mix connection | 
|  | 2177 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2178 | ALOGV("createAudioPatch() source mix but sink is not a device"); | 
|  | 2179 | return INVALID_OPERATION; | 
|  | 2180 | } | 
|  | 2181 | sp<DeviceDescriptor> devDesc = | 
|  | 2182 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2183 | if (devDesc == 0) { | 
|  | 2184 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
|  | 2185 | return BAD_VALUE; | 
|  | 2186 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2187 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2188 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, | 
|  | 2189 | patch->sources[0].sample_rate, | 
|  | 2190 | NULL,  // updatedSamplingRate | 
|  | 2191 | patch->sources[0].format, | 
|  | 2192 | patch->sources[0].channel_mask, | 
|  | 2193 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
|  | 2194 | ALOGV("createAudioPatch() profile not supported for device %08x", | 
|  | 2195 | devDesc->mDeviceType); | 
|  | 2196 | return INVALID_OPERATION; | 
|  | 2197 | } | 
|  | 2198 | devices.add(devDesc); | 
|  | 2199 | } | 
|  | 2200 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2201 | return INVALID_OPERATION; | 
|  | 2202 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2203 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2204 | // TODO: reconfigure output format and channels here | 
|  | 2205 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2206 | devices.types(), outputDesc->mIoHandle); | 
|  | 2207 | setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2208 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2209 | if (index >= 0) { | 
|  | 2210 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2211 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 2212 | } | 
|  | 2213 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2214 | patchDesc->mUid = uid; | 
|  | 2215 | ALOGV("createAudioPatch() success"); | 
|  | 2216 | } else { | 
|  | 2217 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 2218 | return INVALID_OPERATION; | 
|  | 2219 | } | 
|  | 2220 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2221 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2222 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2223 | // only one sink supported when connecting an input device to a mix | 
|  | 2224 | if (patch->num_sinks > 1) { | 
|  | 2225 | return INVALID_OPERATION; | 
|  | 2226 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2227 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2228 | if (inputDesc == NULL) { | 
|  | 2229 | return BAD_VALUE; | 
|  | 2230 | } | 
|  | 2231 | if (patchDesc != 0) { | 
|  | 2232 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 2233 | return BAD_VALUE; | 
|  | 2234 | } | 
|  | 2235 | } | 
|  | 2236 | sp<DeviceDescriptor> devDesc = | 
|  | 2237 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 2238 | if (devDesc == 0) { | 
|  | 2239 | return BAD_VALUE; | 
|  | 2240 | } | 
|  | 2241 |  | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2242 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 2243 | patch->sinks[0].sample_rate, | 
|  | 2244 | NULL, /*updatedSampleRate*/ | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2245 | patch->sinks[0].format, | 
|  | 2246 | patch->sinks[0].channel_mask, | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2247 | // FIXME for the parameter type, | 
|  | 2248 | // and the NONE | 
|  | 2249 | (audio_output_flags_t) | 
|  | 2250 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2251 | return INVALID_OPERATION; | 
|  | 2252 | } | 
|  | 2253 | // TODO: reconfigure output format and channels here | 
|  | 2254 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2255 | devDesc->mDeviceType, inputDesc->mIoHandle); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2256 | setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2257 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2258 | if (index >= 0) { | 
|  | 2259 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2260 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 2261 | } | 
|  | 2262 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2263 | patchDesc->mUid = uid; | 
|  | 2264 | ALOGV("createAudioPatch() success"); | 
|  | 2265 | } else { | 
|  | 2266 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 2267 | return INVALID_OPERATION; | 
|  | 2268 | } | 
|  | 2269 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2270 | // device to device connection | 
|  | 2271 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2272 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2273 | return BAD_VALUE; | 
|  | 2274 | } | 
|  | 2275 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2276 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2277 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2278 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2279 | //update source and sink with our own data as the data passed in the patch may | 
|  | 2280 | // be incomplete. | 
|  | 2281 | struct audio_patch newPatch = *patch; | 
|  | 2282 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2283 | if (srcDeviceDesc == 0) { | 
|  | 2284 | return BAD_VALUE; | 
|  | 2285 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2286 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2287 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2288 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2289 | ALOGV("createAudioPatch() source device but one sink is not a device"); | 
|  | 2290 | return INVALID_OPERATION; | 
|  | 2291 | } | 
|  | 2292 |  | 
|  | 2293 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2294 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2295 | if (sinkDeviceDesc == 0) { | 
|  | 2296 | return BAD_VALUE; | 
|  | 2297 | } | 
|  | 2298 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
|  | 2299 |  | 
|  | 2300 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { | 
|  | 2301 | // only one sink supported when connected devices across HW modules | 
|  | 2302 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2303 | return INVALID_OPERATION; | 
|  | 2304 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2305 | SortedVector<audio_io_handle_t> outputs = | 
|  | 2306 | getOutputsForDevice(sinkDeviceDesc->mDeviceType, | 
|  | 2307 | mOutputs); | 
|  | 2308 | // if the sink device is reachable via an opened output stream, request to go via | 
|  | 2309 | // this output stream by adding a second source to the patch description | 
|  | 2310 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE); | 
|  | 2311 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 2312 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 2313 | if (outputDesc->isDuplicated()) { | 
|  | 2314 | return INVALID_OPERATION; | 
|  | 2315 | } | 
|  | 2316 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
|  | 2317 | newPatch.num_sources = 2; | 
|  | 2318 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2319 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2320 | } | 
|  | 2321 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 2322 |  | 
|  | 2323 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2324 | if (index >= 0) { | 
|  | 2325 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2326 | } | 
|  | 2327 |  | 
|  | 2328 | status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
|  | 2329 | &afPatchHandle, | 
|  | 2330 | 0); | 
|  | 2331 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2332 | status, afPatchHandle); | 
|  | 2333 | if (status == NO_ERROR) { | 
|  | 2334 | if (index < 0) { | 
|  | 2335 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 2336 | &newPatch, uid); | 
|  | 2337 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 2338 | } else { | 
|  | 2339 | patchDesc->mPatch = newPatch; | 
|  | 2340 | } | 
|  | 2341 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 2342 | *handle = patchDesc->mHandle; | 
|  | 2343 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2344 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2345 | } else { | 
|  | 2346 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 2347 | status); | 
|  | 2348 | return INVALID_OPERATION; | 
|  | 2349 | } | 
|  | 2350 | } else { | 
|  | 2351 | return BAD_VALUE; | 
|  | 2352 | } | 
|  | 2353 | } else { | 
|  | 2354 | return BAD_VALUE; | 
|  | 2355 | } | 
|  | 2356 | return NO_ERROR; | 
|  | 2357 | } | 
|  | 2358 |  | 
|  | 2359 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 2360 | uid_t uid) | 
|  | 2361 | { | 
|  | 2362 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 2363 |  | 
|  | 2364 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2365 |  | 
|  | 2366 | if (index < 0) { | 
|  | 2367 | return BAD_VALUE; | 
|  | 2368 | } | 
|  | 2369 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 2370 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2371 | mUidCached, patchDesc->mUid, uid); | 
|  | 2372 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2373 | return INVALID_OPERATION; | 
|  | 2374 | } | 
|  | 2375 |  | 
|  | 2376 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 2377 | patchDesc->mUid = mUidCached; | 
|  | 2378 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2379 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2380 | if (outputDesc == NULL) { | 
|  | 2381 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2382 | return BAD_VALUE; | 
|  | 2383 | } | 
|  | 2384 |  | 
|  | 2385 | setOutputDevice(outputDesc->mIoHandle, | 
|  | 2386 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), | 
|  | 2387 | true, | 
|  | 2388 | 0, | 
|  | 2389 | NULL); | 
|  | 2390 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2391 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2392 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2393 | if (inputDesc == NULL) { | 
|  | 2394 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 2395 | return BAD_VALUE; | 
|  | 2396 | } | 
|  | 2397 | setInputDevice(inputDesc->mIoHandle, | 
|  | 2398 | getNewInputDevice(inputDesc->mIoHandle), | 
|  | 2399 | true, | 
|  | 2400 | NULL); | 
|  | 2401 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2402 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2403 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 2404 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2405 | status, patchDesc->mAfPatchHandle); | 
|  | 2406 | removeAudioPatch(patchDesc->mHandle); | 
|  | 2407 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2408 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2409 | } else { | 
|  | 2410 | return BAD_VALUE; | 
|  | 2411 | } | 
|  | 2412 | } else { | 
|  | 2413 | return BAD_VALUE; | 
|  | 2414 | } | 
|  | 2415 | return NO_ERROR; | 
|  | 2416 | } | 
|  | 2417 |  | 
|  | 2418 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 2419 | struct audio_patch *patches, | 
|  | 2420 | unsigned int *generation) | 
|  | 2421 | { | 
|  | 2422 | if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || | 
|  | 2423 | generation == NULL) { | 
|  | 2424 | return BAD_VALUE; | 
|  | 2425 | } | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2426 | ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2427 | *num_patches, patches, mAudioPatches.size()); | 
|  | 2428 | if (patches == NULL) { | 
|  | 2429 | *num_patches = 0; | 
|  | 2430 | } | 
|  | 2431 |  | 
|  | 2432 | size_t patchesWritten = 0; | 
|  | 2433 | size_t patchesMax = *num_patches; | 
|  | 2434 | for (size_t i = 0; | 
|  | 2435 | i  < mAudioPatches.size() && patchesWritten < patchesMax; i++) { | 
|  | 2436 | patches[patchesWritten] = mAudioPatches[i]->mPatch; | 
|  | 2437 | patches[patchesWritten++].id = mAudioPatches[i]->mHandle; | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2438 | ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2439 | i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks); | 
|  | 2440 | } | 
|  | 2441 | *num_patches = mAudioPatches.size(); | 
|  | 2442 |  | 
|  | 2443 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2444 | ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2445 | return NO_ERROR; | 
|  | 2446 | } | 
|  | 2447 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2448 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2449 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2450 | ALOGV("setAudioPortConfig()"); | 
|  | 2451 |  | 
|  | 2452 | if (config == NULL) { | 
|  | 2453 | return BAD_VALUE; | 
|  | 2454 | } | 
|  | 2455 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 2456 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2457 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 2458 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2459 | } | 
|  | 2460 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2461 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2462 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2463 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2464 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2465 | if (outputDesc == NULL) { | 
|  | 2466 | return BAD_VALUE; | 
|  | 2467 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2468 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 2469 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 2470 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2471 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2472 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2473 | sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2474 | if (inputDesc == NULL) { | 
|  | 2475 | return BAD_VALUE; | 
|  | 2476 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2477 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2478 | } else { | 
|  | 2479 | return BAD_VALUE; | 
|  | 2480 | } | 
|  | 2481 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2482 | sp<DeviceDescriptor> deviceDesc; | 
|  | 2483 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 2484 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 2485 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 2486 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 2487 | } else { | 
|  | 2488 | return BAD_VALUE; | 
|  | 2489 | } | 
|  | 2490 | if (deviceDesc == NULL) { | 
|  | 2491 | return BAD_VALUE; | 
|  | 2492 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2493 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2494 | } else { | 
|  | 2495 | return BAD_VALUE; | 
|  | 2496 | } | 
|  | 2497 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2498 | struct audio_port_config backupConfig; | 
|  | 2499 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 2500 | if (status == NO_ERROR) { | 
|  | 2501 | struct audio_port_config newConfig; | 
|  | 2502 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 2503 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2504 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2505 | if (status != NO_ERROR) { | 
|  | 2506 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2507 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2508 |  | 
|  | 2509 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2510 | } | 
|  | 2511 |  | 
|  | 2512 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 2513 | { | 
|  | 2514 | for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++)  { | 
|  | 2515 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 2516 | if (patchDesc->mUid == uid) { | 
|  | 2517 | // releaseAudioPatch() removes the patch from mAudioPatches | 
|  | 2518 | if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) { | 
|  | 2519 | i--; | 
|  | 2520 | } | 
|  | 2521 | } | 
|  | 2522 | } | 
|  | 2523 | } | 
|  | 2524 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2525 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 2526 | audio_io_handle_t *ioHandle, | 
|  | 2527 | audio_devices_t *device) | 
|  | 2528 | { | 
|  | 2529 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); | 
|  | 2530 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); | 
|  | 2531 | *device = getDeviceForInputSource(AUDIO_SOURCE_HOTWORD); | 
|  | 2532 |  | 
|  | 2533 | mSoundTriggerSessions.add(*session, *ioHandle); | 
|  | 2534 |  | 
|  | 2535 | return NO_ERROR; | 
|  | 2536 | } | 
|  | 2537 |  | 
|  | 2538 | status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session) | 
|  | 2539 | { | 
|  | 2540 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 2541 | if (index < 0) { | 
|  | 2542 | ALOGW("acquireSoundTriggerSession() session %d not registered", session); | 
|  | 2543 | return BAD_VALUE; | 
|  | 2544 | } | 
|  | 2545 |  | 
|  | 2546 | mSoundTriggerSessions.removeItem(session); | 
|  | 2547 | return NO_ERROR; | 
|  | 2548 | } | 
|  | 2549 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2550 | status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle, | 
|  | 2551 | const sp<AudioPatch>& patch) | 
|  | 2552 | { | 
|  | 2553 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2554 |  | 
|  | 2555 | if (index >= 0) { | 
|  | 2556 | ALOGW("addAudioPatch() patch %d already in", handle); | 
|  | 2557 | return ALREADY_EXISTS; | 
|  | 2558 | } | 
|  | 2559 | mAudioPatches.add(handle, patch); | 
|  | 2560 | ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d" | 
|  | 2561 | "sink handle %d", | 
|  | 2562 | handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks, | 
|  | 2563 | patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id); | 
|  | 2564 | return NO_ERROR; | 
|  | 2565 | } | 
|  | 2566 |  | 
|  | 2567 | status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle) | 
|  | 2568 | { | 
|  | 2569 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2570 |  | 
|  | 2571 | if (index < 0) { | 
|  | 2572 | ALOGW("removeAudioPatch() patch %d not in", handle); | 
|  | 2573 | return ALREADY_EXISTS; | 
|  | 2574 | } | 
|  | 2575 | ALOGV("removeAudioPatch() handle %d af handle %d", handle, | 
|  | 2576 | mAudioPatches.valueAt(index)->mAfPatchHandle); | 
|  | 2577 | mAudioPatches.removeItemsAt(index); | 
|  | 2578 | return NO_ERROR; | 
|  | 2579 | } | 
|  | 2580 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2581 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2582 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2583 | // ---------------------------------------------------------------------------- | 
|  | 2584 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2585 | uint32_t AudioPolicyManager::nextUniqueId() | 
|  | 2586 | { | 
|  | 2587 | return android_atomic_inc(&mNextUniqueId); | 
|  | 2588 | } | 
|  | 2589 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2590 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 2591 | { | 
|  | 2592 | return android_atomic_inc(&mAudioPortGeneration); | 
|  | 2593 | } | 
|  | 2594 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2595 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2596 | : | 
|  | 2597 | #ifdef AUDIO_POLICY_TEST | 
|  | 2598 | Thread(false), | 
|  | 2599 | #endif //AUDIO_POLICY_TEST | 
|  | 2600 | mPrimaryOutput((audio_io_handle_t)0), | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2601 | mPhoneState(AUDIO_MODE_NORMAL), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2602 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
|  | 2603 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2604 | mA2dpSuspended(false), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2605 | mSpeakerDrcEnabled(false), mNextUniqueId(1), | 
|  | 2606 | mAudioPortGeneration(1) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2607 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2608 | mUidCached = getuid(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2609 | mpClientInterface = clientInterface; | 
|  | 2610 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2611 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { | 
|  | 2612 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2613 | } | 
|  | 2614 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2615 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2616 | if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { | 
|  | 2617 | if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { | 
|  | 2618 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
|  | 2619 | defaultAudioPolicyConfig(); | 
|  | 2620 | } | 
|  | 2621 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2622 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2623 |  | 
|  | 2624 | // must be done after reading the policy | 
|  | 2625 | initializeVolumeCurves(); | 
|  | 2626 |  | 
|  | 2627 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2628 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 2629 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2630 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 2631 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); | 
|  | 2632 | if (mHwModules[i]->mHandle == 0) { | 
|  | 2633 | ALOGW("could not open HW module %s", mHwModules[i]->mName); | 
|  | 2634 | continue; | 
|  | 2635 | } | 
|  | 2636 | // open all output streams needed to access attached devices | 
|  | 2637 | // except for direct output streams that are only opened when they are actually | 
|  | 2638 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2639 | // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2640 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 2641 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2642 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2643 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2644 | if (outProfile->mSupportedDevices.isEmpty()) { | 
|  | 2645 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); | 
|  | 2646 | continue; | 
|  | 2647 | } | 
|  | 2648 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2649 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); | 
|  | 2650 | if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) { | 
|  | 2651 | profileType = mDefaultOutputDevice->mDeviceType; | 
|  | 2652 | } else { | 
|  | 2653 | profileType = outProfile->mSupportedDevices[0]->mDeviceType; | 
|  | 2654 | } | 
|  | 2655 | if ((profileType & outputDeviceTypes) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2656 | ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2657 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2658 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2659 | outputDesc->mDevice = profileType; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2660 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 2661 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 2662 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 2663 | config.format = outputDesc->mFormat; | 
|  | 2664 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 2665 | status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, | 
|  | 2666 | &output, | 
|  | 2667 | &config, | 
|  | 2668 | &outputDesc->mDevice, | 
|  | 2669 | String8(""), | 
|  | 2670 | &outputDesc->mLatency, | 
|  | 2671 | outputDesc->mFlags); | 
|  | 2672 |  | 
|  | 2673 | if (status != NO_ERROR) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2674 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 2675 | outputDesc->mDevice, | 
|  | 2676 | mHwModules[i]->mName); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2677 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2678 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 2679 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 2680 | outputDesc->mFormat = config.format; | 
|  | 2681 |  | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2682 | for (size_t k = 0; k  < outProfile->mSupportedDevices.size(); k++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2683 | audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2684 | ssize_t index = | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2685 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2686 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 2687 | if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) { | 
|  | 2688 | mAvailableOutputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2689 | mAvailableOutputDevices[index]->mModule = mHwModules[i]; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2690 | } | 
|  | 2691 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2692 | if (mPrimaryOutput == 0 && | 
|  | 2693 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 2694 | mPrimaryOutput = output; | 
|  | 2695 | } | 
|  | 2696 | addOutput(output, outputDesc); | 
|  | 2697 | setOutputDevice(output, | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2698 | outputDesc->mDevice, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2699 | true); | 
|  | 2700 | } | 
|  | 2701 | } | 
|  | 2702 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2703 | // open input streams needed to access attached devices to validate | 
|  | 2704 | // mAvailableInputDevices list | 
|  | 2705 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 2706 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2707 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2708 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2709 | if (inProfile->mSupportedDevices.isEmpty()) { | 
|  | 2710 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); | 
|  | 2711 | continue; | 
|  | 2712 | } | 
|  | 2713 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2714 | audio_devices_t profileType = inProfile->mSupportedDevices[0]->mDeviceType; | 
|  | 2715 | if (profileType & inputDeviceTypes) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2716 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2717 |  | 
|  | 2718 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2719 | inputDesc->mDevice = profileType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2720 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2721 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 2722 | config.sample_rate = inputDesc->mSamplingRate; | 
|  | 2723 | config.channel_mask = inputDesc->mChannelMask; | 
|  | 2724 | config.format = inputDesc->mFormat; | 
|  | 2725 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 2726 | status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, | 
|  | 2727 | &input, | 
|  | 2728 | &config, | 
|  | 2729 | &inputDesc->mDevice, | 
|  | 2730 | String8(""), | 
|  | 2731 | AUDIO_SOURCE_MIC, | 
|  | 2732 | AUDIO_INPUT_FLAG_NONE); | 
|  | 2733 |  | 
|  | 2734 | if (status == NO_ERROR) { | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2735 | for (size_t k = 0; k  < inProfile->mSupportedDevices.size(); k++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2736 | audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2737 | ssize_t index = | 
| Eric Laurent | 5b61ddd | 2014-05-07 09:10:01 -0700 | [diff] [blame] | 2738 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2739 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 2740 | if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) { | 
|  | 2741 | mAvailableInputDevices[index]->mId = nextUniqueId(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2742 | mAvailableInputDevices[index]->mModule = mHwModules[i]; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2743 | } | 
|  | 2744 | } | 
|  | 2745 | mpClientInterface->closeInput(input); | 
|  | 2746 | } else { | 
|  | 2747 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
|  | 2748 | inputDesc->mDevice, | 
|  | 2749 | mHwModules[i]->mName); | 
|  | 2750 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2751 | } | 
|  | 2752 | } | 
|  | 2753 | } | 
|  | 2754 | // make sure all attached devices have been allocated a unique ID | 
|  | 2755 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
|  | 2756 | if (mAvailableOutputDevices[i]->mId == 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2757 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2758 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 2759 | continue; | 
|  | 2760 | } | 
|  | 2761 | i++; | 
|  | 2762 | } | 
|  | 2763 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
|  | 2764 | if (mAvailableInputDevices[i]->mId == 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2765 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2766 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 2767 | continue; | 
|  | 2768 | } | 
|  | 2769 | i++; | 
|  | 2770 | } | 
|  | 2771 | // make sure default device is reachable | 
|  | 2772 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2773 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2774 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2775 |  | 
|  | 2776 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
|  | 2777 |  | 
|  | 2778 | updateDevicesAndOutputs(); | 
|  | 2779 |  | 
|  | 2780 | #ifdef AUDIO_POLICY_TEST | 
|  | 2781 | if (mPrimaryOutput != 0) { | 
|  | 2782 | AudioParameter outputCmd = AudioParameter(); | 
|  | 2783 | outputCmd.addInt(String8("set_id"), 0); | 
|  | 2784 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); | 
|  | 2785 |  | 
|  | 2786 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 2787 | mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2788 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 2789 | mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2790 | mTestLatencyMs = 0; | 
|  | 2791 | mCurOutput = 0; | 
|  | 2792 | mDirectOutput = false; | 
|  | 2793 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 2794 | mTestOutputs[i] = 0; | 
|  | 2795 | } | 
|  | 2796 |  | 
|  | 2797 | const size_t SIZE = 256; | 
|  | 2798 | char buffer[SIZE]; | 
|  | 2799 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
|  | 2800 | run(buffer, ANDROID_PRIORITY_AUDIO); | 
|  | 2801 | } | 
|  | 2802 | #endif //AUDIO_POLICY_TEST | 
|  | 2803 | } | 
|  | 2804 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2805 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2806 | { | 
|  | 2807 | #ifdef AUDIO_POLICY_TEST | 
|  | 2808 | exit(); | 
|  | 2809 | #endif //AUDIO_POLICY_TEST | 
|  | 2810 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2811 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2812 | } | 
|  | 2813 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2814 | mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2815 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2816 | mAvailableOutputDevices.clear(); | 
|  | 2817 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2818 | mOutputs.clear(); | 
|  | 2819 | mInputs.clear(); | 
|  | 2820 | mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2821 | } | 
|  | 2822 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2823 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2824 | { | 
|  | 2825 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; | 
|  | 2826 | } | 
|  | 2827 |  | 
|  | 2828 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2829 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2830 | { | 
|  | 2831 | ALOGV("entering threadLoop()"); | 
|  | 2832 | while (!exitPending()) | 
|  | 2833 | { | 
|  | 2834 | String8 command; | 
|  | 2835 | int valueInt; | 
|  | 2836 | String8 value; | 
|  | 2837 |  | 
|  | 2838 | Mutex::Autolock _l(mLock); | 
|  | 2839 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
|  | 2840 |  | 
|  | 2841 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
|  | 2842 | AudioParameter param = AudioParameter(command); | 
|  | 2843 |  | 
|  | 2844 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
|  | 2845 | valueInt != 0) { | 
|  | 2846 | ALOGV("Test command %s received", command.string()); | 
|  | 2847 | String8 target; | 
|  | 2848 | if (param.get(String8("target"), target) != NO_ERROR) { | 
|  | 2849 | target = "Manager"; | 
|  | 2850 | } | 
|  | 2851 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
|  | 2852 | param.remove(String8("test_cmd_policy_output")); | 
|  | 2853 | mCurOutput = valueInt; | 
|  | 2854 | } | 
|  | 2855 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
|  | 2856 | param.remove(String8("test_cmd_policy_direct")); | 
|  | 2857 | if (value == "false") { | 
|  | 2858 | mDirectOutput = false; | 
|  | 2859 | } else if (value == "true") { | 
|  | 2860 | mDirectOutput = true; | 
|  | 2861 | } | 
|  | 2862 | } | 
|  | 2863 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
|  | 2864 | param.remove(String8("test_cmd_policy_input")); | 
|  | 2865 | mTestInput = valueInt; | 
|  | 2866 | } | 
|  | 2867 |  | 
|  | 2868 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
|  | 2869 | param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2870 | int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2871 | if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2872 | format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2873 | } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2874 | format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2875 | } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2876 | format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2877 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2878 | if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2879 | if (target == "Manager") { | 
|  | 2880 | mTestFormat = format; | 
|  | 2881 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2882 | AudioParameter outputParam = AudioParameter(); | 
|  | 2883 | outputParam.addInt(String8("format"), format); | 
|  | 2884 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2885 | } | 
|  | 2886 | } | 
|  | 2887 | } | 
|  | 2888 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
|  | 2889 | param.remove(String8("test_cmd_policy_channels")); | 
|  | 2890 | int channels = 0; | 
|  | 2891 |  | 
|  | 2892 | if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2893 | channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2894 | } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2895 | channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2896 | } | 
|  | 2897 | if (channels != 0) { | 
|  | 2898 | if (target == "Manager") { | 
|  | 2899 | mTestChannels = channels; | 
|  | 2900 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2901 | AudioParameter outputParam = AudioParameter(); | 
|  | 2902 | outputParam.addInt(String8("channels"), channels); | 
|  | 2903 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2904 | } | 
|  | 2905 | } | 
|  | 2906 | } | 
|  | 2907 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
|  | 2908 | param.remove(String8("test_cmd_policy_sampleRate")); | 
|  | 2909 | if (valueInt >= 0 && valueInt <= 96000) { | 
|  | 2910 | int samplingRate = valueInt; | 
|  | 2911 | if (target == "Manager") { | 
|  | 2912 | mTestSamplingRate = samplingRate; | 
|  | 2913 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 2914 | AudioParameter outputParam = AudioParameter(); | 
|  | 2915 | outputParam.addInt(String8("sampling_rate"), samplingRate); | 
|  | 2916 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 2917 | } | 
|  | 2918 | } | 
|  | 2919 | } | 
|  | 2920 |  | 
|  | 2921 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
|  | 2922 | param.remove(String8("test_cmd_policy_reopen")); | 
|  | 2923 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2924 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2925 | mpClientInterface->closeOutput(mPrimaryOutput); | 
|  | 2926 |  | 
|  | 2927 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; | 
|  | 2928 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2929 | mOutputs.removeItem(mPrimaryOutput); | 
|  | 2930 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2931 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2932 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2933 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 2934 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 2935 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 2936 | config.format = outputDesc->mFormat; | 
|  | 2937 | status_t status = mpClientInterface->openOutput(moduleHandle, | 
|  | 2938 | &mPrimaryOutput, | 
|  | 2939 | &config, | 
|  | 2940 | &outputDesc->mDevice, | 
|  | 2941 | String8(""), | 
|  | 2942 | &outputDesc->mLatency, | 
|  | 2943 | outputDesc->mFlags); | 
|  | 2944 | if (status != NO_ERROR) { | 
|  | 2945 | ALOGE("Failed to reopen hardware output stream, " | 
|  | 2946 | "samplingRate: %d, format %d, channels %d", | 
|  | 2947 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2948 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2949 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 2950 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 2951 | outputDesc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2952 | AudioParameter outputCmd = AudioParameter(); | 
|  | 2953 | outputCmd.addInt(String8("set_id"), 0); | 
|  | 2954 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); | 
|  | 2955 | addOutput(mPrimaryOutput, outputDesc); | 
|  | 2956 | } | 
|  | 2957 | } | 
|  | 2958 |  | 
|  | 2959 |  | 
|  | 2960 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
|  | 2961 | } | 
|  | 2962 | } | 
|  | 2963 | return false; | 
|  | 2964 | } | 
|  | 2965 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2966 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2967 | { | 
|  | 2968 | { | 
|  | 2969 | AutoMutex _l(mLock); | 
|  | 2970 | requestExit(); | 
|  | 2971 | mWaitWorkCV.signal(); | 
|  | 2972 | } | 
|  | 2973 | requestExitAndWait(); | 
|  | 2974 | } | 
|  | 2975 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2976 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2977 | { | 
|  | 2978 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 2979 | if (output == mTestOutputs[i]) return i; | 
|  | 2980 | } | 
|  | 2981 | return 0; | 
|  | 2982 | } | 
|  | 2983 | #endif //AUDIO_POLICY_TEST | 
|  | 2984 |  | 
|  | 2985 | // --- | 
|  | 2986 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2987 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2988 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2989 | outputDesc->mIoHandle = output; | 
|  | 2990 | outputDesc->mId = nextUniqueId(); | 
|  | 2991 | mOutputs.add(output, outputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2992 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2993 | } | 
|  | 2994 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2995 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2996 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2997 | inputDesc->mIoHandle = input; | 
|  | 2998 | inputDesc->mId = nextUniqueId(); | 
|  | 2999 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3000 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3001 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3002 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3003 | void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/, | 
|  | 3004 | const String8 address /*in*/, | 
|  | 3005 | SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
|  | 3006 | // look for a match on the given address on the addresses of the outputs: | 
|  | 3007 | // find the address by finding the patch that maps to this output | 
|  | 3008 | ssize_t patchIdx = mAudioPatches.indexOfKey(desc->mPatchHandle); | 
|  | 3009 | //ALOGV("    inspecting output %d (patch %d) for supported device=0x%x", | 
|  | 3010 | //        outputIdx, patchIdx,  desc->mProfile->mSupportedDevices.types()); | 
|  | 3011 | if (patchIdx >= 0) { | 
|  | 3012 | const sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patchIdx); | 
|  | 3013 | const int numSinks = patchDesc->mPatch.num_sinks; | 
|  | 3014 | for (ssize_t j=0; j < numSinks; j++) { | 
|  | 3015 | if (patchDesc->mPatch.sinks[j].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3016 | const char* patchAddr = | 
|  | 3017 | patchDesc->mPatch.sinks[j].ext.device.address; | 
|  | 3018 | if (strncmp(patchAddr, | 
|  | 3019 | address.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3020 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3021 | desc->mIoHandle,  patchDesc->mPatch.sinks[j].ext.device.address); | 
|  | 3022 | outputs.add(desc->mIoHandle); | 
|  | 3023 | break; | 
|  | 3024 | } | 
|  | 3025 | } | 
|  | 3026 | } | 
|  | 3027 | } | 
|  | 3028 | } | 
|  | 3029 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3030 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3031 | audio_policy_dev_state_t state, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3032 | SortedVector<audio_io_handle_t>& outputs, | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3033 | const String8 address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3034 | { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3035 | audio_devices_t device = devDesc->mDeviceType; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3036 | sp<AudioOutputDescriptor> desc; | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3037 | // erase all current sample rates, formats and channel masks | 
|  | 3038 | devDesc->clearCapabilities(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3039 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3040 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3041 | // first list already open outputs that can be routed to this device | 
|  | 3042 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3043 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3044 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3045 | if (!deviceDistinguishesOnAddress(device)) { | 
|  | 3046 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 3047 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3048 | } else { | 
|  | 3049 | ALOGV("  checking address match due to device 0x%x", device); | 
|  | 3050 | findIoHandlesByAddress(desc, address, outputs); | 
|  | 3051 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3052 | } | 
|  | 3053 | } | 
|  | 3054 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3055 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3056 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3057 | { | 
|  | 3058 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3059 | continue; | 
|  | 3060 | } | 
|  | 3061 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3062 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3063 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3064 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3065 | profiles.add(mHwModules[i]->mOutputProfiles[j]); | 
|  | 3066 | } | 
|  | 3067 | } | 
|  | 3068 | } | 
|  | 3069 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3070 | ALOGV("  found %d profiles, %d outputs", profiles.size(), outputs.size()); | 
|  | 3071 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3072 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 3073 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3074 | return BAD_VALUE; | 
|  | 3075 | } | 
|  | 3076 |  | 
|  | 3077 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 3078 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3079 | 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] | 3080 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3081 |  | 
|  | 3082 | // nothing to do if one output is already opened for this profile | 
|  | 3083 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3084 | for (j = 0; j < outputs.size(); j++) { | 
|  | 3085 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3086 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3087 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 3088 | // by the profile in our device descriptor | 
|  | 3089 | devDesc->importAudioPort(profile); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3090 | break; | 
|  | 3091 | } | 
|  | 3092 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3093 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3094 | continue; | 
|  | 3095 | } | 
|  | 3096 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3097 | ALOGV("opening output for device %08x with params %s profile %p", | 
|  | 3098 | device, address.string(), profile.get()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3099 | desc = new AudioOutputDescriptor(profile); | 
|  | 3100 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3101 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3102 | config.sample_rate = desc->mSamplingRate; | 
|  | 3103 | config.channel_mask = desc->mChannelMask; | 
|  | 3104 | config.format = desc->mFormat; | 
|  | 3105 | config.offload_info.sample_rate = desc->mSamplingRate; | 
|  | 3106 | config.offload_info.channel_mask = desc->mChannelMask; | 
|  | 3107 | config.offload_info.format = desc->mFormat; | 
|  | 3108 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 3109 | status_t status = mpClientInterface->openOutput(profile->mModule->mHandle, | 
|  | 3110 | &output, | 
|  | 3111 | &config, | 
|  | 3112 | &desc->mDevice, | 
|  | 3113 | address, | 
|  | 3114 | &desc->mLatency, | 
|  | 3115 | desc->mFlags); | 
|  | 3116 | if (status == NO_ERROR) { | 
|  | 3117 | desc->mSamplingRate = config.sample_rate; | 
|  | 3118 | desc->mChannelMask = config.channel_mask; | 
|  | 3119 | desc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3120 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3121 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3122 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3123 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3124 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 3125 | free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3126 | } | 
|  | 3127 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3128 | // Here is where we step through and resolve any "dynamic" fields | 
|  | 3129 | String8 reply; | 
|  | 3130 | char *value; | 
|  | 3131 | if (profile->mSamplingRates[0] == 0) { | 
|  | 3132 | reply = mpClientInterface->getParameters(output, | 
|  | 3133 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3134 | ALOGV("checkOutputsForDevice() supported sampling rates %s", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3135 | reply.string()); | 
|  | 3136 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3137 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3138 | profile->loadSamplingRates(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3139 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3140 | } | 
|  | 3141 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 3142 | reply = mpClientInterface->getParameters(output, | 
|  | 3143 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3144 | ALOGV("checkOutputsForDevice() supported formats %s", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3145 | reply.string()); | 
|  | 3146 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3147 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3148 | profile->loadFormats(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3149 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3150 | } | 
|  | 3151 | if (profile->mChannelMasks[0] == 0) { | 
|  | 3152 | reply = mpClientInterface->getParameters(output, | 
|  | 3153 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3154 | ALOGV("checkOutputsForDevice() supported channel masks %s", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3155 | reply.string()); | 
|  | 3156 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3157 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3158 | profile->loadOutChannels(value + 1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3159 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3160 | } | 
|  | 3161 | if (((profile->mSamplingRates[0] == 0) && | 
|  | 3162 | (profile->mSamplingRates.size() < 2)) || | 
|  | 3163 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && | 
|  | 3164 | (profile->mFormats.size() < 2)) || | 
|  | 3165 | ((profile->mChannelMasks[0] == 0) && | 
|  | 3166 | (profile->mChannelMasks.size() < 2))) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3167 | ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3168 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3169 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3170 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || | 
|  | 3171 | profile->mChannelMasks[0] == 0) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3172 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3173 | config.sample_rate = profile->pickSamplingRate(); | 
|  | 3174 | config.channel_mask = profile->pickChannelMask(); | 
|  | 3175 | config.format = profile->pickFormat(); | 
|  | 3176 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 3177 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 3178 | config.offload_info.format = config.format; | 
|  | 3179 | status = mpClientInterface->openOutput(profile->mModule->mHandle, | 
|  | 3180 | &output, | 
|  | 3181 | &config, | 
|  | 3182 | &desc->mDevice, | 
|  | 3183 | address, | 
|  | 3184 | &desc->mLatency, | 
|  | 3185 | desc->mFlags); | 
|  | 3186 | if (status == NO_ERROR) { | 
|  | 3187 | desc->mSamplingRate = config.sample_rate; | 
|  | 3188 | desc->mChannelMask = config.channel_mask; | 
|  | 3189 | desc->mFormat = config.format; | 
|  | 3190 | } else { | 
|  | 3191 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 3192 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3193 | } | 
|  | 3194 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3195 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3196 | addOutput(output, desc); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3197 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3198 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3199 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3200 | // set initial stream volume for device | 
|  | 3201 | applyStreamVolumes(output, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3202 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3203 | //TODO: configure audio effect output stage here | 
|  | 3204 |  | 
|  | 3205 | // open a duplicating output thread for the new output and the primary output | 
|  | 3206 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, | 
|  | 3207 | mPrimaryOutput); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3208 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3209 | // add duplicated output descriptor | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3210 | sp<AudioOutputDescriptor> dupOutputDesc = | 
|  | 3211 | new AudioOutputDescriptor(NULL); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3212 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); | 
|  | 3213 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); | 
|  | 3214 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
|  | 3215 | dupOutputDesc->mFormat = desc->mFormat; | 
|  | 3216 | dupOutputDesc->mChannelMask = desc->mChannelMask; | 
|  | 3217 | dupOutputDesc->mLatency = desc->mLatency; | 
|  | 3218 | addOutput(duplicatedOutput, dupOutputDesc); | 
|  | 3219 | applyStreamVolumes(duplicatedOutput, device, 0, true); | 
|  | 3220 | } else { | 
|  | 3221 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
|  | 3222 | mPrimaryOutput, output); | 
|  | 3223 | mpClientInterface->closeOutput(output); | 
|  | 3224 | mOutputs.removeItem(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3225 | nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3226 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3227 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3228 | } | 
|  | 3229 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3230 | } else { | 
|  | 3231 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3232 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3233 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3234 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3235 | profiles.removeAt(profile_index); | 
|  | 3236 | profile_index--; | 
|  | 3237 | } else { | 
|  | 3238 | outputs.add(output); | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3239 | devDesc->importAudioPort(profile); | 
|  | 3240 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3241 | if (deviceDistinguishesOnAddress(device)) { | 
|  | 3242 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
|  | 3243 | device, address.string()); | 
|  | 3244 | setOutputDevice(output, device, true/*force*/, 0/*delay*/, | 
|  | 3245 | NULL/*patch handle*/, address.string()); | 
|  | 3246 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3247 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 3248 | } | 
|  | 3249 | } | 
|  | 3250 |  | 
|  | 3251 | if (profiles.isEmpty()) { | 
|  | 3252 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3253 | return BAD_VALUE; | 
|  | 3254 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3255 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3256 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 3257 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3258 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3259 | if (!desc->isDuplicated()) { | 
|  | 3260 | if  (!(desc->mProfile->mSupportedDevices.types() | 
|  | 3261 | & mAvailableOutputDevices.types())) { | 
|  | 3262 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 3263 | mOutputs.keyAt(i)); | 
|  | 3264 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3265 | } else if (deviceDistinguishesOnAddress(device) && | 
|  | 3266 | // exact match on device | 
|  | 3267 | (desc->mProfile->mSupportedDevices.types() == device)) { | 
|  | 3268 | findIoHandlesByAddress(desc, address, outputs); | 
|  | 3269 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3270 | } | 
|  | 3271 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3272 | // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3273 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3274 | { | 
|  | 3275 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3276 | continue; | 
|  | 3277 | } | 
|  | 3278 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3279 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3280 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3281 | if (profile->mSupportedDevices.types() & device) { | 
|  | 3282 | ALOGV("checkOutputsForDevice(): " | 
|  | 3283 | "clearing direct output profile %zu on module %zu", j, i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3284 | if (profile->mSamplingRates[0] == 0) { | 
|  | 3285 | profile->mSamplingRates.clear(); | 
|  | 3286 | profile->mSamplingRates.add(0); | 
|  | 3287 | } | 
|  | 3288 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 3289 | profile->mFormats.clear(); | 
|  | 3290 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 3291 | } | 
|  | 3292 | if (profile->mChannelMasks[0] == 0) { | 
|  | 3293 | profile->mChannelMasks.clear(); | 
|  | 3294 | profile->mChannelMasks.add(0); | 
|  | 3295 | } | 
|  | 3296 | } | 
|  | 3297 | } | 
|  | 3298 | } | 
|  | 3299 | } | 
|  | 3300 | return NO_ERROR; | 
|  | 3301 | } | 
|  | 3302 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3303 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, | 
|  | 3304 | audio_policy_dev_state_t state, | 
|  | 3305 | SortedVector<audio_io_handle_t>& inputs, | 
|  | 3306 | const String8 address) | 
|  | 3307 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3308 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3309 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 3310 | // first list already open inputs that can be routed to this device | 
|  | 3311 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3312 | desc = mInputs.valueAt(input_index); | 
|  | 3313 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 3314 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 3315 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 3316 | } | 
|  | 3317 | } | 
|  | 3318 |  | 
|  | 3319 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3320 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3321 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
|  | 3322 | { | 
|  | 3323 | if (mHwModules[module_idx]->mHandle == 0) { | 
|  | 3324 | continue; | 
|  | 3325 | } | 
|  | 3326 | for (size_t profile_index = 0; | 
|  | 3327 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
|  | 3328 | profile_index++) | 
|  | 3329 | { | 
|  | 3330 | if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types() | 
|  | 3331 | & (device & ~AUDIO_DEVICE_BIT_IN)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3332 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3333 | profile_index, module_idx); | 
|  | 3334 | profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]); | 
|  | 3335 | } | 
|  | 3336 | } | 
|  | 3337 | } | 
|  | 3338 |  | 
|  | 3339 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 3340 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 3341 | return BAD_VALUE; | 
|  | 3342 | } | 
|  | 3343 |  | 
|  | 3344 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 3345 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3346 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 3347 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3348 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3349 | // nothing to do if one input is already opened for this profile | 
|  | 3350 | size_t input_index; | 
|  | 3351 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3352 | desc = mInputs.valueAt(input_index); | 
|  | 3353 | if (desc->mProfile == profile) { | 
|  | 3354 | break; | 
|  | 3355 | } | 
|  | 3356 | } | 
|  | 3357 | if (input_index != mInputs.size()) { | 
|  | 3358 | continue; | 
|  | 3359 | } | 
|  | 3360 |  | 
|  | 3361 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
|  | 3362 | desc = new AudioInputDescriptor(profile); | 
|  | 3363 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3364 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3365 | config.sample_rate = desc->mSamplingRate; | 
|  | 3366 | config.channel_mask = desc->mChannelMask; | 
|  | 3367 | config.format = desc->mFormat; | 
|  | 3368 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 3369 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, | 
|  | 3370 | &input, | 
|  | 3371 | &config, | 
|  | 3372 | &desc->mDevice, | 
|  | 3373 | address, | 
|  | 3374 | AUDIO_SOURCE_MIC, | 
|  | 3375 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3376 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3377 | if (status == NO_ERROR) { | 
|  | 3378 | desc->mSamplingRate = config.sample_rate; | 
|  | 3379 | desc->mChannelMask = config.channel_mask; | 
|  | 3380 | desc->mFormat = config.format; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3381 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3382 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3383 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 3384 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 3385 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3386 | } | 
|  | 3387 |  | 
|  | 3388 | // Here is where we step through and resolve any "dynamic" fields | 
|  | 3389 | String8 reply; | 
|  | 3390 | char *value; | 
|  | 3391 | if (profile->mSamplingRates[0] == 0) { | 
|  | 3392 | reply = mpClientInterface->getParameters(input, | 
|  | 3393 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); | 
|  | 3394 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", | 
|  | 3395 | reply.string()); | 
|  | 3396 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3397 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3398 | profile->loadSamplingRates(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3399 | } | 
|  | 3400 | } | 
|  | 3401 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 3402 | reply = mpClientInterface->getParameters(input, | 
|  | 3403 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
|  | 3404 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); | 
|  | 3405 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3406 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3407 | profile->loadFormats(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3408 | } | 
|  | 3409 | } | 
|  | 3410 | if (profile->mChannelMasks[0] == 0) { | 
|  | 3411 | reply = mpClientInterface->getParameters(input, | 
|  | 3412 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); | 
|  | 3413 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", | 
|  | 3414 | reply.string()); | 
|  | 3415 | value = strpbrk((char *)reply.string(), "="); | 
|  | 3416 | if (value != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3417 | profile->loadInChannels(value + 1); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3418 | } | 
|  | 3419 | } | 
|  | 3420 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || | 
|  | 3421 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || | 
|  | 3422 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { | 
|  | 3423 | ALOGW("checkInputsForDevice() direct input missing param"); | 
|  | 3424 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3425 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3426 | } | 
|  | 3427 |  | 
|  | 3428 | if (input != 0) { | 
|  | 3429 | addInput(input, desc); | 
|  | 3430 | } | 
|  | 3431 | } // endif input != 0 | 
|  | 3432 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3433 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3434 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3435 | profiles.removeAt(profile_index); | 
|  | 3436 | profile_index--; | 
|  | 3437 | } else { | 
|  | 3438 | inputs.add(input); | 
|  | 3439 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 3440 | } | 
|  | 3441 | } // end scan profiles | 
|  | 3442 |  | 
|  | 3443 | if (profiles.isEmpty()) { | 
|  | 3444 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 3445 | return BAD_VALUE; | 
|  | 3446 | } | 
|  | 3447 | } else { | 
|  | 3448 | // Disconnect | 
|  | 3449 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 3450 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 3451 | desc = mInputs.valueAt(input_index); | 
|  | 3452 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types())) { | 
|  | 3453 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 3454 | mInputs.keyAt(input_index)); | 
|  | 3455 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 3456 | } | 
|  | 3457 | } | 
|  | 3458 | // Clear any profiles associated with the disconnected device. | 
|  | 3459 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
|  | 3460 | if (mHwModules[module_index]->mHandle == 0) { | 
|  | 3461 | continue; | 
|  | 3462 | } | 
|  | 3463 | for (size_t profile_index = 0; | 
|  | 3464 | profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
|  | 3465 | profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3466 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3467 | if (profile->mSupportedDevices.types() & device) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3468 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3469 | profile_index, module_index); | 
|  | 3470 | if (profile->mSamplingRates[0] == 0) { | 
|  | 3471 | profile->mSamplingRates.clear(); | 
|  | 3472 | profile->mSamplingRates.add(0); | 
|  | 3473 | } | 
|  | 3474 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { | 
|  | 3475 | profile->mFormats.clear(); | 
|  | 3476 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 3477 | } | 
|  | 3478 | if (profile->mChannelMasks[0] == 0) { | 
|  | 3479 | profile->mChannelMasks.clear(); | 
|  | 3480 | profile->mChannelMasks.add(0); | 
|  | 3481 | } | 
|  | 3482 | } | 
|  | 3483 | } | 
|  | 3484 | } | 
|  | 3485 | } // end disconnect | 
|  | 3486 |  | 
|  | 3487 | return NO_ERROR; | 
|  | 3488 | } | 
|  | 3489 |  | 
|  | 3490 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3491 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3492 | { | 
|  | 3493 | ALOGV("closeOutput(%d)", output); | 
|  | 3494 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3495 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3496 | if (outputDesc == NULL) { | 
|  | 3497 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 3498 | return; | 
|  | 3499 | } | 
|  | 3500 |  | 
|  | 3501 | // look for duplicated outputs connected to the output being removed. | 
|  | 3502 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3503 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3504 | if (dupOutputDesc->isDuplicated() && | 
|  | 3505 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 3506 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3507 | sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3508 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 3509 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 3510 | } else { | 
|  | 3511 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 3512 | } | 
|  | 3513 | // As all active tracks on duplicated output will be deleted, | 
|  | 3514 | // and as they were also referenced on the other output, the reference | 
|  | 3515 | // count for their stream type must be adjusted accordingly on | 
|  | 3516 | // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3517 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3518 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3519 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3520 | } | 
|  | 3521 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 3522 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 3523 |  | 
|  | 3524 | mpClientInterface->closeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 | mOutputs.removeItem(duplicatedOutput); | 
|  | 3526 | } | 
|  | 3527 | } | 
|  | 3528 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3529 | nextAudioPortGeneration(); | 
|  | 3530 |  | 
|  | 3531 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 3532 | if (index >= 0) { | 
|  | 3533 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3534 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3535 | mAudioPatches.removeItemsAt(index); | 
|  | 3536 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 3537 | } | 
|  | 3538 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3539 | AudioParameter param; | 
|  | 3540 | param.add(String8("closing"), String8("true")); | 
|  | 3541 | mpClientInterface->setParameters(output, param.toString()); | 
|  | 3542 |  | 
|  | 3543 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | mOutputs.removeItem(output); | 
|  | 3545 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3546 | } | 
|  | 3547 |  | 
|  | 3548 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 3549 | { | 
|  | 3550 | ALOGV("closeInput(%d)", input); | 
|  | 3551 |  | 
|  | 3552 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 3553 | if (inputDesc == NULL) { | 
|  | 3554 | ALOGW("closeInput() unknown input %d", input); | 
|  | 3555 | return; | 
|  | 3556 | } | 
|  | 3557 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3558 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3559 |  | 
|  | 3560 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 3561 | if (index >= 0) { | 
|  | 3562 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3563 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3564 | mAudioPatches.removeItemsAt(index); | 
|  | 3565 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 3566 | } | 
|  | 3567 |  | 
|  | 3568 | mpClientInterface->closeInput(input); | 
|  | 3569 | mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3570 | } | 
|  | 3571 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3572 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3573 | DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3574 | { | 
|  | 3575 | SortedVector<audio_io_handle_t> outputs; | 
|  | 3576 |  | 
|  | 3577 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 3578 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
|  | 3579 | ALOGVV("output %d isDuplicated=%d device=%04x", | 
|  | 3580 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); | 
|  | 3581 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 3582 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 3583 | outputs.add(openOutputs.keyAt(i)); | 
|  | 3584 | } | 
|  | 3585 | } | 
|  | 3586 | return outputs; | 
|  | 3587 | } | 
|  | 3588 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3589 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3590 | SortedVector<audio_io_handle_t>& outputs2) | 
|  | 3591 | { | 
|  | 3592 | if (outputs1.size() != outputs2.size()) { | 
|  | 3593 | return false; | 
|  | 3594 | } | 
|  | 3595 | for (size_t i = 0; i < outputs1.size(); i++) { | 
|  | 3596 | if (outputs1[i] != outputs2[i]) { | 
|  | 3597 | return false; | 
|  | 3598 | } | 
|  | 3599 | } | 
|  | 3600 | return true; | 
|  | 3601 | } | 
|  | 3602 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3603 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3604 | { | 
|  | 3605 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 3606 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 3607 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 3608 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 3609 |  | 
|  | 3610 | if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
|  | 3611 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 3612 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 3613 | // mute strategy while moving tracks from one output to another | 
|  | 3614 | for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3615 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3616 | if (desc->isStrategyActive(strategy)) { | 
|  | 3617 | setStrategyMute(strategy, true, srcOutputs[i]); | 
|  | 3618 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); | 
|  | 3619 | } | 
|  | 3620 | } | 
|  | 3621 |  | 
|  | 3622 | // Move effects associated to this strategy from previous output to new output | 
|  | 3623 | if (strategy == STRATEGY_MEDIA) { | 
|  | 3624 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); | 
|  | 3625 | SortedVector<audio_io_handle_t> moved; | 
|  | 3626 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3627 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
|  | 3628 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && | 
|  | 3629 | effectDesc->mIo != fxOutput) { | 
|  | 3630 | if (moved.indexOf(effectDesc->mIo) < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3631 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", | 
|  | 3632 | mEffects.keyAt(i), fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3633 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3634 | fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3635 | moved.add(effectDesc->mIo); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3636 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3637 | effectDesc->mIo = fxOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3638 | } | 
|  | 3639 | } | 
|  | 3640 | } | 
|  | 3641 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3642 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
|  | 3643 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 3644 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3645 | } | 
|  | 3646 | } | 
|  | 3647 | } | 
|  | 3648 | } | 
|  | 3649 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3650 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3651 | { | 
|  | 3652 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
|  | 3653 | checkOutputForStrategy(STRATEGY_PHONE); | 
|  | 3654 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 3655 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 3656 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 3657 | checkOutputForStrategy(STRATEGY_DTMF); | 
|  | 3658 | } | 
|  | 3659 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3660 | audio_io_handle_t AudioPolicyManager::getA2dpOutput() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3661 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3662 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3663 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3664 | if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { | 
|  | 3665 | return mOutputs.keyAt(i); | 
|  | 3666 | } | 
|  | 3667 | } | 
|  | 3668 |  | 
|  | 3669 | return 0; | 
|  | 3670 | } | 
|  | 3671 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3672 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3673 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3674 | audio_io_handle_t a2dpOutput = getA2dpOutput(); | 
|  | 3675 | if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3676 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3677 | return; | 
|  | 3678 | } | 
|  | 3679 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3680 | bool isScoConnected = | 
|  | 3681 | (mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3682 | // suspend A2DP output if: | 
|  | 3683 | //      (NOT already suspended) && | 
|  | 3684 | //      ((SCO device is connected && | 
|  | 3685 | //       (forced usage for communication || for record is SCO))) || | 
|  | 3686 | //      (phone state is ringing || in call) | 
|  | 3687 | // | 
|  | 3688 | // restore A2DP output if: | 
|  | 3689 | //      (Already suspended) && | 
|  | 3690 | //      ((SCO device is NOT connected || | 
|  | 3691 | //       (forced usage NOT for communication && NOT for record is SCO))) && | 
|  | 3692 | //      (phone state is NOT ringing && NOT in call) | 
|  | 3693 | // | 
|  | 3694 | if (mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3695 | if ((!isScoConnected || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3696 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 3697 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && | 
|  | 3698 | ((mPhoneState != AUDIO_MODE_IN_CALL) && | 
|  | 3699 | (mPhoneState != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3700 |  | 
|  | 3701 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 3702 | mA2dpSuspended = false; | 
|  | 3703 | } | 
|  | 3704 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3705 | if ((isScoConnected && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3706 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 3707 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || | 
|  | 3708 | ((mPhoneState == AUDIO_MODE_IN_CALL) || | 
|  | 3709 | (mPhoneState == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3710 |  | 
|  | 3711 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 3712 | mA2dpSuspended = true; | 
|  | 3713 | } | 
|  | 3714 | } | 
|  | 3715 | } | 
|  | 3716 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3717 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3718 | { | 
|  | 3719 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 3720 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3721 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3722 |  | 
|  | 3723 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 3724 | if (index >= 0) { | 
|  | 3725 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3726 | if (patchDesc->mUid != mUidCached) { | 
|  | 3727 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
|  | 3728 | outputDesc->device(), outputDesc->mPatchHandle); | 
|  | 3729 | return outputDesc->device(); | 
|  | 3730 | } | 
|  | 3731 | } | 
|  | 3732 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3733 | // check the following by order of priority to request a routing change if necessary: | 
|  | 3734 | // 1: the strategy enforced audible is active on the output: | 
|  | 3735 | //      use device for strategy enforced audible | 
|  | 3736 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 3737 | //      use device for strategy phone | 
|  | 3738 | // 3: the strategy sonification is active on the output: | 
|  | 3739 | //      use device for strategy sonification | 
|  | 3740 | // 4: the strategy "respectful" sonification is active on the output: | 
|  | 3741 | //      use device for strategy "respectful" sonification | 
|  | 3742 | // 5: the strategy media is active on the output: | 
|  | 3743 | //      use device for strategy media | 
|  | 3744 | // 6: the strategy DTMF is active on the output: | 
|  | 3745 | //      use device for strategy DTMF | 
|  | 3746 | if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { | 
|  | 3747 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 3748 | } else if (isInCall() || | 
|  | 3749 | outputDesc->isStrategyActive(STRATEGY_PHONE)) { | 
|  | 3750 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
|  | 3751 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { | 
|  | 3752 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
|  | 3753 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { | 
|  | 3754 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
|  | 3755 | } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { | 
|  | 3756 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
|  | 3757 | } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { | 
|  | 3758 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
|  | 3759 | } | 
|  | 3760 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3761 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 3762 | return device; | 
|  | 3763 | } | 
|  | 3764 |  | 
|  | 3765 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) | 
|  | 3766 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3767 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3768 |  | 
|  | 3769 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 3770 | if (index >= 0) { | 
|  | 3771 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 3772 | if (patchDesc->mUid != mUidCached) { | 
|  | 3773 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
|  | 3774 | inputDesc->mDevice, inputDesc->mPatchHandle); | 
|  | 3775 | return inputDesc->mDevice; | 
|  | 3776 | } | 
|  | 3777 | } | 
|  | 3778 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3779 | audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource); | 
|  | 3780 |  | 
|  | 3781 | ALOGV("getNewInputDevice() selected device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3782 | return device; | 
|  | 3783 | } | 
|  | 3784 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3785 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3786 | return (uint32_t)getStrategy(stream); | 
|  | 3787 | } | 
|  | 3788 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3789 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3790 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 3791 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 3792 | // 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] | 3793 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3794 | return AUDIO_DEVICE_NONE; | 
|  | 3795 | } | 
|  | 3796 | audio_devices_t devices; | 
|  | 3797 | AudioPolicyManager::routing_strategy strategy = getStrategy(stream); | 
|  | 3798 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 3799 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); | 
|  | 3800 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3801 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3802 | if (outputDesc->isStrategyActive(strategy)) { | 
|  | 3803 | devices = outputDesc->device(); | 
|  | 3804 | break; | 
|  | 3805 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3806 | } | 
|  | 3807 | return devices; | 
|  | 3808 | } | 
|  | 3809 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3810 | AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy( | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3811 | audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3812 | // stream to strategy mapping | 
|  | 3813 | switch (stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3814 | case AUDIO_STREAM_VOICE_CALL: | 
|  | 3815 | case AUDIO_STREAM_BLUETOOTH_SCO: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3816 | return STRATEGY_PHONE; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3817 | case AUDIO_STREAM_RING: | 
|  | 3818 | case AUDIO_STREAM_ALARM: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | return STRATEGY_SONIFICATION; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3820 | case AUDIO_STREAM_NOTIFICATION: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3821 | return STRATEGY_SONIFICATION_RESPECTFUL; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3822 | case AUDIO_STREAM_DTMF: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3823 | return STRATEGY_DTMF; | 
|  | 3824 | default: | 
|  | 3825 | ALOGE("unknown stream type"); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3826 | case AUDIO_STREAM_SYSTEM: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3827 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs | 
|  | 3828 | // while key clicks are played produces a poor result | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3829 | case AUDIO_STREAM_TTS: | 
|  | 3830 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3831 | return STRATEGY_MEDIA; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3832 | case AUDIO_STREAM_ENFORCED_AUDIBLE: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3833 | return STRATEGY_ENFORCED_AUDIBLE; | 
|  | 3834 | } | 
|  | 3835 | } | 
|  | 3836 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3837 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 3838 | // flags to strategy mapping | 
|  | 3839 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 3840 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 3841 | } | 
|  | 3842 |  | 
|  | 3843 | // usage to strategy mapping | 
|  | 3844 | switch (attr->usage) { | 
|  | 3845 | case AUDIO_USAGE_MEDIA: | 
|  | 3846 | case AUDIO_USAGE_GAME: | 
|  | 3847 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 3848 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 3849 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 3850 | return (uint32_t) STRATEGY_MEDIA; | 
|  | 3851 |  | 
|  | 3852 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 3853 | return (uint32_t) STRATEGY_PHONE; | 
|  | 3854 |  | 
|  | 3855 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 3856 | return (uint32_t) STRATEGY_DTMF; | 
|  | 3857 |  | 
|  | 3858 | case AUDIO_USAGE_ALARM: | 
|  | 3859 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 3860 | return (uint32_t) STRATEGY_SONIFICATION; | 
|  | 3861 |  | 
|  | 3862 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 3863 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 3864 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 3865 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 3866 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 3867 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; | 
|  | 3868 |  | 
|  | 3869 | case AUDIO_USAGE_UNKNOWN: | 
|  | 3870 | default: | 
|  | 3871 | return (uint32_t) STRATEGY_MEDIA; | 
|  | 3872 | } | 
|  | 3873 | } | 
|  | 3874 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3875 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3876 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3877 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3878 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 3879 | updateDevicesAndOutputs(); | 
|  | 3880 | break; | 
|  | 3881 | default: | 
|  | 3882 | break; | 
|  | 3883 | } | 
|  | 3884 | } | 
|  | 3885 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3886 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3887 | bool fromCache) | 
|  | 3888 | { | 
|  | 3889 | uint32_t device = AUDIO_DEVICE_NONE; | 
|  | 3890 |  | 
|  | 3891 | if (fromCache) { | 
|  | 3892 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 3893 | strategy, mDeviceForStrategy[strategy]); | 
|  | 3894 | return mDeviceForStrategy[strategy]; | 
|  | 3895 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3896 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3897 | switch (strategy) { | 
|  | 3898 |  | 
|  | 3899 | case STRATEGY_SONIFICATION_RESPECTFUL: | 
|  | 3900 | if (isInCall()) { | 
|  | 3901 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3902 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3903 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { | 
|  | 3904 | // while media is playing on a remote device, use the the sonification behavior. | 
|  | 3905 | // Note that we test this usecase before testing if media is playing because | 
|  | 3906 | //   the isStreamActive() method only informs about the activity of a stream, not | 
|  | 3907 | //   if it's for local playback. Note also that we use the same delay between both tests | 
|  | 3908 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3909 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3910 | // while media is playing (or has recently played), use the same device | 
|  | 3911 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); | 
|  | 3912 | } else { | 
|  | 3913 | // when media is not playing anymore, fall back on the sonification behavior | 
|  | 3914 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); | 
|  | 3915 | } | 
|  | 3916 |  | 
|  | 3917 | break; | 
|  | 3918 |  | 
|  | 3919 | case STRATEGY_DTMF: | 
|  | 3920 | if (!isInCall()) { | 
|  | 3921 | // when off call, DTMF strategy follows the same rules as MEDIA strategy | 
|  | 3922 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); | 
|  | 3923 | break; | 
|  | 3924 | } | 
|  | 3925 | // when in call, DTMF and PHONE strategies follow the same rules | 
|  | 3926 | // FALL THROUGH | 
|  | 3927 |  | 
|  | 3928 | case STRATEGY_PHONE: | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 3929 | // Force use of only devices on primary output if: | 
|  | 3930 | // - in call AND | 
|  | 3931 | //   - cannot route from voice call RX OR | 
|  | 3932 | //   - audio HAL version is < 3.0 and TX device is on the primary HW module | 
|  | 3933 | if (mPhoneState == AUDIO_MODE_IN_CALL) { | 
|  | 3934 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
|  | 3935 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); | 
|  | 3936 | if (((mAvailableInputDevices.types() & | 
|  | 3937 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || | 
|  | 3938 | (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) && | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 3939 | (hwOutputDesc->getAudioPort()->mModule->mHalVersion < | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 3940 | AUDIO_DEVICE_API_VERSION_3_0))) { | 
|  | 3941 | availableOutputDeviceTypes = availablePrimaryOutputDevices(); | 
|  | 3942 | } | 
|  | 3943 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3944 | // for phone strategy, we first consider the forced use and then the available devices by order | 
|  | 3945 | // of priority | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3946 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { | 
|  | 3947 | case AUDIO_POLICY_FORCE_BT_SCO: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3948 | if (!isInCall() || strategy != STRATEGY_DTMF) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3949 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3950 | if (device) break; | 
|  | 3951 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3952 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3953 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3954 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3955 | if (device) break; | 
|  | 3956 | // if SCO device is requested but no SCO device is available, fall back to default case | 
|  | 3957 | // FALL THROUGH | 
|  | 3958 |  | 
|  | 3959 | default:    // FORCE_NONE | 
|  | 3960 | // 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] | 3961 | if (!isInCall() && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3962 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3963 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3964 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3965 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3966 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3967 | if (device) break; | 
|  | 3968 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3969 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3970 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3971 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3972 | if (device) break; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 3973 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
|  | 3974 | if (device) break; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3975 | if (mPhoneState != AUDIO_MODE_IN_CALL) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3976 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3977 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3978 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3979 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3980 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3981 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3982 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3983 | if (device) break; | 
|  | 3984 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3985 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3986 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3987 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3988 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 3989 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); | 
|  | 3990 | } | 
|  | 3991 | break; | 
|  | 3992 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3993 | case AUDIO_POLICY_FORCE_SPEAKER: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3994 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to | 
|  | 3995 | // A2DP speaker when forcing to speaker output | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3996 | if (!isInCall() && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3997 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3998 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3999 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4000 | if (device) break; | 
|  | 4001 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4002 | if (mPhoneState != AUDIO_MODE_IN_CALL) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4003 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4004 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4005 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4007 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4008 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4009 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4010 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4011 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4012 | if (device) break; | 
|  | 4013 | } | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4014 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; | 
|  | 4015 | if (device) break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4016 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4018 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4019 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4020 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); | 
|  | 4021 | } | 
|  | 4022 | break; | 
|  | 4023 | } | 
|  | 4024 | break; | 
|  | 4025 |  | 
|  | 4026 | case STRATEGY_SONIFICATION: | 
|  | 4027 |  | 
|  | 4028 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by | 
|  | 4029 | // handleIncallSonification(). | 
|  | 4030 | if (isInCall()) { | 
|  | 4031 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); | 
|  | 4032 | break; | 
|  | 4033 | } | 
|  | 4034 | // FALL THROUGH | 
|  | 4035 |  | 
|  | 4036 | case STRATEGY_ENFORCED_AUDIBLE: | 
|  | 4037 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION | 
|  | 4038 | // except: | 
|  | 4039 | //   - when in call where it doesn't default to STRATEGY_PHONE behavior | 
|  | 4040 | //   - in countries where not enforced in which case it follows STRATEGY_MEDIA | 
|  | 4041 |  | 
|  | 4042 | if ((strategy == STRATEGY_SONIFICATION) || | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4043 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4044 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4045 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4046 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); | 
|  | 4047 | } | 
|  | 4048 | } | 
|  | 4049 | // The second device used for sonification is the same as the device used by media strategy | 
|  | 4050 | // FALL THROUGH | 
|  | 4051 |  | 
|  | 4052 | case STRATEGY_MEDIA: { | 
|  | 4053 | uint32_t device2 = AUDIO_DEVICE_NONE; | 
|  | 4054 | if (strategy != STRATEGY_SONIFICATION) { | 
|  | 4055 | // no sonification on remote submix (e.g. WFD) | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4056 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4057 | } | 
|  | 4058 | if ((device2 == AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4059 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4060 | (getA2dpOutput() != 0) && !mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4061 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4062 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4063 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4064 | } | 
|  | 4065 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4066 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4067 | } | 
|  | 4068 | } | 
|  | 4069 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4070 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4071 | } | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4072 | if ((device2 == AUDIO_DEVICE_NONE)) { | 
|  | 4073 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; | 
|  | 4074 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4076 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4077 | } | 
|  | 4078 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4079 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4080 | } | 
|  | 4081 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4082 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4083 | } | 
|  | 4084 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4085 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | } | 
|  | 4087 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { | 
|  | 4088 | // no sonification on aux digital (e.g. HDMI) | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4089 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4090 | } | 
|  | 4091 | if ((device2 == AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4092 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4093 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4094 | } | 
|  | 4095 | if (device2 == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4096 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4097 | } | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4098 | int device3 = AUDIO_DEVICE_NONE; | 
|  | 4099 | if (strategy == STRATEGY_MEDIA) { | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4100 | // ARC, SPDIF and AUX_LINE can co-exist with others. | 
| Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 4101 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; | 
|  | 4102 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4103 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4104 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4105 |  | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4106 | device2 |= device3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or | 
|  | 4108 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise | 
|  | 4109 | device |= device2; | 
| Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4110 |  | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4111 | // If hdmi system audio mode is on, remove speaker out of output list. | 
|  | 4112 | if ((strategy == STRATEGY_MEDIA) && | 
|  | 4113 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == | 
|  | 4114 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { | 
|  | 4115 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4116 | } | 
|  | 4117 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | if (device) break; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4119 | device = mDefaultOutputDevice->mDeviceType; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4120 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4121 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); | 
|  | 4122 | } | 
|  | 4123 | } break; | 
|  | 4124 |  | 
|  | 4125 | default: | 
|  | 4126 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); | 
|  | 4127 | break; | 
|  | 4128 | } | 
|  | 4129 |  | 
|  | 4130 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); | 
|  | 4131 | return device; | 
|  | 4132 | } | 
|  | 4133 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4134 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4135 | { | 
|  | 4136 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4137 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 4138 | } | 
|  | 4139 | mPreviousOutputs = mOutputs; | 
|  | 4140 | } | 
|  | 4141 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4142 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4143 | audio_devices_t prevDevice, | 
|  | 4144 | uint32_t delayMs) | 
|  | 4145 | { | 
|  | 4146 | // mute/unmute strategies using an incompatible device combination | 
|  | 4147 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 4148 | // if unmuting, unmute only after the specified delay | 
|  | 4149 | if (outputDesc->isDuplicated()) { | 
|  | 4150 | return 0; | 
|  | 4151 | } | 
|  | 4152 |  | 
|  | 4153 | uint32_t muteWaitMs = 0; | 
|  | 4154 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4155 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 |  | 
|  | 4157 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4158 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 4159 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 4160 | bool doMute = false; | 
|  | 4161 |  | 
|  | 4162 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 4163 | doMute = true; | 
|  | 4164 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 4165 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 4166 | doMute = true; | 
|  | 4167 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 4168 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4169 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4171 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4172 | // skip output if it does not share any device with current output | 
|  | 4173 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 4174 | == AUDIO_DEVICE_NONE) { | 
|  | 4175 | continue; | 
|  | 4176 | } | 
|  | 4177 | audio_io_handle_t curOutput = mOutputs.keyAt(j); | 
|  | 4178 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", | 
|  | 4179 | mute ? "muting" : "unmuting", i, curDevice, curOutput); | 
|  | 4180 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); | 
|  | 4181 | if (desc->isStrategyActive((routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4182 | if (mute) { | 
|  | 4183 | // FIXME: should not need to double latency if volume could be applied | 
|  | 4184 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 4185 | // between now and the next time the audioflinger thread for this output | 
|  | 4186 | // will process a buffer (which corresponds to one buffer size, | 
|  | 4187 | // usually 1/2 or 1/4 of the latency). | 
|  | 4188 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 4189 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4190 | } | 
|  | 4191 | } | 
|  | 4192 | } | 
|  | 4193 | } | 
|  | 4194 | } | 
|  | 4195 | } | 
|  | 4196 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4197 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 4198 | // different per device volumes | 
|  | 4199 | if (outputDesc->isActive() && (device != prevDevice)) { | 
|  | 4200 | if (muteWaitMs < outputDesc->latency() * 2) { | 
|  | 4201 | muteWaitMs = outputDesc->latency() * 2; | 
|  | 4202 | } | 
|  | 4203 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4204 | if (outputDesc->isStrategyActive((routing_strategy)i)) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4205 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4206 | // do tempMute unmute after twice the mute wait time | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4207 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4208 | muteWaitMs *2, device); | 
|  | 4209 | } | 
|  | 4210 | } | 
|  | 4211 | } | 
|  | 4212 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 4214 | if (muteWaitMs > delayMs) { | 
|  | 4215 | muteWaitMs -= delayMs; | 
|  | 4216 | usleep(muteWaitMs * 1000); | 
|  | 4217 | return muteWaitMs; | 
|  | 4218 | } | 
|  | 4219 | return 0; | 
|  | 4220 | } | 
|  | 4221 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4222 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4223 | audio_devices_t device, | 
|  | 4224 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4225 | int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4226 | audio_patch_handle_t *patchHandle, | 
|  | 4227 | const char* address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4228 | { | 
|  | 4229 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4230 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | AudioParameter param; | 
|  | 4232 | uint32_t muteWaitMs; | 
|  | 4233 |  | 
|  | 4234 | if (outputDesc->isDuplicated()) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4235 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); | 
|  | 4236 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4237 | return muteWaitMs; | 
|  | 4238 | } | 
|  | 4239 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 4240 | // output profile | 
|  | 4241 | if ((device != AUDIO_DEVICE_NONE) && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4242 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4243 | return 0; | 
|  | 4244 | } | 
|  | 4245 |  | 
|  | 4246 | // filter devices according to output selected | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4247 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4248 |  | 
|  | 4249 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 4250 |  | 
|  | 4251 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); | 
|  | 4252 |  | 
|  | 4253 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 4254 | outputDesc->mDevice = device; | 
|  | 4255 | } | 
|  | 4256 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 4257 |  | 
|  | 4258 | // Do not change the routing if: | 
|  | 4259 | //  - the requested device is AUDIO_DEVICE_NONE | 
|  | 4260 | //  - the requested device is the same as current device and force is not specified. | 
|  | 4261 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
|  | 4262 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) { | 
|  | 4263 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output); | 
|  | 4264 | return muteWaitMs; | 
|  | 4265 | } | 
|  | 4266 |  | 
|  | 4267 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4268 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4269 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4270 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4271 | resetOutputDevice(output, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4272 | } else { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4273 | DeviceVector deviceList = (address == NULL) ? | 
|  | 4274 | mAvailableOutputDevices.getDevicesFromType(device) | 
|  | 4275 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4276 | if (!deviceList.isEmpty()) { | 
|  | 4277 | struct audio_patch patch; | 
|  | 4278 | outputDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 4279 | patch.num_sources = 1; | 
|  | 4280 | patch.num_sinks = 0; | 
|  | 4281 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
|  | 4282 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4283 | patch.num_sinks++; | 
|  | 4284 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4285 | ssize_t index; | 
|  | 4286 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4287 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4288 | } else { | 
|  | 4289 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 4290 | } | 
|  | 4291 | sp< AudioPatch> patchDesc; | 
|  | 4292 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4293 | if (index >= 0) { | 
|  | 4294 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4295 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4296 | } | 
|  | 4297 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4298 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4299 | &afPatchHandle, | 
|  | 4300 | delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4301 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
|  | 4302 | "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4303 | status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4304 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4305 | if (index < 0) { | 
|  | 4306 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 4307 | &patch, mUidCached); | 
|  | 4308 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4309 | } else { | 
|  | 4310 | patchDesc->mPatch = patch; | 
|  | 4311 | } | 
|  | 4312 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 4313 | patchDesc->mUid = mUidCached; | 
|  | 4314 | if (patchHandle) { | 
|  | 4315 | *patchHandle = patchDesc->mHandle; | 
|  | 4316 | } | 
|  | 4317 | outputDesc->mPatchHandle = patchDesc->mHandle; | 
|  | 4318 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4319 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4320 | } | 
|  | 4321 | } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4322 |  | 
|  | 4323 | // inform all input as well | 
|  | 4324 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4325 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
|  | 4326 | if (!isVirtualInputDevice(inputDescriptor->mDevice)) { | 
|  | 4327 | AudioParameter inputCmd = AudioParameter(); | 
|  | 4328 | ALOGV("%s: inform input %d of device:%d", __func__, | 
|  | 4329 | inputDescriptor->mIoHandle, device); | 
|  | 4330 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
|  | 4331 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
|  | 4332 | inputCmd.toString(), | 
|  | 4333 | delayMs); | 
|  | 4334 | } | 
|  | 4335 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4336 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 |  | 
|  | 4338 | // update stream volumes according to new device | 
|  | 4339 | applyStreamVolumes(output, device, delayMs); | 
|  | 4340 |  | 
|  | 4341 | return muteWaitMs; | 
|  | 4342 | } | 
|  | 4343 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4344 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4345 | int delayMs, | 
|  | 4346 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4347 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4348 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4349 | ssize_t index; | 
|  | 4350 | if (patchHandle) { | 
|  | 4351 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4352 | } else { | 
|  | 4353 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
|  | 4354 | } | 
|  | 4355 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4356 | return INVALID_OPERATION; | 
|  | 4357 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4358 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4359 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4360 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
|  | 4361 | outputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4362 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4363 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4364 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4365 | return status; | 
|  | 4366 | } | 
|  | 4367 |  | 
|  | 4368 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 4369 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4370 | bool force, | 
|  | 4371 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4372 | { | 
|  | 4373 | status_t status = NO_ERROR; | 
|  | 4374 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4375 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4376 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 4377 | inputDesc->mDevice = device; | 
|  | 4378 |  | 
|  | 4379 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 4380 | if (!deviceList.isEmpty()) { | 
|  | 4381 | struct audio_patch patch; | 
|  | 4382 | inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4383 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 4384 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4385 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && | 
|  | 4386 | !inputDesc->mIsSoundTrigger) { | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4387 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 4388 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4389 | patch.num_sinks = 1; | 
|  | 4390 | //only one input device for now | 
|  | 4391 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4392 | patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4393 | ssize_t index; | 
|  | 4394 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4395 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4396 | } else { | 
|  | 4397 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 4398 | } | 
|  | 4399 | sp< AudioPatch> patchDesc; | 
|  | 4400 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4401 | if (index >= 0) { | 
|  | 4402 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4403 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4404 | } | 
|  | 4405 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4406 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4407 | &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4408 | 0); | 
|  | 4409 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4410 | status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4411 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4412 | if (index < 0) { | 
|  | 4413 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), | 
|  | 4414 | &patch, mUidCached); | 
|  | 4415 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 4416 | } else { | 
|  | 4417 | patchDesc->mPatch = patch; | 
|  | 4418 | } | 
|  | 4419 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 4420 | patchDesc->mUid = mUidCached; | 
|  | 4421 | if (patchHandle) { | 
|  | 4422 | *patchHandle = patchDesc->mHandle; | 
|  | 4423 | } | 
|  | 4424 | inputDesc->mPatchHandle = patchDesc->mHandle; | 
|  | 4425 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4426 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4427 | } | 
|  | 4428 | } | 
|  | 4429 | } | 
|  | 4430 | return status; | 
|  | 4431 | } | 
|  | 4432 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4433 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 4434 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4435 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4436 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4437 | ssize_t index; | 
|  | 4438 | if (patchHandle) { | 
|  | 4439 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4440 | } else { | 
|  | 4441 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
|  | 4442 | } | 
|  | 4443 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4444 | return INVALID_OPERATION; | 
|  | 4445 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4446 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4447 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4448 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
|  | 4449 | inputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4450 | removeAudioPatch(patchDesc->mHandle); | 
|  | 4451 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4452 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4453 | return status; | 
|  | 4454 | } | 
|  | 4455 |  | 
|  | 4456 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4457 | uint32_t& samplingRate, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4458 | audio_format_t format, | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4459 | audio_channel_mask_t channelMask, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4460 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4461 | { | 
|  | 4462 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 4463 | // profile supporting all requested parameters. | 
|  | 4464 |  | 
|  | 4465 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 4466 | { | 
|  | 4467 | if (mHwModules[i]->mHandle == 0) { | 
|  | 4468 | continue; | 
|  | 4469 | } | 
|  | 4470 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 4471 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4472 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4473 | // profile->log(); | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4474 | if (profile->isCompatibleProfile(device, samplingRate, | 
|  | 4475 | &samplingRate /*updatedSamplingRate*/, | 
|  | 4476 | format, channelMask, (audio_output_flags_t) flags)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4477 | return profile; | 
|  | 4478 | } | 
|  | 4479 | } | 
|  | 4480 | } | 
|  | 4481 | return NULL; | 
|  | 4482 | } | 
|  | 4483 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4484 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4485 | { | 
|  | 4486 | uint32_t device = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4487 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & | 
|  | 4488 | ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4489 | switch (inputSource) { | 
|  | 4490 | case AUDIO_SOURCE_VOICE_UPLINK: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4491 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4492 | device = AUDIO_DEVICE_IN_VOICE_CALL; | 
|  | 4493 | break; | 
|  | 4494 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4495 | break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4496 |  | 
|  | 4497 | case AUDIO_SOURCE_DEFAULT: | 
|  | 4498 | case AUDIO_SOURCE_MIC: | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4499 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { | 
|  | 4500 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4501 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { | 
|  | 4502 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; | 
|  | 4503 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { | 
|  | 4504 | device = AUDIO_DEVICE_IN_USB_DEVICE; | 
|  | 4505 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
|  | 4506 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4507 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4508 | break; | 
|  | 4509 |  | 
|  | 4510 | case AUDIO_SOURCE_VOICE_COMMUNICATION: | 
|  | 4511 | // Allow only use of devices on primary input if in call and HAL does not support routing | 
|  | 4512 | // to voice call path. | 
|  | 4513 | if ((mPhoneState == AUDIO_MODE_IN_CALL) && | 
|  | 4514 | (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { | 
|  | 4515 | availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN; | 
|  | 4516 | } | 
|  | 4517 |  | 
|  | 4518 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { | 
|  | 4519 | case AUDIO_POLICY_FORCE_BT_SCO: | 
|  | 4520 | // if SCO device is requested but no SCO device is available, fall back to default case | 
|  | 4521 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { | 
|  | 4522 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; | 
|  | 4523 | break; | 
|  | 4524 | } | 
|  | 4525 | // FALL THROUGH | 
|  | 4526 |  | 
|  | 4527 | default:    // FORCE_NONE | 
|  | 4528 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { | 
|  | 4529 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; | 
|  | 4530 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { | 
|  | 4531 | device = AUDIO_DEVICE_IN_USB_DEVICE; | 
|  | 4532 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
|  | 4533 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 4534 | } | 
|  | 4535 | break; | 
|  | 4536 |  | 
|  | 4537 | case AUDIO_POLICY_FORCE_SPEAKER: | 
|  | 4538 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { | 
|  | 4539 | device = AUDIO_DEVICE_IN_BACK_MIC; | 
|  | 4540 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
|  | 4541 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 4542 | } | 
|  | 4543 | break; | 
|  | 4544 | } | 
|  | 4545 | break; | 
| Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4546 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4547 | case AUDIO_SOURCE_VOICE_RECOGNITION: | 
|  | 4548 | case AUDIO_SOURCE_HOTWORD: | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4549 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4550 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4551 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4552 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4553 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4554 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { | 
|  | 4555 | device = AUDIO_DEVICE_IN_USB_DEVICE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4556 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4557 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 4558 | } | 
|  | 4559 | break; | 
|  | 4560 | case AUDIO_SOURCE_CAMCORDER: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4561 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | device = AUDIO_DEVICE_IN_BACK_MIC; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4563 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4564 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; | 
|  | 4565 | } | 
|  | 4566 | break; | 
|  | 4567 | case AUDIO_SOURCE_VOICE_DOWNLINK: | 
|  | 4568 | case AUDIO_SOURCE_VOICE_CALL: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4569 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4570 | device = AUDIO_DEVICE_IN_VOICE_CALL; | 
|  | 4571 | } | 
|  | 4572 | break; | 
|  | 4573 | case AUDIO_SOURCE_REMOTE_SUBMIX: | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4574 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4575 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 4576 | } | 
|  | 4577 | break; | 
|  | 4578 | default: | 
|  | 4579 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); | 
|  | 4580 | break; | 
|  | 4581 | } | 
|  | 4582 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); | 
|  | 4583 | return device; | 
|  | 4584 | } | 
|  | 4585 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4586 | bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4587 | { | 
|  | 4588 | if ((device & AUDIO_DEVICE_BIT_IN) != 0) { | 
|  | 4589 | device &= ~AUDIO_DEVICE_BIT_IN; | 
|  | 4590 | if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) | 
|  | 4591 | return true; | 
|  | 4592 | } | 
|  | 4593 | return false; | 
|  | 4594 | } | 
|  | 4595 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4596 | bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) { | 
|  | 4597 | return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL) != 0); | 
|  | 4598 | } | 
|  | 4599 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4600 | audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | { | 
|  | 4602 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4603 | const sp<AudioInputDescriptor>  input_descriptor = mInputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4604 | if ((input_descriptor->mRefCount > 0) | 
|  | 4605 | && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { | 
|  | 4606 | return mInputs.keyAt(i); | 
|  | 4607 | } | 
|  | 4608 | } | 
|  | 4609 | return 0; | 
|  | 4610 | } | 
|  | 4611 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4612 | uint32_t AudioPolicyManager::activeInputsCount() const | 
|  | 4613 | { | 
|  | 4614 | uint32_t count = 0; | 
|  | 4615 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4616 | const sp<AudioInputDescriptor>  desc = mInputs.valueAt(i); | 
|  | 4617 | if (desc->mRefCount > 0) { | 
|  | 4618 | return count++; | 
|  | 4619 | } | 
|  | 4620 | } | 
|  | 4621 | return count; | 
|  | 4622 | } | 
|  | 4623 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4625 | audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4626 | { | 
|  | 4627 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4628 | // this happens when forcing a route update and no track is active on an output. | 
|  | 4629 | // In this case the returned category is not important. | 
|  | 4630 | device =  AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4631 | } else if (popcount(device) > 1) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4632 | // Multiple device selection is either: | 
|  | 4633 | //  - speaker + one other device: give priority to speaker in this case. | 
|  | 4634 | //  - one A2DP device + another device: happens with duplicated output. In this case | 
|  | 4635 | // retain the device on the A2DP output as the other must not correspond to an active | 
|  | 4636 | // selection if not the speaker. | 
| Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame^] | 4637 | //  - HDMI-CEC system audio mode only output: give priority to available item in order. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4638 | if (device & AUDIO_DEVICE_OUT_SPEAKER) { | 
|  | 4639 | device = AUDIO_DEVICE_OUT_SPEAKER; | 
| Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame^] | 4640 | } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) { | 
|  | 4641 | device = AUDIO_DEVICE_OUT_HDMI_ARC; | 
|  | 4642 | } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) { | 
|  | 4643 | device = AUDIO_DEVICE_OUT_AUX_LINE; | 
|  | 4644 | } else if (device & AUDIO_DEVICE_OUT_SPDIF) { | 
|  | 4645 | device = AUDIO_DEVICE_OUT_SPDIF; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | } else { | 
|  | 4647 | device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); | 
|  | 4648 | } | 
|  | 4649 | } | 
|  | 4650 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4651 | ALOGW_IF(popcount(device) != 1, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4652 | "getDeviceForVolume() invalid device combination: %08x", | 
|  | 4653 | device); | 
|  | 4654 |  | 
|  | 4655 | return device; | 
|  | 4656 | } | 
|  | 4657 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4658 | AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4659 | { | 
|  | 4660 | switch(getDeviceForVolume(device)) { | 
|  | 4661 | case AUDIO_DEVICE_OUT_EARPIECE: | 
|  | 4662 | return DEVICE_CATEGORY_EARPIECE; | 
|  | 4663 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: | 
|  | 4664 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: | 
|  | 4665 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: | 
|  | 4666 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: | 
|  | 4667 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: | 
|  | 4668 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: | 
|  | 4669 | return DEVICE_CATEGORY_HEADSET; | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4670 | case AUDIO_DEVICE_OUT_LINE: | 
|  | 4671 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: | 
|  | 4672 | /*USB?  Remote submix?*/ | 
|  | 4673 | return DEVICE_CATEGORY_EXT_MEDIA; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4674 | case AUDIO_DEVICE_OUT_SPEAKER: | 
|  | 4675 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: | 
|  | 4676 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4677 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: | 
|  | 4678 | case AUDIO_DEVICE_OUT_USB_DEVICE: | 
|  | 4679 | case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: | 
|  | 4680 | default: | 
|  | 4681 | return DEVICE_CATEGORY_SPEAKER; | 
|  | 4682 | } | 
|  | 4683 | } | 
|  | 4684 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4685 | float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4686 | int indexInUi) | 
|  | 4687 | { | 
|  | 4688 | device_category deviceCategory = getDeviceCategory(device); | 
|  | 4689 | const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory]; | 
|  | 4690 |  | 
|  | 4691 | // the volume index in the UI is relative to the min and max volume indices for this stream type | 
|  | 4692 | int nbSteps = 1 + curve[VOLMAX].mIndex - | 
|  | 4693 | curve[VOLMIN].mIndex; | 
|  | 4694 | int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) / | 
|  | 4695 | (streamDesc.mIndexMax - streamDesc.mIndexMin); | 
|  | 4696 |  | 
|  | 4697 | // find what part of the curve this index volume belongs to, or if it's out of bounds | 
|  | 4698 | int segment = 0; | 
|  | 4699 | if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds | 
|  | 4700 | return 0.0f; | 
|  | 4701 | } else if (volIdx < curve[VOLKNEE1].mIndex) { | 
|  | 4702 | segment = 0; | 
|  | 4703 | } else if (volIdx < curve[VOLKNEE2].mIndex) { | 
|  | 4704 | segment = 1; | 
|  | 4705 | } else if (volIdx <= curve[VOLMAX].mIndex) { | 
|  | 4706 | segment = 2; | 
|  | 4707 | } else {                                                               // out of bounds | 
|  | 4708 | return 1.0f; | 
|  | 4709 | } | 
|  | 4710 |  | 
|  | 4711 | // linear interpolation in the attenuation table in dB | 
|  | 4712 | float decibels = curve[segment].mDBAttenuation + | 
|  | 4713 | ((float)(volIdx - curve[segment].mIndex)) * | 
|  | 4714 | ( (curve[segment+1].mDBAttenuation - | 
|  | 4715 | curve[segment].mDBAttenuation) / | 
|  | 4716 | ((float)(curve[segment+1].mIndex - | 
|  | 4717 | curve[segment].mIndex)) ); | 
|  | 4718 |  | 
|  | 4719 | float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) | 
|  | 4720 |  | 
|  | 4721 | ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f", | 
|  | 4722 | curve[segment].mIndex, volIdx, | 
|  | 4723 | curve[segment+1].mIndex, | 
|  | 4724 | curve[segment].mDBAttenuation, | 
|  | 4725 | decibels, | 
|  | 4726 | curve[segment+1].mDBAttenuation, | 
|  | 4727 | amplification); | 
|  | 4728 |  | 
|  | 4729 | return amplification; | 
|  | 4730 | } | 
|  | 4731 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4732 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4733 | AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4734 | {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f} | 
|  | 4735 | }; | 
|  | 4736 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4737 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4738 | AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4739 | {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f} | 
|  | 4740 | }; | 
|  | 4741 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4742 | const AudioPolicyManager::VolumeCurvePoint | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4743 | AudioPolicyManager::sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
|  | 4744 | {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f} | 
|  | 4745 | }; | 
|  | 4746 |  | 
|  | 4747 | const AudioPolicyManager::VolumeCurvePoint | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4748 | AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4749 | {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f} | 
|  | 4750 | }; | 
|  | 4751 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4752 | const AudioPolicyManager::VolumeCurvePoint | 
| Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 4753 | AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Jean-Michel Trivi | 98c6043 | 2014-07-09 08:51:34 -0700 | [diff] [blame] | 4754 | {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] | 4755 | }; | 
|  | 4756 |  | 
|  | 4757 | const AudioPolicyManager::VolumeCurvePoint | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4758 | AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4759 | {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f} | 
|  | 4760 | }; | 
|  | 4761 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4762 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4763 | AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4764 | {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f} | 
|  | 4765 | }; | 
|  | 4766 |  | 
|  | 4767 | // AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks | 
|  | 4768 | // AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets. | 
|  | 4769 | // AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java). | 
|  | 4770 | // The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset. | 
|  | 4771 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4772 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4773 | AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4774 | {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f} | 
|  | 4775 | }; | 
|  | 4776 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4777 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4778 | AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 | {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f} | 
|  | 4780 | }; | 
|  | 4781 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4782 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4783 | AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4784 | {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f} | 
|  | 4785 | }; | 
|  | 4786 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4787 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4788 | AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4789 | {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f} | 
|  | 4790 | }; | 
|  | 4791 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4792 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4793 | AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4794 | {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f} | 
|  | 4795 | }; | 
|  | 4796 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4797 | const AudioPolicyManager::VolumeCurvePoint | 
|  | 4798 | *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT] | 
|  | 4799 | [AudioPolicyManager::DEVICE_CATEGORY_CNT] = { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4800 | { // AUDIO_STREAM_VOICE_CALL | 
|  | 4801 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4802 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4803 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4804 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4805 | }, | 
|  | 4806 | { // AUDIO_STREAM_SYSTEM | 
|  | 4807 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4808 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4809 | sDefaultSystemVolumeCurve,  // DEVICE_CATEGORY_EARPIECE | 
|  | 4810 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4811 | }, | 
|  | 4812 | { // AUDIO_STREAM_RING | 
|  | 4813 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4814 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4815 | sDefaultVolumeCurve,  // DEVICE_CATEGORY_EARPIECE | 
|  | 4816 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4817 | }, | 
|  | 4818 | { // AUDIO_STREAM_MUSIC | 
|  | 4819 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4820 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4821 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4822 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4823 | }, | 
|  | 4824 | { // AUDIO_STREAM_ALARM | 
|  | 4825 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4826 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4827 | sDefaultVolumeCurve,  // DEVICE_CATEGORY_EARPIECE | 
|  | 4828 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4829 | }, | 
|  | 4830 | { // AUDIO_STREAM_NOTIFICATION | 
|  | 4831 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4832 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4833 | sDefaultVolumeCurve,  // DEVICE_CATEGORY_EARPIECE | 
|  | 4834 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4835 | }, | 
|  | 4836 | { // AUDIO_STREAM_BLUETOOTH_SCO | 
|  | 4837 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4838 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4839 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4840 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | }, | 
|  | 4842 | { // AUDIO_STREAM_ENFORCED_AUDIBLE | 
|  | 4843 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4844 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4845 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4846 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4847 | }, | 
|  | 4848 | {  // AUDIO_STREAM_DTMF | 
|  | 4849 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4850 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4851 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4852 | sExtMediaSystemVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4853 | }, | 
|  | 4854 | { // AUDIO_STREAM_TTS | 
|  | 4855 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET | 
|  | 4856 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER | 
| Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4857 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE | 
|  | 4858 | sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EXT_MEDIA | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4859 | }, | 
|  | 4860 | }; | 
|  | 4861 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4862 | void AudioPolicyManager::initializeVolumeCurves() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4863 | { | 
|  | 4864 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
|  | 4865 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { | 
|  | 4866 | mStreams[i].mVolumeCurve[j] = | 
|  | 4867 | sVolumeProfiles[i][j]; | 
|  | 4868 | } | 
|  | 4869 | } | 
|  | 4870 |  | 
|  | 4871 | // Check availability of DRC on speaker path: if available, override some of the speaker curves | 
|  | 4872 | if (mSpeakerDrcEnabled) { | 
|  | 4873 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4874 | sDefaultSystemVolumeCurveDrc; | 
|  | 4875 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4876 | sSpeakerSonificationVolumeCurveDrc; | 
|  | 4877 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4878 | sSpeakerSonificationVolumeCurveDrc; | 
|  | 4879 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4880 | sSpeakerSonificationVolumeCurveDrc; | 
| Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 4881 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = | 
|  | 4882 | sSpeakerMediaVolumeCurveDrc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | } | 
|  | 4884 | } | 
|  | 4885 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4886 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4887 | int index, | 
|  | 4888 | audio_io_handle_t output, | 
|  | 4889 | audio_devices_t device) | 
|  | 4890 | { | 
|  | 4891 | float volume = 1.0; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4892 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4893 | StreamDescriptor &streamDesc = mStreams[stream]; | 
|  | 4894 |  | 
|  | 4895 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 4896 | device = outputDesc->device(); | 
|  | 4897 | } | 
|  | 4898 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4899 | volume = volIndexToAmpl(device, streamDesc, index); | 
|  | 4900 |  | 
|  | 4901 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 4902 | // to avoid sound level bursts in user's ears: | 
|  | 4903 | // - always attenuate ring tones and notifications volume by 6dB | 
|  | 4904 | // - if music is playing, always limit the volume to current music volume, | 
|  | 4905 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4906 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4907 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 4908 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 4909 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
|  | 4910 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && | 
|  | 4911 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 4912 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4913 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4915 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4916 | streamDesc.mCanBeMuted) { | 
|  | 4917 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; | 
|  | 4918 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 4919 | // by the music application and behave as if music was active if the last music track was | 
|  | 4920 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4921 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | mLimitRingtoneVolume) { | 
|  | 4923 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4924 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, | 
|  | 4925 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4926 | output, | 
|  | 4927 | musicDevice); | 
|  | 4928 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? | 
|  | 4929 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; | 
|  | 4930 | if (volume > minVol) { | 
|  | 4931 | volume = minVol; | 
|  | 4932 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); | 
|  | 4933 | } | 
|  | 4934 | } | 
|  | 4935 | } | 
|  | 4936 |  | 
|  | 4937 | return volume; | 
|  | 4938 | } | 
|  | 4939 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4940 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4941 | int index, | 
|  | 4942 | audio_io_handle_t output, | 
|  | 4943 | audio_devices_t device, | 
|  | 4944 | int delayMs, | 
|  | 4945 | bool force) | 
|  | 4946 | { | 
|  | 4947 |  | 
|  | 4948 | // do not change actual stream volume if the stream is muted | 
|  | 4949 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { | 
|  | 4950 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
|  | 4951 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); | 
|  | 4952 | return NO_ERROR; | 
|  | 4953 | } | 
|  | 4954 |  | 
|  | 4955 | // 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] | 4956 | if ((stream == AUDIO_STREAM_VOICE_CALL && | 
|  | 4957 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4958 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && | 
|  | 4959 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4960 | 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] | 4961 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4962 | return INVALID_OPERATION; | 
|  | 4963 | } | 
|  | 4964 |  | 
|  | 4965 | float volume = computeVolume(stream, index, output, device); | 
|  | 4966 | // We actually change the volume if: | 
|  | 4967 | // - the float value returned by computeVolume() changed | 
|  | 4968 | // - the force flag is set | 
|  | 4969 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || | 
|  | 4970 | force) { | 
|  | 4971 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; | 
|  | 4972 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); | 
|  | 4973 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is | 
|  | 4974 | // enabled | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4975 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
|  | 4976 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4977 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4978 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4979 | } | 
|  | 4980 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4981 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 4982 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | float voiceVolume; | 
|  | 4984 | // 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] | 4985 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; | 
|  | 4987 | } else { | 
|  | 4988 | voiceVolume = 1.0; | 
|  | 4989 | } | 
|  | 4990 |  | 
|  | 4991 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { | 
|  | 4992 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 4993 | mLastVoiceVolume = voiceVolume; | 
|  | 4994 | } | 
|  | 4995 | } | 
|  | 4996 |  | 
|  | 4997 | return NO_ERROR; | 
|  | 4998 | } | 
|  | 4999 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5000 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5001 | audio_devices_t device, | 
|  | 5002 | int delayMs, | 
|  | 5003 | bool force) | 
|  | 5004 | { | 
|  | 5005 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); | 
|  | 5006 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5007 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 5008 | checkAndSetVolume((audio_stream_type_t)stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5009 | mStreams[stream].getVolumeIndex(device), | 
|  | 5010 | output, | 
|  | 5011 | device, | 
|  | 5012 | delayMs, | 
|  | 5013 | force); | 
|  | 5014 | } | 
|  | 5015 | } | 
|  | 5016 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5017 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5018 | bool on, | 
|  | 5019 | audio_io_handle_t output, | 
|  | 5020 | int delayMs, | 
|  | 5021 | audio_devices_t device) | 
|  | 5022 | { | 
|  | 5023 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5024 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
|  | 5025 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 5026 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5027 | } | 
|  | 5028 | } | 
|  | 5029 | } | 
|  | 5030 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5031 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5032 | bool on, | 
|  | 5033 | audio_io_handle_t output, | 
|  | 5034 | int delayMs, | 
|  | 5035 | audio_devices_t device) | 
|  | 5036 | { | 
|  | 5037 | StreamDescriptor &streamDesc = mStreams[stream]; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5038 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5039 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5040 | device = outputDesc->device(); | 
|  | 5041 | } | 
|  | 5042 |  | 
|  | 5043 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", | 
|  | 5044 | stream, on, output, outputDesc->mMuteCount[stream], device); | 
|  | 5045 |  | 
|  | 5046 | if (on) { | 
|  | 5047 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5048 | if (streamDesc.mCanBeMuted && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5049 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
|  | 5050 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5051 | checkAndSetVolume(stream, 0, output, device, delayMs); | 
|  | 5052 | } | 
|  | 5053 | } | 
|  | 5054 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 5055 | outputDesc->mMuteCount[stream]++; | 
|  | 5056 | } else { | 
|  | 5057 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5058 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 5059 | return; | 
|  | 5060 | } | 
|  | 5061 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 5062 | checkAndSetVolume(stream, | 
|  | 5063 | streamDesc.getVolumeIndex(device), | 
|  | 5064 | output, | 
|  | 5065 | device, | 
|  | 5066 | delayMs); | 
|  | 5067 | } | 
|  | 5068 | } | 
|  | 5069 | } | 
|  | 5070 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5071 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5072 | bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5073 | { | 
|  | 5074 | // if the stream pertains to sonification strategy and we are in call we must | 
|  | 5075 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
|  | 5076 | // in the device used for phone strategy and play the tone if the selected device does not | 
|  | 5077 | // interfere with the device used for phone strategy | 
|  | 5078 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
|  | 5079 | // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5080 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5081 | if ((stream_strategy == STRATEGY_SONIFICATION) || | 
|  | 5082 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5083 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
|  | 5085 | stream, starting, outputDesc->mDevice, stateChange); | 
|  | 5086 | if (outputDesc->mRefCount[stream]) { | 
|  | 5087 | int muteCount = 1; | 
|  | 5088 | if (stateChange) { | 
|  | 5089 | muteCount = outputDesc->mRefCount[stream]; | 
|  | 5090 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5091 | if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
|  | 5093 | for (int i = 0; i < muteCount; i++) { | 
|  | 5094 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5095 | } | 
|  | 5096 | } else { | 
|  | 5097 | ALOGV("handleIncallSonification() high visibility"); | 
|  | 5098 | if (outputDesc->device() & | 
|  | 5099 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
|  | 5100 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
|  | 5101 | for (int i = 0; i < muteCount; i++) { | 
|  | 5102 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5103 | } | 
|  | 5104 | } | 
|  | 5105 | if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5106 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
|  | 5107 | AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5108 | } else { | 
|  | 5109 | mpClientInterface->stopTone(); | 
|  | 5110 | } | 
|  | 5111 | } | 
|  | 5112 | } | 
|  | 5113 | } | 
|  | 5114 | } | 
|  | 5115 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5116 | bool AudioPolicyManager::isInCall() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5117 | { | 
|  | 5118 | return isStateInCall(mPhoneState); | 
|  | 5119 | } | 
|  | 5120 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5121 | bool AudioPolicyManager::isStateInCall(int state) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5122 | return ((state == AUDIO_MODE_IN_CALL) || | 
|  | 5123 | (state == AUDIO_MODE_IN_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5124 | } | 
|  | 5125 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5126 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5127 | { | 
|  | 5128 | return MAX_EFFECTS_CPU_LOAD; | 
|  | 5129 | } | 
|  | 5130 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5131 | uint32_t AudioPolicyManager::getMaxEffectsMemory() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5132 | { | 
|  | 5133 | return MAX_EFFECTS_MEMORY; | 
|  | 5134 | } | 
|  | 5135 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5136 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5137 | // --- AudioOutputDescriptor class implementation | 
|  | 5138 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5139 | AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor( | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5140 | const sp<IOProfile>& profile) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5141 | : mId(0), mIoHandle(0), mLatency(0), | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5142 | mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5143 | mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0) | 
|  | 5144 | { | 
|  | 5145 | // clear usage count for all stream types | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5146 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | mRefCount[i] = 0; | 
|  | 5148 | mCurVolume[i] = -1.0; | 
|  | 5149 | mMuteCount[i] = 0; | 
|  | 5150 | mStopTime[i] = 0; | 
|  | 5151 | } | 
|  | 5152 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 5153 | mStrategyMutedByDevice[i] = false; | 
|  | 5154 | } | 
|  | 5155 | if (profile != NULL) { | 
| Eric Laurent | d862237 | 2014-07-27 13:47:31 -0700 | [diff] [blame] | 5156 | mFlags = profile->mFlags; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5157 | mSamplingRate = profile->pickSamplingRate(); | 
|  | 5158 | mFormat = profile->pickFormat(); | 
|  | 5159 | mChannelMask = profile->pickChannelMask(); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5160 | if (profile->mGains.size() > 0) { | 
|  | 5161 | profile->mGains[0]->getDefaultConfig(&mGain); | 
|  | 5162 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5163 | } | 
|  | 5164 | } | 
|  | 5165 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5166 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5167 | { | 
|  | 5168 | if (isDuplicated()) { | 
|  | 5169 | return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); | 
|  | 5170 | } else { | 
|  | 5171 | return mDevice; | 
|  | 5172 | } | 
|  | 5173 | } | 
|  | 5174 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5175 | uint32_t AudioPolicyManager::AudioOutputDescriptor::latency() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5176 | { | 
|  | 5177 | if (isDuplicated()) { | 
|  | 5178 | return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; | 
|  | 5179 | } else { | 
|  | 5180 | return mLatency; | 
|  | 5181 | } | 
|  | 5182 | } | 
|  | 5183 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5184 | bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith( | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5185 | const sp<AudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5186 | { | 
|  | 5187 | if (isDuplicated()) { | 
|  | 5188 | return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); | 
|  | 5189 | } else if (outputDesc->isDuplicated()){ | 
|  | 5190 | return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2); | 
|  | 5191 | } else { | 
|  | 5192 | return (mProfile->mModule == outputDesc->mProfile->mModule); | 
|  | 5193 | } | 
|  | 5194 | } | 
|  | 5195 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5196 | void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5197 | int delta) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5198 | { | 
|  | 5199 | // forward usage count change to attached outputs | 
|  | 5200 | if (isDuplicated()) { | 
|  | 5201 | mOutput1->changeRefCount(stream, delta); | 
|  | 5202 | mOutput2->changeRefCount(stream, delta); | 
|  | 5203 | } | 
|  | 5204 | if ((delta + (int)mRefCount[stream]) < 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5205 | ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", | 
|  | 5206 | delta, stream, mRefCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | mRefCount[stream] = 0; | 
|  | 5208 | return; | 
|  | 5209 | } | 
|  | 5210 | mRefCount[stream] += delta; | 
|  | 5211 | ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); | 
|  | 5212 | } | 
|  | 5213 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5214 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5215 | { | 
|  | 5216 | if (isDuplicated()) { | 
|  | 5217 | return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); | 
|  | 5218 | } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5219 | return mProfile->mSupportedDevices.types() ; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5220 | } | 
|  | 5221 | } | 
|  | 5222 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5223 | bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5224 | { | 
|  | 5225 | return isStrategyActive(NUM_STRATEGIES, inPastMs); | 
|  | 5226 | } | 
|  | 5227 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5228 | bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5229 | uint32_t inPastMs, | 
|  | 5230 | nsecs_t sysTime) const | 
|  | 5231 | { | 
|  | 5232 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 5233 | sysTime = systemTime(); | 
|  | 5234 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5235 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { | 
|  | 5236 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5237 | (NUM_STRATEGIES == strategy)) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5238 | isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5239 | return true; | 
|  | 5240 | } | 
|  | 5241 | } | 
|  | 5242 | return false; | 
|  | 5243 | } | 
|  | 5244 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5245 | bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | uint32_t inPastMs, | 
|  | 5247 | nsecs_t sysTime) const | 
|  | 5248 | { | 
|  | 5249 | if (mRefCount[stream] != 0) { | 
|  | 5250 | return true; | 
|  | 5251 | } | 
|  | 5252 | if (inPastMs == 0) { | 
|  | 5253 | return false; | 
|  | 5254 | } | 
|  | 5255 | if (sysTime == 0) { | 
|  | 5256 | sysTime = systemTime(); | 
|  | 5257 | } | 
|  | 5258 | if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { | 
|  | 5259 | return true; | 
|  | 5260 | } | 
|  | 5261 | return false; | 
|  | 5262 | } | 
|  | 5263 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5264 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5265 | struct audio_port_config *dstConfig, | 
|  | 5266 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5267 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5268 | ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle); | 
|  | 5269 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5270 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 5271 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; | 
|  | 5272 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5273 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5274 | } | 
|  | 5275 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 5276 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5277 | dstConfig->id = mId; | 
|  | 5278 | dstConfig->role = AUDIO_PORT_ROLE_SOURCE; | 
|  | 5279 | dstConfig->type = AUDIO_PORT_TYPE_MIX; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5280 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; | 
|  | 5281 | dstConfig->ext.mix.handle = mIoHandle; | 
|  | 5282 | dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5283 | } | 
|  | 5284 |  | 
|  | 5285 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPort( | 
|  | 5286 | struct audio_port *port) const | 
|  | 5287 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5288 | ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5289 | mProfile->toAudioPort(port); | 
|  | 5290 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5291 | toAudioPortConfig(&port->active_config); | 
|  | 5292 | port->ext.mix.hw_module = mProfile->mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5293 | port->ext.mix.handle = mIoHandle; | 
|  | 5294 | port->ext.mix.latency_class = | 
|  | 5295 | mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL; | 
|  | 5296 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5297 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5298 | status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5299 | { | 
|  | 5300 | const size_t SIZE = 256; | 
|  | 5301 | char buffer[SIZE]; | 
|  | 5302 | String8 result; | 
|  | 5303 |  | 
| Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5304 | snprintf(buffer, SIZE, " ID: %d\n", mId); | 
|  | 5305 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5306 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); | 
|  | 5307 | result.append(buffer); | 
|  | 5308 | snprintf(buffer, SIZE, " Format: %08x\n", mFormat); | 
|  | 5309 | result.append(buffer); | 
|  | 5310 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); | 
|  | 5311 | result.append(buffer); | 
|  | 5312 | snprintf(buffer, SIZE, " Latency: %d\n", mLatency); | 
|  | 5313 | result.append(buffer); | 
|  | 5314 | snprintf(buffer, SIZE, " Flags %08x\n", mFlags); | 
|  | 5315 | result.append(buffer); | 
|  | 5316 | snprintf(buffer, SIZE, " Devices %08x\n", device()); | 
|  | 5317 | result.append(buffer); | 
|  | 5318 | snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); | 
|  | 5319 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5320 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { | 
|  | 5321 | snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", | 
|  | 5322 | i, mCurVolume[i], mRefCount[i], mMuteCount[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5323 | result.append(buffer); | 
|  | 5324 | } | 
|  | 5325 | write(fd, result.string(), result.size()); | 
|  | 5326 |  | 
|  | 5327 | return NO_ERROR; | 
|  | 5328 | } | 
|  | 5329 |  | 
|  | 5330 | // --- AudioInputDescriptor class implementation | 
|  | 5331 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5332 | AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5333 | : mId(0), mIoHandle(0), | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5334 | mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0), | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5335 | mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5336 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5337 | if (profile != NULL) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5338 | mSamplingRate = profile->pickSamplingRate(); | 
|  | 5339 | mFormat = profile->pickFormat(); | 
|  | 5340 | mChannelMask = profile->pickChannelMask(); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5341 | if (profile->mGains.size() > 0) { | 
|  | 5342 | profile->mGains[0]->getDefaultConfig(&mGain); | 
|  | 5343 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5344 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5345 | } | 
|  | 5346 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5347 | void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig( | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5348 | struct audio_port_config *dstConfig, | 
|  | 5349 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5350 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5351 | ALOG_ASSERT(mProfile != 0, | 
|  | 5352 | "toAudioPortConfig() called on input with null profile %d", mIoHandle); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5353 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| | 
|  | 5354 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; | 
|  | 5355 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5356 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5357 | } | 
|  | 5358 |  | 
|  | 5359 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 5360 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5361 | dstConfig->id = mId; | 
|  | 5362 | dstConfig->role = AUDIO_PORT_ROLE_SINK; | 
|  | 5363 | dstConfig->type = AUDIO_PORT_TYPE_MIX; | 
| Eric Laurent | 62aaabb | 2014-06-02 10:40:54 -0700 | [diff] [blame] | 5364 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; | 
|  | 5365 | dstConfig->ext.mix.handle = mIoHandle; | 
|  | 5366 | dstConfig->ext.mix.usecase.source = mInputSource; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5367 | } | 
|  | 5368 |  | 
|  | 5369 | void AudioPolicyManager::AudioInputDescriptor::toAudioPort( | 
|  | 5370 | struct audio_port *port) const | 
|  | 5371 | { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5372 | ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle); | 
|  | 5373 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5374 | mProfile->toAudioPort(port); | 
|  | 5375 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5376 | toAudioPortConfig(&port->active_config); | 
|  | 5377 | port->ext.mix.hw_module = mProfile->mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5378 | port->ext.mix.handle = mIoHandle; | 
|  | 5379 | port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL; | 
|  | 5380 | } | 
|  | 5381 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5382 | status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5383 | { | 
|  | 5384 | const size_t SIZE = 256; | 
|  | 5385 | char buffer[SIZE]; | 
|  | 5386 | String8 result; | 
|  | 5387 |  | 
| Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5388 | snprintf(buffer, SIZE, " ID: %d\n", mId); | 
|  | 5389 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5390 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); | 
|  | 5391 | result.append(buffer); | 
|  | 5392 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); | 
|  | 5393 | result.append(buffer); | 
|  | 5394 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); | 
|  | 5395 | result.append(buffer); | 
|  | 5396 | snprintf(buffer, SIZE, " Devices %08x\n", mDevice); | 
|  | 5397 | result.append(buffer); | 
|  | 5398 | snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); | 
|  | 5399 | result.append(buffer); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 5400 | snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount); | 
|  | 5401 | result.append(buffer); | 
|  | 5402 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5403 | write(fd, result.string(), result.size()); | 
|  | 5404 |  | 
|  | 5405 | return NO_ERROR; | 
|  | 5406 | } | 
|  | 5407 |  | 
|  | 5408 | // --- StreamDescriptor class implementation | 
|  | 5409 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5410 | AudioPolicyManager::StreamDescriptor::StreamDescriptor() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5411 | :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true) | 
|  | 5412 | { | 
|  | 5413 | mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0); | 
|  | 5414 | } | 
|  | 5415 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5416 | int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5417 | { | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5418 | device = AudioPolicyManager::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5419 | // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT | 
|  | 5420 | if (mIndexCur.indexOfKey(device) < 0) { | 
|  | 5421 | device = AUDIO_DEVICE_OUT_DEFAULT; | 
|  | 5422 | } | 
|  | 5423 | return mIndexCur.valueFor(device); | 
|  | 5424 | } | 
|  | 5425 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5426 | void AudioPolicyManager::StreamDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5427 | { | 
|  | 5428 | const size_t SIZE = 256; | 
|  | 5429 | char buffer[SIZE]; | 
|  | 5430 | String8 result; | 
|  | 5431 |  | 
|  | 5432 | snprintf(buffer, SIZE, "%s         %02d         %02d         ", | 
|  | 5433 | mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax); | 
|  | 5434 | result.append(buffer); | 
|  | 5435 | for (size_t i = 0; i < mIndexCur.size(); i++) { | 
|  | 5436 | snprintf(buffer, SIZE, "%04x : %02d, ", | 
|  | 5437 | mIndexCur.keyAt(i), | 
|  | 5438 | mIndexCur.valueAt(i)); | 
|  | 5439 | result.append(buffer); | 
|  | 5440 | } | 
|  | 5441 | result.append("\n"); | 
|  | 5442 |  | 
|  | 5443 | write(fd, result.string(), result.size()); | 
|  | 5444 | } | 
|  | 5445 |  | 
|  | 5446 | // --- EffectDescriptor class implementation | 
|  | 5447 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5448 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5449 | { | 
|  | 5450 | const size_t SIZE = 256; | 
|  | 5451 | char buffer[SIZE]; | 
|  | 5452 | String8 result; | 
|  | 5453 |  | 
|  | 5454 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); | 
|  | 5455 | result.append(buffer); | 
|  | 5456 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); | 
|  | 5457 | result.append(buffer); | 
|  | 5458 | snprintf(buffer, SIZE, " Session: %d\n", mSession); | 
|  | 5459 | result.append(buffer); | 
|  | 5460 | snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name); | 
|  | 5461 | result.append(buffer); | 
|  | 5462 | snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled"); | 
|  | 5463 | result.append(buffer); | 
|  | 5464 | write(fd, result.string(), result.size()); | 
|  | 5465 |  | 
|  | 5466 | return NO_ERROR; | 
|  | 5467 | } | 
|  | 5468 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5469 | // --- HwModule class implementation | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5470 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5471 | AudioPolicyManager::HwModule::HwModule(const char *name) | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5472 | : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), | 
|  | 5473 | mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5474 | { | 
|  | 5475 | } | 
|  | 5476 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5477 | AudioPolicyManager::HwModule::~HwModule() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5478 | { | 
|  | 5479 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5480 | mOutputProfiles[i]->mSupportedDevices.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5481 | } | 
|  | 5482 | for (size_t i = 0; i < mInputProfiles.size(); i++) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5483 | mInputProfiles[i]->mSupportedDevices.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5484 | } | 
|  | 5485 | free((void *)mName); | 
|  | 5486 | } | 
|  | 5487 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5488 | status_t AudioPolicyManager::HwModule::loadInput(cnode *root) | 
|  | 5489 | { | 
|  | 5490 | cnode *node = root->first_child; | 
|  | 5491 |  | 
|  | 5492 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this); | 
|  | 5493 |  | 
|  | 5494 | while (node) { | 
|  | 5495 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { | 
|  | 5496 | profile->loadSamplingRates((char *)node->value); | 
|  | 5497 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { | 
|  | 5498 | profile->loadFormats((char *)node->value); | 
|  | 5499 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 5500 | profile->loadInChannels((char *)node->value); | 
|  | 5501 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { | 
|  | 5502 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, | 
|  | 5503 | mDeclaredDevices); | 
|  | 5504 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 5505 | profile->loadGains(node); | 
|  | 5506 | } | 
|  | 5507 | node = node->next; | 
|  | 5508 | } | 
|  | 5509 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), | 
|  | 5510 | "loadInput() invalid supported devices"); | 
|  | 5511 | ALOGW_IF(profile->mChannelMasks.size() == 0, | 
|  | 5512 | "loadInput() invalid supported channel masks"); | 
|  | 5513 | ALOGW_IF(profile->mSamplingRates.size() == 0, | 
|  | 5514 | "loadInput() invalid supported sampling rates"); | 
|  | 5515 | ALOGW_IF(profile->mFormats.size() == 0, | 
|  | 5516 | "loadInput() invalid supported formats"); | 
|  | 5517 | if (!profile->mSupportedDevices.isEmpty() && | 
|  | 5518 | (profile->mChannelMasks.size() != 0) && | 
|  | 5519 | (profile->mSamplingRates.size() != 0) && | 
|  | 5520 | (profile->mFormats.size() != 0)) { | 
|  | 5521 |  | 
|  | 5522 | ALOGV("loadInput() adding input Supported Devices %04x", | 
|  | 5523 | profile->mSupportedDevices.types()); | 
|  | 5524 |  | 
|  | 5525 | mInputProfiles.add(profile); | 
|  | 5526 | return NO_ERROR; | 
|  | 5527 | } else { | 
|  | 5528 | return BAD_VALUE; | 
|  | 5529 | } | 
|  | 5530 | } | 
|  | 5531 |  | 
|  | 5532 | status_t AudioPolicyManager::HwModule::loadOutput(cnode *root) | 
|  | 5533 | { | 
|  | 5534 | cnode *node = root->first_child; | 
|  | 5535 |  | 
|  | 5536 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this); | 
|  | 5537 |  | 
|  | 5538 | while (node) { | 
|  | 5539 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { | 
|  | 5540 | profile->loadSamplingRates((char *)node->value); | 
|  | 5541 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { | 
|  | 5542 | profile->loadFormats((char *)node->value); | 
|  | 5543 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 5544 | profile->loadOutChannels((char *)node->value); | 
|  | 5545 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { | 
|  | 5546 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, | 
|  | 5547 | mDeclaredDevices); | 
|  | 5548 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { | 
|  | 5549 | profile->mFlags = parseFlagNames((char *)node->value); | 
|  | 5550 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 5551 | profile->loadGains(node); | 
|  | 5552 | } | 
|  | 5553 | node = node->next; | 
|  | 5554 | } | 
|  | 5555 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), | 
|  | 5556 | "loadOutput() invalid supported devices"); | 
|  | 5557 | ALOGW_IF(profile->mChannelMasks.size() == 0, | 
|  | 5558 | "loadOutput() invalid supported channel masks"); | 
|  | 5559 | ALOGW_IF(profile->mSamplingRates.size() == 0, | 
|  | 5560 | "loadOutput() invalid supported sampling rates"); | 
|  | 5561 | ALOGW_IF(profile->mFormats.size() == 0, | 
|  | 5562 | "loadOutput() invalid supported formats"); | 
|  | 5563 | if (!profile->mSupportedDevices.isEmpty() && | 
|  | 5564 | (profile->mChannelMasks.size() != 0) && | 
|  | 5565 | (profile->mSamplingRates.size() != 0) && | 
|  | 5566 | (profile->mFormats.size() != 0)) { | 
|  | 5567 |  | 
|  | 5568 | ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x", | 
|  | 5569 | profile->mSupportedDevices.types(), profile->mFlags); | 
|  | 5570 |  | 
|  | 5571 | mOutputProfiles.add(profile); | 
|  | 5572 | return NO_ERROR; | 
|  | 5573 | } else { | 
|  | 5574 | return BAD_VALUE; | 
|  | 5575 | } | 
|  | 5576 | } | 
|  | 5577 |  | 
|  | 5578 | status_t AudioPolicyManager::HwModule::loadDevice(cnode *root) | 
|  | 5579 | { | 
|  | 5580 | cnode *node = root->first_child; | 
|  | 5581 |  | 
|  | 5582 | audio_devices_t type = AUDIO_DEVICE_NONE; | 
|  | 5583 | while (node) { | 
|  | 5584 | if (strcmp(node->name, DEVICE_TYPE) == 0) { | 
|  | 5585 | type = parseDeviceNames((char *)node->value); | 
|  | 5586 | break; | 
|  | 5587 | } | 
|  | 5588 | node = node->next; | 
|  | 5589 | } | 
|  | 5590 | if (type == AUDIO_DEVICE_NONE || | 
|  | 5591 | (!audio_is_input_device(type) && !audio_is_output_device(type))) { | 
|  | 5592 | ALOGW("loadDevice() bad type %08x", type); | 
|  | 5593 | return BAD_VALUE; | 
|  | 5594 | } | 
|  | 5595 | sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type); | 
|  | 5596 | deviceDesc->mModule = this; | 
|  | 5597 |  | 
|  | 5598 | node = root->first_child; | 
|  | 5599 | while (node) { | 
|  | 5600 | if (strcmp(node->name, DEVICE_ADDRESS) == 0) { | 
|  | 5601 | deviceDesc->mAddress = String8((char *)node->value); | 
|  | 5602 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { | 
|  | 5603 | if (audio_is_input_device(type)) { | 
|  | 5604 | deviceDesc->loadInChannels((char *)node->value); | 
|  | 5605 | } else { | 
|  | 5606 | deviceDesc->loadOutChannels((char *)node->value); | 
|  | 5607 | } | 
|  | 5608 | } else if (strcmp(node->name, GAINS_TAG) == 0) { | 
|  | 5609 | deviceDesc->loadGains(node); | 
|  | 5610 | } | 
|  | 5611 | node = node->next; | 
|  | 5612 | } | 
|  | 5613 |  | 
|  | 5614 | ALOGV("loadDevice() adding device name %s type %08x address %s", | 
|  | 5615 | deviceDesc->mName.string(), type, deviceDesc->mAddress.string()); | 
|  | 5616 |  | 
|  | 5617 | mDeclaredDevices.add(deviceDesc); | 
|  | 5618 |  | 
|  | 5619 | return NO_ERROR; | 
|  | 5620 | } | 
|  | 5621 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5622 | void AudioPolicyManager::HwModule::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5623 | { | 
|  | 5624 | const size_t SIZE = 256; | 
|  | 5625 | char buffer[SIZE]; | 
|  | 5626 | String8 result; | 
|  | 5627 |  | 
|  | 5628 | snprintf(buffer, SIZE, "  - name: %s\n", mName); | 
|  | 5629 | result.append(buffer); | 
|  | 5630 | snprintf(buffer, SIZE, "  - handle: %d\n", mHandle); | 
|  | 5631 | result.append(buffer); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5632 | snprintf(buffer, SIZE, "  - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF); | 
|  | 5633 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5634 | write(fd, result.string(), result.size()); | 
|  | 5635 | if (mOutputProfiles.size()) { | 
|  | 5636 | write(fd, "  - outputs:\n", strlen("  - outputs:\n")); | 
|  | 5637 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5638 | snprintf(buffer, SIZE, "    output %zu:\n", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5639 | write(fd, buffer, strlen(buffer)); | 
|  | 5640 | mOutputProfiles[i]->dump(fd); | 
|  | 5641 | } | 
|  | 5642 | } | 
|  | 5643 | if (mInputProfiles.size()) { | 
|  | 5644 | write(fd, "  - inputs:\n", strlen("  - inputs:\n")); | 
|  | 5645 | for (size_t i = 0; i < mInputProfiles.size(); i++) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5646 | snprintf(buffer, SIZE, "    input %zu:\n", i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | write(fd, buffer, strlen(buffer)); | 
|  | 5648 | mInputProfiles[i]->dump(fd); | 
|  | 5649 | } | 
|  | 5650 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5651 | if (mDeclaredDevices.size()) { | 
|  | 5652 | write(fd, "  - devices:\n", strlen("  - devices:\n")); | 
|  | 5653 | for (size_t i = 0; i < mDeclaredDevices.size(); i++) { | 
|  | 5654 | mDeclaredDevices[i]->dump(fd, 4, i); | 
|  | 5655 | } | 
|  | 5656 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5657 | } | 
|  | 5658 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5659 | // --- AudioPort class implementation | 
|  | 5660 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5661 |  | 
|  | 5662 | AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type, | 
|  | 5663 | audio_port_role_t role, const sp<HwModule>& module) : | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5664 | 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] | 5665 | { | 
|  | 5666 | mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || | 
|  | 5667 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); | 
|  | 5668 | } | 
|  | 5669 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5670 | void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const | 
|  | 5671 | { | 
|  | 5672 | port->role = mRole; | 
|  | 5673 | port->type = mType; | 
|  | 5674 | unsigned int i; | 
|  | 5675 | for (i = 0; i < mSamplingRates.size() && i < AUDIO_PORT_MAX_SAMPLING_RATES; i++) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5676 | if (mSamplingRates[i] != 0) { | 
|  | 5677 | port->sample_rates[i] = mSamplingRates[i]; | 
|  | 5678 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5679 | } | 
|  | 5680 | port->num_sample_rates = i; | 
|  | 5681 | for (i = 0; i < mChannelMasks.size() && i < AUDIO_PORT_MAX_CHANNEL_MASKS; i++) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5682 | if (mChannelMasks[i] != 0) { | 
|  | 5683 | port->channel_masks[i] = mChannelMasks[i]; | 
|  | 5684 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5685 | } | 
|  | 5686 | port->num_channel_masks = i; | 
|  | 5687 | for (i = 0; i < mFormats.size() && i < AUDIO_PORT_MAX_FORMATS; i++) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5688 | if (mFormats[i] != 0) { | 
|  | 5689 | port->formats[i] = mFormats[i]; | 
|  | 5690 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5691 | } | 
|  | 5692 | port->num_formats = i; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5693 |  | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 5694 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5695 |  | 
|  | 5696 | for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) { | 
|  | 5697 | port->gains[i] = mGains[i]->mGain; | 
|  | 5698 | } | 
|  | 5699 | port->num_gains = i; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5700 | } | 
|  | 5701 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5702 | void AudioPolicyManager::AudioPort::importAudioPort(const sp<AudioPort> port) { | 
|  | 5703 | for (size_t k = 0 ; k < port->mSamplingRates.size() ; k++) { | 
|  | 5704 | const uint32_t rate = port->mSamplingRates.itemAt(k); | 
|  | 5705 | if (rate != 0) { // skip "dynamic" rates | 
|  | 5706 | bool hasRate = false; | 
|  | 5707 | for (size_t l = 0 ; l < mSamplingRates.size() ; l++) { | 
|  | 5708 | if (rate == mSamplingRates.itemAt(l)) { | 
|  | 5709 | hasRate = true; | 
|  | 5710 | break; | 
|  | 5711 | } | 
|  | 5712 | } | 
|  | 5713 | if (!hasRate) { // never import a sampling rate twice | 
|  | 5714 | mSamplingRates.add(rate); | 
|  | 5715 | } | 
|  | 5716 | } | 
|  | 5717 | } | 
|  | 5718 | for (size_t k = 0 ; k < port->mChannelMasks.size() ; k++) { | 
|  | 5719 | const audio_channel_mask_t mask = port->mChannelMasks.itemAt(k); | 
|  | 5720 | if (mask != 0) { // skip "dynamic" masks | 
|  | 5721 | bool hasMask = false; | 
|  | 5722 | for (size_t l = 0 ; l < mChannelMasks.size() ; l++) { | 
|  | 5723 | if (mask == mChannelMasks.itemAt(l)) { | 
|  | 5724 | hasMask = true; | 
|  | 5725 | break; | 
|  | 5726 | } | 
|  | 5727 | } | 
|  | 5728 | if (!hasMask) { // never import a channel mask twice | 
|  | 5729 | mChannelMasks.add(mask); | 
|  | 5730 | } | 
|  | 5731 | } | 
|  | 5732 | } | 
|  | 5733 | for (size_t k = 0 ; k < port->mFormats.size() ; k++) { | 
|  | 5734 | const audio_format_t format = port->mFormats.itemAt(k); | 
|  | 5735 | if (format != 0) { // skip "dynamic" formats | 
|  | 5736 | bool hasFormat = false; | 
|  | 5737 | for (size_t l = 0 ; l < mFormats.size() ; l++) { | 
|  | 5738 | if (format == mFormats.itemAt(l)) { | 
|  | 5739 | hasFormat = true; | 
|  | 5740 | break; | 
|  | 5741 | } | 
|  | 5742 | } | 
|  | 5743 | if (!hasFormat) { // never import a channel mask twice | 
|  | 5744 | mFormats.add(format); | 
|  | 5745 | } | 
|  | 5746 | } | 
|  | 5747 | } | 
|  | 5748 | } | 
|  | 5749 |  | 
|  | 5750 | void AudioPolicyManager::AudioPort::clearCapabilities() { | 
|  | 5751 | mChannelMasks.clear(); | 
|  | 5752 | mFormats.clear(); | 
|  | 5753 | mSamplingRates.clear(); | 
|  | 5754 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5755 |  | 
|  | 5756 | void AudioPolicyManager::AudioPort::loadSamplingRates(char *name) | 
|  | 5757 | { | 
|  | 5758 | char *str = strtok(name, "|"); | 
|  | 5759 |  | 
|  | 5760 | // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling | 
|  | 5761 | // rates should be read from the output stream after it is opened for the first time | 
|  | 5762 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5763 | mSamplingRates.add(0); | 
|  | 5764 | return; | 
|  | 5765 | } | 
|  | 5766 |  | 
|  | 5767 | while (str != NULL) { | 
|  | 5768 | uint32_t rate = atoi(str); | 
|  | 5769 | if (rate != 0) { | 
|  | 5770 | ALOGV("loadSamplingRates() adding rate %d", rate); | 
|  | 5771 | mSamplingRates.add(rate); | 
|  | 5772 | } | 
|  | 5773 | str = strtok(NULL, "|"); | 
|  | 5774 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5775 | } | 
|  | 5776 |  | 
|  | 5777 | void AudioPolicyManager::AudioPort::loadFormats(char *name) | 
|  | 5778 | { | 
|  | 5779 | char *str = strtok(name, "|"); | 
|  | 5780 |  | 
|  | 5781 | // by convention, "0' in the first entry in mFormats indicates the supported formats | 
|  | 5782 | // should be read from the output stream after it is opened for the first time | 
|  | 5783 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5784 | mFormats.add(AUDIO_FORMAT_DEFAULT); | 
|  | 5785 | return; | 
|  | 5786 | } | 
|  | 5787 |  | 
|  | 5788 | while (str != NULL) { | 
|  | 5789 | audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable, | 
|  | 5790 | ARRAY_SIZE(sFormatNameToEnumTable), | 
|  | 5791 | str); | 
|  | 5792 | if (format != AUDIO_FORMAT_DEFAULT) { | 
|  | 5793 | mFormats.add(format); | 
|  | 5794 | } | 
|  | 5795 | str = strtok(NULL, "|"); | 
|  | 5796 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5797 | } | 
|  | 5798 |  | 
|  | 5799 | void AudioPolicyManager::AudioPort::loadInChannels(char *name) | 
|  | 5800 | { | 
|  | 5801 | const char *str = strtok(name, "|"); | 
|  | 5802 |  | 
|  | 5803 | ALOGV("loadInChannels() %s", name); | 
|  | 5804 |  | 
|  | 5805 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5806 | mChannelMasks.add(0); | 
|  | 5807 | return; | 
|  | 5808 | } | 
|  | 5809 |  | 
|  | 5810 | while (str != NULL) { | 
|  | 5811 | audio_channel_mask_t channelMask = | 
|  | 5812 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, | 
|  | 5813 | ARRAY_SIZE(sInChannelsNameToEnumTable), | 
|  | 5814 | str); | 
|  | 5815 | if (channelMask != 0) { | 
|  | 5816 | ALOGV("loadInChannels() adding channelMask %04x", channelMask); | 
|  | 5817 | mChannelMasks.add(channelMask); | 
|  | 5818 | } | 
|  | 5819 | str = strtok(NULL, "|"); | 
|  | 5820 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5821 | } | 
|  | 5822 |  | 
|  | 5823 | void AudioPolicyManager::AudioPort::loadOutChannels(char *name) | 
|  | 5824 | { | 
|  | 5825 | const char *str = strtok(name, "|"); | 
|  | 5826 |  | 
|  | 5827 | ALOGV("loadOutChannels() %s", name); | 
|  | 5828 |  | 
|  | 5829 | // by convention, "0' in the first entry in mChannelMasks indicates the supported channel | 
|  | 5830 | // masks should be read from the output stream after it is opened for the first time | 
|  | 5831 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { | 
|  | 5832 | mChannelMasks.add(0); | 
|  | 5833 | return; | 
|  | 5834 | } | 
|  | 5835 |  | 
|  | 5836 | while (str != NULL) { | 
|  | 5837 | audio_channel_mask_t channelMask = | 
|  | 5838 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, | 
|  | 5839 | ARRAY_SIZE(sOutChannelsNameToEnumTable), | 
|  | 5840 | str); | 
|  | 5841 | if (channelMask != 0) { | 
|  | 5842 | mChannelMasks.add(channelMask); | 
|  | 5843 | } | 
|  | 5844 | str = strtok(NULL, "|"); | 
|  | 5845 | } | 
|  | 5846 | return; | 
|  | 5847 | } | 
|  | 5848 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5849 | audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name) | 
|  | 5850 | { | 
|  | 5851 | const char *str = strtok(name, "|"); | 
|  | 5852 |  | 
|  | 5853 | ALOGV("loadGainMode() %s", name); | 
|  | 5854 | audio_gain_mode_t mode = 0; | 
|  | 5855 | while (str != NULL) { | 
|  | 5856 | mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable, | 
|  | 5857 | ARRAY_SIZE(sGainModeNameToEnumTable), | 
|  | 5858 | str); | 
|  | 5859 | str = strtok(NULL, "|"); | 
|  | 5860 | } | 
|  | 5861 | return mode; | 
|  | 5862 | } | 
|  | 5863 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5864 | void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index) | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5865 | { | 
|  | 5866 | cnode *node = root->first_child; | 
|  | 5867 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5868 | sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5869 |  | 
|  | 5870 | while (node) { | 
|  | 5871 | if (strcmp(node->name, GAIN_MODE) == 0) { | 
|  | 5872 | gain->mGain.mode = loadGainMode((char *)node->value); | 
|  | 5873 | } else if (strcmp(node->name, GAIN_CHANNELS) == 0) { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5874 | if (mUseInChannelMask) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5875 | gain->mGain.channel_mask = | 
|  | 5876 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, | 
|  | 5877 | ARRAY_SIZE(sInChannelsNameToEnumTable), | 
|  | 5878 | (char *)node->value); | 
|  | 5879 | } else { | 
|  | 5880 | gain->mGain.channel_mask = | 
|  | 5881 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, | 
|  | 5882 | ARRAY_SIZE(sOutChannelsNameToEnumTable), | 
|  | 5883 | (char *)node->value); | 
|  | 5884 | } | 
|  | 5885 | } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) { | 
|  | 5886 | gain->mGain.min_value = atoi((char *)node->value); | 
|  | 5887 | } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) { | 
|  | 5888 | gain->mGain.max_value = atoi((char *)node->value); | 
|  | 5889 | } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) { | 
|  | 5890 | gain->mGain.default_value = atoi((char *)node->value); | 
|  | 5891 | } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) { | 
|  | 5892 | gain->mGain.step_value = atoi((char *)node->value); | 
|  | 5893 | } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) { | 
|  | 5894 | gain->mGain.min_ramp_ms = atoi((char *)node->value); | 
|  | 5895 | } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) { | 
|  | 5896 | gain->mGain.max_ramp_ms = atoi((char *)node->value); | 
|  | 5897 | } | 
|  | 5898 | node = node->next; | 
|  | 5899 | } | 
|  | 5900 |  | 
|  | 5901 | ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d", | 
|  | 5902 | gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value); | 
|  | 5903 |  | 
|  | 5904 | if (gain->mGain.mode == 0) { | 
|  | 5905 | return; | 
|  | 5906 | } | 
|  | 5907 | mGains.add(gain); | 
|  | 5908 | } | 
|  | 5909 |  | 
|  | 5910 | void AudioPolicyManager::AudioPort::loadGains(cnode *root) | 
|  | 5911 | { | 
|  | 5912 | cnode *node = root->first_child; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5913 | int index = 0; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5914 | while (node) { | 
|  | 5915 | ALOGV("loadGains() loading gain %s", node->name); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5916 | loadGain(node, index++); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5917 | node = node->next; | 
|  | 5918 | } | 
|  | 5919 | } | 
|  | 5920 |  | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5921 | status_t AudioPolicyManager::AudioPort::checkExactSamplingRate(uint32_t samplingRate) const | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5922 | { | 
|  | 5923 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { | 
|  | 5924 | if (mSamplingRates[i] == samplingRate) { | 
|  | 5925 | return NO_ERROR; | 
|  | 5926 | } | 
|  | 5927 | } | 
|  | 5928 | return BAD_VALUE; | 
|  | 5929 | } | 
|  | 5930 |  | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5931 | status_t AudioPolicyManager::AudioPort::checkCompatibleSamplingRate(uint32_t samplingRate, | 
|  | 5932 | uint32_t *updatedSamplingRate) const | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5933 | { | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5934 | // Search for the closest supported sampling rate that is above (preferred) | 
|  | 5935 | // or below (acceptable) the desired sampling rate, within a permitted ratio. | 
|  | 5936 | // The sampling rates do not need to be sorted in ascending order. | 
|  | 5937 | ssize_t maxBelow = -1; | 
|  | 5938 | ssize_t minAbove = -1; | 
|  | 5939 | uint32_t candidate; | 
|  | 5940 | for (size_t i = 0; i < mSamplingRates.size(); i++) { | 
|  | 5941 | candidate = mSamplingRates[i]; | 
|  | 5942 | if (candidate == samplingRate) { | 
|  | 5943 | if (updatedSamplingRate != NULL) { | 
|  | 5944 | *updatedSamplingRate = candidate; | 
|  | 5945 | } | 
|  | 5946 | return NO_ERROR; | 
|  | 5947 | } | 
|  | 5948 | // candidate < desired | 
|  | 5949 | if (candidate < samplingRate) { | 
|  | 5950 | if (maxBelow < 0 || candidate > mSamplingRates[maxBelow]) { | 
|  | 5951 | maxBelow = i; | 
|  | 5952 | } | 
|  | 5953 | // candidate > desired | 
|  | 5954 | } else { | 
|  | 5955 | if (minAbove < 0 || candidate < mSamplingRates[minAbove]) { | 
|  | 5956 | minAbove = i; | 
|  | 5957 | } | 
|  | 5958 | } | 
|  | 5959 | } | 
|  | 5960 | // This uses hard-coded knowledge about AudioFlinger resampling ratios. | 
|  | 5961 | // TODO Move these assumptions out. | 
|  | 5962 | static const uint32_t kMaxDownSampleRatio = 6;  // beyond this aliasing occurs | 
|  | 5963 | static const uint32_t kMaxUpSampleRatio = 256;  // beyond this sample rate inaccuracies occur | 
|  | 5964 | // due to approximation by an int32_t of the | 
|  | 5965 | // phase increments | 
|  | 5966 | // Prefer to down-sample from a higher sampling rate, as we get the desired frequency spectrum. | 
|  | 5967 | if (minAbove >= 0) { | 
|  | 5968 | candidate = mSamplingRates[minAbove]; | 
|  | 5969 | if (candidate / kMaxDownSampleRatio <= samplingRate) { | 
|  | 5970 | if (updatedSamplingRate != NULL) { | 
|  | 5971 | *updatedSamplingRate = candidate; | 
|  | 5972 | } | 
|  | 5973 | return NO_ERROR; | 
|  | 5974 | } | 
|  | 5975 | } | 
|  | 5976 | // But if we have to up-sample from a lower sampling rate, that's OK. | 
|  | 5977 | if (maxBelow >= 0) { | 
|  | 5978 | candidate = mSamplingRates[maxBelow]; | 
|  | 5979 | if (candidate * kMaxUpSampleRatio >= samplingRate) { | 
|  | 5980 | if (updatedSamplingRate != NULL) { | 
|  | 5981 | *updatedSamplingRate = candidate; | 
|  | 5982 | } | 
|  | 5983 | return NO_ERROR; | 
|  | 5984 | } | 
|  | 5985 | } | 
|  | 5986 | // leave updatedSamplingRate unmodified | 
|  | 5987 | return BAD_VALUE; | 
|  | 5988 | } | 
|  | 5989 |  | 
|  | 5990 | status_t AudioPolicyManager::AudioPort::checkExactChannelMask(audio_channel_mask_t channelMask) const | 
|  | 5991 | { | 
|  | 5992 | for (size_t i = 0; i < mChannelMasks.size(); i++) { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5993 | if (mChannelMasks[i] == channelMask) { | 
|  | 5994 | return NO_ERROR; | 
|  | 5995 | } | 
|  | 5996 | } | 
|  | 5997 | return BAD_VALUE; | 
|  | 5998 | } | 
|  | 5999 |  | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6000 | status_t AudioPolicyManager::AudioPort::checkCompatibleChannelMask(audio_channel_mask_t channelMask) | 
|  | 6001 | const | 
|  | 6002 | { | 
|  | 6003 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; | 
|  | 6004 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { | 
|  | 6005 | // FIXME Does not handle multi-channel automatic conversions yet | 
|  | 6006 | audio_channel_mask_t supported = mChannelMasks[i]; | 
|  | 6007 | if (supported == channelMask) { | 
|  | 6008 | return NO_ERROR; | 
|  | 6009 | } | 
|  | 6010 | if (isRecordThread) { | 
|  | 6011 | // This uses hard-coded knowledge that AudioFlinger can silently down-mix and up-mix. | 
|  | 6012 | // FIXME Abstract this out to a table. | 
|  | 6013 | if (((supported == AUDIO_CHANNEL_IN_FRONT_BACK || supported == AUDIO_CHANNEL_IN_STEREO) | 
|  | 6014 | && channelMask == AUDIO_CHANNEL_IN_MONO) || | 
|  | 6015 | (supported == AUDIO_CHANNEL_IN_MONO && (channelMask == AUDIO_CHANNEL_IN_FRONT_BACK | 
|  | 6016 | || channelMask == AUDIO_CHANNEL_IN_STEREO))) { | 
|  | 6017 | return NO_ERROR; | 
|  | 6018 | } | 
|  | 6019 | } | 
|  | 6020 | } | 
|  | 6021 | return BAD_VALUE; | 
|  | 6022 | } | 
|  | 6023 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6024 | status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const | 
|  | 6025 | { | 
|  | 6026 | for (size_t i = 0; i < mFormats.size(); i ++) { | 
|  | 6027 | if (mFormats[i] == format) { | 
|  | 6028 | return NO_ERROR; | 
|  | 6029 | } | 
|  | 6030 | } | 
|  | 6031 | return BAD_VALUE; | 
|  | 6032 | } | 
|  | 6033 |  | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6034 |  | 
|  | 6035 | uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const | 
|  | 6036 | { | 
|  | 6037 | // special case for uninitialized dynamic profile | 
|  | 6038 | if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) { | 
|  | 6039 | return 0; | 
|  | 6040 | } | 
|  | 6041 |  | 
|  | 6042 | uint32_t samplingRate = 0; | 
|  | 6043 | uint32_t maxRate = MAX_MIXER_SAMPLING_RATE; | 
|  | 6044 |  | 
|  | 6045 | // For mixed output and inputs, use max mixer sampling rates. Do not | 
|  | 6046 | // limit sampling rate otherwise | 
|  | 6047 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 6048 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
|  | 6049 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) { | 
|  | 6050 | maxRate = UINT_MAX; | 
|  | 6051 | } | 
|  | 6052 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { | 
|  | 6053 | if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) { | 
|  | 6054 | samplingRate = mSamplingRates[i]; | 
|  | 6055 | } | 
|  | 6056 | } | 
|  | 6057 | return samplingRate; | 
|  | 6058 | } | 
|  | 6059 |  | 
|  | 6060 | audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const | 
|  | 6061 | { | 
|  | 6062 | // special case for uninitialized dynamic profile | 
|  | 6063 | if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) { | 
|  | 6064 | return AUDIO_CHANNEL_NONE; | 
|  | 6065 | } | 
|  | 6066 |  | 
|  | 6067 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE; | 
|  | 6068 | uint32_t channelCount = 0; | 
|  | 6069 | uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; | 
|  | 6070 |  | 
|  | 6071 | // For mixed output and inputs, use max mixer channel count. Do not | 
|  | 6072 | // limit channel count otherwise | 
|  | 6073 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 6074 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
|  | 6075 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)))) { | 
|  | 6076 | maxCount = UINT_MAX; | 
|  | 6077 | } | 
|  | 6078 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { | 
|  | 6079 | uint32_t cnlCount; | 
|  | 6080 | if (mUseInChannelMask) { | 
|  | 6081 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); | 
|  | 6082 | } else { | 
|  | 6083 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); | 
|  | 6084 | } | 
|  | 6085 | if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { | 
|  | 6086 | channelMask = mChannelMasks[i]; | 
|  | 6087 | } | 
|  | 6088 | } | 
|  | 6089 | return channelMask; | 
|  | 6090 | } | 
|  | 6091 |  | 
| Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6092 | /* format in order of increasing preference */ | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6093 | const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = { | 
|  | 6094 | AUDIO_FORMAT_DEFAULT, | 
|  | 6095 | AUDIO_FORMAT_PCM_16_BIT, | 
| Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6096 | AUDIO_FORMAT_PCM_8_24_BIT, | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6097 | AUDIO_FORMAT_PCM_24_BIT_PACKED, | 
| Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6098 | AUDIO_FORMAT_PCM_32_BIT, | 
| Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6099 | AUDIO_FORMAT_PCM_FLOAT, | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6100 | }; | 
|  | 6101 |  | 
|  | 6102 | int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1, | 
|  | 6103 | audio_format_t format2) | 
|  | 6104 | { | 
|  | 6105 | // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any | 
|  | 6106 | // compressed format and better than any PCM format. This is by design of pickFormat() | 
|  | 6107 | if (!audio_is_linear_pcm(format1)) { | 
|  | 6108 | if (!audio_is_linear_pcm(format2)) { | 
|  | 6109 | return 0; | 
|  | 6110 | } | 
|  | 6111 | return 1; | 
|  | 6112 | } | 
|  | 6113 | if (!audio_is_linear_pcm(format2)) { | 
|  | 6114 | return -1; | 
|  | 6115 | } | 
|  | 6116 |  | 
|  | 6117 | int index1 = -1, index2 = -1; | 
|  | 6118 | for (size_t i = 0; | 
|  | 6119 | (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1)); | 
|  | 6120 | i ++) { | 
|  | 6121 | if (sPcmFormatCompareTable[i] == format1) { | 
|  | 6122 | index1 = i; | 
|  | 6123 | } | 
|  | 6124 | if (sPcmFormatCompareTable[i] == format2) { | 
|  | 6125 | index2 = i; | 
|  | 6126 | } | 
|  | 6127 | } | 
|  | 6128 | // format1 not found => index1 < 0 => format2 > format1 | 
|  | 6129 | // format2 not found => index2 < 0 => format2 < format1 | 
|  | 6130 | return index1 - index2; | 
|  | 6131 | } | 
|  | 6132 |  | 
|  | 6133 | audio_format_t AudioPolicyManager::AudioPort::pickFormat() const | 
|  | 6134 | { | 
|  | 6135 | // special case for uninitialized dynamic profile | 
|  | 6136 | if (mFormats.size() == 1 && mFormats[0] == 0) { | 
|  | 6137 | return AUDIO_FORMAT_DEFAULT; | 
|  | 6138 | } | 
|  | 6139 |  | 
|  | 6140 | audio_format_t format = AUDIO_FORMAT_DEFAULT; | 
| Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6141 | audio_format_t bestFormat = | 
|  | 6142 | AudioPolicyManager::AudioPort::sPcmFormatCompareTable[ | 
|  | 6143 | ARRAY_SIZE(AudioPolicyManager::AudioPort::sPcmFormatCompareTable) - 1]; | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6144 | // For mixed output and inputs, use best mixer output format. Do not | 
|  | 6145 | // limit format otherwise | 
|  | 6146 | if ((mType != AUDIO_PORT_TYPE_MIX) || | 
|  | 6147 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && | 
| Eric Laurent | d862237 | 2014-07-27 13:47:31 -0700 | [diff] [blame] | 6148 | (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) != 0)))) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6149 | bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 6150 | } | 
|  | 6151 |  | 
|  | 6152 | for (size_t i = 0; i < mFormats.size(); i ++) { | 
|  | 6153 | if ((compareFormats(mFormats[i], format) > 0) && | 
|  | 6154 | (compareFormats(mFormats[i], bestFormat) <= 0)) { | 
|  | 6155 | format = mFormats[i]; | 
|  | 6156 | } | 
|  | 6157 | } | 
|  | 6158 | return format; | 
|  | 6159 | } | 
|  | 6160 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6161 | status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig, | 
|  | 6162 | int index) const | 
|  | 6163 | { | 
|  | 6164 | if (index < 0 || (size_t)index >= mGains.size()) { | 
|  | 6165 | return BAD_VALUE; | 
|  | 6166 | } | 
|  | 6167 | return mGains[index]->checkConfig(gainConfig); | 
|  | 6168 | } | 
|  | 6169 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6170 | void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const | 
|  | 6171 | { | 
|  | 6172 | const size_t SIZE = 256; | 
|  | 6173 | char buffer[SIZE]; | 
|  | 6174 | String8 result; | 
|  | 6175 |  | 
|  | 6176 | if (mName.size() != 0) { | 
|  | 6177 | snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string()); | 
|  | 6178 | result.append(buffer); | 
|  | 6179 | } | 
|  | 6180 |  | 
|  | 6181 | if (mSamplingRates.size() != 0) { | 
|  | 6182 | snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, ""); | 
|  | 6183 | result.append(buffer); | 
|  | 6184 | for (size_t i = 0; i < mSamplingRates.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6185 | if (i == 0 && mSamplingRates[i] == 0) { | 
|  | 6186 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 6187 | } else { | 
|  | 6188 | snprintf(buffer, SIZE, "%d", mSamplingRates[i]); | 
|  | 6189 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6190 | result.append(buffer); | 
|  | 6191 | result.append(i == (mSamplingRates.size() - 1) ? "" : ", "); | 
|  | 6192 | } | 
|  | 6193 | result.append("\n"); | 
|  | 6194 | } | 
|  | 6195 |  | 
|  | 6196 | if (mChannelMasks.size() != 0) { | 
|  | 6197 | snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, ""); | 
|  | 6198 | result.append(buffer); | 
|  | 6199 | for (size_t i = 0; i < mChannelMasks.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6200 | ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]); | 
|  | 6201 |  | 
|  | 6202 | if (i == 0 && mChannelMasks[i] == 0) { | 
|  | 6203 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 6204 | } else { | 
|  | 6205 | snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]); | 
|  | 6206 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6207 | result.append(buffer); | 
|  | 6208 | result.append(i == (mChannelMasks.size() - 1) ? "" : ", "); | 
|  | 6209 | } | 
|  | 6210 | result.append("\n"); | 
|  | 6211 | } | 
|  | 6212 |  | 
|  | 6213 | if (mFormats.size() != 0) { | 
|  | 6214 | snprintf(buffer, SIZE, "%*s- formats: ", spaces, ""); | 
|  | 6215 | result.append(buffer); | 
|  | 6216 | for (size_t i = 0; i < mFormats.size(); i++) { | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6217 | const char *formatStr = enumToString(sFormatNameToEnumTable, | 
|  | 6218 | ARRAY_SIZE(sFormatNameToEnumTable), | 
|  | 6219 | mFormats[i]); | 
|  | 6220 | if (i == 0 && strcmp(formatStr, "") == 0) { | 
|  | 6221 | snprintf(buffer, SIZE, "Dynamic"); | 
|  | 6222 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6223 | snprintf(buffer, SIZE, "%s", formatStr); | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6224 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6225 | result.append(buffer); | 
|  | 6226 | result.append(i == (mFormats.size() - 1) ? "" : ", "); | 
|  | 6227 | } | 
|  | 6228 | result.append("\n"); | 
|  | 6229 | } | 
|  | 6230 | write(fd, result.string(), result.size()); | 
|  | 6231 | if (mGains.size() != 0) { | 
|  | 6232 | snprintf(buffer, SIZE, "%*s- gains:\n", spaces, ""); | 
|  | 6233 | write(fd, buffer, strlen(buffer) + 1); | 
|  | 6234 | result.append(buffer); | 
|  | 6235 | for (size_t i = 0; i < mGains.size(); i++) { | 
|  | 6236 | mGains[i]->dump(fd, spaces + 2, i); | 
|  | 6237 | } | 
|  | 6238 | } | 
|  | 6239 | } | 
|  | 6240 |  | 
|  | 6241 | // --- AudioGain class implementation | 
|  | 6242 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6243 | AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask) | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6244 | { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6245 | mIndex = index; | 
|  | 6246 | mUseInChannelMask = useInChannelMask; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6247 | memset(&mGain, 0, sizeof(struct audio_gain)); | 
|  | 6248 | } | 
|  | 6249 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6250 | void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config) | 
|  | 6251 | { | 
|  | 6252 | config->index = mIndex; | 
|  | 6253 | config->mode = mGain.mode; | 
|  | 6254 | config->channel_mask = mGain.channel_mask; | 
|  | 6255 | if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { | 
|  | 6256 | config->values[0] = mGain.default_value; | 
|  | 6257 | } else { | 
|  | 6258 | uint32_t numValues; | 
|  | 6259 | if (mUseInChannelMask) { | 
|  | 6260 | numValues = audio_channel_count_from_in_mask(mGain.channel_mask); | 
|  | 6261 | } else { | 
|  | 6262 | numValues = audio_channel_count_from_out_mask(mGain.channel_mask); | 
|  | 6263 | } | 
|  | 6264 | for (size_t i = 0; i < numValues; i++) { | 
|  | 6265 | config->values[i] = mGain.default_value; | 
|  | 6266 | } | 
|  | 6267 | } | 
|  | 6268 | if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { | 
|  | 6269 | config->ramp_duration_ms = mGain.min_ramp_ms; | 
|  | 6270 | } | 
|  | 6271 | } | 
|  | 6272 |  | 
|  | 6273 | status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config) | 
|  | 6274 | { | 
|  | 6275 | if ((config->mode & ~mGain.mode) != 0) { | 
|  | 6276 | return BAD_VALUE; | 
|  | 6277 | } | 
|  | 6278 | if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { | 
|  | 6279 | if ((config->values[0] < mGain.min_value) || | 
|  | 6280 | (config->values[0] > mGain.max_value)) { | 
|  | 6281 | return BAD_VALUE; | 
|  | 6282 | } | 
|  | 6283 | } else { | 
|  | 6284 | if ((config->channel_mask & ~mGain.channel_mask) != 0) { | 
|  | 6285 | return BAD_VALUE; | 
|  | 6286 | } | 
|  | 6287 | uint32_t numValues; | 
|  | 6288 | if (mUseInChannelMask) { | 
|  | 6289 | numValues = audio_channel_count_from_in_mask(config->channel_mask); | 
|  | 6290 | } else { | 
|  | 6291 | numValues = audio_channel_count_from_out_mask(config->channel_mask); | 
|  | 6292 | } | 
|  | 6293 | for (size_t i = 0; i < numValues; i++) { | 
|  | 6294 | if ((config->values[i] < mGain.min_value) || | 
|  | 6295 | (config->values[i] > mGain.max_value)) { | 
|  | 6296 | return BAD_VALUE; | 
|  | 6297 | } | 
|  | 6298 | } | 
|  | 6299 | } | 
|  | 6300 | if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { | 
|  | 6301 | if ((config->ramp_duration_ms < mGain.min_ramp_ms) || | 
|  | 6302 | (config->ramp_duration_ms > mGain.max_ramp_ms)) { | 
|  | 6303 | return BAD_VALUE; | 
|  | 6304 | } | 
|  | 6305 | } | 
|  | 6306 | return NO_ERROR; | 
|  | 6307 | } | 
|  | 6308 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6309 | void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const | 
|  | 6310 | { | 
|  | 6311 | const size_t SIZE = 256; | 
|  | 6312 | char buffer[SIZE]; | 
|  | 6313 | String8 result; | 
|  | 6314 |  | 
|  | 6315 | snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1); | 
|  | 6316 | result.append(buffer); | 
|  | 6317 | snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode); | 
|  | 6318 | result.append(buffer); | 
|  | 6319 | snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask); | 
|  | 6320 | result.append(buffer); | 
|  | 6321 | snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value); | 
|  | 6322 | result.append(buffer); | 
|  | 6323 | snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value); | 
|  | 6324 | result.append(buffer); | 
|  | 6325 | snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value); | 
|  | 6326 | result.append(buffer); | 
|  | 6327 | snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value); | 
|  | 6328 | result.append(buffer); | 
|  | 6329 | snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms); | 
|  | 6330 | result.append(buffer); | 
|  | 6331 | snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms); | 
|  | 6332 | result.append(buffer); | 
|  | 6333 |  | 
|  | 6334 | write(fd, result.string(), result.size()); | 
|  | 6335 | } | 
|  | 6336 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6337 | // --- AudioPortConfig class implementation | 
|  | 6338 |  | 
|  | 6339 | AudioPolicyManager::AudioPortConfig::AudioPortConfig() | 
|  | 6340 | { | 
|  | 6341 | mSamplingRate = 0; | 
|  | 6342 | mChannelMask = AUDIO_CHANNEL_NONE; | 
|  | 6343 | mFormat = AUDIO_FORMAT_INVALID; | 
|  | 6344 | mGain.index = -1; | 
|  | 6345 | } | 
|  | 6346 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6347 | status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig( | 
|  | 6348 | const struct audio_port_config *config, | 
|  | 6349 | struct audio_port_config *backupConfig) | 
|  | 6350 | { | 
|  | 6351 | struct audio_port_config localBackupConfig; | 
|  | 6352 | status_t status = NO_ERROR; | 
|  | 6353 |  | 
|  | 6354 | localBackupConfig.config_mask = config->config_mask; | 
|  | 6355 | toAudioPortConfig(&localBackupConfig); | 
|  | 6356 |  | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6357 | sp<AudioPort> audioport = getAudioPort(); | 
|  | 6358 | if (audioport == 0) { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6359 | status = NO_INIT; | 
|  | 6360 | goto exit; | 
|  | 6361 | } | 
|  | 6362 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6363 | status = audioport->checkExactSamplingRate(config->sample_rate); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6364 | if (status != NO_ERROR) { | 
|  | 6365 | goto exit; | 
|  | 6366 | } | 
|  | 6367 | mSamplingRate = config->sample_rate; | 
|  | 6368 | } | 
|  | 6369 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6370 | status = audioport->checkExactChannelMask(config->channel_mask); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6371 | if (status != NO_ERROR) { | 
|  | 6372 | goto exit; | 
|  | 6373 | } | 
|  | 6374 | mChannelMask = config->channel_mask; | 
|  | 6375 | } | 
|  | 6376 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6377 | status = audioport->checkFormat(config->format); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6378 | if (status != NO_ERROR) { | 
|  | 6379 | goto exit; | 
|  | 6380 | } | 
|  | 6381 | mFormat = config->format; | 
|  | 6382 | } | 
|  | 6383 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { | 
| Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6384 | status = audioport->checkGain(&config->gain, config->gain.index); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6385 | if (status != NO_ERROR) { | 
|  | 6386 | goto exit; | 
|  | 6387 | } | 
|  | 6388 | mGain = config->gain; | 
|  | 6389 | } | 
|  | 6390 |  | 
|  | 6391 | exit: | 
|  | 6392 | if (status != NO_ERROR) { | 
|  | 6393 | applyAudioPortConfig(&localBackupConfig); | 
|  | 6394 | } | 
|  | 6395 | if (backupConfig != NULL) { | 
|  | 6396 | *backupConfig = localBackupConfig; | 
|  | 6397 | } | 
|  | 6398 | return status; | 
|  | 6399 | } | 
|  | 6400 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6401 | void AudioPolicyManager::AudioPortConfig::toAudioPortConfig( | 
|  | 6402 | struct audio_port_config *dstConfig, | 
|  | 6403 | const struct audio_port_config *srcConfig) const | 
|  | 6404 | { | 
|  | 6405 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { | 
|  | 6406 | dstConfig->sample_rate = mSamplingRate; | 
|  | 6407 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) { | 
|  | 6408 | dstConfig->sample_rate = srcConfig->sample_rate; | 
|  | 6409 | } | 
|  | 6410 | } else { | 
|  | 6411 | dstConfig->sample_rate = 0; | 
|  | 6412 | } | 
|  | 6413 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { | 
|  | 6414 | dstConfig->channel_mask = mChannelMask; | 
|  | 6415 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) { | 
|  | 6416 | dstConfig->channel_mask = srcConfig->channel_mask; | 
|  | 6417 | } | 
|  | 6418 | } else { | 
|  | 6419 | dstConfig->channel_mask = AUDIO_CHANNEL_NONE; | 
|  | 6420 | } | 
|  | 6421 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) { | 
|  | 6422 | dstConfig->format = mFormat; | 
|  | 6423 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) { | 
|  | 6424 | dstConfig->format = srcConfig->format; | 
|  | 6425 | } | 
|  | 6426 | } else { | 
|  | 6427 | dstConfig->format = AUDIO_FORMAT_INVALID; | 
|  | 6428 | } | 
|  | 6429 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) { | 
|  | 6430 | dstConfig->gain = mGain; | 
|  | 6431 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) { | 
|  | 6432 | dstConfig->gain = srcConfig->gain; | 
|  | 6433 | } | 
|  | 6434 | } else { | 
|  | 6435 | dstConfig->gain.index = -1; | 
|  | 6436 | } | 
|  | 6437 | if (dstConfig->gain.index != -1) { | 
|  | 6438 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; | 
|  | 6439 | } else { | 
|  | 6440 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; | 
|  | 6441 | } | 
|  | 6442 | } | 
|  | 6443 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6444 | // --- IOProfile class implementation | 
|  | 6445 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6446 | AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role, | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6447 | const sp<HwModule>& module) | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6448 | : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6449 | { | 
|  | 6450 | } | 
|  | 6451 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6452 | AudioPolicyManager::IOProfile::~IOProfile() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6453 | { | 
|  | 6454 | } | 
|  | 6455 |  | 
|  | 6456 | // checks if the IO profile is compatible with specified parameters. | 
|  | 6457 | // Sampling rate, format and channel mask must be specified in order to | 
|  | 6458 | // get a valid a match | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6459 | bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6460 | uint32_t samplingRate, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6461 | uint32_t *updatedSamplingRate, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6462 | audio_format_t format, | 
|  | 6463 | audio_channel_mask_t channelMask, | 
|  | 6464 | audio_output_flags_t flags) const | 
|  | 6465 | { | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6466 | const bool isPlaybackThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SOURCE; | 
|  | 6467 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; | 
|  | 6468 | ALOG_ASSERT(isPlaybackThread != isRecordThread); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6469 |  | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6470 | if ((mSupportedDevices.types() & device) != device) { | 
|  | 6471 | return false; | 
|  | 6472 | } | 
|  | 6473 |  | 
|  | 6474 | if (samplingRate == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6475 | return false; | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6476 | } | 
|  | 6477 | uint32_t myUpdatedSamplingRate = samplingRate; | 
|  | 6478 | if (isPlaybackThread && checkExactSamplingRate(samplingRate) != NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6479 | return false; | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6480 | } | 
|  | 6481 | if (isRecordThread && checkCompatibleSamplingRate(samplingRate, &myUpdatedSamplingRate) != | 
|  | 6482 | NO_ERROR) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6483 | return false; | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6484 | } | 
|  | 6485 |  | 
|  | 6486 | if (!audio_is_valid_format(format) || checkFormat(format) != NO_ERROR) { | 
|  | 6487 | return false; | 
|  | 6488 | } | 
|  | 6489 |  | 
|  | 6490 | if (isPlaybackThread && (!audio_is_output_channel(channelMask) || | 
|  | 6491 | checkExactChannelMask(channelMask) != NO_ERROR)) { | 
|  | 6492 | return false; | 
|  | 6493 | } | 
|  | 6494 | if (isRecordThread && (!audio_is_input_channel(channelMask) || | 
|  | 6495 | checkCompatibleChannelMask(channelMask) != NO_ERROR)) { | 
|  | 6496 | return false; | 
|  | 6497 | } | 
|  | 6498 |  | 
|  | 6499 | if (isPlaybackThread && (mFlags & flags) != flags) { | 
|  | 6500 | return false; | 
|  | 6501 | } | 
|  | 6502 | // The only input flag that is allowed to be different is the fast flag. | 
|  | 6503 | // An existing fast stream is compatible with a normal track request. | 
|  | 6504 | // An existing normal stream is compatible with a fast track request, | 
|  | 6505 | // but the fast request will be denied by AudioFlinger and converted to normal track. | 
|  | 6506 | if (isRecordThread && (((audio_input_flags_t) mFlags ^ (audio_input_flags_t) flags) & | 
|  | 6507 | ~AUDIO_INPUT_FLAG_FAST)) { | 
|  | 6508 | return false; | 
|  | 6509 | } | 
|  | 6510 |  | 
|  | 6511 | if (updatedSamplingRate != NULL) { | 
|  | 6512 | *updatedSamplingRate = myUpdatedSamplingRate; | 
|  | 6513 | } | 
|  | 6514 | return true; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6515 | } | 
|  | 6516 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6517 | void AudioPolicyManager::IOProfile::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6518 | { | 
|  | 6519 | const size_t SIZE = 256; | 
|  | 6520 | char buffer[SIZE]; | 
|  | 6521 | String8 result; | 
|  | 6522 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6523 | AudioPort::dump(fd, 4); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6524 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6525 | snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags); | 
|  | 6526 | result.append(buffer); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6527 | snprintf(buffer, SIZE, "    - devices:\n"); | 
|  | 6528 | result.append(buffer); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6529 | write(fd, result.string(), result.size()); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6530 | for (size_t i = 0; i < mSupportedDevices.size(); i++) { | 
|  | 6531 | mSupportedDevices[i]->dump(fd, 6, i); | 
|  | 6532 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6533 | } | 
|  | 6534 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6535 | void AudioPolicyManager::IOProfile::log() | 
|  | 6536 | { | 
|  | 6537 | const size_t SIZE = 256; | 
|  | 6538 | char buffer[SIZE]; | 
|  | 6539 | String8 result; | 
|  | 6540 |  | 
|  | 6541 | ALOGV("    - sampling rates: "); | 
|  | 6542 | for (size_t i = 0; i < mSamplingRates.size(); i++) { | 
|  | 6543 | ALOGV("  %d", mSamplingRates[i]); | 
|  | 6544 | } | 
|  | 6545 |  | 
|  | 6546 | ALOGV("    - channel masks: "); | 
|  | 6547 | for (size_t i = 0; i < mChannelMasks.size(); i++) { | 
|  | 6548 | ALOGV("  0x%04x", mChannelMasks[i]); | 
|  | 6549 | } | 
|  | 6550 |  | 
|  | 6551 | ALOGV("    - formats: "); | 
|  | 6552 | for (size_t i = 0; i < mFormats.size(); i++) { | 
|  | 6553 | ALOGV("  0x%08x", mFormats[i]); | 
|  | 6554 | } | 
|  | 6555 |  | 
|  | 6556 | ALOGV("    - devices: 0x%04x\n", mSupportedDevices.types()); | 
|  | 6557 | ALOGV("    - flags: 0x%04x\n", mFlags); | 
|  | 6558 | } | 
|  | 6559 |  | 
|  | 6560 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6561 | // --- DeviceDescriptor implementation | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6562 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6563 |  | 
|  | 6564 | AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) : | 
|  | 6565 | AudioPort(name, AUDIO_PORT_TYPE_DEVICE, | 
|  | 6566 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : | 
|  | 6567 | AUDIO_PORT_ROLE_SOURCE, | 
|  | 6568 | NULL), | 
| Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6569 | mDeviceType(type), mAddress(""), mId(0) | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6570 | { | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6571 | if (mGains.size() > 0) { | 
|  | 6572 | mGains[0]->getDefaultConfig(&mGain); | 
|  | 6573 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6574 | } | 
|  | 6575 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6576 | bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6577 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6578 | // Devices are considered equal if they: | 
|  | 6579 | // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) | 
|  | 6580 | // - have the same address or one device does not specify the address | 
|  | 6581 | // - 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] | 6582 | return (mDeviceType == other->mDeviceType) && | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6583 | (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && | 
| Eric Laurent | 2f8a36f | 2014-03-26 19:05:55 -0700 | [diff] [blame] | 6584 | (mChannelMask == 0 || other->mChannelMask == 0 || | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6585 | mChannelMask == other->mChannelMask); | 
|  | 6586 | } | 
|  | 6587 |  | 
|  | 6588 | void AudioPolicyManager::DeviceVector::refreshTypes() | 
|  | 6589 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6590 | mDeviceTypes = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6591 | for(size_t i = 0; i < size(); i++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6592 | mDeviceTypes |= itemAt(i)->mDeviceType; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6593 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6594 | ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6595 | } | 
|  | 6596 |  | 
|  | 6597 | ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const | 
|  | 6598 | { | 
|  | 6599 | for(size_t i = 0; i < size(); i++) { | 
|  | 6600 | if (item->equals(itemAt(i))) { | 
|  | 6601 | return i; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6602 | } | 
|  | 6603 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6604 | return -1; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6605 | } | 
|  | 6606 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6607 | ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6608 | { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6609 | ssize_t ret = indexOf(item); | 
|  | 6610 |  | 
|  | 6611 | if (ret < 0) { | 
|  | 6612 | ret = SortedVector::add(item); | 
|  | 6613 | if (ret >= 0) { | 
|  | 6614 | refreshTypes(); | 
|  | 6615 | } | 
|  | 6616 | } else { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6617 | ALOGW("DeviceVector::add device %08x already in", item->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6618 | ret = -1; | 
|  | 6619 | } | 
|  | 6620 | return ret; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6621 | } | 
|  | 6622 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6623 | ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item) | 
|  | 6624 | { | 
|  | 6625 | size_t i; | 
|  | 6626 | ssize_t ret = indexOf(item); | 
|  | 6627 |  | 
|  | 6628 | if (ret < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6629 | ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6630 | } else { | 
|  | 6631 | ret = SortedVector::removeAt(ret); | 
|  | 6632 | if (ret >= 0) { | 
|  | 6633 | refreshTypes(); | 
|  | 6634 | } | 
|  | 6635 | } | 
|  | 6636 | return ret; | 
|  | 6637 | } | 
|  | 6638 |  | 
|  | 6639 | void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types) | 
|  | 6640 | { | 
|  | 6641 | DeviceVector deviceList; | 
|  | 6642 |  | 
|  | 6643 | uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; | 
|  | 6644 | types &= ~role_bit; | 
|  | 6645 |  | 
|  | 6646 | while (types) { | 
|  | 6647 | uint32_t i = 31 - __builtin_clz(types); | 
|  | 6648 | uint32_t type = 1 << i; | 
|  | 6649 | types &= ~type; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6650 | add(new DeviceDescriptor(String8(""), type | role_bit)); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6651 | } | 
|  | 6652 | } | 
|  | 6653 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6654 | void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name, | 
|  | 6655 | const DeviceVector& declaredDevices) | 
|  | 6656 | { | 
|  | 6657 | char *devName = strtok(name, "|"); | 
|  | 6658 | while (devName != NULL) { | 
|  | 6659 | if (strlen(devName) != 0) { | 
|  | 6660 | audio_devices_t type = stringToEnum(sDeviceNameToEnumTable, | 
|  | 6661 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6662 | devName); | 
|  | 6663 | if (type != AUDIO_DEVICE_NONE) { | 
|  | 6664 | add(new DeviceDescriptor(String8(""), type)); | 
|  | 6665 | } else { | 
|  | 6666 | sp<DeviceDescriptor> deviceDesc = | 
|  | 6667 | declaredDevices.getDeviceFromName(String8(devName)); | 
|  | 6668 | if (deviceDesc != 0) { | 
|  | 6669 | add(deviceDesc); | 
|  | 6670 | } | 
|  | 6671 | } | 
|  | 6672 | } | 
|  | 6673 | devName = strtok(NULL, "|"); | 
|  | 6674 | } | 
|  | 6675 | } | 
|  | 6676 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6677 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice( | 
|  | 6678 | audio_devices_t type, String8 address) const | 
|  | 6679 | { | 
|  | 6680 | sp<DeviceDescriptor> device; | 
|  | 6681 | for (size_t i = 0; i < size(); i++) { | 
|  | 6682 | if (itemAt(i)->mDeviceType == type) { | 
|  | 6683 | device = itemAt(i); | 
|  | 6684 | if (itemAt(i)->mAddress = address) { | 
|  | 6685 | break; | 
|  | 6686 | } | 
|  | 6687 | } | 
|  | 6688 | } | 
|  | 6689 | ALOGV("DeviceVector::getDevice() for type %d address %s found %p", | 
|  | 6690 | type, address.string(), device.get()); | 
|  | 6691 | return device; | 
|  | 6692 | } | 
|  | 6693 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6694 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId( | 
|  | 6695 | audio_port_handle_t id) const | 
|  | 6696 | { | 
|  | 6697 | sp<DeviceDescriptor> device; | 
|  | 6698 | for (size_t i = 0; i < size(); i++) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 6699 | 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] | 6700 | if (itemAt(i)->mId == id) { | 
|  | 6701 | device = itemAt(i); | 
|  | 6702 | break; | 
|  | 6703 | } | 
|  | 6704 | } | 
|  | 6705 | return device; | 
|  | 6706 | } | 
|  | 6707 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6708 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType( | 
|  | 6709 | audio_devices_t type) const | 
|  | 6710 | { | 
|  | 6711 | DeviceVector devices; | 
|  | 6712 | for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { | 
|  | 6713 | if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) { | 
|  | 6714 | devices.add(itemAt(i)); | 
|  | 6715 | type &= ~itemAt(i)->mDeviceType; | 
|  | 6716 | ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", | 
|  | 6717 | itemAt(i)->mDeviceType, itemAt(i).get()); | 
|  | 6718 | } | 
|  | 6719 | } | 
|  | 6720 | return devices; | 
|  | 6721 | } | 
|  | 6722 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6723 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromTypeAddr( | 
|  | 6724 | audio_devices_t type, String8 address) const | 
|  | 6725 | { | 
|  | 6726 | DeviceVector devices; | 
|  | 6727 | //ALOGV("   looking for device=%x, addr=%s", type, address.string()); | 
|  | 6728 | for (size_t i = 0; i < size(); i++) { | 
|  | 6729 | //ALOGV("     at i=%d: device=%x, addr=%s", | 
|  | 6730 | //        i, itemAt(i)->mDeviceType, itemAt(i)->mAddress.string()); | 
|  | 6731 | if (itemAt(i)->mDeviceType == type) { | 
|  | 6732 | if (itemAt(i)->mAddress == address) { | 
|  | 6733 | //ALOGV("      found matching address %s", address.string()); | 
|  | 6734 | devices.add(itemAt(i)); | 
|  | 6735 | } | 
|  | 6736 | } | 
|  | 6737 | } | 
|  | 6738 | return devices; | 
|  | 6739 | } | 
|  | 6740 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6741 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName( | 
|  | 6742 | const String8& name) const | 
|  | 6743 | { | 
|  | 6744 | sp<DeviceDescriptor> device; | 
|  | 6745 | for (size_t i = 0; i < size(); i++) { | 
|  | 6746 | if (itemAt(i)->mName == name) { | 
|  | 6747 | device = itemAt(i); | 
|  | 6748 | break; | 
|  | 6749 | } | 
|  | 6750 | } | 
|  | 6751 | return device; | 
|  | 6752 | } | 
|  | 6753 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6754 | void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig( | 
|  | 6755 | struct audio_port_config *dstConfig, | 
|  | 6756 | const struct audio_port_config *srcConfig) const | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6757 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6758 | dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; | 
|  | 6759 | if (srcConfig != NULL) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 6760 | dstConfig->config_mask |= srcConfig->config_mask; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6761 | } | 
|  | 6762 |  | 
|  | 6763 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); | 
|  | 6764 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6765 | dstConfig->id = mId; | 
|  | 6766 | dstConfig->role = audio_is_output_device(mDeviceType) ? | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6767 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6768 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6769 | dstConfig->ext.device.type = mDeviceType; | 
|  | 6770 | dstConfig->ext.device.hw_module = mModule->mHandle; | 
|  | 6771 | strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6772 | } | 
|  | 6773 |  | 
|  | 6774 | void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const | 
|  | 6775 | { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6776 | ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6777 | AudioPort::toAudioPort(port); | 
|  | 6778 | port->id = mId; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6779 | toAudioPortConfig(&port->active_config); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6780 | port->ext.device.type = mDeviceType; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6781 | port->ext.device.hw_module = mModule->mHandle; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6782 | strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); | 
|  | 6783 | } | 
|  | 6784 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6785 | status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6786 | { | 
|  | 6787 | const size_t SIZE = 256; | 
|  | 6788 | char buffer[SIZE]; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6789 | String8 result; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6790 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6791 | snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); | 
|  | 6792 | result.append(buffer); | 
|  | 6793 | if (mId != 0) { | 
|  | 6794 | snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); | 
|  | 6795 | result.append(buffer); | 
|  | 6796 | } | 
|  | 6797 | snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", | 
|  | 6798 | enumToString(sDeviceNameToEnumTable, | 
|  | 6799 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6800 | mDeviceType)); | 
|  | 6801 | result.append(buffer); | 
|  | 6802 | if (mAddress.size() != 0) { | 
|  | 6803 | snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); | 
|  | 6804 | result.append(buffer); | 
|  | 6805 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6806 | write(fd, result.string(), result.size()); | 
|  | 6807 | AudioPort::dump(fd, spaces); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6808 |  | 
|  | 6809 | return NO_ERROR; | 
|  | 6810 | } | 
|  | 6811 |  | 
| Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 6812 | status_t AudioPolicyManager::AudioPatch::dump(int fd, int spaces, int index) const | 
|  | 6813 | { | 
|  | 6814 | const size_t SIZE = 256; | 
|  | 6815 | char buffer[SIZE]; | 
|  | 6816 | String8 result; | 
|  | 6817 |  | 
|  | 6818 |  | 
|  | 6819 | snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1); | 
|  | 6820 | result.append(buffer); | 
|  | 6821 | snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle); | 
|  | 6822 | result.append(buffer); | 
|  | 6823 | snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle); | 
|  | 6824 | result.append(buffer); | 
|  | 6825 | snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid); | 
|  | 6826 | result.append(buffer); | 
|  | 6827 | snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources); | 
|  | 6828 | result.append(buffer); | 
|  | 6829 | for (size_t i = 0; i < mPatch.num_sources; i++) { | 
|  | 6830 | if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 6831 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", | 
|  | 6832 | mPatch.sources[i].id, enumToString(sDeviceNameToEnumTable, | 
|  | 6833 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6834 | mPatch.sources[i].ext.device.type)); | 
|  | 6835 | } else { | 
|  | 6836 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", | 
|  | 6837 | mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle); | 
|  | 6838 | } | 
|  | 6839 | result.append(buffer); | 
|  | 6840 | } | 
|  | 6841 | snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks); | 
|  | 6842 | result.append(buffer); | 
|  | 6843 | for (size_t i = 0; i < mPatch.num_sinks; i++) { | 
|  | 6844 | if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 6845 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", | 
|  | 6846 | mPatch.sinks[i].id, enumToString(sDeviceNameToEnumTable, | 
|  | 6847 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6848 | mPatch.sinks[i].ext.device.type)); | 
|  | 6849 | } else { | 
|  | 6850 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", | 
|  | 6851 | mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle); | 
|  | 6852 | } | 
|  | 6853 | result.append(buffer); | 
|  | 6854 | } | 
|  | 6855 |  | 
|  | 6856 | write(fd, result.string(), result.size()); | 
|  | 6857 | return NO_ERROR; | 
|  | 6858 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6859 |  | 
|  | 6860 | // --- audio_policy.conf file parsing | 
|  | 6861 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6862 | audio_output_flags_t AudioPolicyManager::parseFlagNames(char *name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6863 | { | 
|  | 6864 | uint32_t flag = 0; | 
|  | 6865 |  | 
|  | 6866 | // it is OK to cast name to non const here as we are not going to use it after | 
|  | 6867 | // strtok() modifies it | 
|  | 6868 | char *flagName = strtok(name, "|"); | 
|  | 6869 | while (flagName != NULL) { | 
|  | 6870 | if (strlen(flagName) != 0) { | 
|  | 6871 | flag |= stringToEnum(sFlagNameToEnumTable, | 
|  | 6872 | ARRAY_SIZE(sFlagNameToEnumTable), | 
|  | 6873 | flagName); | 
|  | 6874 | } | 
|  | 6875 | flagName = strtok(NULL, "|"); | 
|  | 6876 | } | 
|  | 6877 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 6878 | // and all common behaviors are driven by checking only the direct flag | 
|  | 6879 | // this should normally be set appropriately in the policy configuration file | 
|  | 6880 | if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 6881 | flag |= AUDIO_OUTPUT_FLAG_DIRECT; | 
|  | 6882 | } | 
|  | 6883 |  | 
|  | 6884 | return (audio_output_flags_t)flag; | 
|  | 6885 | } | 
|  | 6886 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6887 | audio_devices_t AudioPolicyManager::parseDeviceNames(char *name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6888 | { | 
|  | 6889 | uint32_t device = 0; | 
|  | 6890 |  | 
|  | 6891 | char *devName = strtok(name, "|"); | 
|  | 6892 | while (devName != NULL) { | 
|  | 6893 | if (strlen(devName) != 0) { | 
|  | 6894 | device |= stringToEnum(sDeviceNameToEnumTable, | 
|  | 6895 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6896 | devName); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6897 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6898 | devName = strtok(NULL, "|"); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6899 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6900 | return device; | 
|  | 6901 | } | 
|  | 6902 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6903 | void AudioPolicyManager::loadHwModule(cnode *root) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6904 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6905 | status_t status = NAME_NOT_FOUND; | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6906 | cnode *node; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6907 | sp<HwModule> module = new HwModule(root->name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6908 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6909 | node = config_find(root, DEVICES_TAG); | 
|  | 6910 | if (node != NULL) { | 
|  | 6911 | node = node->first_child; | 
|  | 6912 | while (node) { | 
|  | 6913 | ALOGV("loadHwModule() loading device %s", node->name); | 
|  | 6914 | status_t tmpStatus = module->loadDevice(node); | 
|  | 6915 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6916 | status = tmpStatus; | 
|  | 6917 | } | 
|  | 6918 | node = node->next; | 
|  | 6919 | } | 
|  | 6920 | } | 
|  | 6921 | node = config_find(root, OUTPUTS_TAG); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6922 | if (node != NULL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6923 | node = node->first_child; | 
|  | 6924 | while (node) { | 
|  | 6925 | ALOGV("loadHwModule() loading output %s", node->name); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6926 | status_t tmpStatus = module->loadOutput(node); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6927 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6928 | status = tmpStatus; | 
|  | 6929 | } | 
|  | 6930 | node = node->next; | 
|  | 6931 | } | 
|  | 6932 | } | 
|  | 6933 | node = config_find(root, INPUTS_TAG); | 
|  | 6934 | if (node != NULL) { | 
|  | 6935 | node = node->first_child; | 
|  | 6936 | while (node) { | 
|  | 6937 | ALOGV("loadHwModule() loading input %s", node->name); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6938 | status_t tmpStatus = module->loadInput(node); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6939 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { | 
|  | 6940 | status = tmpStatus; | 
|  | 6941 | } | 
|  | 6942 | node = node->next; | 
|  | 6943 | } | 
|  | 6944 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6945 | loadGlobalConfig(root, module); | 
|  | 6946 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6947 | if (status == NO_ERROR) { | 
|  | 6948 | mHwModules.add(module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6949 | } | 
|  | 6950 | } | 
|  | 6951 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6952 | void AudioPolicyManager::loadHwModules(cnode *root) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6953 | { | 
|  | 6954 | cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); | 
|  | 6955 | if (node == NULL) { | 
|  | 6956 | return; | 
|  | 6957 | } | 
|  | 6958 |  | 
|  | 6959 | node = node->first_child; | 
|  | 6960 | while (node) { | 
|  | 6961 | ALOGV("loadHwModules() loading module %s", node->name); | 
|  | 6962 | loadHwModule(node); | 
|  | 6963 | node = node->next; | 
|  | 6964 | } | 
|  | 6965 | } | 
|  | 6966 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6967 | void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6968 | { | 
|  | 6969 | cnode *node = config_find(root, GLOBAL_CONFIG_TAG); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 6970 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6971 | if (node == NULL) { | 
|  | 6972 | return; | 
|  | 6973 | } | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6974 | DeviceVector declaredDevices; | 
|  | 6975 | if (module != NULL) { | 
|  | 6976 | declaredDevices = module->mDeclaredDevices; | 
|  | 6977 | } | 
|  | 6978 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6979 | node = node->first_child; | 
|  | 6980 | while (node) { | 
|  | 6981 | if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6982 | mAvailableOutputDevices.loadDevicesFromName((char *)node->value, | 
|  | 6983 | declaredDevices); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6984 | ALOGV("loadGlobalConfig() Attached Output Devices %08x", | 
|  | 6985 | mAvailableOutputDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6986 | } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6987 | audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6988 | ARRAY_SIZE(sDeviceNameToEnumTable), | 
|  | 6989 | (char *)node->value); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6990 | if (device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6991 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), device); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6992 | } else { | 
|  | 6993 | ALOGW("loadGlobalConfig() default device not specified"); | 
|  | 6994 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6995 | ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6996 | } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6997 | mAvailableInputDevices.loadDevicesFromName((char *)node->value, | 
|  | 6998 | declaredDevices); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6999 | ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7000 | } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) { | 
|  | 7001 | mSpeakerDrcEnabled = stringToBool((char *)node->value); | 
|  | 7002 | ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled); | 
| Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 7003 | } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) { | 
|  | 7004 | uint32_t major, minor; | 
|  | 7005 | sscanf((char *)node->value, "%u.%u", &major, &minor); | 
|  | 7006 | module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor); | 
|  | 7007 | ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u", | 
|  | 7008 | module->mHalVersion, major, minor); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7009 | } | 
|  | 7010 | node = node->next; | 
|  | 7011 | } | 
|  | 7012 | } | 
|  | 7013 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7014 | status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7015 | { | 
|  | 7016 | cnode *root; | 
|  | 7017 | char *data; | 
|  | 7018 |  | 
|  | 7019 | data = (char *)load_file(path, NULL); | 
|  | 7020 | if (data == NULL) { | 
|  | 7021 | return -ENODEV; | 
|  | 7022 | } | 
|  | 7023 | root = config_node("", ""); | 
|  | 7024 | config_load(root, data); | 
|  | 7025 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7026 | loadHwModules(root); | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7027 | // legacy audio_policy.conf files have one global_configuration section | 
|  | 7028 | loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7029 | config_free(root); | 
|  | 7030 | free(root); | 
|  | 7031 | free(data); | 
|  | 7032 |  | 
|  | 7033 | ALOGI("loadAudioPolicyConfig() loaded %s\n", path); | 
|  | 7034 |  | 
|  | 7035 | return NO_ERROR; | 
|  | 7036 | } | 
|  | 7037 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7038 | void AudioPolicyManager::defaultAudioPolicyConfig(void) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7039 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7040 | sp<HwModule> module; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7041 | sp<IOProfile> profile; | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7042 | sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""), | 
|  | 7043 | AUDIO_DEVICE_IN_BUILTIN_MIC); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7044 | mAvailableOutputDevices.add(mDefaultOutputDevice); | 
|  | 7045 | mAvailableInputDevices.add(defaultInputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7046 |  | 
|  | 7047 | module = new HwModule("primary"); | 
|  | 7048 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7049 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7050 | profile->mSamplingRates.add(44100); | 
|  | 7051 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); | 
|  | 7052 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7053 | profile->mSupportedDevices.add(mDefaultOutputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7054 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; | 
|  | 7055 | module->mOutputProfiles.add(profile); | 
|  | 7056 |  | 
| Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7057 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7058 | profile->mSamplingRates.add(8000); | 
|  | 7059 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); | 
|  | 7060 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7061 | profile->mSupportedDevices.add(defaultInputDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7062 | module->mInputProfiles.add(profile); | 
|  | 7063 |  | 
|  | 7064 | mHwModules.add(module); | 
|  | 7065 | } | 
|  | 7066 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7067 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 7068 | { | 
|  | 7069 | // flags to stream type mapping | 
|  | 7070 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 7071 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 7072 | } | 
|  | 7073 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 7074 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 7075 | } | 
|  | 7076 |  | 
|  | 7077 | // usage to stream type mapping | 
|  | 7078 | switch (attr->usage) { | 
|  | 7079 | case AUDIO_USAGE_MEDIA: | 
|  | 7080 | case AUDIO_USAGE_GAME: | 
|  | 7081 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 7082 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 7083 | return AUDIO_STREAM_MUSIC; | 
|  | 7084 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 7085 | return AUDIO_STREAM_SYSTEM; | 
|  | 7086 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 7087 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 7088 |  | 
|  | 7089 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 7090 | return AUDIO_STREAM_DTMF; | 
|  | 7091 |  | 
|  | 7092 | case AUDIO_USAGE_ALARM: | 
|  | 7093 | return AUDIO_STREAM_ALARM; | 
|  | 7094 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 7095 | return AUDIO_STREAM_RING; | 
|  | 7096 |  | 
|  | 7097 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 7098 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 7099 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 7100 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 7101 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 7102 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 7103 |  | 
|  | 7104 | case AUDIO_USAGE_UNKNOWN: | 
|  | 7105 | default: | 
|  | 7106 | return AUDIO_STREAM_MUSIC; | 
|  | 7107 | } | 
|  | 7108 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7109 | }; // namespace android |