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() |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 29 | #define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_FM_TUNER) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 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), |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 67 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER_SAFE), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 68 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET), |
| 69 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), |
| 70 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO), |
| 71 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET), |
| 72 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT), |
| 73 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO), |
| 74 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP), |
| 75 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES), |
| 76 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), |
| 77 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP), |
| 78 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 79 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 80 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET), |
| 81 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET), |
| 82 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY), |
| 83 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE), |
| 84 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB), |
| 85 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 86 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX), |
| 87 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE), |
| 88 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC), |
| 89 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF), |
| 90 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM), |
Eric Laurent | e1d37b7 | 2014-07-29 10:26:26 -0700 | [diff] [blame] | 91 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_LINE), |
Eric Laurent | a57ab8d | 2014-07-30 10:01:42 -0500 | [diff] [blame] | 92 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AMBIENT), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 93 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC), |
| 94 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET), |
| 95 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO), |
| 96 | STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET), |
| 97 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 98 | STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 99 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX), |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 100 | STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 101 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC), |
| 102 | STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX), |
| 103 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET), |
| 104 | STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET), |
| 105 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY), |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 106 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 107 | STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER), |
| 108 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER), |
| 109 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE), |
| 110 | STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF), |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 111 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP), |
Terry Heo | 7999a22 | 2014-06-27 15:23:36 +0900 | [diff] [blame] | 112 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 115 | const StringToEnum sOutputFlagNameToEnumTable[] = { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 116 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), |
| 117 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), |
| 118 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), |
| 119 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), |
| 120 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), |
| 121 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 122 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 125 | const StringToEnum sInputFlagNameToEnumTable[] = { |
| 126 | STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST), |
| 127 | STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD), |
| 128 | }; |
| 129 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 130 | const StringToEnum sFormatNameToEnumTable[] = { |
| 131 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), |
| 132 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), |
| 133 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT), |
| 134 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT), |
| 135 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT), |
| 136 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED), |
| 137 | STRING_TO_ENUM(AUDIO_FORMAT_MP3), |
| 138 | STRING_TO_ENUM(AUDIO_FORMAT_AAC), |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 139 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN), |
| 140 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC), |
| 141 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR), |
| 142 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP), |
| 143 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1), |
| 144 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE), |
| 145 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC), |
| 146 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD), |
| 147 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2), |
| 148 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 150 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1), |
| 151 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2), |
| 152 | STRING_TO_ENUM(AUDIO_FORMAT_OPUS), |
| 153 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), |
| 154 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | const StringToEnum sOutChannelsNameToEnumTable[] = { |
| 158 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO), |
| 159 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
Andy Hung | 3a0fe12 | 2014-07-29 17:56:46 -0700 | [diff] [blame] | 160 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 161 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 162 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 163 | }; |
| 164 | |
| 165 | const StringToEnum sInChannelsNameToEnumTable[] = { |
| 166 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), |
| 167 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), |
| 168 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), |
| 169 | }; |
| 170 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 171 | const StringToEnum sGainModeNameToEnumTable[] = { |
| 172 | STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT), |
| 173 | STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS), |
| 174 | STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP), |
| 175 | }; |
| 176 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 177 | |
| 178 | uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table, |
| 179 | size_t size, |
| 180 | const char *name) |
| 181 | { |
| 182 | for (size_t i = 0; i < size; i++) { |
| 183 | if (strcmp(table[i].name, name) == 0) { |
| 184 | ALOGV("stringToEnum() found %s", table[i].name); |
| 185 | return table[i].value; |
| 186 | } |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | const char *AudioPolicyManager::enumToString(const struct StringToEnum *table, |
| 192 | size_t size, |
| 193 | uint32_t value) |
| 194 | { |
| 195 | for (size_t i = 0; i < size; i++) { |
| 196 | if (table[i].value == value) { |
| 197 | return table[i].name; |
| 198 | } |
| 199 | } |
| 200 | return ""; |
| 201 | } |
| 202 | |
| 203 | bool AudioPolicyManager::stringToBool(const char *value) |
| 204 | { |
| 205 | return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0)); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | // ---------------------------------------------------------------------------- |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | // AudioPolicyInterface implementation |
| 211 | // ---------------------------------------------------------------------------- |
| 212 | |
| 213 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 214 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 215 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 216 | const char *device_address) |
| 217 | { |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 218 | String8 address = (device_address == NULL) ? String8("") : String8(device_address); |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 219 | // handle legacy remote submix case where the address was not always specified |
| 220 | if (deviceDistinguishesOnAddress(device) && (address.length() == 0)) { |
| 221 | address = String8("0"); |
| 222 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 224 | ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", |
| 225 | device, state, address.string()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 226 | |
| 227 | // connect/disconnect only 1 device at a time |
| 228 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 229 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 230 | // handle output devices |
| 231 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 232 | SortedVector <audio_io_handle_t> outputs; |
| 233 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 234 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
| 235 | devDesc->mAddress = address; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 236 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 237 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | // save a copy of the opened output descriptors before any output is opened or closed |
| 239 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 240 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 241 | switch (state) |
| 242 | { |
| 243 | // handle output device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 244 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 245 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 246 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 247 | return INVALID_OPERATION; |
| 248 | } |
| 249 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 250 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 251 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 252 | index = mAvailableOutputDevices.add(devDesc); |
| 253 | if (index >= 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 254 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 255 | if (module == 0) { |
| 256 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 257 | device); |
| 258 | mAvailableOutputDevices.remove(devDesc); |
| 259 | return INVALID_OPERATION; |
| 260 | } |
| 261 | mAvailableOutputDevices[index]->mId = nextUniqueId(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 262 | mAvailableOutputDevices[index]->mModule = module; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 263 | } else { |
| 264 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 267 | if (checkOutputsForDevice(devDesc, state, outputs, address) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 268 | mAvailableOutputDevices.remove(devDesc); |
| 269 | return INVALID_OPERATION; |
| 270 | } |
| 271 | // outputs should never be empty here |
| 272 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 273 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 274 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 275 | outputs.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 276 | break; |
| 277 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 278 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 279 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 281 | return INVALID_OPERATION; |
| 282 | } |
| 283 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 284 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 285 | |
| 286 | // Set Disconnect to HALs |
| 287 | AudioParameter param = AudioParameter(address); |
| 288 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 289 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 290 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 291 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 292 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 293 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 294 | checkOutputsForDevice(devDesc, state, outputs, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 295 | } break; |
| 296 | |
| 297 | default: |
| 298 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 299 | return BAD_VALUE; |
| 300 | } |
| 301 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 302 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 303 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 304 | checkA2dpSuspend(); |
| 305 | checkOutputForAllStrategies(); |
| 306 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 307 | if (!outputs.isEmpty()) { |
| 308 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 309 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 | // close unused outputs after device disconnection or direct outputs that have been |
| 311 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 312 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 313 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 314 | (desc->mDirectOpenCount == 0))) { |
| 315 | closeOutput(outputs[i]); |
| 316 | } |
| 317 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 318 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 319 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 323 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 324 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 325 | updateCallRouting(newDevice); |
| 326 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 327 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | audio_io_handle_t output = mOutputs.keyAt(i); |
| 329 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 330 | audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i), |
| 331 | true /*fromCache*/); |
| 332 | // do not force device change on duplicated output because if device is 0, it will |
| 333 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 334 | // a valid device selection on those outputs. |
| 335 | bool force = !mOutputs.valueAt(i)->isDuplicated() |
| 336 | && (!deviceDistinguishesOnAddress(device) |
| 337 | // always force when disconnecting (a non-duplicated device) |
| 338 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
| 339 | setOutputDevice(output, newDevice, force, 0); |
| 340 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 343 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 344 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 345 | } // end if is output device |
| 346 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 347 | // handle input devices |
| 348 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 349 | SortedVector <audio_io_handle_t> inputs; |
| 350 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 351 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
| 352 | devDesc->mAddress = address; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 353 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 354 | switch (state) |
| 355 | { |
| 356 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 357 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 358 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 359 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 360 | return INVALID_OPERATION; |
| 361 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 362 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 363 | if (module == NULL) { |
| 364 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 365 | device); |
| 366 | return INVALID_OPERATION; |
| 367 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 368 | if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) { |
| 369 | return INVALID_OPERATION; |
| 370 | } |
| 371 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 372 | index = mAvailableInputDevices.add(devDesc); |
| 373 | if (index >= 0) { |
| 374 | mAvailableInputDevices[index]->mId = nextUniqueId(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 375 | mAvailableInputDevices[index]->mModule = module; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 376 | } else { |
| 377 | return NO_MEMORY; |
| 378 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 379 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 380 | |
| 381 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 382 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 383 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 384 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 385 | return INVALID_OPERATION; |
| 386 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 387 | |
| 388 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 389 | |
| 390 | // Set Disconnect to HALs |
| 391 | AudioParameter param = AudioParameter(address); |
| 392 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 393 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 394 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 395 | checkInputsForDevice(device, state, inputs, address); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 396 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 397 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 398 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 399 | |
| 400 | default: |
| 401 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 402 | return BAD_VALUE; |
| 403 | } |
| 404 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 405 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 406 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 407 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 408 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 409 | updateCallRouting(newDevice); |
| 410 | } |
| 411 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 412 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 413 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 414 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 415 | |
| 416 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 417 | return BAD_VALUE; |
| 418 | } |
| 419 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 420 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 421 | const char *device_address) |
| 422 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 423 | audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 424 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 425 | devDesc->mAddress = (device_address == NULL) ? String8("") : String8(device_address); |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 426 | // handle legacy remote submix case where the address was not always specified |
| 427 | if (deviceDistinguishesOnAddress(device) && (devDesc->mAddress.length() == 0)) { |
| 428 | devDesc->mAddress = String8("0"); |
| 429 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 430 | ssize_t index; |
| 431 | DeviceVector *deviceVector; |
| 432 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 433 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 434 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 435 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 436 | deviceVector = &mAvailableInputDevices; |
| 437 | } else { |
| 438 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 439 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 442 | index = deviceVector->indexOf(devDesc); |
| 443 | if (index >= 0) { |
| 444 | return AUDIO_POLICY_DEVICE_STATE_AVAILABLE; |
| 445 | } else { |
| 446 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 447 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 450 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 451 | { |
| 452 | bool createTxPatch = false; |
| 453 | struct audio_patch patch; |
| 454 | patch.num_sources = 1; |
| 455 | patch.num_sinks = 1; |
| 456 | status_t status; |
| 457 | audio_patch_handle_t afPatchHandle; |
| 458 | DeviceVector deviceList; |
| 459 | |
| 460 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 461 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 462 | |
| 463 | // release existing RX patch if any |
| 464 | if (mCallRxPatch != 0) { |
| 465 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 466 | mCallRxPatch.clear(); |
| 467 | } |
| 468 | // release TX patch if any |
| 469 | if (mCallTxPatch != 0) { |
| 470 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 471 | mCallTxPatch.clear(); |
| 472 | } |
| 473 | |
| 474 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 475 | // via setOutputDevice() on primary output. |
| 476 | // Otherwise, create two audio patches for TX and RX path. |
| 477 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 478 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 479 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 480 | // of it due to legacy implementation. If not, create a patch. |
| 481 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 482 | == AUDIO_DEVICE_NONE) { |
| 483 | createTxPatch = true; |
| 484 | } |
| 485 | } else { |
| 486 | // create RX path audio patch |
| 487 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 488 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 489 | "updateCallRouting() selected device not in output device list"); |
| 490 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 491 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 492 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 493 | "updateCallRouting() no telephony RX device"); |
| 494 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 495 | |
| 496 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 497 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 498 | |
| 499 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 500 | SortedVector<audio_io_handle_t> outputs = |
| 501 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 502 | audio_io_handle_t output = selectOutput(outputs, |
| 503 | AUDIO_OUTPUT_FLAG_NONE, |
| 504 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 505 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 506 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 507 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 508 | "updateCallRouting() RX device output is duplicated"); |
| 509 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 510 | patch.num_sources = 2; |
| 511 | } |
| 512 | |
| 513 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 514 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 515 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 516 | status); |
| 517 | if (status == NO_ERROR) { |
| 518 | mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 519 | &patch, mUidCached); |
| 520 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 521 | mCallRxPatch->mUid = mUidCached; |
| 522 | } |
| 523 | createTxPatch = true; |
| 524 | } |
| 525 | if (createTxPatch) { |
| 526 | |
| 527 | struct audio_patch patch; |
| 528 | patch.num_sources = 1; |
| 529 | patch.num_sinks = 1; |
| 530 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 531 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 532 | "updateCallRouting() selected device not in input device list"); |
| 533 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 534 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 535 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 536 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 537 | "updateCallRouting() no telephony TX device"); |
| 538 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 539 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 540 | |
| 541 | SortedVector<audio_io_handle_t> outputs = |
| 542 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 543 | audio_io_handle_t output = selectOutput(outputs, |
| 544 | AUDIO_OUTPUT_FLAG_NONE, |
| 545 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 546 | // request to reuse existing output stream if one is already opened to reach the TX |
| 547 | // path output device |
| 548 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 549 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 550 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 551 | "updateCallRouting() RX device output is duplicated"); |
| 552 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 553 | patch.num_sources = 2; |
| 554 | } |
| 555 | |
| 556 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 557 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 558 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 559 | status); |
| 560 | if (status == NO_ERROR) { |
| 561 | mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 562 | &patch, mUidCached); |
| 563 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 564 | mCallTxPatch->mUid = mUidCached; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 569 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | { |
| 571 | ALOGV("setPhoneState() state %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 572 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | ALOGW("setPhoneState() invalid state %d", state); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | if (state == mPhoneState ) { |
| 578 | ALOGW("setPhoneState() setting same state %d", state); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | // if leaving call state, handle special case of active streams |
| 583 | // pertaining to sonification strategy see handleIncallSonification() |
| 584 | if (isInCall()) { |
| 585 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 586 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 587 | if (stream == AUDIO_STREAM_PATCH) { |
| 588 | continue; |
| 589 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 590 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | // store previous phone state for management of sonification strategy below |
| 595 | int oldState = mPhoneState; |
| 596 | mPhoneState = state; |
| 597 | bool force = false; |
| 598 | |
| 599 | // are we entering or starting a call |
| 600 | if (!isStateInCall(oldState) && isStateInCall(state)) { |
| 601 | ALOGV(" Entering call in setPhoneState()"); |
| 602 | // force routing command to audio hardware when starting a call |
| 603 | // even if no device change is needed |
| 604 | force = true; |
| 605 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 606 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
| 607 | sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; |
| 608 | } |
| 609 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { |
| 610 | ALOGV(" Exiting call in setPhoneState()"); |
| 611 | // force routing command to audio hardware when exiting a call |
| 612 | // even if no device change is needed |
| 613 | force = true; |
| 614 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 615 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
| 616 | sVolumeProfiles[AUDIO_STREAM_DTMF][j]; |
| 617 | } |
| 618 | } else if (isStateInCall(state) && (state != oldState)) { |
| 619 | ALOGV(" Switching between telephony and VoIP in setPhoneState()"); |
| 620 | // force routing command to audio hardware when switching between telephony and VoIP |
| 621 | // even if no device change is needed |
| 622 | force = true; |
| 623 | } |
| 624 | |
| 625 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | checkA2dpSuspend(); |
| 627 | checkOutputForAllStrategies(); |
| 628 | updateDevicesAndOutputs(); |
| 629 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 630 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 631 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | int delayMs = 0; |
| 633 | if (isStateInCall(state)) { |
| 634 | nsecs_t sysTime = systemTime(); |
| 635 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 636 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | // mute media and sonification strategies and delay device switch by the largest |
| 638 | // latency of any output where either strategy is active. |
| 639 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
| 640 | if ((desc->isStrategyActive(STRATEGY_MEDIA, |
| 641 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 642 | sysTime) || |
| 643 | desc->isStrategyActive(STRATEGY_SONIFICATION, |
| 644 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 645 | sysTime)) && |
| 646 | (delayMs < (int)desc->mLatency*2)) { |
| 647 | delayMs = desc->mLatency*2; |
| 648 | } |
| 649 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); |
| 650 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 651 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
| 652 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); |
| 653 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 654 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 655 | } |
| 656 | } |
| 657 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 658 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 659 | // the device returned is not necessarily reachable via this output |
| 660 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 661 | // force routing command to audio hardware when ending call |
| 662 | // even if no device change is needed |
| 663 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 664 | rxDevice = hwOutputDesc->device(); |
| 665 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 667 | if (state == AUDIO_MODE_IN_CALL) { |
| 668 | updateCallRouting(rxDevice, delayMs); |
| 669 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 670 | if (mCallRxPatch != 0) { |
| 671 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 672 | mCallRxPatch.clear(); |
| 673 | } |
| 674 | if (mCallTxPatch != 0) { |
| 675 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 676 | mCallTxPatch.clear(); |
| 677 | } |
| 678 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 679 | } else { |
| 680 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 681 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 682 | // if entering in call state, handle special case of active streams |
| 683 | // pertaining to sonification strategy see handleIncallSonification() |
| 684 | if (isStateInCall(state)) { |
| 685 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 686 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 687 | if (stream == AUDIO_STREAM_PATCH) { |
| 688 | continue; |
| 689 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 690 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| 694 | // 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] | 695 | if (state == AUDIO_MODE_RINGTONE && |
| 696 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 697 | mLimitRingtoneVolume = true; |
| 698 | } else { |
| 699 | mLimitRingtoneVolume = false; |
| 700 | } |
| 701 | } |
| 702 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 703 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 704 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 705 | { |
| 706 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 707 | |
| 708 | bool forceVolumeReeval = false; |
| 709 | switch(usage) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 710 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 711 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 712 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 713 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 714 | return; |
| 715 | } |
| 716 | forceVolumeReeval = true; |
| 717 | mForceUse[usage] = config; |
| 718 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 719 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 720 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 721 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 722 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 723 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 724 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 725 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 726 | return; |
| 727 | } |
| 728 | mForceUse[usage] = config; |
| 729 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 730 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 731 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 732 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 733 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 734 | return; |
| 735 | } |
| 736 | mForceUse[usage] = config; |
| 737 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 738 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 739 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 740 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 741 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 742 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 743 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 744 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 745 | } |
| 746 | forceVolumeReeval = true; |
| 747 | mForceUse[usage] = config; |
| 748 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 749 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 750 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 751 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 752 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 753 | } |
| 754 | forceVolumeReeval = true; |
| 755 | mForceUse[usage] = config; |
| 756 | break; |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 757 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 758 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 759 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
| 760 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); |
| 761 | } |
| 762 | mForceUse[usage] = config; |
| 763 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 764 | default: |
| 765 | ALOGW("setForceUse() invalid usage %d", usage); |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | // check for device and output changes triggered by new force usage |
| 770 | checkA2dpSuspend(); |
| 771 | checkOutputForAllStrategies(); |
| 772 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 773 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 774 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 775 | updateCallRouting(newDevice); |
| 776 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 777 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 778 | audio_io_handle_t output = mOutputs.keyAt(i); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 779 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 780 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 781 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
| 782 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 783 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
| 784 | applyStreamVolumes(output, newDevice, 0, true); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | audio_io_handle_t activeInput = getActiveInput(); |
| 789 | if (activeInput != 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 790 | setInputDevice(activeInput, getNewInputDevice(activeInput)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | } |
| 794 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 795 | 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] | 796 | { |
| 797 | return mForceUse[usage]; |
| 798 | } |
| 799 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 800 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 801 | { |
| 802 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 803 | } |
| 804 | |
| 805 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 806 | // not explicitly request a direct output |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 807 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 808 | audio_devices_t device, |
| 809 | uint32_t samplingRate, |
| 810 | audio_format_t format, |
| 811 | audio_channel_mask_t channelMask, |
| 812 | audio_output_flags_t flags) |
| 813 | { |
| 814 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 815 | if (mHwModules[i]->mHandle == 0) { |
| 816 | continue; |
| 817 | } |
| 818 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 819 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 820 | bool found = profile->isCompatibleProfile(device, samplingRate, |
| 821 | NULL /*updatedSamplingRate*/, format, channelMask, |
| 822 | flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ? |
| 823 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 824 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { |
| 825 | return profile; |
| 826 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 832 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 833 | uint32_t samplingRate, |
| 834 | audio_format_t format, |
| 835 | audio_channel_mask_t channelMask, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 836 | audio_output_flags_t flags, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 837 | const audio_offload_info_t *offloadInfo) |
| 838 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 839 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 840 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 841 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 842 | device, stream, samplingRate, format, channelMask, flags); |
| 843 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 844 | return getOutputForDevice(device, stream, samplingRate,format, channelMask, flags, |
| 845 | offloadInfo); |
| 846 | } |
| 847 | |
| 848 | audio_io_handle_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 849 | uint32_t samplingRate, |
| 850 | audio_format_t format, |
| 851 | audio_channel_mask_t channelMask, |
| 852 | audio_output_flags_t flags, |
| 853 | const audio_offload_info_t *offloadInfo) |
| 854 | { |
| 855 | if (attr == NULL) { |
| 856 | ALOGE("getOutputForAttr() called with NULL audio attributes"); |
| 857 | return 0; |
| 858 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 859 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x", |
| 860 | attr->usage, attr->content_type, attr->tags, attr->flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 861 | |
| 862 | // TODO this is where filtering for custom policies (rerouting, dynamic sources) will go |
| 863 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(attr); |
| 864 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 865 | |
| 866 | if ((attr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
| 867 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 868 | } |
| 869 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 870 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 871 | device, samplingRate, format, channelMask, flags); |
| 872 | |
| 873 | audio_stream_type_t stream = streamTypefromAttributesInt(attr); |
| 874 | return getOutputForDevice(device, stream, samplingRate, format, channelMask, flags, |
| 875 | offloadInfo); |
| 876 | } |
| 877 | |
| 878 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 879 | audio_devices_t device, |
| 880 | audio_stream_type_t stream, |
| 881 | uint32_t samplingRate, |
| 882 | audio_format_t format, |
| 883 | audio_channel_mask_t channelMask, |
| 884 | audio_output_flags_t flags, |
| 885 | const audio_offload_info_t *offloadInfo) |
| 886 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 887 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 888 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 889 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 890 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 891 | #ifdef AUDIO_POLICY_TEST |
| 892 | if (mCurOutput != 0) { |
| 893 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 894 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 895 | |
| 896 | if (mTestOutputs[mCurOutput] == 0) { |
| 897 | ALOGV("getOutput() opening test output"); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 898 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 899 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 900 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 901 | outputDesc->mFlags = |
| 902 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 903 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 904 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 905 | config.sample_rate = mTestSamplingRate; |
| 906 | config.channel_mask = mTestChannels; |
| 907 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 908 | if (offloadInfo != NULL) { |
| 909 | config.offload_info = *offloadInfo; |
| 910 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 911 | status = mpClientInterface->openOutput(0, |
| 912 | &mTestOutputs[mCurOutput], |
| 913 | &config, |
| 914 | &outputDesc->mDevice, |
| 915 | String8(""), |
| 916 | &outputDesc->mLatency, |
| 917 | outputDesc->mFlags); |
| 918 | if (status == NO_ERROR) { |
| 919 | outputDesc->mSamplingRate = config.sample_rate; |
| 920 | outputDesc->mFormat = config.format; |
| 921 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 922 | AudioParameter outputCmd = AudioParameter(); |
| 923 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 924 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 925 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 926 | } |
| 927 | } |
| 928 | return mTestOutputs[mCurOutput]; |
| 929 | } |
| 930 | #endif //AUDIO_POLICY_TEST |
| 931 | |
| 932 | // open a direct output if required by specified parameters |
| 933 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 934 | // and all common behaviors are driven by checking only the direct flag |
| 935 | // this should normally be set appropriately in the policy configuration file |
| 936 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 937 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 938 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 939 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 940 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 941 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 942 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 943 | sp<IOProfile> profile; |
| 944 | |
| 945 | // skip direct output selection if the request can obviously be attached to a mixed output |
Eric Laurent | c260784 | 2014-09-29 09:43:03 -0700 | [diff] [blame] | 946 | // and not explicitly requested |
| 947 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 948 | audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 949 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 950 | goto non_direct_output; |
| 951 | } |
| 952 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 953 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 954 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 955 | // detects there is an active non offloadable effect. |
| 956 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 957 | // This may prevent offloading in rare situations where effects are left active by apps |
| 958 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 959 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 960 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 961 | !isNonOffloadableEffectEnabled()) { |
| 962 | profile = getProfileForDirectOutput(device, |
| 963 | samplingRate, |
| 964 | format, |
| 965 | channelMask, |
| 966 | (audio_output_flags_t)flags); |
| 967 | } |
| 968 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 969 | if (profile != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 970 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 971 | |
| 972 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 973 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 975 | outputDesc = desc; |
| 976 | // reuse direct output if currently open and configured with same parameters |
| 977 | if ((samplingRate == outputDesc->mSamplingRate) && |
| 978 | (format == outputDesc->mFormat) && |
| 979 | (channelMask == outputDesc->mChannelMask)) { |
| 980 | outputDesc->mDirectOpenCount++; |
| 981 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 982 | return mOutputs.keyAt(i); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | // close direct output if currently open and configured with different parameters |
| 987 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 988 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | } |
| 990 | outputDesc = new AudioOutputDescriptor(profile); |
| 991 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 992 | outputDesc->mLatency = 0; |
| 993 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 994 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 995 | config.sample_rate = samplingRate; |
| 996 | config.channel_mask = channelMask; |
| 997 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 998 | if (offloadInfo != NULL) { |
| 999 | config.offload_info = *offloadInfo; |
| 1000 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1001 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 1002 | &output, |
| 1003 | &config, |
| 1004 | &outputDesc->mDevice, |
| 1005 | String8(""), |
| 1006 | &outputDesc->mLatency, |
| 1007 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1008 | |
| 1009 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1010 | if (status != NO_ERROR || |
| 1011 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
| 1012 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || |
| 1013 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1015 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1016 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1017 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1018 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1019 | mpClientInterface->closeOutput(output); |
| 1020 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1021 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1022 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1023 | outputDesc->mSamplingRate = config.sample_rate; |
| 1024 | outputDesc->mChannelMask = config.channel_mask; |
| 1025 | outputDesc->mFormat = config.format; |
| 1026 | outputDesc->mRefCount[stream] = 0; |
| 1027 | outputDesc->mStopTime[stream] = 0; |
| 1028 | outputDesc->mDirectOpenCount = 1; |
| 1029 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1030 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 1031 | addOutput(output, outputDesc); |
| 1032 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1033 | if (dstOutput == output) { |
| 1034 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 1035 | } |
| 1036 | mPreviousOutputs = mOutputs; |
| 1037 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1038 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1039 | return output; |
| 1040 | } |
| 1041 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1042 | non_direct_output: |
| 1043 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | // ignoring channel mask due to downmix capability in mixer |
| 1045 | |
| 1046 | // open a non direct output |
| 1047 | |
| 1048 | // for non direct outputs, only PCM is supported |
| 1049 | if (audio_is_linear_pcm(format)) { |
| 1050 | // get which output is suitable for the specified stream. The actual |
| 1051 | // routing change will happen when startOutput() will be called |
| 1052 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1053 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1054 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1055 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1056 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1057 | } |
| 1058 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1059 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1060 | |
| 1061 | ALOGV("getOutput() returns output %d", output); |
| 1062 | |
| 1063 | return output; |
| 1064 | } |
| 1065 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1066 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1067 | audio_output_flags_t flags, |
| 1068 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1069 | { |
| 1070 | // select one output among several that provide a path to a particular device or set of |
| 1071 | // devices (the list was previously build by getOutputsForDevice()). |
| 1072 | // The priority is as follows: |
| 1073 | // 1: the output with the highest number of requested policy flags |
| 1074 | // 2: the primary output |
| 1075 | // 3: the first output in the list |
| 1076 | |
| 1077 | if (outputs.size() == 0) { |
| 1078 | return 0; |
| 1079 | } |
| 1080 | if (outputs.size() == 1) { |
| 1081 | return outputs[0]; |
| 1082 | } |
| 1083 | |
| 1084 | int maxCommonFlags = 0; |
| 1085 | audio_io_handle_t outputFlags = 0; |
| 1086 | audio_io_handle_t outputPrimary = 0; |
| 1087 | |
| 1088 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1089 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1090 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1091 | // if a valid format is specified, skip output if not compatible |
| 1092 | if (format != AUDIO_FORMAT_INVALID) { |
| 1093 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1094 | if (format != outputDesc->mFormat) { |
| 1095 | continue; |
| 1096 | } |
| 1097 | } else if (!audio_is_linear_pcm(format)) { |
| 1098 | continue; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1102 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1103 | if (commonFlags > maxCommonFlags) { |
| 1104 | outputFlags = outputs[i]; |
| 1105 | maxCommonFlags = commonFlags; |
| 1106 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1107 | } |
| 1108 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 1109 | outputPrimary = outputs[i]; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | if (outputFlags != 0) { |
| 1115 | return outputFlags; |
| 1116 | } |
| 1117 | if (outputPrimary != 0) { |
| 1118 | return outputPrimary; |
| 1119 | } |
| 1120 | |
| 1121 | return outputs[0]; |
| 1122 | } |
| 1123 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1124 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1125 | audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1126 | int session) |
| 1127 | { |
| 1128 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
| 1129 | ssize_t index = mOutputs.indexOfKey(output); |
| 1130 | if (index < 0) { |
| 1131 | ALOGW("startOutput() unknown output %d", output); |
| 1132 | return BAD_VALUE; |
| 1133 | } |
| 1134 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1135 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1136 | uint32_t beaconMuteLatency = 0; |
| 1137 | if (stream == AUDIO_STREAM_TTS) { |
| 1138 | ALOGV("\t found BEACON stream"); |
| 1139 | if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
| 1140 | return INVALID_OPERATION; |
| 1141 | } else { |
| 1142 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1143 | } |
| 1144 | } else { |
| 1145 | // some playback other than beacon starts |
| 1146 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1147 | } |
| 1148 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1149 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1150 | |
| 1151 | // increment usage count for this stream on the requested output: |
| 1152 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1153 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1154 | outputDesc->changeRefCount(stream, 1); |
| 1155 | |
| 1156 | if (outputDesc->mRefCount[stream] == 1) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1157 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1158 | routing_strategy strategy = getStrategy(stream); |
| 1159 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1160 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1161 | (beaconMuteLatency > 0); |
| 1162 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1163 | bool force = false; |
| 1164 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1165 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1166 | if (desc != outputDesc) { |
| 1167 | // force a device change if any other output is managed by the same hw |
| 1168 | // module and has a current device selection that differs from selected device. |
| 1169 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1170 | // change the device currently selected by the other active output. |
| 1171 | if (outputDesc->sharesHwModuleWith(desc) && |
| 1172 | desc->device() != newDevice) { |
| 1173 | force = true; |
| 1174 | } |
| 1175 | // wait for audio on other active outputs to be presented when starting |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1176 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1177 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1178 | uint32_t latency = desc->latency(); |
| 1179 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1180 | waitMs = latency; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); |
| 1185 | |
| 1186 | // handle special case for sonification while in call |
| 1187 | if (isInCall()) { |
| 1188 | handleIncallSonification(stream, true, false); |
| 1189 | } |
| 1190 | |
| 1191 | // apply volume rules for current stream and device if necessary |
| 1192 | checkAndSetVolume(stream, |
| 1193 | mStreams[stream].getVolumeIndex(newDevice), |
| 1194 | output, |
| 1195 | newDevice); |
| 1196 | |
| 1197 | // update the outputs if starting an output with a stream that can affect notification |
| 1198 | // routing |
| 1199 | handleNotificationRoutingForStream(stream); |
| 1200 | if (waitMs > muteWaitMs) { |
| 1201 | usleep((waitMs - muteWaitMs) * 2 * 1000); |
| 1202 | } |
| 1203 | } |
| 1204 | return NO_ERROR; |
| 1205 | } |
| 1206 | |
| 1207 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1208 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1209 | audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1210 | int session) |
| 1211 | { |
| 1212 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1213 | ssize_t index = mOutputs.indexOfKey(output); |
| 1214 | if (index < 0) { |
| 1215 | ALOGW("stopOutput() unknown output %d", output); |
| 1216 | return BAD_VALUE; |
| 1217 | } |
| 1218 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1219 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1220 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1221 | // always handle stream stop, check which stream type is stopping |
| 1222 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1223 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1224 | // handle special case for sonification while in call |
| 1225 | if (isInCall()) { |
| 1226 | handleIncallSonification(stream, false, false); |
| 1227 | } |
| 1228 | |
| 1229 | if (outputDesc->mRefCount[stream] > 0) { |
| 1230 | // decrement usage count of this stream on the output |
| 1231 | outputDesc->changeRefCount(stream, -1); |
| 1232 | // store time at which the stream was stopped - see isStreamActive() |
| 1233 | if (outputDesc->mRefCount[stream] == 0) { |
| 1234 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1235 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1236 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1237 | // the track stop() command is received and at that time the audio track buffer can |
| 1238 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1239 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1240 | // audio path (audio DSP, CODEC ...) |
| 1241 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); |
| 1242 | |
| 1243 | // force restoring the device selection on other active outputs if it differs from the |
| 1244 | // one being selected for this output |
| 1245 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1246 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1247 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1248 | if (curOutput != output && |
| 1249 | desc->isActive() && |
| 1250 | outputDesc->sharesHwModuleWith(desc) && |
| 1251 | (newDevice != desc->device())) { |
| 1252 | setOutputDevice(curOutput, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1253 | getNewOutputDevice(curOutput, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1254 | true, |
| 1255 | outputDesc->mLatency*2); |
| 1256 | } |
| 1257 | } |
| 1258 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1259 | handleNotificationRoutingForStream(stream); |
| 1260 | } |
| 1261 | return NO_ERROR; |
| 1262 | } else { |
| 1263 | ALOGW("stopOutput() refcount is already 0 for output %d", output); |
| 1264 | return INVALID_OPERATION; |
| 1265 | } |
| 1266 | } |
| 1267 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1268 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1269 | { |
| 1270 | ALOGV("releaseOutput() %d", output); |
| 1271 | ssize_t index = mOutputs.indexOfKey(output); |
| 1272 | if (index < 0) { |
| 1273 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | #ifdef AUDIO_POLICY_TEST |
| 1278 | int testIndex = testOutputIndex(output); |
| 1279 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1280 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | if (outputDesc->isActive()) { |
| 1282 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1283 | mOutputs.removeItem(output); |
| 1284 | mTestOutputs[testIndex] = 0; |
| 1285 | } |
| 1286 | return; |
| 1287 | } |
| 1288 | #endif //AUDIO_POLICY_TEST |
| 1289 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1290 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1291 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1292 | if (desc->mDirectOpenCount <= 0) { |
| 1293 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1294 | desc->mDirectOpenCount, output); |
| 1295 | return; |
| 1296 | } |
| 1297 | if (--desc->mDirectOpenCount == 0) { |
| 1298 | closeOutput(output); |
| 1299 | // If effects where present on the output, audioflinger moved them to the primary |
| 1300 | // output by default: move them back to the appropriate output. |
| 1301 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1302 | if (dstOutput != mPrimaryOutput) { |
| 1303 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); |
| 1304 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1305 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1311 | audio_io_handle_t AudioPolicyManager::getInput(audio_source_t inputSource, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1312 | uint32_t samplingRate, |
| 1313 | audio_format_t format, |
| 1314 | audio_channel_mask_t channelMask, |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1315 | audio_session_t session, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1316 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1317 | { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1318 | ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, session %d, " |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1319 | "flags %#x", |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1320 | inputSource, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1321 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1322 | audio_devices_t device = getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (device == AUDIO_DEVICE_NONE) { |
| 1325 | ALOGW("getInput() could not find device for inputSource %d", inputSource); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1326 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | // adapt channel selection to input source |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1330 | switch (inputSource) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1331 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1332 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; |
| 1333 | break; |
| 1334 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1335 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1336 | break; |
| 1337 | case AUDIO_SOURCE_VOICE_CALL: |
| 1338 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1339 | break; |
| 1340 | default: |
| 1341 | break; |
| 1342 | } |
| 1343 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1344 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1345 | bool isSoundTrigger = false; |
| 1346 | audio_source_t halInputSource = inputSource; |
| 1347 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1348 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1349 | if (index >= 0) { |
| 1350 | input = mSoundTriggerSessions.valueFor(session); |
| 1351 | isSoundTrigger = true; |
| 1352 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1353 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1354 | } else { |
| 1355 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 1356 | } |
| 1357 | } |
| 1358 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1359 | sp<IOProfile> profile = getInputProfile(device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1360 | samplingRate, |
| 1361 | format, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1362 | channelMask, |
| 1363 | flags); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1364 | if (profile == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1365 | //retry without flags |
| 1366 | audio_input_flags_t log_flags = flags; |
| 1367 | flags = AUDIO_INPUT_FLAG_NONE; |
| 1368 | profile = getInputProfile(device, |
| 1369 | samplingRate, |
| 1370 | format, |
| 1371 | channelMask, |
| 1372 | flags); |
| 1373 | if (profile == 0) { |
| 1374 | ALOGW("getInput() could not find profile for device 0x%X, samplingRate %u, format %#x, " |
| 1375 | "channelMask 0x%X, flags %#x", |
| 1376 | device, samplingRate, format, channelMask, log_flags); |
| 1377 | return AUDIO_IO_HANDLE_NONE; |
| 1378 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | if (profile->mModule->mHandle == 0) { |
| 1382 | ALOGE("getInput(): HW module %s not opened", profile->mModule->mName); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1383 | return AUDIO_IO_HANDLE_NONE; |
| 1384 | } |
| 1385 | |
| 1386 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1387 | config.sample_rate = samplingRate; |
| 1388 | config.channel_mask = channelMask; |
| 1389 | config.format = format; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1390 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1391 | // handle legacy remote submix case where the address was not always specified |
| 1392 | String8 address = deviceDistinguishesOnAddress(device) ? String8("0") : String8(""); |
| 1393 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1394 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
| 1395 | &input, |
| 1396 | &config, |
| 1397 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1398 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1399 | halInputSource, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1400 | flags); |
| 1401 | |
| 1402 | // only accept input with the exact requested set of parameters |
| 1403 | if (status != NO_ERROR || |
| 1404 | (samplingRate != config.sample_rate) || |
| 1405 | (format != config.format) || |
| 1406 | (channelMask != config.channel_mask)) { |
| 1407 | ALOGW("getInput() failed opening input: samplingRate %d, format %d, channelMask %x", |
| 1408 | samplingRate, format, channelMask); |
| 1409 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1410 | mpClientInterface->closeInput(input); |
| 1411 | } |
| 1412 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1415 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1416 | inputDesc->mInputSource = inputSource; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1417 | inputDesc->mRefCount = 0; |
| 1418 | inputDesc->mOpenRefCount = 1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1419 | inputDesc->mSamplingRate = samplingRate; |
| 1420 | inputDesc->mFormat = format; |
| 1421 | inputDesc->mChannelMask = channelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1422 | inputDesc->mDevice = device; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1423 | inputDesc->mSessions.add(session); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1424 | inputDesc->mIsSoundTrigger = isSoundTrigger; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1425 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1426 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1427 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1428 | return input; |
| 1429 | } |
| 1430 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1431 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
| 1432 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1433 | { |
| 1434 | ALOGV("startInput() input %d", input); |
| 1435 | ssize_t index = mInputs.indexOfKey(input); |
| 1436 | if (index < 0) { |
| 1437 | ALOGW("startInput() unknown input %d", input); |
| 1438 | return BAD_VALUE; |
| 1439 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1440 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1441 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1442 | index = inputDesc->mSessions.indexOf(session); |
| 1443 | if (index < 0) { |
| 1444 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1445 | return BAD_VALUE; |
| 1446 | } |
| 1447 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1448 | // virtual input devices are compatible with other input devices |
| 1449 | if (!isVirtualInputDevice(inputDesc->mDevice)) { |
| 1450 | |
| 1451 | // 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] | 1452 | audio_io_handle_t activeInput = getActiveInput(); |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1453 | if (activeInput != 0 && activeInput != input) { |
| 1454 | |
| 1455 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1456 | // otherwise the active input continues and the new input cannot be started. |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1457 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1459 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1460 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); |
| 1461 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1462 | } else { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1463 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1464 | return INVALID_OPERATION; |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1469 | if (inputDesc->mRefCount == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1470 | if (activeInputsCount() == 0) { |
| 1471 | SoundTrigger::setCaptureState(true); |
| 1472 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1473 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1474 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1475 | // Automatically enable the remote submix output when input is started. |
| 1476 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1477 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1478 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1479 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); |
| 1480 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1483 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
| 1484 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1485 | inputDesc->mRefCount++; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1486 | return NO_ERROR; |
| 1487 | } |
| 1488 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1489 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1490 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1491 | { |
| 1492 | ALOGV("stopInput() input %d", input); |
| 1493 | ssize_t index = mInputs.indexOfKey(input); |
| 1494 | if (index < 0) { |
| 1495 | ALOGW("stopInput() unknown input %d", input); |
| 1496 | return BAD_VALUE; |
| 1497 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1498 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1499 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1500 | index = inputDesc->mSessions.indexOf(session); |
| 1501 | if (index < 0) { |
| 1502 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1503 | return BAD_VALUE; |
| 1504 | } |
| 1505 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1506 | if (inputDesc->mRefCount == 0) { |
| 1507 | ALOGW("stopInput() input %d already stopped", input); |
| 1508 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | inputDesc->mRefCount--; |
| 1512 | if (inputDesc->mRefCount == 0) { |
| 1513 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1514 | // automatically disable the remote submix output when input is stopped |
| 1515 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1516 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1517 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1520 | resetInputDevice(input); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1521 | |
| 1522 | if (activeInputsCount() == 0) { |
| 1523 | SoundTrigger::setCaptureState(false); |
| 1524 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1525 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1526 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1529 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1530 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1531 | { |
| 1532 | ALOGV("releaseInput() %d", input); |
| 1533 | ssize_t index = mInputs.indexOfKey(input); |
| 1534 | if (index < 0) { |
| 1535 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1536 | return; |
| 1537 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1538 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1539 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1540 | |
| 1541 | index = inputDesc->mSessions.indexOf(session); |
| 1542 | if (index < 0) { |
| 1543 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1544 | return; |
| 1545 | } |
| 1546 | inputDesc->mSessions.remove(session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1547 | if (inputDesc->mOpenRefCount == 0) { |
| 1548 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); |
| 1549 | return; |
| 1550 | } |
| 1551 | inputDesc->mOpenRefCount--; |
| 1552 | if (inputDesc->mOpenRefCount > 0) { |
| 1553 | ALOGV("releaseInput() exit > 0"); |
| 1554 | return; |
| 1555 | } |
| 1556 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1557 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1558 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1559 | ALOGV("releaseInput() exit"); |
| 1560 | } |
| 1561 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1562 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1563 | bool patchRemoved = false; |
| 1564 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1565 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1566 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
| 1567 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 1568 | if (patch_index >= 0) { |
| 1569 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1570 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1571 | mAudioPatches.removeItemsAt(patch_index); |
| 1572 | patchRemoved = true; |
| 1573 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1574 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1575 | } |
| 1576 | mInputs.clear(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1577 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (patchRemoved) { |
| 1580 | mpClientInterface->onAudioPatchListUpdate(); |
| 1581 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1584 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1585 | int indexMin, |
| 1586 | int indexMax) |
| 1587 | { |
| 1588 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1589 | if (indexMin < 0 || indexMin >= indexMax) { |
| 1590 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1591 | return; |
| 1592 | } |
| 1593 | mStreams[stream].mIndexMin = indexMin; |
| 1594 | mStreams[stream].mIndexMax = indexMax; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 1595 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1596 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1597 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin; |
| 1598 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax; |
| 1599 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1602 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1603 | int index, |
| 1604 | audio_devices_t device) |
| 1605 | { |
| 1606 | |
| 1607 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { |
| 1608 | return BAD_VALUE; |
| 1609 | } |
| 1610 | if (!audio_is_output_device(device)) { |
| 1611 | return BAD_VALUE; |
| 1612 | } |
| 1613 | |
| 1614 | // Force max volume if stream cannot be muted |
| 1615 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; |
| 1616 | |
| 1617 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1618 | stream, device, index); |
| 1619 | |
| 1620 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1621 | // clear all device specific values |
| 1622 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1623 | mStreams[stream].mIndexCur.clear(); |
| 1624 | } |
| 1625 | mStreams[stream].mIndexCur.add(device, index); |
| 1626 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1627 | // update volume on all outputs whose current device is also selected by the same |
| 1628 | // strategy as the device specified by the caller |
| 1629 | audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 1630 | |
| 1631 | |
| 1632 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1633 | audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; |
| 1634 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1635 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index); |
| 1636 | accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); |
| 1637 | } |
| 1638 | if ((device != AUDIO_DEVICE_OUT_DEFAULT) && |
| 1639 | (device & (strategyDevice | accessibilityDevice)) == 0) { |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1640 | return NO_ERROR; |
| 1641 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1642 | status_t status = NO_ERROR; |
| 1643 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1644 | audio_devices_t curDevice = |
| 1645 | getDeviceForVolume(mOutputs.valueAt(i)->device()); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1646 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1647 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); |
| 1648 | if (volStatus != NO_ERROR) { |
| 1649 | status = volStatus; |
| 1650 | } |
| 1651 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 1652 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) { |
| 1653 | status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, |
| 1654 | index, mOutputs.keyAt(i), curDevice); |
| 1655 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1656 | } |
| 1657 | return status; |
| 1658 | } |
| 1659 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1660 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1661 | int *index, |
| 1662 | audio_devices_t device) |
| 1663 | { |
| 1664 | if (index == NULL) { |
| 1665 | return BAD_VALUE; |
| 1666 | } |
| 1667 | if (!audio_is_output_device(device)) { |
| 1668 | return BAD_VALUE; |
| 1669 | } |
| 1670 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1671 | // the strategy the stream belongs to. |
| 1672 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1673 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1674 | } |
| 1675 | device = getDeviceForVolume(device); |
| 1676 | |
| 1677 | *index = mStreams[stream].getVolumeIndex(device); |
| 1678 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1679 | return NO_ERROR; |
| 1680 | } |
| 1681 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1682 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1683 | const SortedVector<audio_io_handle_t>& outputs) |
| 1684 | { |
| 1685 | // select one output among several suitable for global effects. |
| 1686 | // The priority is as follows: |
| 1687 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1688 | // AudioFlinger will invalidate the track and the offloaded output |
| 1689 | // will be closed causing the effect to be moved to a PCM output. |
| 1690 | // 2: A deep buffer output |
| 1691 | // 3: the first output in the list |
| 1692 | |
| 1693 | if (outputs.size() == 0) { |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | audio_io_handle_t outputOffloaded = 0; |
| 1698 | audio_io_handle_t outputDeepBuffer = 0; |
| 1699 | |
| 1700 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1701 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1702 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1703 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1704 | outputOffloaded = outputs[i]; |
| 1705 | } |
| 1706 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1707 | outputDeepBuffer = outputs[i]; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1712 | outputOffloaded, outputDeepBuffer); |
| 1713 | if (outputOffloaded != 0) { |
| 1714 | return outputOffloaded; |
| 1715 | } |
| 1716 | if (outputDeepBuffer != 0) { |
| 1717 | return outputDeepBuffer; |
| 1718 | } |
| 1719 | |
| 1720 | return outputs[0]; |
| 1721 | } |
| 1722 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1723 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1724 | { |
| 1725 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1726 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1727 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1728 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1729 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1730 | |
| 1731 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1732 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1733 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1734 | |
| 1735 | return output; |
| 1736 | } |
| 1737 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1738 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1739 | audio_io_handle_t io, |
| 1740 | uint32_t strategy, |
| 1741 | int session, |
| 1742 | int id) |
| 1743 | { |
| 1744 | ssize_t index = mOutputs.indexOfKey(io); |
| 1745 | if (index < 0) { |
| 1746 | index = mInputs.indexOfKey(io); |
| 1747 | if (index < 0) { |
| 1748 | ALOGW("registerEffect() unknown io %d", io); |
| 1749 | return INVALID_OPERATION; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { |
| 1754 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", |
| 1755 | desc->name, desc->memoryUsage); |
| 1756 | return INVALID_OPERATION; |
| 1757 | } |
| 1758 | mTotalEffectsMemory += desc->memoryUsage; |
| 1759 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", |
| 1760 | desc->name, io, strategy, session, id); |
| 1761 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); |
| 1762 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1763 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); |
| 1764 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); |
| 1765 | effectDesc->mIo = io; |
| 1766 | effectDesc->mStrategy = (routing_strategy)strategy; |
| 1767 | effectDesc->mSession = session; |
| 1768 | effectDesc->mEnabled = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1769 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1770 | mEffects.add(id, effectDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1771 | |
| 1772 | return NO_ERROR; |
| 1773 | } |
| 1774 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1775 | status_t AudioPolicyManager::unregisterEffect(int id) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1776 | { |
| 1777 | ssize_t index = mEffects.indexOfKey(id); |
| 1778 | if (index < 0) { |
| 1779 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1780 | return INVALID_OPERATION; |
| 1781 | } |
| 1782 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1783 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1784 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1785 | setEffectEnabled(effectDesc, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1786 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1787 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1788 | ALOGW("unregisterEffect() memory %d too big for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1789 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
| 1790 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1792 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1794 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1795 | |
| 1796 | mEffects.removeItem(id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1797 | |
| 1798 | return NO_ERROR; |
| 1799 | } |
| 1800 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1801 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1802 | { |
| 1803 | ssize_t index = mEffects.indexOfKey(id); |
| 1804 | if (index < 0) { |
| 1805 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1806 | return INVALID_OPERATION; |
| 1807 | } |
| 1808 | |
| 1809 | return setEffectEnabled(mEffects.valueAt(index), enabled); |
| 1810 | } |
| 1811 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1812 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1813 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1814 | if (enabled == effectDesc->mEnabled) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 | ALOGV("setEffectEnabled(%s) effect already %s", |
| 1816 | enabled?"true":"false", enabled?"enabled":"disabled"); |
| 1817 | return INVALID_OPERATION; |
| 1818 | } |
| 1819 | |
| 1820 | if (enabled) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1821 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1822 | 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] | 1823 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1824 | return INVALID_OPERATION; |
| 1825 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1826 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1827 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); |
| 1828 | } else { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1829 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1830 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1831 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); |
| 1832 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1833 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1834 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1835 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); |
| 1836 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1837 | effectDesc->mEnabled = enabled; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1838 | return NO_ERROR; |
| 1839 | } |
| 1840 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1841 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1842 | { |
| 1843 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1844 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 1845 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && |
| 1846 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1847 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1848 | effectDesc->mDesc.name, effectDesc->mSession); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | } |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1855 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1856 | { |
| 1857 | nsecs_t sysTime = systemTime(); |
| 1858 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1859 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1860 | if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | return true; |
| 1862 | } |
| 1863 | } |
| 1864 | return false; |
| 1865 | } |
| 1866 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1867 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1868 | uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1869 | { |
| 1870 | nsecs_t sysTime = systemTime(); |
| 1871 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1872 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1873 | if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1874 | outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1875 | return true; |
| 1876 | } |
| 1877 | } |
| 1878 | return false; |
| 1879 | } |
| 1880 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1881 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1882 | { |
| 1883 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1884 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1885 | if ((inputDescriptor->mInputSource == (int)source || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1886 | (source == AUDIO_SOURCE_VOICE_RECOGNITION && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1887 | inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) |
| 1888 | && (inputDescriptor->mRefCount > 0)) { |
| 1889 | return true; |
| 1890 | } |
| 1891 | } |
| 1892 | return false; |
| 1893 | } |
| 1894 | |
| 1895 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1896 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1897 | { |
| 1898 | const size_t SIZE = 256; |
| 1899 | char buffer[SIZE]; |
| 1900 | String8 result; |
| 1901 | |
| 1902 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 1903 | result.append(buffer); |
| 1904 | |
| 1905 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); |
| 1906 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1907 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); |
| 1908 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1909 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
| 1910 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1911 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1912 | 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] | 1913 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1914 | 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] | 1915 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1916 | 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] | 1917 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1918 | 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] | 1919 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1920 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
| 1921 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); |
| 1922 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1923 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1924 | snprintf(buffer, SIZE, " Available output devices:\n"); |
| 1925 | result.append(buffer); |
| 1926 | write(fd, result.string(), result.size()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1927 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1928 | mAvailableOutputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1929 | } |
| 1930 | snprintf(buffer, SIZE, "\n Available input devices:\n"); |
| 1931 | write(fd, buffer, strlen(buffer)); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1932 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1933 | mAvailableInputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1934 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | |
| 1936 | snprintf(buffer, SIZE, "\nHW Modules dump:\n"); |
| 1937 | write(fd, buffer, strlen(buffer)); |
| 1938 | for (size_t i = 0; i < mHwModules.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1939 | snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1940 | write(fd, buffer, strlen(buffer)); |
| 1941 | mHwModules[i]->dump(fd); |
| 1942 | } |
| 1943 | |
| 1944 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); |
| 1945 | write(fd, buffer, strlen(buffer)); |
| 1946 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1947 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); |
| 1948 | write(fd, buffer, strlen(buffer)); |
| 1949 | mOutputs.valueAt(i)->dump(fd); |
| 1950 | } |
| 1951 | |
| 1952 | snprintf(buffer, SIZE, "\nInputs dump:\n"); |
| 1953 | write(fd, buffer, strlen(buffer)); |
| 1954 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1955 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); |
| 1956 | write(fd, buffer, strlen(buffer)); |
| 1957 | mInputs.valueAt(i)->dump(fd); |
| 1958 | } |
| 1959 | |
| 1960 | snprintf(buffer, SIZE, "\nStreams dump:\n"); |
| 1961 | write(fd, buffer, strlen(buffer)); |
| 1962 | snprintf(buffer, SIZE, |
| 1963 | " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n"); |
| 1964 | write(fd, buffer, strlen(buffer)); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 1965 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1966 | snprintf(buffer, SIZE, " %02zu ", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1967 | write(fd, buffer, strlen(buffer)); |
| 1968 | mStreams[i].dump(fd); |
| 1969 | } |
| 1970 | |
| 1971 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", |
| 1972 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); |
| 1973 | write(fd, buffer, strlen(buffer)); |
| 1974 | |
| 1975 | snprintf(buffer, SIZE, "Registered effects:\n"); |
| 1976 | write(fd, buffer, strlen(buffer)); |
| 1977 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 1978 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); |
| 1979 | write(fd, buffer, strlen(buffer)); |
| 1980 | mEffects.valueAt(i)->dump(fd); |
| 1981 | } |
| 1982 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 1983 | snprintf(buffer, SIZE, "\nAudio Patches:\n"); |
| 1984 | write(fd, buffer, strlen(buffer)); |
| 1985 | for (size_t i = 0; i < mAudioPatches.size(); i++) { |
| 1986 | mAudioPatches[i]->dump(fd, 2, i); |
| 1987 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1988 | |
| 1989 | return NO_ERROR; |
| 1990 | } |
| 1991 | |
| 1992 | // This function checks for the parameters which can be offloaded. |
| 1993 | // This can be enhanced depending on the capability of the DSP and policy |
| 1994 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1995 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1996 | { |
| 1997 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1998 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1999 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2000 | offloadInfo.format, |
| 2001 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2002 | offloadInfo.has_video); |
| 2003 | |
| 2004 | // Check if offload has been disabled |
| 2005 | char propValue[PROPERTY_VALUE_MAX]; |
| 2006 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2007 | if (atoi(propValue) != 0) { |
| 2008 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2009 | return false; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | // Check if stream type is music, then only allow offload as of now. |
| 2014 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2015 | { |
| 2016 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2017 | return false; |
| 2018 | } |
| 2019 | |
| 2020 | //TODO: enable audio offloading with video when ready |
| 2021 | if (offloadInfo.has_video) |
| 2022 | { |
| 2023 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
| 2027 | //If duration is less than minimum value defined in property, return false |
| 2028 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2029 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2030 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2031 | return false; |
| 2032 | } |
| 2033 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2034 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
| 2038 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2039 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2040 | // detects there is an active non offloadable effect. |
| 2041 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2042 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2043 | // in the background. |
| 2044 | if (isNonOffloadableEffectEnabled()) { |
| 2045 | return false; |
| 2046 | } |
| 2047 | |
| 2048 | // See if there is a profile to support this. |
| 2049 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2050 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2051 | offloadInfo.sample_rate, |
| 2052 | offloadInfo.format, |
| 2053 | offloadInfo.channel_mask, |
| 2054 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2055 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2056 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2059 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2060 | audio_port_type_t type, |
| 2061 | unsigned int *num_ports, |
| 2062 | struct audio_port *ports, |
| 2063 | unsigned int *generation) |
| 2064 | { |
| 2065 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2066 | generation == NULL) { |
| 2067 | return BAD_VALUE; |
| 2068 | } |
| 2069 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2070 | if (ports == NULL) { |
| 2071 | *num_ports = 0; |
| 2072 | } |
| 2073 | |
| 2074 | size_t portsWritten = 0; |
| 2075 | size_t portsMax = *num_ports; |
| 2076 | *num_ports = 0; |
| 2077 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2078 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2079 | for (size_t i = 0; |
| 2080 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2081 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2082 | } |
| 2083 | *num_ports += mAvailableOutputDevices.size(); |
| 2084 | } |
| 2085 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2086 | for (size_t i = 0; |
| 2087 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2088 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2089 | } |
| 2090 | *num_ports += mAvailableInputDevices.size(); |
| 2091 | } |
| 2092 | } |
| 2093 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2094 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2095 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2096 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2097 | } |
| 2098 | *num_ports += mInputs.size(); |
| 2099 | } |
| 2100 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2101 | size_t numOutputs = 0; |
| 2102 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2103 | if (!mOutputs[i]->isDuplicated()) { |
| 2104 | numOutputs++; |
| 2105 | if (portsWritten < portsMax) { |
| 2106 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2107 | } |
| 2108 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2109 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2110 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2114 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2115 | return NO_ERROR; |
| 2116 | } |
| 2117 | |
| 2118 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2119 | { |
| 2120 | return NO_ERROR; |
| 2121 | } |
| 2122 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2123 | sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2124 | audio_port_handle_t id) const |
| 2125 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2126 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2127 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2128 | outputDesc = mOutputs.valueAt(i); |
| 2129 | if (outputDesc->mId == id) { |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | return outputDesc; |
| 2134 | } |
| 2135 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2136 | sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2137 | audio_port_handle_t id) const |
| 2138 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2139 | sp<AudioInputDescriptor> inputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2140 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2141 | inputDesc = mInputs.valueAt(i); |
| 2142 | if (inputDesc->mId == id) { |
| 2143 | break; |
| 2144 | } |
| 2145 | } |
| 2146 | return inputDesc; |
| 2147 | } |
| 2148 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2149 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice( |
| 2150 | audio_devices_t device) const |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2151 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2152 | sp <HwModule> module; |
| 2153 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2154 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2155 | if (mHwModules[i]->mHandle == 0) { |
| 2156 | continue; |
| 2157 | } |
| 2158 | if (audio_is_output_device(device)) { |
| 2159 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2160 | { |
| 2161 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { |
| 2162 | return mHwModules[i]; |
| 2163 | } |
| 2164 | } |
| 2165 | } else { |
| 2166 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { |
| 2167 | if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() & |
| 2168 | device & ~AUDIO_DEVICE_BIT_IN) { |
| 2169 | return mHwModules[i]; |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2174 | return module; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2177 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2178 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2179 | sp <HwModule> module; |
| 2180 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2181 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 2182 | { |
| 2183 | if (strcmp(mHwModules[i]->mName, name) == 0) { |
| 2184 | return mHwModules[i]; |
| 2185 | } |
| 2186 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2187 | return module; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2190 | audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices() |
| 2191 | { |
| 2192 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 2193 | audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types(); |
| 2194 | return devices & mAvailableOutputDevices.types(); |
| 2195 | } |
| 2196 | |
| 2197 | audio_devices_t AudioPolicyManager::availablePrimaryInputDevices() |
| 2198 | { |
| 2199 | audio_module_handle_t primaryHandle = |
| 2200 | mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle; |
| 2201 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
| 2202 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2203 | if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) { |
| 2204 | devices |= mAvailableInputDevices[i]->mDeviceType; |
| 2205 | } |
| 2206 | } |
| 2207 | return devices; |
| 2208 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2209 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2210 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2211 | audio_patch_handle_t *handle, |
| 2212 | uid_t uid) |
| 2213 | { |
| 2214 | ALOGV("createAudioPatch()"); |
| 2215 | |
| 2216 | if (handle == NULL || patch == NULL) { |
| 2217 | return BAD_VALUE; |
| 2218 | } |
| 2219 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2220 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2221 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2222 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2223 | return BAD_VALUE; |
| 2224 | } |
| 2225 | // only one source per audio patch supported for now |
| 2226 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2227 | return INVALID_OPERATION; |
| 2228 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2229 | |
| 2230 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2231 | return INVALID_OPERATION; |
| 2232 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2233 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2234 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2235 | return INVALID_OPERATION; |
| 2236 | } |
| 2237 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2238 | |
| 2239 | sp<AudioPatch> patchDesc; |
| 2240 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2241 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2242 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2243 | patch->sources[0].role, |
| 2244 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2245 | #if LOG_NDEBUG == 0 |
| 2246 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2247 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, |
| 2248 | patch->sinks[i].role, |
| 2249 | patch->sinks[i].type); |
| 2250 | } |
| 2251 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2252 | |
| 2253 | if (index >= 0) { |
| 2254 | patchDesc = mAudioPatches.valueAt(index); |
| 2255 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2256 | mUidCached, patchDesc->mUid, uid); |
| 2257 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2258 | return INVALID_OPERATION; |
| 2259 | } |
| 2260 | } else { |
| 2261 | *handle = 0; |
| 2262 | } |
| 2263 | |
| 2264 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2265 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2266 | if (outputDesc == NULL) { |
| 2267 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2268 | return BAD_VALUE; |
| 2269 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2270 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2271 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2272 | if (patchDesc != 0) { |
| 2273 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2274 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2275 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2276 | return BAD_VALUE; |
| 2277 | } |
| 2278 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2279 | DeviceVector devices; |
| 2280 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2281 | // Only support mix to devices connection |
| 2282 | // TODO add support for mix to mix connection |
| 2283 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2284 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2285 | return INVALID_OPERATION; |
| 2286 | } |
| 2287 | sp<DeviceDescriptor> devDesc = |
| 2288 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2289 | if (devDesc == 0) { |
| 2290 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2291 | return BAD_VALUE; |
| 2292 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2293 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2294 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
| 2295 | patch->sources[0].sample_rate, |
| 2296 | NULL, // updatedSamplingRate |
| 2297 | patch->sources[0].format, |
| 2298 | patch->sources[0].channel_mask, |
| 2299 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
| 2300 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2301 | devDesc->mDeviceType); |
| 2302 | return INVALID_OPERATION; |
| 2303 | } |
| 2304 | devices.add(devDesc); |
| 2305 | } |
| 2306 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2307 | return INVALID_OPERATION; |
| 2308 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2309 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2310 | // TODO: reconfigure output format and channels here |
| 2311 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2312 | devices.types(), outputDesc->mIoHandle); |
| 2313 | setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2314 | index = mAudioPatches.indexOfKey(*handle); |
| 2315 | if (index >= 0) { |
| 2316 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2317 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2318 | } |
| 2319 | patchDesc = mAudioPatches.valueAt(index); |
| 2320 | patchDesc->mUid = uid; |
| 2321 | ALOGV("createAudioPatch() success"); |
| 2322 | } else { |
| 2323 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2324 | return INVALID_OPERATION; |
| 2325 | } |
| 2326 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2327 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2328 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2329 | // only one sink supported when connecting an input device to a mix |
| 2330 | if (patch->num_sinks > 1) { |
| 2331 | return INVALID_OPERATION; |
| 2332 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2333 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2334 | if (inputDesc == NULL) { |
| 2335 | return BAD_VALUE; |
| 2336 | } |
| 2337 | if (patchDesc != 0) { |
| 2338 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2339 | return BAD_VALUE; |
| 2340 | } |
| 2341 | } |
| 2342 | sp<DeviceDescriptor> devDesc = |
| 2343 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2344 | if (devDesc == 0) { |
| 2345 | return BAD_VALUE; |
| 2346 | } |
| 2347 | |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2348 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 2349 | patch->sinks[0].sample_rate, |
| 2350 | NULL, /*updatedSampleRate*/ |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2351 | patch->sinks[0].format, |
| 2352 | patch->sinks[0].channel_mask, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2353 | // FIXME for the parameter type, |
| 2354 | // and the NONE |
| 2355 | (audio_output_flags_t) |
| 2356 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2357 | return INVALID_OPERATION; |
| 2358 | } |
| 2359 | // TODO: reconfigure output format and channels here |
| 2360 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2361 | devDesc->mDeviceType, inputDesc->mIoHandle); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2362 | setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2363 | index = mAudioPatches.indexOfKey(*handle); |
| 2364 | if (index >= 0) { |
| 2365 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2366 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2367 | } |
| 2368 | patchDesc = mAudioPatches.valueAt(index); |
| 2369 | patchDesc->mUid = uid; |
| 2370 | ALOGV("createAudioPatch() success"); |
| 2371 | } else { |
| 2372 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2373 | return INVALID_OPERATION; |
| 2374 | } |
| 2375 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2376 | // device to device connection |
| 2377 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2378 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2379 | return BAD_VALUE; |
| 2380 | } |
| 2381 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2382 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2383 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2384 | if (srcDeviceDesc == 0) { |
| 2385 | return BAD_VALUE; |
| 2386 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2387 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2388 | //update source and sink with our own data as the data passed in the patch may |
| 2389 | // be incomplete. |
| 2390 | struct audio_patch newPatch = *patch; |
| 2391 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2392 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2393 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2394 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2395 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2396 | return INVALID_OPERATION; |
| 2397 | } |
| 2398 | |
| 2399 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2400 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2401 | if (sinkDeviceDesc == 0) { |
| 2402 | return BAD_VALUE; |
| 2403 | } |
| 2404 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2405 | |
| 2406 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { |
| 2407 | // only one sink supported when connected devices across HW modules |
| 2408 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2409 | return INVALID_OPERATION; |
| 2410 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2411 | SortedVector<audio_io_handle_t> outputs = |
| 2412 | getOutputsForDevice(sinkDeviceDesc->mDeviceType, |
| 2413 | mOutputs); |
| 2414 | // if the sink device is reachable via an opened output stream, request to go via |
| 2415 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2416 | audio_io_handle_t output = selectOutput(outputs, |
| 2417 | AUDIO_OUTPUT_FLAG_NONE, |
| 2418 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2419 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2420 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2421 | if (outputDesc->isDuplicated()) { |
| 2422 | return INVALID_OPERATION; |
| 2423 | } |
| 2424 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
| 2425 | newPatch.num_sources = 2; |
| 2426 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2427 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2428 | } |
| 2429 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2430 | |
| 2431 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2432 | if (index >= 0) { |
| 2433 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2434 | } |
| 2435 | |
| 2436 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2437 | &afPatchHandle, |
| 2438 | 0); |
| 2439 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2440 | status, afPatchHandle); |
| 2441 | if (status == NO_ERROR) { |
| 2442 | if (index < 0) { |
| 2443 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 2444 | &newPatch, uid); |
| 2445 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2446 | } else { |
| 2447 | patchDesc->mPatch = newPatch; |
| 2448 | } |
| 2449 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2450 | *handle = patchDesc->mHandle; |
| 2451 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2452 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2453 | } else { |
| 2454 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2455 | status); |
| 2456 | return INVALID_OPERATION; |
| 2457 | } |
| 2458 | } else { |
| 2459 | return BAD_VALUE; |
| 2460 | } |
| 2461 | } else { |
| 2462 | return BAD_VALUE; |
| 2463 | } |
| 2464 | return NO_ERROR; |
| 2465 | } |
| 2466 | |
| 2467 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2468 | uid_t uid) |
| 2469 | { |
| 2470 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2471 | |
| 2472 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2473 | |
| 2474 | if (index < 0) { |
| 2475 | return BAD_VALUE; |
| 2476 | } |
| 2477 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2478 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2479 | mUidCached, patchDesc->mUid, uid); |
| 2480 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2481 | return INVALID_OPERATION; |
| 2482 | } |
| 2483 | |
| 2484 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2485 | patchDesc->mUid = mUidCached; |
| 2486 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2487 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2488 | if (outputDesc == NULL) { |
| 2489 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2490 | return BAD_VALUE; |
| 2491 | } |
| 2492 | |
| 2493 | setOutputDevice(outputDesc->mIoHandle, |
| 2494 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), |
| 2495 | true, |
| 2496 | 0, |
| 2497 | NULL); |
| 2498 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2499 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2500 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2501 | if (inputDesc == NULL) { |
| 2502 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2503 | return BAD_VALUE; |
| 2504 | } |
| 2505 | setInputDevice(inputDesc->mIoHandle, |
| 2506 | getNewInputDevice(inputDesc->mIoHandle), |
| 2507 | true, |
| 2508 | NULL); |
| 2509 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2510 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2511 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2512 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2513 | status, patchDesc->mAfPatchHandle); |
| 2514 | removeAudioPatch(patchDesc->mHandle); |
| 2515 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2516 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2517 | } else { |
| 2518 | return BAD_VALUE; |
| 2519 | } |
| 2520 | } else { |
| 2521 | return BAD_VALUE; |
| 2522 | } |
| 2523 | return NO_ERROR; |
| 2524 | } |
| 2525 | |
| 2526 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2527 | struct audio_patch *patches, |
| 2528 | unsigned int *generation) |
| 2529 | { |
| 2530 | if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || |
| 2531 | generation == NULL) { |
| 2532 | return BAD_VALUE; |
| 2533 | } |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2534 | ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2535 | *num_patches, patches, mAudioPatches.size()); |
| 2536 | if (patches == NULL) { |
| 2537 | *num_patches = 0; |
| 2538 | } |
| 2539 | |
| 2540 | size_t patchesWritten = 0; |
| 2541 | size_t patchesMax = *num_patches; |
| 2542 | for (size_t i = 0; |
| 2543 | i < mAudioPatches.size() && patchesWritten < patchesMax; i++) { |
| 2544 | patches[patchesWritten] = mAudioPatches[i]->mPatch; |
| 2545 | patches[patchesWritten++].id = mAudioPatches[i]->mHandle; |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2546 | ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2547 | i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks); |
| 2548 | } |
| 2549 | *num_patches = mAudioPatches.size(); |
| 2550 | |
| 2551 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2552 | ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2553 | return NO_ERROR; |
| 2554 | } |
| 2555 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2556 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2557 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2558 | ALOGV("setAudioPortConfig()"); |
| 2559 | |
| 2560 | if (config == NULL) { |
| 2561 | return BAD_VALUE; |
| 2562 | } |
| 2563 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2564 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2565 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2566 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2569 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2570 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2571 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2572 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2573 | if (outputDesc == NULL) { |
| 2574 | return BAD_VALUE; |
| 2575 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2576 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2577 | "setAudioPortConfig() called on duplicated output %d", |
| 2578 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2579 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2580 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2581 | sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2582 | if (inputDesc == NULL) { |
| 2583 | return BAD_VALUE; |
| 2584 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2585 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2586 | } else { |
| 2587 | return BAD_VALUE; |
| 2588 | } |
| 2589 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2590 | sp<DeviceDescriptor> deviceDesc; |
| 2591 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2592 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2593 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2594 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2595 | } else { |
| 2596 | return BAD_VALUE; |
| 2597 | } |
| 2598 | if (deviceDesc == NULL) { |
| 2599 | return BAD_VALUE; |
| 2600 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2601 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2602 | } else { |
| 2603 | return BAD_VALUE; |
| 2604 | } |
| 2605 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2606 | struct audio_port_config backupConfig; |
| 2607 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2608 | if (status == NO_ERROR) { |
| 2609 | struct audio_port_config newConfig; |
| 2610 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2611 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2612 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2613 | if (status != NO_ERROR) { |
| 2614 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2615 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2616 | |
| 2617 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2621 | { |
| 2622 | for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++) { |
| 2623 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2624 | if (patchDesc->mUid == uid) { |
| 2625 | // releaseAudioPatch() removes the patch from mAudioPatches |
| 2626 | if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) { |
| 2627 | i--; |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2633 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2634 | audio_io_handle_t *ioHandle, |
| 2635 | audio_devices_t *device) |
| 2636 | { |
| 2637 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); |
| 2638 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); |
| 2639 | *device = getDeviceForInputSource(AUDIO_SOURCE_HOTWORD); |
| 2640 | |
| 2641 | mSoundTriggerSessions.add(*session, *ioHandle); |
| 2642 | |
| 2643 | return NO_ERROR; |
| 2644 | } |
| 2645 | |
| 2646 | status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session) |
| 2647 | { |
| 2648 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2649 | if (index < 0) { |
| 2650 | ALOGW("acquireSoundTriggerSession() session %d not registered", session); |
| 2651 | return BAD_VALUE; |
| 2652 | } |
| 2653 | |
| 2654 | mSoundTriggerSessions.removeItem(session); |
| 2655 | return NO_ERROR; |
| 2656 | } |
| 2657 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2658 | status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle, |
| 2659 | const sp<AudioPatch>& patch) |
| 2660 | { |
| 2661 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2662 | |
| 2663 | if (index >= 0) { |
| 2664 | ALOGW("addAudioPatch() patch %d already in", handle); |
| 2665 | return ALREADY_EXISTS; |
| 2666 | } |
| 2667 | mAudioPatches.add(handle, patch); |
| 2668 | ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d" |
| 2669 | "sink handle %d", |
| 2670 | handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks, |
| 2671 | patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id); |
| 2672 | return NO_ERROR; |
| 2673 | } |
| 2674 | |
| 2675 | status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle) |
| 2676 | { |
| 2677 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2678 | |
| 2679 | if (index < 0) { |
| 2680 | ALOGW("removeAudioPatch() patch %d not in", handle); |
| 2681 | return ALREADY_EXISTS; |
| 2682 | } |
| 2683 | ALOGV("removeAudioPatch() handle %d af handle %d", handle, |
| 2684 | mAudioPatches.valueAt(index)->mAfPatchHandle); |
| 2685 | mAudioPatches.removeItemsAt(index); |
| 2686 | return NO_ERROR; |
| 2687 | } |
| 2688 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2689 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2690 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2691 | // ---------------------------------------------------------------------------- |
| 2692 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2693 | uint32_t AudioPolicyManager::nextUniqueId() |
| 2694 | { |
| 2695 | return android_atomic_inc(&mNextUniqueId); |
| 2696 | } |
| 2697 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2698 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 2699 | { |
| 2700 | return android_atomic_inc(&mAudioPortGeneration); |
| 2701 | } |
| 2702 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2703 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2704 | : |
| 2705 | #ifdef AUDIO_POLICY_TEST |
| 2706 | Thread(false), |
| 2707 | #endif //AUDIO_POLICY_TEST |
| 2708 | mPrimaryOutput((audio_io_handle_t)0), |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2709 | mPhoneState(AUDIO_MODE_NORMAL), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2710 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
| 2711 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2712 | mA2dpSuspended(false), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2713 | mSpeakerDrcEnabled(false), mNextUniqueId(1), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2714 | mAudioPortGeneration(1), |
| 2715 | mBeaconMuteRefCount(0), |
| 2716 | mBeaconPlayingRefCount(0), |
| 2717 | mBeaconMuted(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2718 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2719 | mUidCached = getuid(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2720 | mpClientInterface = clientInterface; |
| 2721 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2722 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { |
| 2723 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2726 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2727 | if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { |
| 2728 | if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { |
| 2729 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 2730 | defaultAudioPolicyConfig(); |
| 2731 | } |
| 2732 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2733 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2734 | |
| 2735 | // must be done after reading the policy |
| 2736 | initializeVolumeCurves(); |
| 2737 | |
| 2738 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2739 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 2740 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2741 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2742 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); |
| 2743 | if (mHwModules[i]->mHandle == 0) { |
| 2744 | ALOGW("could not open HW module %s", mHwModules[i]->mName); |
| 2745 | continue; |
| 2746 | } |
| 2747 | // open all output streams needed to access attached devices |
| 2748 | // except for direct output streams that are only opened when they are actually |
| 2749 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2750 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2751 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2752 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2753 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2754 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2755 | if (outProfile->mSupportedDevices.isEmpty()) { |
| 2756 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); |
| 2757 | continue; |
| 2758 | } |
| 2759 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2760 | if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 2761 | continue; |
| 2762 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2763 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); |
| 2764 | if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) { |
| 2765 | profileType = mDefaultOutputDevice->mDeviceType; |
| 2766 | } else { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2767 | // chose first device present in mSupportedDevices also part of |
| 2768 | // outputDeviceTypes |
| 2769 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2770 | profileType = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2771 | if ((profileType & outputDeviceTypes) != 0) { |
| 2772 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2773 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2774 | } |
| 2775 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2776 | if ((profileType & outputDeviceTypes) == 0) { |
| 2777 | continue; |
| 2778 | } |
| 2779 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); |
| 2780 | |
| 2781 | outputDesc->mDevice = profileType; |
| 2782 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2783 | config.sample_rate = outputDesc->mSamplingRate; |
| 2784 | config.channel_mask = outputDesc->mChannelMask; |
| 2785 | config.format = outputDesc->mFormat; |
| 2786 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2787 | status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, |
| 2788 | &output, |
| 2789 | &config, |
| 2790 | &outputDesc->mDevice, |
| 2791 | String8(""), |
| 2792 | &outputDesc->mLatency, |
| 2793 | outputDesc->mFlags); |
| 2794 | |
| 2795 | if (status != NO_ERROR) { |
| 2796 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 2797 | outputDesc->mDevice, |
| 2798 | mHwModules[i]->mName); |
| 2799 | } else { |
| 2800 | outputDesc->mSamplingRate = config.sample_rate; |
| 2801 | outputDesc->mChannelMask = config.channel_mask; |
| 2802 | outputDesc->mFormat = config.format; |
| 2803 | |
| 2804 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2805 | audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2806 | ssize_t index = |
| 2807 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); |
| 2808 | // give a valid ID to an attached device once confirmed it is reachable |
| 2809 | if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) { |
| 2810 | mAvailableOutputDevices[index]->mId = nextUniqueId(); |
| 2811 | mAvailableOutputDevices[index]->mModule = mHwModules[i]; |
| 2812 | } |
| 2813 | } |
| 2814 | if (mPrimaryOutput == 0 && |
| 2815 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 2816 | mPrimaryOutput = output; |
| 2817 | } |
| 2818 | addOutput(output, outputDesc); |
| 2819 | setOutputDevice(output, |
| 2820 | outputDesc->mDevice, |
| 2821 | true); |
| 2822 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2823 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2824 | // open input streams needed to access attached devices to validate |
| 2825 | // mAvailableInputDevices list |
| 2826 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 2827 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2828 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2829 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2830 | if (inProfile->mSupportedDevices.isEmpty()) { |
| 2831 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); |
| 2832 | continue; |
| 2833 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2834 | // chose first device present in mSupportedDevices also part of |
| 2835 | // inputDeviceTypes |
| 2836 | audio_devices_t profileType = AUDIO_DEVICE_NONE; |
| 2837 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 2838 | profileType = inProfile->mSupportedDevices[k]->mDeviceType; |
| 2839 | if (profileType & inputDeviceTypes) { |
| 2840 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2841 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2842 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2843 | if ((profileType & inputDeviceTypes) == 0) { |
| 2844 | continue; |
| 2845 | } |
| 2846 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); |
| 2847 | |
| 2848 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; |
| 2849 | inputDesc->mDevice = profileType; |
| 2850 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2851 | // find the address |
| 2852 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 2853 | // the inputs vector must be of size 1, but we don't want to crash here |
| 2854 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 2855 | : String8(""); |
| 2856 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 2857 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 2858 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2859 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2860 | config.sample_rate = inputDesc->mSamplingRate; |
| 2861 | config.channel_mask = inputDesc->mChannelMask; |
| 2862 | config.format = inputDesc->mFormat; |
| 2863 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 2864 | status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, |
| 2865 | &input, |
| 2866 | &config, |
| 2867 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2868 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2869 | AUDIO_SOURCE_MIC, |
| 2870 | AUDIO_INPUT_FLAG_NONE); |
| 2871 | |
| 2872 | if (status == NO_ERROR) { |
| 2873 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 2874 | audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; |
| 2875 | ssize_t index = |
| 2876 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); |
| 2877 | // give a valid ID to an attached device once confirmed it is reachable |
| 2878 | if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) { |
| 2879 | mAvailableInputDevices[index]->mId = nextUniqueId(); |
| 2880 | mAvailableInputDevices[index]->mModule = mHwModules[i]; |
| 2881 | } |
| 2882 | } |
| 2883 | mpClientInterface->closeInput(input); |
| 2884 | } else { |
| 2885 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 2886 | inputDesc->mDevice, |
| 2887 | mHwModules[i]->mName); |
| 2888 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | // make sure all attached devices have been allocated a unique ID |
| 2892 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
| 2893 | if (mAvailableOutputDevices[i]->mId == 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2894 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2895 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 2896 | continue; |
| 2897 | } |
| 2898 | i++; |
| 2899 | } |
| 2900 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
| 2901 | if (mAvailableInputDevices[i]->mId == 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2902 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2903 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 2904 | continue; |
| 2905 | } |
| 2906 | i++; |
| 2907 | } |
| 2908 | // make sure default device is reachable |
| 2909 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2910 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2911 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2912 | |
| 2913 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 2914 | |
| 2915 | updateDevicesAndOutputs(); |
| 2916 | |
| 2917 | #ifdef AUDIO_POLICY_TEST |
| 2918 | if (mPrimaryOutput != 0) { |
| 2919 | AudioParameter outputCmd = AudioParameter(); |
| 2920 | outputCmd.addInt(String8("set_id"), 0); |
| 2921 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 2922 | |
| 2923 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 2924 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2925 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2926 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2927 | mTestLatencyMs = 0; |
| 2928 | mCurOutput = 0; |
| 2929 | mDirectOutput = false; |
| 2930 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 2931 | mTestOutputs[i] = 0; |
| 2932 | } |
| 2933 | |
| 2934 | const size_t SIZE = 256; |
| 2935 | char buffer[SIZE]; |
| 2936 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 2937 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 2938 | } |
| 2939 | #endif //AUDIO_POLICY_TEST |
| 2940 | } |
| 2941 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2942 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2943 | { |
| 2944 | #ifdef AUDIO_POLICY_TEST |
| 2945 | exit(); |
| 2946 | #endif //AUDIO_POLICY_TEST |
| 2947 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2948 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2949 | } |
| 2950 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2951 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2952 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2953 | mAvailableOutputDevices.clear(); |
| 2954 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2955 | mOutputs.clear(); |
| 2956 | mInputs.clear(); |
| 2957 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2960 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2961 | { |
| 2962 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; |
| 2963 | } |
| 2964 | |
| 2965 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2966 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2967 | { |
| 2968 | ALOGV("entering threadLoop()"); |
| 2969 | while (!exitPending()) |
| 2970 | { |
| 2971 | String8 command; |
| 2972 | int valueInt; |
| 2973 | String8 value; |
| 2974 | |
| 2975 | Mutex::Autolock _l(mLock); |
| 2976 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 2977 | |
| 2978 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 2979 | AudioParameter param = AudioParameter(command); |
| 2980 | |
| 2981 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 2982 | valueInt != 0) { |
| 2983 | ALOGV("Test command %s received", command.string()); |
| 2984 | String8 target; |
| 2985 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 2986 | target = "Manager"; |
| 2987 | } |
| 2988 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 2989 | param.remove(String8("test_cmd_policy_output")); |
| 2990 | mCurOutput = valueInt; |
| 2991 | } |
| 2992 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 2993 | param.remove(String8("test_cmd_policy_direct")); |
| 2994 | if (value == "false") { |
| 2995 | mDirectOutput = false; |
| 2996 | } else if (value == "true") { |
| 2997 | mDirectOutput = true; |
| 2998 | } |
| 2999 | } |
| 3000 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3001 | param.remove(String8("test_cmd_policy_input")); |
| 3002 | mTestInput = valueInt; |
| 3003 | } |
| 3004 | |
| 3005 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3006 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3007 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3008 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3009 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3010 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3011 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3012 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3013 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3014 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3015 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3016 | if (target == "Manager") { |
| 3017 | mTestFormat = format; |
| 3018 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3019 | AudioParameter outputParam = AudioParameter(); |
| 3020 | outputParam.addInt(String8("format"), format); |
| 3021 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3026 | param.remove(String8("test_cmd_policy_channels")); |
| 3027 | int channels = 0; |
| 3028 | |
| 3029 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3030 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3031 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3032 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3033 | } |
| 3034 | if (channels != 0) { |
| 3035 | if (target == "Manager") { |
| 3036 | mTestChannels = channels; |
| 3037 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3038 | AudioParameter outputParam = AudioParameter(); |
| 3039 | outputParam.addInt(String8("channels"), channels); |
| 3040 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3041 | } |
| 3042 | } |
| 3043 | } |
| 3044 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3045 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3046 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3047 | int samplingRate = valueInt; |
| 3048 | if (target == "Manager") { |
| 3049 | mTestSamplingRate = samplingRate; |
| 3050 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3051 | AudioParameter outputParam = AudioParameter(); |
| 3052 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3053 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3059 | param.remove(String8("test_cmd_policy_reopen")); |
| 3060 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3061 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3062 | mpClientInterface->closeOutput(mPrimaryOutput); |
| 3063 | |
| 3064 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; |
| 3065 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3066 | mOutputs.removeItem(mPrimaryOutput); |
| 3067 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3068 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3069 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3070 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3071 | config.sample_rate = outputDesc->mSamplingRate; |
| 3072 | config.channel_mask = outputDesc->mChannelMask; |
| 3073 | config.format = outputDesc->mFormat; |
| 3074 | status_t status = mpClientInterface->openOutput(moduleHandle, |
| 3075 | &mPrimaryOutput, |
| 3076 | &config, |
| 3077 | &outputDesc->mDevice, |
| 3078 | String8(""), |
| 3079 | &outputDesc->mLatency, |
| 3080 | outputDesc->mFlags); |
| 3081 | if (status != NO_ERROR) { |
| 3082 | ALOGE("Failed to reopen hardware output stream, " |
| 3083 | "samplingRate: %d, format %d, channels %d", |
| 3084 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3085 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3086 | outputDesc->mSamplingRate = config.sample_rate; |
| 3087 | outputDesc->mChannelMask = config.channel_mask; |
| 3088 | outputDesc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3089 | AudioParameter outputCmd = AudioParameter(); |
| 3090 | outputCmd.addInt(String8("set_id"), 0); |
| 3091 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 3092 | addOutput(mPrimaryOutput, outputDesc); |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3098 | } |
| 3099 | } |
| 3100 | return false; |
| 3101 | } |
| 3102 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3103 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3104 | { |
| 3105 | { |
| 3106 | AutoMutex _l(mLock); |
| 3107 | requestExit(); |
| 3108 | mWaitWorkCV.signal(); |
| 3109 | } |
| 3110 | requestExitAndWait(); |
| 3111 | } |
| 3112 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3113 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3114 | { |
| 3115 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3116 | if (output == mTestOutputs[i]) return i; |
| 3117 | } |
| 3118 | return 0; |
| 3119 | } |
| 3120 | #endif //AUDIO_POLICY_TEST |
| 3121 | |
| 3122 | // --- |
| 3123 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3124 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3125 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3126 | outputDesc->mIoHandle = output; |
| 3127 | outputDesc->mId = nextUniqueId(); |
| 3128 | mOutputs.add(output, outputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3129 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3130 | } |
| 3131 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3132 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3133 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3134 | inputDesc->mIoHandle = input; |
| 3135 | inputDesc->mId = nextUniqueId(); |
| 3136 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3137 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3138 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3139 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3140 | void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/, |
| 3141 | const String8 address /*in*/, |
| 3142 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
| 3143 | // look for a match on the given address on the addresses of the outputs: |
| 3144 | // find the address by finding the patch that maps to this output |
| 3145 | ssize_t patchIdx = mAudioPatches.indexOfKey(desc->mPatchHandle); |
| 3146 | //ALOGV(" inspecting output %d (patch %d) for supported device=0x%x", |
| 3147 | // outputIdx, patchIdx, desc->mProfile->mSupportedDevices.types()); |
| 3148 | if (patchIdx >= 0) { |
| 3149 | const sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patchIdx); |
| 3150 | const int numSinks = patchDesc->mPatch.num_sinks; |
| 3151 | for (ssize_t j=0; j < numSinks; j++) { |
| 3152 | if (patchDesc->mPatch.sinks[j].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3153 | const char* patchAddr = |
| 3154 | patchDesc->mPatch.sinks[j].ext.device.address; |
| 3155 | if (strncmp(patchAddr, |
| 3156 | address.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3157 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3158 | desc->mIoHandle, patchDesc->mPatch.sinks[j].ext.device.address); |
| 3159 | outputs.add(desc->mIoHandle); |
| 3160 | break; |
| 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | } |
| 3166 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3167 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3168 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3169 | SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3170 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3171 | { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3172 | audio_devices_t device = devDesc->mDeviceType; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3173 | sp<AudioOutputDescriptor> desc; |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3174 | // erase all current sample rates, formats and channel masks |
| 3175 | devDesc->clearCapabilities(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3176 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3177 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3178 | // first list already open outputs that can be routed to this device |
| 3179 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3180 | desc = mOutputs.valueAt(i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3181 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3182 | if (!deviceDistinguishesOnAddress(device)) { |
| 3183 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3184 | outputs.add(mOutputs.keyAt(i)); |
| 3185 | } else { |
| 3186 | ALOGV(" checking address match due to device 0x%x", device); |
| 3187 | findIoHandlesByAddress(desc, address, outputs); |
| 3188 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3189 | } |
| 3190 | } |
| 3191 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3192 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3193 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3194 | { |
| 3195 | if (mHwModules[i]->mHandle == 0) { |
| 3196 | continue; |
| 3197 | } |
| 3198 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3199 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3200 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3201 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3202 | profiles.add(mHwModules[i]->mOutputProfiles[j]); |
| 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3207 | ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size()); |
| 3208 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3209 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3210 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3211 | return BAD_VALUE; |
| 3212 | } |
| 3213 | |
| 3214 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3215 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3216 | 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] | 3217 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3218 | |
| 3219 | // nothing to do if one output is already opened for this profile |
| 3220 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3221 | for (j = 0; j < outputs.size(); j++) { |
| 3222 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3223 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3224 | // matching profile: save the sample rates, format and channel masks supported |
| 3225 | // by the profile in our device descriptor |
| 3226 | devDesc->importAudioPort(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3227 | break; |
| 3228 | } |
| 3229 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3230 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3231 | continue; |
| 3232 | } |
| 3233 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3234 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3235 | device, address.string(), profile.get()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3236 | desc = new AudioOutputDescriptor(profile); |
| 3237 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3238 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3239 | config.sample_rate = desc->mSamplingRate; |
| 3240 | config.channel_mask = desc->mChannelMask; |
| 3241 | config.format = desc->mFormat; |
| 3242 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3243 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3244 | config.offload_info.format = desc->mFormat; |
| 3245 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3246 | status_t status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3247 | &output, |
| 3248 | &config, |
| 3249 | &desc->mDevice, |
| 3250 | address, |
| 3251 | &desc->mLatency, |
| 3252 | desc->mFlags); |
| 3253 | if (status == NO_ERROR) { |
| 3254 | desc->mSamplingRate = config.sample_rate; |
| 3255 | desc->mChannelMask = config.channel_mask; |
| 3256 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3257 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3258 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3259 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3260 | char *param = audio_device_address_to_parameter(device, address); |
| 3261 | mpClientInterface->setParameters(output, String8(param)); |
| 3262 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3263 | } |
| 3264 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3265 | // Here is where we step through and resolve any "dynamic" fields |
| 3266 | String8 reply; |
| 3267 | char *value; |
| 3268 | if (profile->mSamplingRates[0] == 0) { |
| 3269 | reply = mpClientInterface->getParameters(output, |
| 3270 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3271 | ALOGV("checkOutputsForDevice() supported sampling rates %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3272 | reply.string()); |
| 3273 | value = strpbrk((char *)reply.string(), "="); |
| 3274 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3275 | profile->loadSamplingRates(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3276 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3277 | } |
| 3278 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3279 | reply = mpClientInterface->getParameters(output, |
| 3280 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3281 | ALOGV("checkOutputsForDevice() supported formats %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3282 | reply.string()); |
| 3283 | value = strpbrk((char *)reply.string(), "="); |
| 3284 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3285 | profile->loadFormats(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3286 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3287 | } |
| 3288 | if (profile->mChannelMasks[0] == 0) { |
| 3289 | reply = mpClientInterface->getParameters(output, |
| 3290 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3291 | ALOGV("checkOutputsForDevice() supported channel masks %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3292 | reply.string()); |
| 3293 | value = strpbrk((char *)reply.string(), "="); |
| 3294 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3295 | profile->loadOutChannels(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3296 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3297 | } |
| 3298 | if (((profile->mSamplingRates[0] == 0) && |
| 3299 | (profile->mSamplingRates.size() < 2)) || |
| 3300 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && |
| 3301 | (profile->mFormats.size() < 2)) || |
| 3302 | ((profile->mChannelMasks[0] == 0) && |
| 3303 | (profile->mChannelMasks.size() < 2))) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3304 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3305 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3306 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3307 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || |
| 3308 | profile->mChannelMasks[0] == 0) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3309 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3310 | config.sample_rate = profile->pickSamplingRate(); |
| 3311 | config.channel_mask = profile->pickChannelMask(); |
| 3312 | config.format = profile->pickFormat(); |
| 3313 | config.offload_info.sample_rate = config.sample_rate; |
| 3314 | config.offload_info.channel_mask = config.channel_mask; |
| 3315 | config.offload_info.format = config.format; |
| 3316 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3317 | &output, |
| 3318 | &config, |
| 3319 | &desc->mDevice, |
| 3320 | address, |
| 3321 | &desc->mLatency, |
| 3322 | desc->mFlags); |
| 3323 | if (status == NO_ERROR) { |
| 3324 | desc->mSamplingRate = config.sample_rate; |
| 3325 | desc->mChannelMask = config.channel_mask; |
| 3326 | desc->mFormat = config.format; |
| 3327 | } else { |
| 3328 | output = AUDIO_IO_HANDLE_NONE; |
| 3329 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3332 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3333 | addOutput(output, desc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3334 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3335 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3336 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3337 | // set initial stream volume for device |
| 3338 | applyStreamVolumes(output, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3339 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3340 | //TODO: configure audio effect output stage here |
| 3341 | |
| 3342 | // open a duplicating output thread for the new output and the primary output |
| 3343 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, |
| 3344 | mPrimaryOutput); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3345 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3346 | // add duplicated output descriptor |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3347 | sp<AudioOutputDescriptor> dupOutputDesc = |
| 3348 | new AudioOutputDescriptor(NULL); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3349 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); |
| 3350 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); |
| 3351 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3352 | dupOutputDesc->mFormat = desc->mFormat; |
| 3353 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3354 | dupOutputDesc->mLatency = desc->mLatency; |
| 3355 | addOutput(duplicatedOutput, dupOutputDesc); |
| 3356 | applyStreamVolumes(duplicatedOutput, device, 0, true); |
| 3357 | } else { |
| 3358 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 3359 | mPrimaryOutput, output); |
| 3360 | mpClientInterface->closeOutput(output); |
| 3361 | mOutputs.removeItem(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3362 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3363 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3364 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3365 | } |
| 3366 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3367 | } else { |
| 3368 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3369 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3370 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3371 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3372 | profiles.removeAt(profile_index); |
| 3373 | profile_index--; |
| 3374 | } else { |
| 3375 | outputs.add(output); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3376 | devDesc->importAudioPort(profile); |
| 3377 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3378 | if (deviceDistinguishesOnAddress(device)) { |
| 3379 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3380 | device, address.string()); |
| 3381 | setOutputDevice(output, device, true/*force*/, 0/*delay*/, |
| 3382 | NULL/*patch handle*/, address.string()); |
| 3383 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3384 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | if (profiles.isEmpty()) { |
| 3389 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3390 | return BAD_VALUE; |
| 3391 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3392 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3393 | // check if one opened output is not needed any more after disconnecting one device |
| 3394 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3395 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3396 | if (!desc->isDuplicated()) { |
| 3397 | if (!(desc->mProfile->mSupportedDevices.types() |
| 3398 | & mAvailableOutputDevices.types())) { |
| 3399 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3400 | mOutputs.keyAt(i)); |
| 3401 | outputs.add(mOutputs.keyAt(i)); |
| 3402 | } else if (deviceDistinguishesOnAddress(device) && |
| 3403 | // exact match on device |
| 3404 | (desc->mProfile->mSupportedDevices.types() == device)) { |
| 3405 | findIoHandlesByAddress(desc, address, outputs); |
| 3406 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3407 | } |
| 3408 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3409 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3410 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3411 | { |
| 3412 | if (mHwModules[i]->mHandle == 0) { |
| 3413 | continue; |
| 3414 | } |
| 3415 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3416 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3417 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3418 | if (profile->mSupportedDevices.types() & device) { |
| 3419 | ALOGV("checkOutputsForDevice(): " |
| 3420 | "clearing direct output profile %zu on module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3421 | if (profile->mSamplingRates[0] == 0) { |
| 3422 | profile->mSamplingRates.clear(); |
| 3423 | profile->mSamplingRates.add(0); |
| 3424 | } |
| 3425 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3426 | profile->mFormats.clear(); |
| 3427 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3428 | } |
| 3429 | if (profile->mChannelMasks[0] == 0) { |
| 3430 | profile->mChannelMasks.clear(); |
| 3431 | profile->mChannelMasks.add(0); |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | return NO_ERROR; |
| 3438 | } |
| 3439 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3440 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, |
| 3441 | audio_policy_dev_state_t state, |
| 3442 | SortedVector<audio_io_handle_t>& inputs, |
| 3443 | const String8 address) |
| 3444 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3445 | sp<AudioInputDescriptor> desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3446 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3447 | // first list already open inputs that can be routed to this device |
| 3448 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3449 | desc = mInputs.valueAt(input_index); |
| 3450 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3451 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3452 | inputs.add(mInputs.keyAt(input_index)); |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3457 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3458 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3459 | { |
| 3460 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3461 | continue; |
| 3462 | } |
| 3463 | for (size_t profile_index = 0; |
| 3464 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3465 | profile_index++) |
| 3466 | { |
| 3467 | if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types() |
| 3468 | & (device & ~AUDIO_DEVICE_BIT_IN)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3469 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3470 | profile_index, module_idx); |
| 3471 | profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]); |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3477 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3478 | return BAD_VALUE; |
| 3479 | } |
| 3480 | |
| 3481 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3482 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3483 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3484 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3485 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3486 | // nothing to do if one input is already opened for this profile |
| 3487 | size_t input_index; |
| 3488 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3489 | desc = mInputs.valueAt(input_index); |
| 3490 | if (desc->mProfile == profile) { |
| 3491 | break; |
| 3492 | } |
| 3493 | } |
| 3494 | if (input_index != mInputs.size()) { |
| 3495 | continue; |
| 3496 | } |
| 3497 | |
| 3498 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3499 | desc = new AudioInputDescriptor(profile); |
| 3500 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3501 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3502 | config.sample_rate = desc->mSamplingRate; |
| 3503 | config.channel_mask = desc->mChannelMask; |
| 3504 | config.format = desc->mFormat; |
| 3505 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 3506 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
| 3507 | &input, |
| 3508 | &config, |
| 3509 | &desc->mDevice, |
| 3510 | address, |
| 3511 | AUDIO_SOURCE_MIC, |
| 3512 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3513 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3514 | if (status == NO_ERROR) { |
| 3515 | desc->mSamplingRate = config.sample_rate; |
| 3516 | desc->mChannelMask = config.channel_mask; |
| 3517 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3518 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3519 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3520 | char *param = audio_device_address_to_parameter(device, address); |
| 3521 | mpClientInterface->setParameters(input, String8(param)); |
| 3522 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | // Here is where we step through and resolve any "dynamic" fields |
| 3526 | String8 reply; |
| 3527 | char *value; |
| 3528 | if (profile->mSamplingRates[0] == 0) { |
| 3529 | reply = mpClientInterface->getParameters(input, |
| 3530 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 3531 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", |
| 3532 | reply.string()); |
| 3533 | value = strpbrk((char *)reply.string(), "="); |
| 3534 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3535 | profile->loadSamplingRates(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3539 | reply = mpClientInterface->getParameters(input, |
| 3540 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 3541 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); |
| 3542 | value = strpbrk((char *)reply.string(), "="); |
| 3543 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3544 | profile->loadFormats(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | if (profile->mChannelMasks[0] == 0) { |
| 3548 | reply = mpClientInterface->getParameters(input, |
| 3549 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 3550 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", |
| 3551 | reply.string()); |
| 3552 | value = strpbrk((char *)reply.string(), "="); |
| 3553 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3554 | profile->loadInChannels(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3555 | } |
| 3556 | } |
| 3557 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || |
| 3558 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || |
| 3559 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { |
| 3560 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3561 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3562 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | if (input != 0) { |
| 3566 | addInput(input, desc); |
| 3567 | } |
| 3568 | } // endif input != 0 |
| 3569 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3570 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3571 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3572 | profiles.removeAt(profile_index); |
| 3573 | profile_index--; |
| 3574 | } else { |
| 3575 | inputs.add(input); |
| 3576 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3577 | } |
| 3578 | } // end scan profiles |
| 3579 | |
| 3580 | if (profiles.isEmpty()) { |
| 3581 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3582 | return BAD_VALUE; |
| 3583 | } |
| 3584 | } else { |
| 3585 | // Disconnect |
| 3586 | // check if one opened input is not needed any more after disconnecting one device |
| 3587 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3588 | desc = mInputs.valueAt(input_index); |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3589 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() & |
| 3590 | ~AUDIO_DEVICE_BIT_IN)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3591 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3592 | mInputs.keyAt(input_index)); |
| 3593 | inputs.add(mInputs.keyAt(input_index)); |
| 3594 | } |
| 3595 | } |
| 3596 | // Clear any profiles associated with the disconnected device. |
| 3597 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3598 | if (mHwModules[module_index]->mHandle == 0) { |
| 3599 | continue; |
| 3600 | } |
| 3601 | for (size_t profile_index = 0; |
| 3602 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3603 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3604 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3605 | if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3606 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3607 | profile_index, module_index); |
| 3608 | if (profile->mSamplingRates[0] == 0) { |
| 3609 | profile->mSamplingRates.clear(); |
| 3610 | profile->mSamplingRates.add(0); |
| 3611 | } |
| 3612 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3613 | profile->mFormats.clear(); |
| 3614 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3615 | } |
| 3616 | if (profile->mChannelMasks[0] == 0) { |
| 3617 | profile->mChannelMasks.clear(); |
| 3618 | profile->mChannelMasks.add(0); |
| 3619 | } |
| 3620 | } |
| 3621 | } |
| 3622 | } |
| 3623 | } // end disconnect |
| 3624 | |
| 3625 | return NO_ERROR; |
| 3626 | } |
| 3627 | |
| 3628 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3629 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3630 | { |
| 3631 | ALOGV("closeOutput(%d)", output); |
| 3632 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3633 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3634 | if (outputDesc == NULL) { |
| 3635 | ALOGW("closeOutput() unknown output %d", output); |
| 3636 | return; |
| 3637 | } |
| 3638 | |
| 3639 | // look for duplicated outputs connected to the output being removed. |
| 3640 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3641 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3642 | if (dupOutputDesc->isDuplicated() && |
| 3643 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3644 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3645 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 3647 | outputDesc2 = dupOutputDesc->mOutput2; |
| 3648 | } else { |
| 3649 | outputDesc2 = dupOutputDesc->mOutput1; |
| 3650 | } |
| 3651 | // As all active tracks on duplicated output will be deleted, |
| 3652 | // and as they were also referenced on the other output, the reference |
| 3653 | // count for their stream type must be adjusted accordingly on |
| 3654 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3655 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3656 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3657 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3658 | } |
| 3659 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 3660 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 3661 | |
| 3662 | mpClientInterface->closeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3663 | mOutputs.removeItem(duplicatedOutput); |
| 3664 | } |
| 3665 | } |
| 3666 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3667 | nextAudioPortGeneration(); |
| 3668 | |
| 3669 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3670 | if (index >= 0) { |
| 3671 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3672 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3673 | mAudioPatches.removeItemsAt(index); |
| 3674 | mpClientInterface->onAudioPatchListUpdate(); |
| 3675 | } |
| 3676 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3677 | AudioParameter param; |
| 3678 | param.add(String8("closing"), String8("true")); |
| 3679 | mpClientInterface->setParameters(output, param.toString()); |
| 3680 | |
| 3681 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3682 | mOutputs.removeItem(output); |
| 3683 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 3687 | { |
| 3688 | ALOGV("closeInput(%d)", input); |
| 3689 | |
| 3690 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 3691 | if (inputDesc == NULL) { |
| 3692 | ALOGW("closeInput() unknown input %d", input); |
| 3693 | return; |
| 3694 | } |
| 3695 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3696 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3697 | |
| 3698 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3699 | if (index >= 0) { |
| 3700 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3701 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3702 | mAudioPatches.removeItemsAt(index); |
| 3703 | mpClientInterface->onAudioPatchListUpdate(); |
| 3704 | } |
| 3705 | |
| 3706 | mpClientInterface->closeInput(input); |
| 3707 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3710 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3711 | DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3712 | { |
| 3713 | SortedVector<audio_io_handle_t> outputs; |
| 3714 | |
| 3715 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 3716 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 3717 | ALOGVV("output %d isDuplicated=%d device=%04x", |
| 3718 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); |
| 3719 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 3720 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 3721 | outputs.add(openOutputs.keyAt(i)); |
| 3722 | } |
| 3723 | } |
| 3724 | return outputs; |
| 3725 | } |
| 3726 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3727 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3728 | SortedVector<audio_io_handle_t>& outputs2) |
| 3729 | { |
| 3730 | if (outputs1.size() != outputs2.size()) { |
| 3731 | return false; |
| 3732 | } |
| 3733 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 3734 | if (outputs1[i] != outputs2[i]) { |
| 3735 | return false; |
| 3736 | } |
| 3737 | } |
| 3738 | return true; |
| 3739 | } |
| 3740 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3741 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3742 | { |
| 3743 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3744 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3745 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 3746 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 3747 | |
| 3748 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 3749 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 3750 | strategy, srcOutputs[0], dstOutputs[0]); |
| 3751 | // mute strategy while moving tracks from one output to another |
| 3752 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3753 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3754 | if (desc->isStrategyActive(strategy)) { |
| 3755 | setStrategyMute(strategy, true, srcOutputs[i]); |
| 3756 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | // Move effects associated to this strategy from previous output to new output |
| 3761 | if (strategy == STRATEGY_MEDIA) { |
| 3762 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 3763 | SortedVector<audio_io_handle_t> moved; |
| 3764 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3765 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 3766 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 3767 | effectDesc->mIo != fxOutput) { |
| 3768 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3769 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 3770 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3771 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3772 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3773 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3774 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3775 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3776 | } |
| 3777 | } |
| 3778 | } |
| 3779 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3780 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3781 | if (i == AUDIO_STREAM_PATCH) { |
| 3782 | continue; |
| 3783 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3784 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 3785 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3786 | } |
| 3787 | } |
| 3788 | } |
| 3789 | } |
| 3790 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3791 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3792 | { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3793 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3794 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3795 | checkOutputForStrategy(STRATEGY_PHONE); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3796 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3797 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3798 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 3799 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3800 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3801 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 3802 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3803 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3806 | audio_io_handle_t AudioPolicyManager::getA2dpOutput() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3807 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3809 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3810 | if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 3811 | return mOutputs.keyAt(i); |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | return 0; |
| 3816 | } |
| 3817 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3818 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3820 | audio_io_handle_t a2dpOutput = getA2dpOutput(); |
| 3821 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3822 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3823 | return; |
| 3824 | } |
| 3825 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3826 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3827 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 3828 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 3829 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3830 | // suspend A2DP output if: |
| 3831 | // (NOT already suspended) && |
| 3832 | // ((SCO device is connected && |
| 3833 | // (forced usage for communication || for record is SCO))) || |
| 3834 | // (phone state is ringing || in call) |
| 3835 | // |
| 3836 | // restore A2DP output if: |
| 3837 | // (Already suspended) && |
| 3838 | // ((SCO device is NOT connected || |
| 3839 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 3840 | // (phone state is NOT ringing && NOT in call) |
| 3841 | // |
| 3842 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3843 | if ((!isScoConnected || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3844 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && |
| 3845 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 3846 | ((mPhoneState != AUDIO_MODE_IN_CALL) && |
| 3847 | (mPhoneState != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3848 | |
| 3849 | mpClientInterface->restoreOutput(a2dpOutput); |
| 3850 | mA2dpSuspended = false; |
| 3851 | } |
| 3852 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3853 | if ((isScoConnected && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3854 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 3855 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 3856 | ((mPhoneState == AUDIO_MODE_IN_CALL) || |
| 3857 | (mPhoneState == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3858 | |
| 3859 | mpClientInterface->suspendOutput(a2dpOutput); |
| 3860 | mA2dpSuspended = true; |
| 3861 | } |
| 3862 | } |
| 3863 | } |
| 3864 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3865 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3866 | { |
| 3867 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 3868 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3869 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3870 | |
| 3871 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3872 | if (index >= 0) { |
| 3873 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3874 | if (patchDesc->mUid != mUidCached) { |
| 3875 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
| 3876 | outputDesc->device(), outputDesc->mPatchHandle); |
| 3877 | return outputDesc->device(); |
| 3878 | } |
| 3879 | } |
| 3880 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3881 | // check the following by order of priority to request a routing change if necessary: |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3882 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3883 | // use device for strategy enforced audible |
| 3884 | // 2: we are in call or the strategy phone is active on the output: |
| 3885 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3886 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 3887 | // use the device for strategy enforced audible |
| 3888 | // 4: the strategy sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3889 | // use device for strategy sonification |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3890 | // 5: the strategy "respectful" sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3891 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3892 | // 6: the strategy accessibility is active on the output: |
| 3893 | // use device for strategy accessibility |
| 3894 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3895 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3896 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3897 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3898 | // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output: |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3899 | // use device for strategy t-t-s |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3900 | if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) && |
| 3901 | mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3902 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 3903 | } else if (isInCall() || |
| 3904 | outputDesc->isStrategyActive(STRATEGY_PHONE)) { |
| 3905 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3906 | } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { |
| 3907 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3908 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { |
| 3909 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
| 3910 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { |
| 3911 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3912 | } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) { |
| 3913 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { |
| 3915 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
| 3916 | } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { |
| 3917 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3918 | } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
| 3919 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3920 | } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) { |
| 3921 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3924 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 3925 | return device; |
| 3926 | } |
| 3927 | |
| 3928 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
| 3929 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3930 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3931 | |
| 3932 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3933 | if (index >= 0) { |
| 3934 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3935 | if (patchDesc->mUid != mUidCached) { |
| 3936 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
| 3937 | inputDesc->mDevice, inputDesc->mPatchHandle); |
| 3938 | return inputDesc->mDevice; |
| 3939 | } |
| 3940 | } |
| 3941 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3942 | audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource); |
| 3943 | |
| 3944 | ALOGV("getNewInputDevice() selected device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3945 | return device; |
| 3946 | } |
| 3947 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3948 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3949 | return (uint32_t)getStrategy(stream); |
| 3950 | } |
| 3951 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3952 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3953 | // By checking the range of stream before calling getStrategy, we avoid |
| 3954 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 3955 | // and then return STRATEGY_MEDIA, but we want to return the empty set. |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3956 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3957 | return AUDIO_DEVICE_NONE; |
| 3958 | } |
| 3959 | audio_devices_t devices; |
| 3960 | AudioPolicyManager::routing_strategy strategy = getStrategy(stream); |
| 3961 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3962 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); |
| 3963 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3964 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3965 | if (outputDesc->isStrategyActive(strategy)) { |
| 3966 | devices = outputDesc->device(); |
| 3967 | break; |
| 3968 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3969 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 3970 | |
| 3971 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 3972 | and doesn't really need to.*/ |
| 3973 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 3974 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 3975 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 3976 | } |
| 3977 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3978 | return devices; |
| 3979 | } |
| 3980 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3981 | AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy( |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3982 | audio_stream_type_t stream) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3983 | |
| 3984 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH"); |
| 3985 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3986 | // stream to strategy mapping |
| 3987 | switch (stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3988 | case AUDIO_STREAM_VOICE_CALL: |
| 3989 | case AUDIO_STREAM_BLUETOOTH_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3990 | return STRATEGY_PHONE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3991 | case AUDIO_STREAM_RING: |
| 3992 | case AUDIO_STREAM_ALARM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3993 | return STRATEGY_SONIFICATION; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3994 | case AUDIO_STREAM_NOTIFICATION: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3995 | return STRATEGY_SONIFICATION_RESPECTFUL; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3996 | case AUDIO_STREAM_DTMF: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3997 | return STRATEGY_DTMF; |
| 3998 | default: |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 3999 | ALOGE("unknown stream type %d", stream); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4000 | case AUDIO_STREAM_SYSTEM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4001 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 4002 | // while key clicks are played produces a poor result |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4003 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4004 | return STRATEGY_MEDIA; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4005 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4007 | case AUDIO_STREAM_TTS: |
| 4008 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 4009 | case AUDIO_STREAM_ACCESSIBILITY: |
| 4010 | return STRATEGY_ACCESSIBILITY; |
| 4011 | case AUDIO_STREAM_REROUTING: |
| 4012 | return STRATEGY_REROUTING; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4016 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4017 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4018 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4019 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4020 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4021 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4022 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4023 | } |
| 4024 | |
| 4025 | // usage to strategy mapping |
| 4026 | switch (attr->usage) { |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4027 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 4028 | if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) { |
| 4029 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4030 | } |
| 4031 | if (isInCall()) { |
| 4032 | return (uint32_t) STRATEGY_PHONE; |
| 4033 | } |
| 4034 | // FALL THROUGH |
| 4035 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4036 | case AUDIO_USAGE_MEDIA: |
| 4037 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4038 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 4039 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 4040 | return (uint32_t) STRATEGY_MEDIA; |
| 4041 | |
| 4042 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 4043 | return (uint32_t) STRATEGY_PHONE; |
| 4044 | |
| 4045 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 4046 | return (uint32_t) STRATEGY_DTMF; |
| 4047 | |
| 4048 | case AUDIO_USAGE_ALARM: |
| 4049 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 4050 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4051 | |
| 4052 | case AUDIO_USAGE_NOTIFICATION: |
| 4053 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 4054 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 4055 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 4056 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 4057 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; |
| 4058 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 4059 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 4060 | if (isStreamActive(AUDIO_STREAM_RING, 0) || |
| 4061 | isStreamActive(AUDIO_STREAM_ALARM, 0)) { |
| 4062 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4063 | } |
| 4064 | return (uint32_t) STRATEGY_ACCESSIBILITY; |
| 4065 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4066 | case AUDIO_USAGE_UNKNOWN: |
| 4067 | default: |
| 4068 | return (uint32_t) STRATEGY_MEDIA; |
| 4069 | } |
| 4070 | } |
| 4071 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4072 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4073 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4074 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4076 | updateDevicesAndOutputs(); |
| 4077 | break; |
| 4078 | default: |
| 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4083 | bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) { |
| 4084 | for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) { |
| 4085 | if (s == (size_t) streamToIgnore) { |
| 4086 | continue; |
| 4087 | } |
| 4088 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4089 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 4090 | if (outputDesc->mRefCount[s] != 0) { |
| 4091 | return true; |
| 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | return false; |
| 4096 | } |
| 4097 | |
| 4098 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
| 4099 | switch(event) { |
| 4100 | case STARTING_OUTPUT: |
| 4101 | mBeaconMuteRefCount++; |
| 4102 | break; |
| 4103 | case STOPPING_OUTPUT: |
| 4104 | if (mBeaconMuteRefCount > 0) { |
| 4105 | mBeaconMuteRefCount--; |
| 4106 | } |
| 4107 | break; |
| 4108 | case STARTING_BEACON: |
| 4109 | mBeaconPlayingRefCount++; |
| 4110 | break; |
| 4111 | case STOPPING_BEACON: |
| 4112 | if (mBeaconPlayingRefCount > 0) { |
| 4113 | mBeaconPlayingRefCount--; |
| 4114 | } |
| 4115 | break; |
| 4116 | } |
| 4117 | |
| 4118 | if (mBeaconMuteRefCount > 0) { |
| 4119 | // any playback causes beacon to be muted |
| 4120 | return setBeaconMute(true); |
| 4121 | } else { |
| 4122 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4123 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4128 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4129 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4130 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4131 | if (mBeaconMuted != mute) { |
| 4132 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4133 | ALOGV("\t muting %d", mute); |
| 4134 | uint32_t maxLatency = 0; |
| 4135 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4136 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4137 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
| 4138 | desc->mIoHandle, |
| 4139 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4140 | const uint32_t latency = desc->latency() * 2; |
| 4141 | if (latency > maxLatency) { |
| 4142 | maxLatency = latency; |
| 4143 | } |
| 4144 | } |
| 4145 | mBeaconMuted = mute; |
| 4146 | return maxLatency; |
| 4147 | } |
| 4148 | return 0; |
| 4149 | } |
| 4150 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4151 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4152 | bool fromCache) |
| 4153 | { |
| 4154 | uint32_t device = AUDIO_DEVICE_NONE; |
| 4155 | |
| 4156 | if (fromCache) { |
| 4157 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4158 | strategy, mDeviceForStrategy[strategy]); |
| 4159 | return mDeviceForStrategy[strategy]; |
| 4160 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4161 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4162 | switch (strategy) { |
| 4163 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4164 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 4165 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4166 | if (!device) { |
| 4167 | ALOGE("getDeviceForStrategy() no device found for "\ |
| 4168 | "STRATEGY_TRANSMITTED_THROUGH_SPEAKER"); |
| 4169 | } |
| 4170 | break; |
| 4171 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4172 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 4173 | if (isInCall()) { |
| 4174 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4175 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 4177 | // while media is playing on a remote device, use the the sonification behavior. |
| 4178 | // Note that we test this usecase before testing if media is playing because |
| 4179 | // the isStreamActive() method only informs about the activity of a stream, not |
| 4180 | // if it's for local playback. Note also that we use the same delay between both tests |
| 4181 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4182 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4183 | //other acoustic safety mechanisms for notification |
| 4184 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4185 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4186 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4187 | // while media is playing (or has recently played), use the same device |
| 4188 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4189 | } else { |
| 4190 | // when media is not playing anymore, fall back on the sonification behavior |
| 4191 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4192 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4193 | //other acoustic safety mechanisms for notification |
| 4194 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4195 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | break; |
| 4199 | |
| 4200 | case STRATEGY_DTMF: |
| 4201 | if (!isInCall()) { |
| 4202 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 4203 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4204 | break; |
| 4205 | } |
| 4206 | // when in call, DTMF and PHONE strategies follow the same rules |
| 4207 | // FALL THROUGH |
| 4208 | |
| 4209 | case STRATEGY_PHONE: |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4210 | // Force use of only devices on primary output if: |
| 4211 | // - in call AND |
| 4212 | // - cannot route from voice call RX OR |
| 4213 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 4214 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 4215 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4216 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 4217 | if (((mAvailableInputDevices.types() & |
| 4218 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 4219 | (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 4220 | (hwOutputDesc->getAudioPort()->mModule->mHalVersion < |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4221 | AUDIO_DEVICE_API_VERSION_3_0))) { |
| 4222 | availableOutputDeviceTypes = availablePrimaryOutputDevices(); |
| 4223 | } |
| 4224 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 4226 | // of priority |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4227 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4228 | case AUDIO_POLICY_FORCE_BT_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4229 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4230 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | if (device) break; |
| 4232 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4233 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4234 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4235 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4236 | if (device) break; |
| 4237 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4238 | // FALL THROUGH |
| 4239 | |
| 4240 | default: // FORCE_NONE |
| 4241 | // 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] | 4242 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4243 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4244 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4245 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4246 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4247 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4248 | if (device) break; |
| 4249 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4250 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4251 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4252 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4253 | if (device) break; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4254 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 4255 | if (device) break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4256 | if (mPhoneState != AUDIO_MODE_IN_CALL) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4257 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4258 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4259 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4260 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4261 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4262 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4263 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4264 | if (device) break; |
| 4265 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4266 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4268 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4269 | if (device == AUDIO_DEVICE_NONE) { |
| 4270 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 4271 | } |
| 4272 | break; |
| 4273 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4274 | case AUDIO_POLICY_FORCE_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4275 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 4276 | // A2DP speaker when forcing to speaker output |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4277 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4278 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4279 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4280 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4281 | if (device) break; |
| 4282 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4283 | if (mPhoneState != AUDIO_MODE_IN_CALL) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4284 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4285 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4286 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4287 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4288 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4289 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4290 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4292 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | if (device) break; |
| 4294 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4295 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4296 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4297 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4299 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4300 | if (device == AUDIO_DEVICE_NONE) { |
| 4301 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 4302 | } |
| 4303 | break; |
| 4304 | } |
| 4305 | break; |
| 4306 | |
| 4307 | case STRATEGY_SONIFICATION: |
| 4308 | |
| 4309 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 4310 | // handleIncallSonification(). |
| 4311 | if (isInCall()) { |
| 4312 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); |
| 4313 | break; |
| 4314 | } |
| 4315 | // FALL THROUGH |
| 4316 | |
| 4317 | case STRATEGY_ENFORCED_AUDIBLE: |
| 4318 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 4319 | // except: |
| 4320 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 4321 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 4322 | |
| 4323 | if ((strategy == STRATEGY_SONIFICATION) || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4324 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4325 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4326 | if (device == AUDIO_DEVICE_NONE) { |
| 4327 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 4328 | } |
| 4329 | } |
| 4330 | // The second device used for sonification is the same as the device used by media strategy |
| 4331 | // FALL THROUGH |
| 4332 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 4333 | // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
| 4334 | case STRATEGY_ACCESSIBILITY: |
| 4335 | case STRATEGY_REROUTING: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | case STRATEGY_MEDIA: { |
| 4337 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 4338 | if (strategy != STRATEGY_SONIFICATION) { |
| 4339 | // no sonification on remote submix (e.g. WFD) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4340 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4341 | } |
| 4342 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4343 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4344 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4345 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4346 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4347 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | } |
| 4349 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4350 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4351 | } |
| 4352 | } |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4353 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 4354 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { |
| 4355 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4356 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4357 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4358 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4359 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4360 | if ((device2 == AUDIO_DEVICE_NONE)) { |
| 4361 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4362 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4363 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4364 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | } |
| 4366 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4367 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4368 | } |
| 4369 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4370 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | } |
| 4372 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4373 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4374 | } |
| 4375 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 4376 | // no sonification on aux digital (e.g. HDMI) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4377 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4378 | } |
| 4379 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4380 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4381 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4382 | } |
| 4383 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4384 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | } |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4386 | int device3 = AUDIO_DEVICE_NONE; |
| 4387 | if (strategy == STRATEGY_MEDIA) { |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4388 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 4389 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4390 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4391 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4392 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4393 | |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4394 | device2 |= device3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4395 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 4396 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 4397 | device |= device2; |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4398 | |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4399 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 4400 | if ((strategy == STRATEGY_MEDIA) && |
| 4401 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == |
| 4402 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 4403 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4404 | } |
| 4405 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4406 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4407 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | if (device == AUDIO_DEVICE_NONE) { |
| 4409 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); |
| 4410 | } |
| 4411 | } break; |
| 4412 | |
| 4413 | default: |
| 4414 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 4415 | break; |
| 4416 | } |
| 4417 | |
| 4418 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 4419 | return device; |
| 4420 | } |
| 4421 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4422 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | { |
| 4424 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4425 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4426 | } |
| 4427 | mPreviousOutputs = mOutputs; |
| 4428 | } |
| 4429 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4430 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4431 | audio_devices_t prevDevice, |
| 4432 | uint32_t delayMs) |
| 4433 | { |
| 4434 | // mute/unmute strategies using an incompatible device combination |
| 4435 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4436 | // if unmuting, unmute only after the specified delay |
| 4437 | if (outputDesc->isDuplicated()) { |
| 4438 | return 0; |
| 4439 | } |
| 4440 | |
| 4441 | uint32_t muteWaitMs = 0; |
| 4442 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4443 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4444 | |
| 4445 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4446 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 4447 | curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4449 | bool doMute = false; |
| 4450 | |
| 4451 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4452 | doMute = true; |
| 4453 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4454 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4455 | doMute = true; |
| 4456 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4457 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4458 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4459 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4460 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4461 | // skip output if it does not share any device with current output |
| 4462 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4463 | == AUDIO_DEVICE_NONE) { |
| 4464 | continue; |
| 4465 | } |
| 4466 | audio_io_handle_t curOutput = mOutputs.keyAt(j); |
| 4467 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", |
| 4468 | mute ? "muting" : "unmuting", i, curDevice, curOutput); |
| 4469 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); |
| 4470 | if (desc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4471 | if (mute) { |
| 4472 | // FIXME: should not need to double latency if volume could be applied |
| 4473 | // immediately by the audioflinger mixer. We must account for the delay |
| 4474 | // between now and the next time the audioflinger thread for this output |
| 4475 | // will process a buffer (which corresponds to one buffer size, |
| 4476 | // usually 1/2 or 1/4 of the latency). |
| 4477 | if (muteWaitMs < desc->latency() * 2) { |
| 4478 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4479 | } |
| 4480 | } |
| 4481 | } |
| 4482 | } |
| 4483 | } |
| 4484 | } |
| 4485 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4486 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4487 | // different per device volumes |
| 4488 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4489 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4490 | muteWaitMs = outputDesc->latency() * 2; |
| 4491 | } |
| 4492 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4493 | if (outputDesc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4494 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4495 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4496 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4497 | muteWaitMs *2, device); |
| 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4502 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4503 | if (muteWaitMs > delayMs) { |
| 4504 | muteWaitMs -= delayMs; |
| 4505 | usleep(muteWaitMs * 1000); |
| 4506 | return muteWaitMs; |
| 4507 | } |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4511 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | audio_devices_t device, |
| 4513 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4514 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4515 | audio_patch_handle_t *patchHandle, |
| 4516 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | { |
| 4518 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4519 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | AudioParameter param; |
| 4521 | uint32_t muteWaitMs; |
| 4522 | |
| 4523 | if (outputDesc->isDuplicated()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4524 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); |
| 4525 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | return muteWaitMs; |
| 4527 | } |
| 4528 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4529 | // output profile |
| 4530 | if ((device != AUDIO_DEVICE_NONE) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4531 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4532 | return 0; |
| 4533 | } |
| 4534 | |
| 4535 | // filter devices according to output selected |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4536 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | |
| 4538 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4539 | |
| 4540 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); |
| 4541 | |
| 4542 | if (device != AUDIO_DEVICE_NONE) { |
| 4543 | outputDesc->mDevice = device; |
| 4544 | } |
| 4545 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4546 | |
| 4547 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4548 | // the requested device is AUDIO_DEVICE_NONE |
| 4549 | // OR the requested device is the same as current device |
| 4550 | // AND force is not specified |
| 4551 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4552 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4553 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force && |
| 4554 | outputDesc->mPatchHandle != 0) { |
| 4555 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", |
| 4556 | device, output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4557 | return muteWaitMs; |
| 4558 | } |
| 4559 | |
| 4560 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4561 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4563 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4564 | resetOutputDevice(output, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4565 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4566 | DeviceVector deviceList = (address == NULL) ? |
| 4567 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4568 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4569 | if (!deviceList.isEmpty()) { |
| 4570 | struct audio_patch patch; |
| 4571 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4572 | patch.num_sources = 1; |
| 4573 | patch.num_sinks = 0; |
| 4574 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4575 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4576 | patch.num_sinks++; |
| 4577 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4578 | ssize_t index; |
| 4579 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4580 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4581 | } else { |
| 4582 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4583 | } |
| 4584 | sp< AudioPatch> patchDesc; |
| 4585 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4586 | if (index >= 0) { |
| 4587 | patchDesc = mAudioPatches.valueAt(index); |
| 4588 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4589 | } |
| 4590 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4591 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4592 | &afPatchHandle, |
| 4593 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4594 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4595 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4596 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4597 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4598 | if (index < 0) { |
| 4599 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4600 | &patch, mUidCached); |
| 4601 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4602 | } else { |
| 4603 | patchDesc->mPatch = patch; |
| 4604 | } |
| 4605 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4606 | patchDesc->mUid = mUidCached; |
| 4607 | if (patchHandle) { |
| 4608 | *patchHandle = patchDesc->mHandle; |
| 4609 | } |
| 4610 | outputDesc->mPatchHandle = patchDesc->mHandle; |
| 4611 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4612 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4615 | |
| 4616 | // inform all input as well |
| 4617 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4618 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
| 4619 | if (!isVirtualInputDevice(inputDescriptor->mDevice)) { |
| 4620 | AudioParameter inputCmd = AudioParameter(); |
| 4621 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4622 | inputDescriptor->mIoHandle, device); |
| 4623 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4624 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4625 | inputCmd.toString(), |
| 4626 | delayMs); |
| 4627 | } |
| 4628 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4629 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4630 | |
| 4631 | // update stream volumes according to new device |
| 4632 | applyStreamVolumes(output, device, delayMs); |
| 4633 | |
| 4634 | return muteWaitMs; |
| 4635 | } |
| 4636 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4637 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4638 | int delayMs, |
| 4639 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4640 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4641 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4642 | ssize_t index; |
| 4643 | if (patchHandle) { |
| 4644 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4645 | } else { |
| 4646 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4647 | } |
| 4648 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4649 | return INVALID_OPERATION; |
| 4650 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4651 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4652 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4653 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
| 4654 | outputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4655 | removeAudioPatch(patchDesc->mHandle); |
| 4656 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4657 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4658 | return status; |
| 4659 | } |
| 4660 | |
| 4661 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4662 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4663 | bool force, |
| 4664 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4665 | { |
| 4666 | status_t status = NO_ERROR; |
| 4667 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4668 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4669 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4670 | inputDesc->mDevice = device; |
| 4671 | |
| 4672 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4673 | if (!deviceList.isEmpty()) { |
| 4674 | struct audio_patch patch; |
| 4675 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4676 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4677 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4678 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
| 4679 | !inputDesc->mIsSoundTrigger) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4680 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4681 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4682 | patch.num_sinks = 1; |
| 4683 | //only one input device for now |
| 4684 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4685 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4686 | ssize_t index; |
| 4687 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4688 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4689 | } else { |
| 4690 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4691 | } |
| 4692 | sp< AudioPatch> patchDesc; |
| 4693 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4694 | if (index >= 0) { |
| 4695 | patchDesc = mAudioPatches.valueAt(index); |
| 4696 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4697 | } |
| 4698 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4699 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4700 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4701 | 0); |
| 4702 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4703 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4704 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4705 | if (index < 0) { |
| 4706 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4707 | &patch, mUidCached); |
| 4708 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4709 | } else { |
| 4710 | patchDesc->mPatch = patch; |
| 4711 | } |
| 4712 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4713 | patchDesc->mUid = mUidCached; |
| 4714 | if (patchHandle) { |
| 4715 | *patchHandle = patchDesc->mHandle; |
| 4716 | } |
| 4717 | inputDesc->mPatchHandle = patchDesc->mHandle; |
| 4718 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4719 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4720 | } |
| 4721 | } |
| 4722 | } |
| 4723 | return status; |
| 4724 | } |
| 4725 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4726 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4727 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4728 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4729 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4730 | ssize_t index; |
| 4731 | if (patchHandle) { |
| 4732 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4733 | } else { |
| 4734 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4735 | } |
| 4736 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4737 | return INVALID_OPERATION; |
| 4738 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4739 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4740 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4741 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
| 4742 | inputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4743 | removeAudioPatch(patchDesc->mHandle); |
| 4744 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4745 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4746 | return status; |
| 4747 | } |
| 4748 | |
| 4749 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4750 | uint32_t& samplingRate, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 | audio_format_t format, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4752 | audio_channel_mask_t channelMask, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4753 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4754 | { |
| 4755 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4756 | // profile supporting all requested parameters. |
| 4757 | |
| 4758 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4759 | { |
| 4760 | if (mHwModules[i]->mHandle == 0) { |
| 4761 | continue; |
| 4762 | } |
| 4763 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4764 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4765 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4766 | // profile->log(); |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4767 | if (profile->isCompatibleProfile(device, samplingRate, |
| 4768 | &samplingRate /*updatedSamplingRate*/, |
| 4769 | format, channelMask, (audio_output_flags_t) flags)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4770 | return profile; |
| 4771 | } |
| 4772 | } |
| 4773 | } |
| 4774 | return NULL; |
| 4775 | } |
| 4776 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4777 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4778 | { |
| 4779 | uint32_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4780 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & |
| 4781 | ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4782 | switch (inputSource) { |
| 4783 | case AUDIO_SOURCE_VOICE_UPLINK: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4784 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4785 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4786 | break; |
| 4787 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4788 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4789 | |
| 4790 | case AUDIO_SOURCE_DEFAULT: |
| 4791 | case AUDIO_SOURCE_MIC: |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4792 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 4793 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4794 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4795 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4796 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4797 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4798 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4799 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4800 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4801 | break; |
| 4802 | |
| 4803 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 4804 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 4805 | // to voice call path. |
| 4806 | if ((mPhoneState == AUDIO_MODE_IN_CALL) && |
| 4807 | (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 4808 | availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN; |
| 4809 | } |
| 4810 | |
| 4811 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4812 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 4813 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4814 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 4815 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 4816 | break; |
| 4817 | } |
| 4818 | // FALL THROUGH |
| 4819 | |
| 4820 | default: // FORCE_NONE |
| 4821 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4822 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4823 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4824 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4825 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4826 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4827 | } |
| 4828 | break; |
| 4829 | |
| 4830 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 4831 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 4832 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 4833 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4834 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4835 | } |
| 4836 | break; |
| 4837 | } |
| 4838 | break; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4839 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4840 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 4841 | case AUDIO_SOURCE_HOTWORD: |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4842 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4843 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4845 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4846 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4847 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4848 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4849 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4850 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4851 | } |
| 4852 | break; |
| 4853 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4854 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4855 | device = AUDIO_DEVICE_IN_BACK_MIC; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4856 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4857 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4858 | } |
| 4859 | break; |
| 4860 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 4861 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4862 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4863 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4864 | } |
| 4865 | break; |
| 4866 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4867 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4868 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 4869 | } |
| 4870 | break; |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4871 | case AUDIO_SOURCE_FM_TUNER: |
| 4872 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 4873 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 4874 | } |
| 4875 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4876 | default: |
| 4877 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 4878 | break; |
| 4879 | } |
| 4880 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 4881 | return device; |
| 4882 | } |
| 4883 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4884 | bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | { |
| 4886 | if ((device & AUDIO_DEVICE_BIT_IN) != 0) { |
| 4887 | device &= ~AUDIO_DEVICE_BIT_IN; |
| 4888 | if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) |
| 4889 | return true; |
| 4890 | } |
| 4891 | return false; |
| 4892 | } |
| 4893 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4894 | bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) { |
| 4895 | return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL) != 0); |
| 4896 | } |
| 4897 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4898 | audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4899 | { |
| 4900 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4901 | const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4902 | if ((input_descriptor->mRefCount > 0) |
| 4903 | && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { |
| 4904 | return mInputs.keyAt(i); |
| 4905 | } |
| 4906 | } |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4910 | uint32_t AudioPolicyManager::activeInputsCount() const |
| 4911 | { |
| 4912 | uint32_t count = 0; |
| 4913 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4914 | const sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 4915 | if (desc->mRefCount > 0) { |
| 4916 | return count++; |
| 4917 | } |
| 4918 | } |
| 4919 | return count; |
| 4920 | } |
| 4921 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4923 | audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4924 | { |
| 4925 | if (device == AUDIO_DEVICE_NONE) { |
| 4926 | // this happens when forcing a route update and no track is active on an output. |
| 4927 | // In this case the returned category is not important. |
| 4928 | device = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4929 | } else if (popcount(device) > 1) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4930 | // Multiple device selection is either: |
| 4931 | // - speaker + one other device: give priority to speaker in this case. |
| 4932 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 4933 | // retain the device on the A2DP output as the other must not correspond to an active |
| 4934 | // selection if not the speaker. |
Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame] | 4935 | // - 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] | 4936 | if (device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4937 | device = AUDIO_DEVICE_OUT_SPEAKER; |
Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame] | 4938 | } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) { |
| 4939 | device = AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4940 | } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) { |
| 4941 | device = AUDIO_DEVICE_OUT_AUX_LINE; |
| 4942 | } else if (device & AUDIO_DEVICE_OUT_SPDIF) { |
| 4943 | device = AUDIO_DEVICE_OUT_SPDIF; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4944 | } else { |
| 4945 | device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); |
| 4946 | } |
| 4947 | } |
| 4948 | |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4949 | /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/ |
| 4950 | if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 4951 | device = AUDIO_DEVICE_OUT_SPEAKER; |
| 4952 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4953 | ALOGW_IF(popcount(device) != 1, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | "getDeviceForVolume() invalid device combination: %08x", |
| 4955 | device); |
| 4956 | |
| 4957 | return device; |
| 4958 | } |
| 4959 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4960 | AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4961 | { |
| 4962 | switch(getDeviceForVolume(device)) { |
| 4963 | case AUDIO_DEVICE_OUT_EARPIECE: |
| 4964 | return DEVICE_CATEGORY_EARPIECE; |
| 4965 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 4966 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 4967 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: |
| 4968 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: |
| 4969 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 4970 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 4971 | return DEVICE_CATEGORY_HEADSET; |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4972 | case AUDIO_DEVICE_OUT_LINE: |
| 4973 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: |
| 4974 | /*USB? Remote submix?*/ |
| 4975 | return DEVICE_CATEGORY_EXT_MEDIA; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 4977 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 4978 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4979 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: |
| 4980 | case AUDIO_DEVICE_OUT_USB_DEVICE: |
| 4981 | case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: |
| 4982 | default: |
| 4983 | return DEVICE_CATEGORY_SPEAKER; |
| 4984 | } |
| 4985 | } |
| 4986 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 4987 | /* static */ |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4988 | float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4989 | int indexInUi) |
| 4990 | { |
| 4991 | device_category deviceCategory = getDeviceCategory(device); |
| 4992 | const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory]; |
| 4993 | |
| 4994 | // the volume index in the UI is relative to the min and max volume indices for this stream type |
| 4995 | int nbSteps = 1 + curve[VOLMAX].mIndex - |
| 4996 | curve[VOLMIN].mIndex; |
| 4997 | int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) / |
| 4998 | (streamDesc.mIndexMax - streamDesc.mIndexMin); |
| 4999 | |
| 5000 | // find what part of the curve this index volume belongs to, or if it's out of bounds |
| 5001 | int segment = 0; |
| 5002 | if (volIdx < curve[VOLMIN].mIndex) { // out of bounds |
| 5003 | return 0.0f; |
| 5004 | } else if (volIdx < curve[VOLKNEE1].mIndex) { |
| 5005 | segment = 0; |
| 5006 | } else if (volIdx < curve[VOLKNEE2].mIndex) { |
| 5007 | segment = 1; |
| 5008 | } else if (volIdx <= curve[VOLMAX].mIndex) { |
| 5009 | segment = 2; |
| 5010 | } else { // out of bounds |
| 5011 | return 1.0f; |
| 5012 | } |
| 5013 | |
| 5014 | // linear interpolation in the attenuation table in dB |
| 5015 | float decibels = curve[segment].mDBAttenuation + |
| 5016 | ((float)(volIdx - curve[segment].mIndex)) * |
| 5017 | ( (curve[segment+1].mDBAttenuation - |
| 5018 | curve[segment].mDBAttenuation) / |
| 5019 | ((float)(curve[segment+1].mIndex - |
| 5020 | curve[segment].mIndex)) ); |
| 5021 | |
| 5022 | float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) |
| 5023 | |
| 5024 | ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f", |
| 5025 | curve[segment].mIndex, volIdx, |
| 5026 | curve[segment+1].mIndex, |
| 5027 | curve[segment].mDBAttenuation, |
| 5028 | decibels, |
| 5029 | curve[segment+1].mDBAttenuation, |
| 5030 | amplification); |
| 5031 | |
| 5032 | return amplification; |
| 5033 | } |
| 5034 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5035 | const AudioPolicyManager::VolumeCurvePoint |
| 5036 | AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5037 | {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f} |
| 5038 | }; |
| 5039 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5040 | const AudioPolicyManager::VolumeCurvePoint |
| 5041 | AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5042 | {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f} |
| 5043 | }; |
| 5044 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5045 | const AudioPolicyManager::VolumeCurvePoint |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5046 | AudioPolicyManager::sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5047 | {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f} |
| 5048 | }; |
| 5049 | |
| 5050 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5051 | AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5052 | {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f} |
| 5053 | }; |
| 5054 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5055 | const AudioPolicyManager::VolumeCurvePoint |
Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 5056 | AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Jean-Michel Trivi | 98c6043 | 2014-07-09 08:51:34 -0700 | [diff] [blame] | 5057 | {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] | 5058 | }; |
| 5059 | |
| 5060 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5061 | AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5062 | {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f} |
| 5063 | }; |
| 5064 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5065 | const AudioPolicyManager::VolumeCurvePoint |
| 5066 | AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f} |
| 5068 | }; |
| 5069 | |
| 5070 | // AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks |
| 5071 | // AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets. |
| 5072 | // AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java). |
| 5073 | // The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset. |
| 5074 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5075 | const AudioPolicyManager::VolumeCurvePoint |
| 5076 | AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5077 | {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f} |
| 5078 | }; |
| 5079 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5080 | const AudioPolicyManager::VolumeCurvePoint |
| 5081 | AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5082 | {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f} |
| 5083 | }; |
| 5084 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5085 | const AudioPolicyManager::VolumeCurvePoint |
| 5086 | AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5087 | {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f} |
| 5088 | }; |
| 5089 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5090 | const AudioPolicyManager::VolumeCurvePoint |
| 5091 | AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f} |
| 5093 | }; |
| 5094 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5095 | const AudioPolicyManager::VolumeCurvePoint |
| 5096 | AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5097 | {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f} |
| 5098 | }; |
| 5099 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5100 | const AudioPolicyManager::VolumeCurvePoint |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5101 | AudioPolicyManager::sLinearVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5102 | {0, -96.0f}, {33, -68.0f}, {66, -34.0f}, {100, 0.0f} |
| 5103 | }; |
| 5104 | |
| 5105 | const AudioPolicyManager::VolumeCurvePoint |
| 5106 | AudioPolicyManager::sSilentVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5107 | {0, -96.0f}, {1, -96.0f}, {2, -96.0f}, {100, -96.0f} |
| 5108 | }; |
| 5109 | |
| 5110 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5111 | AudioPolicyManager::sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5112 | {0, 0.0f}, {1, 0.0f}, {2, 0.0f}, {100, 0.0f} |
| 5113 | }; |
| 5114 | |
| 5115 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5116 | *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT] |
| 5117 | [AudioPolicyManager::DEVICE_CATEGORY_CNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5118 | { // AUDIO_STREAM_VOICE_CALL |
| 5119 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5120 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5121 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5122 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5123 | }, |
| 5124 | { // AUDIO_STREAM_SYSTEM |
| 5125 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5126 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5127 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5128 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5129 | }, |
| 5130 | { // AUDIO_STREAM_RING |
| 5131 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5132 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5133 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5134 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | }, |
| 5136 | { // AUDIO_STREAM_MUSIC |
| 5137 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5138 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5139 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5140 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5141 | }, |
| 5142 | { // AUDIO_STREAM_ALARM |
| 5143 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5144 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5145 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5146 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | }, |
| 5148 | { // AUDIO_STREAM_NOTIFICATION |
| 5149 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5150 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5151 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5152 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5153 | }, |
| 5154 | { // AUDIO_STREAM_BLUETOOTH_SCO |
| 5155 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5156 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5157 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5158 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5159 | }, |
| 5160 | { // AUDIO_STREAM_ENFORCED_AUDIBLE |
| 5161 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5162 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5163 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5164 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5165 | }, |
| 5166 | { // AUDIO_STREAM_DTMF |
| 5167 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5168 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5169 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5170 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5171 | }, |
| 5172 | { // AUDIO_STREAM_TTS |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5173 | // "Transmitted Through Speaker": always silent except on DEVICE_CATEGORY_SPEAKER |
| 5174 | sSilentVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5175 | sLinearVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5176 | sSilentVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5177 | sSilentVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5178 | }, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5179 | { // AUDIO_STREAM_ACCESSIBILITY |
| 5180 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5181 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5182 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5183 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5184 | }, |
| 5185 | { // AUDIO_STREAM_REROUTING |
| 5186 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5187 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5188 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5189 | sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5190 | }, |
| 5191 | { // AUDIO_STREAM_PATCH |
| 5192 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5193 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5194 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5195 | sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5196 | }, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5197 | }; |
| 5198 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5199 | void AudioPolicyManager::initializeVolumeCurves() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5200 | { |
| 5201 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
| 5202 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 5203 | mStreams[i].mVolumeCurve[j] = |
| 5204 | sVolumeProfiles[i][j]; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | // Check availability of DRC on speaker path: if available, override some of the speaker curves |
| 5209 | if (mSpeakerDrcEnabled) { |
| 5210 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5211 | sDefaultSystemVolumeCurveDrc; |
| 5212 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5213 | sSpeakerSonificationVolumeCurveDrc; |
| 5214 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5215 | sSpeakerSonificationVolumeCurveDrc; |
| 5216 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5217 | sSpeakerSonificationVolumeCurveDrc; |
Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 5218 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5219 | sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5220 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5221 | sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5222 | } |
| 5223 | } |
| 5224 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5225 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5226 | int index, |
| 5227 | audio_io_handle_t output, |
| 5228 | audio_devices_t device) |
| 5229 | { |
| 5230 | float volume = 1.0; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5231 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5232 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 5233 | |
| 5234 | if (device == AUDIO_DEVICE_NONE) { |
| 5235 | device = outputDesc->device(); |
| 5236 | } |
| 5237 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5238 | volume = volIndexToAmpl(device, streamDesc, index); |
| 5239 | |
| 5240 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5241 | // to avoid sound level bursts in user's ears: |
| 5242 | // - always attenuate ring tones and notifications volume by 6dB |
| 5243 | // - if music is playing, always limit the volume to current music volume, |
| 5244 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5245 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5247 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5248 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 5249 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 5250 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5251 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5252 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5253 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5254 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5255 | streamDesc.mCanBeMuted) { |
| 5256 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; |
| 5257 | // when the phone is ringing we must consider that music could have been paused just before |
| 5258 | // by the music application and behave as if music was active if the last music track was |
| 5259 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5260 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5261 | mLimitRingtoneVolume) { |
| 5262 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5263 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, |
| 5264 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | output, |
| 5266 | musicDevice); |
| 5267 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? |
| 5268 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; |
| 5269 | if (volume > minVol) { |
| 5270 | volume = minVol; |
| 5271 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); |
| 5272 | } |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | return volume; |
| 5277 | } |
| 5278 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5279 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5280 | int index, |
| 5281 | audio_io_handle_t output, |
| 5282 | audio_devices_t device, |
| 5283 | int delayMs, |
| 5284 | bool force) |
| 5285 | { |
| 5286 | |
| 5287 | // do not change actual stream volume if the stream is muted |
| 5288 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 5289 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
| 5290 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 5291 | return NO_ERROR; |
| 5292 | } |
| 5293 | |
| 5294 | // 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] | 5295 | if ((stream == AUDIO_STREAM_VOICE_CALL && |
| 5296 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5297 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && |
| 5298 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5299 | 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] | 5300 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5301 | return INVALID_OPERATION; |
| 5302 | } |
| 5303 | |
| 5304 | float volume = computeVolume(stream, index, output, device); |
| 5305 | // We actually change the volume if: |
| 5306 | // - the float value returned by computeVolume() changed |
| 5307 | // - the force flag is set |
| 5308 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
| 5309 | force) { |
| 5310 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 5311 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 5312 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is |
| 5313 | // enabled |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5314 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
| 5315 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5316 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5317 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5320 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5321 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5322 | float voiceVolume; |
| 5323 | // 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] | 5324 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5325 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 5326 | } else { |
| 5327 | voiceVolume = 1.0; |
| 5328 | } |
| 5329 | |
| 5330 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { |
| 5331 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5332 | mLastVoiceVolume = voiceVolume; |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | return NO_ERROR; |
| 5337 | } |
| 5338 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5339 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5340 | audio_devices_t device, |
| 5341 | int delayMs, |
| 5342 | bool force) |
| 5343 | { |
| 5344 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); |
| 5345 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5346 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5347 | if (stream == AUDIO_STREAM_PATCH) { |
| 5348 | continue; |
| 5349 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5350 | checkAndSetVolume((audio_stream_type_t)stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5351 | mStreams[stream].getVolumeIndex(device), |
| 5352 | output, |
| 5353 | device, |
| 5354 | delayMs, |
| 5355 | force); |
| 5356 | } |
| 5357 | } |
| 5358 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5359 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5360 | bool on, |
| 5361 | audio_io_handle_t output, |
| 5362 | int delayMs, |
| 5363 | audio_devices_t device) |
| 5364 | { |
| 5365 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5366 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5367 | if (stream == AUDIO_STREAM_PATCH) { |
| 5368 | continue; |
| 5369 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5370 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5371 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5372 | } |
| 5373 | } |
| 5374 | } |
| 5375 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5376 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5377 | bool on, |
| 5378 | audio_io_handle_t output, |
| 5379 | int delayMs, |
| 5380 | audio_devices_t device) |
| 5381 | { |
| 5382 | StreamDescriptor &streamDesc = mStreams[stream]; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5383 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5384 | if (device == AUDIO_DEVICE_NONE) { |
| 5385 | device = outputDesc->device(); |
| 5386 | } |
| 5387 | |
| 5388 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", |
| 5389 | stream, on, output, outputDesc->mMuteCount[stream], device); |
| 5390 | |
| 5391 | if (on) { |
| 5392 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5393 | if (streamDesc.mCanBeMuted && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5394 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 5395 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5396 | checkAndSetVolume(stream, 0, output, device, delayMs); |
| 5397 | } |
| 5398 | } |
| 5399 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5400 | outputDesc->mMuteCount[stream]++; |
| 5401 | } else { |
| 5402 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5403 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5404 | return; |
| 5405 | } |
| 5406 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5407 | checkAndSetVolume(stream, |
| 5408 | streamDesc.getVolumeIndex(device), |
| 5409 | output, |
| 5410 | device, |
| 5411 | delayMs); |
| 5412 | } |
| 5413 | } |
| 5414 | } |
| 5415 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5416 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5417 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5418 | { |
| 5419 | // if the stream pertains to sonification strategy and we are in call we must |
| 5420 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5421 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5422 | // interfere with the device used for phone strategy |
| 5423 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5424 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5425 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5426 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5427 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5428 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5429 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5430 | stream, starting, outputDesc->mDevice, stateChange); |
| 5431 | if (outputDesc->mRefCount[stream]) { |
| 5432 | int muteCount = 1; |
| 5433 | if (stateChange) { |
| 5434 | muteCount = outputDesc->mRefCount[stream]; |
| 5435 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5436 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5437 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5438 | for (int i = 0; i < muteCount; i++) { |
| 5439 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5440 | } |
| 5441 | } else { |
| 5442 | ALOGV("handleIncallSonification() high visibility"); |
| 5443 | if (outputDesc->device() & |
| 5444 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5445 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5446 | for (int i = 0; i < muteCount; i++) { |
| 5447 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5448 | } |
| 5449 | } |
| 5450 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5451 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5452 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5453 | } else { |
| 5454 | mpClientInterface->stopTone(); |
| 5455 | } |
| 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5461 | bool AudioPolicyManager::isInCall() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5462 | { |
| 5463 | return isStateInCall(mPhoneState); |
| 5464 | } |
| 5465 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5466 | bool AudioPolicyManager::isStateInCall(int state) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5467 | return ((state == AUDIO_MODE_IN_CALL) || |
| 5468 | (state == AUDIO_MODE_IN_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5469 | } |
| 5470 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5471 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5472 | { |
| 5473 | return MAX_EFFECTS_CPU_LOAD; |
| 5474 | } |
| 5475 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5476 | uint32_t AudioPolicyManager::getMaxEffectsMemory() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5477 | { |
| 5478 | return MAX_EFFECTS_MEMORY; |
| 5479 | } |
| 5480 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5481 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5482 | // --- AudioOutputDescriptor class implementation |
| 5483 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5484 | AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5485 | const sp<IOProfile>& profile) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5486 | : mId(0), mIoHandle(0), mLatency(0), |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5487 | mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5488 | mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0) |
| 5489 | { |
| 5490 | // clear usage count for all stream types |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5491 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5492 | mRefCount[i] = 0; |
| 5493 | mCurVolume[i] = -1.0; |
| 5494 | mMuteCount[i] = 0; |
| 5495 | mStopTime[i] = 0; |
| 5496 | } |
| 5497 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5498 | mStrategyMutedByDevice[i] = false; |
| 5499 | } |
| 5500 | if (profile != NULL) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5501 | mFlags = (audio_output_flags_t)profile->mFlags; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5502 | mSamplingRate = profile->pickSamplingRate(); |
| 5503 | mFormat = profile->pickFormat(); |
| 5504 | mChannelMask = profile->pickChannelMask(); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5505 | if (profile->mGains.size() > 0) { |
| 5506 | profile->mGains[0]->getDefaultConfig(&mGain); |
| 5507 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5508 | } |
| 5509 | } |
| 5510 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5511 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5512 | { |
| 5513 | if (isDuplicated()) { |
| 5514 | return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); |
| 5515 | } else { |
| 5516 | return mDevice; |
| 5517 | } |
| 5518 | } |
| 5519 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5520 | uint32_t AudioPolicyManager::AudioOutputDescriptor::latency() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5521 | { |
| 5522 | if (isDuplicated()) { |
| 5523 | return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; |
| 5524 | } else { |
| 5525 | return mLatency; |
| 5526 | } |
| 5527 | } |
| 5528 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5529 | bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith( |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5530 | const sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5531 | { |
| 5532 | if (isDuplicated()) { |
| 5533 | return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); |
| 5534 | } else if (outputDesc->isDuplicated()){ |
| 5535 | return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2); |
| 5536 | } else { |
| 5537 | return (mProfile->mModule == outputDesc->mProfile->mModule); |
| 5538 | } |
| 5539 | } |
| 5540 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5541 | void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5542 | int delta) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5543 | { |
| 5544 | // forward usage count change to attached outputs |
| 5545 | if (isDuplicated()) { |
| 5546 | mOutput1->changeRefCount(stream, delta); |
| 5547 | mOutput2->changeRefCount(stream, delta); |
| 5548 | } |
| 5549 | if ((delta + (int)mRefCount[stream]) < 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5550 | ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", |
| 5551 | delta, stream, mRefCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5552 | mRefCount[stream] = 0; |
| 5553 | return; |
| 5554 | } |
| 5555 | mRefCount[stream] += delta; |
| 5556 | ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); |
| 5557 | } |
| 5558 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5559 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5560 | { |
| 5561 | if (isDuplicated()) { |
| 5562 | return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); |
| 5563 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5564 | return mProfile->mSupportedDevices.types() ; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | } |
| 5566 | } |
| 5567 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5568 | bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5569 | { |
| 5570 | return isStrategyActive(NUM_STRATEGIES, inPastMs); |
| 5571 | } |
| 5572 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5573 | bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5574 | uint32_t inPastMs, |
| 5575 | nsecs_t sysTime) const |
| 5576 | { |
| 5577 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5578 | sysTime = systemTime(); |
| 5579 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5580 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 5581 | if (i == AUDIO_STREAM_PATCH) { |
| 5582 | continue; |
| 5583 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5584 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5585 | (NUM_STRATEGIES == strategy)) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5586 | isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5587 | return true; |
| 5588 | } |
| 5589 | } |
| 5590 | return false; |
| 5591 | } |
| 5592 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5593 | bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5594 | uint32_t inPastMs, |
| 5595 | nsecs_t sysTime) const |
| 5596 | { |
| 5597 | if (mRefCount[stream] != 0) { |
| 5598 | return true; |
| 5599 | } |
| 5600 | if (inPastMs == 0) { |
| 5601 | return false; |
| 5602 | } |
| 5603 | if (sysTime == 0) { |
| 5604 | sysTime = systemTime(); |
| 5605 | } |
| 5606 | if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { |
| 5607 | return true; |
| 5608 | } |
| 5609 | return false; |
| 5610 | } |
| 5611 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5612 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5613 | struct audio_port_config *dstConfig, |
| 5614 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5615 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5616 | ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle); |
| 5617 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5618 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 5619 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; |
| 5620 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5621 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5622 | } |
| 5623 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 5624 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5625 | dstConfig->id = mId; |
| 5626 | dstConfig->role = AUDIO_PORT_ROLE_SOURCE; |
| 5627 | dstConfig->type = AUDIO_PORT_TYPE_MIX; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5628 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; |
| 5629 | dstConfig->ext.mix.handle = mIoHandle; |
| 5630 | dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPort( |
| 5634 | struct audio_port *port) const |
| 5635 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5636 | ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5637 | mProfile->toAudioPort(port); |
| 5638 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5639 | toAudioPortConfig(&port->active_config); |
| 5640 | port->ext.mix.hw_module = mProfile->mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5641 | port->ext.mix.handle = mIoHandle; |
| 5642 | port->ext.mix.latency_class = |
| 5643 | mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL; |
| 5644 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5646 | status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | { |
| 5648 | const size_t SIZE = 256; |
| 5649 | char buffer[SIZE]; |
| 5650 | String8 result; |
| 5651 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5652 | snprintf(buffer, SIZE, " ID: %d\n", mId); |
| 5653 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5654 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 5655 | result.append(buffer); |
| 5656 | snprintf(buffer, SIZE, " Format: %08x\n", mFormat); |
| 5657 | result.append(buffer); |
| 5658 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); |
| 5659 | result.append(buffer); |
| 5660 | snprintf(buffer, SIZE, " Latency: %d\n", mLatency); |
| 5661 | result.append(buffer); |
| 5662 | snprintf(buffer, SIZE, " Flags %08x\n", mFlags); |
| 5663 | result.append(buffer); |
| 5664 | snprintf(buffer, SIZE, " Devices %08x\n", device()); |
| 5665 | result.append(buffer); |
| 5666 | snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); |
| 5667 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5668 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
| 5669 | snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", |
| 5670 | i, mCurVolume[i], mRefCount[i], mMuteCount[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5671 | result.append(buffer); |
| 5672 | } |
| 5673 | write(fd, result.string(), result.size()); |
| 5674 | |
| 5675 | return NO_ERROR; |
| 5676 | } |
| 5677 | |
| 5678 | // --- AudioInputDescriptor class implementation |
| 5679 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5680 | AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5681 | : mId(0), mIoHandle(0), |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5682 | mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0), |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5683 | mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5684 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5685 | if (profile != NULL) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5686 | mSamplingRate = profile->pickSamplingRate(); |
| 5687 | mFormat = profile->pickFormat(); |
| 5688 | mChannelMask = profile->pickChannelMask(); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5689 | if (profile->mGains.size() > 0) { |
| 5690 | profile->mGains[0]->getDefaultConfig(&mGain); |
| 5691 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5693 | } |
| 5694 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5695 | void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5696 | struct audio_port_config *dstConfig, |
| 5697 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5698 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5699 | ALOG_ASSERT(mProfile != 0, |
| 5700 | "toAudioPortConfig() called on input with null profile %d", mIoHandle); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5701 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 5702 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; |
| 5703 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5704 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 5708 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5709 | dstConfig->id = mId; |
| 5710 | dstConfig->role = AUDIO_PORT_ROLE_SINK; |
| 5711 | dstConfig->type = AUDIO_PORT_TYPE_MIX; |
Eric Laurent | 62aaabb | 2014-06-02 10:40:54 -0700 | [diff] [blame] | 5712 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; |
| 5713 | dstConfig->ext.mix.handle = mIoHandle; |
| 5714 | dstConfig->ext.mix.usecase.source = mInputSource; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | void AudioPolicyManager::AudioInputDescriptor::toAudioPort( |
| 5718 | struct audio_port *port) const |
| 5719 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5720 | ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle); |
| 5721 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5722 | mProfile->toAudioPort(port); |
| 5723 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5724 | toAudioPortConfig(&port->active_config); |
| 5725 | port->ext.mix.hw_module = mProfile->mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5726 | port->ext.mix.handle = mIoHandle; |
| 5727 | port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL; |
| 5728 | } |
| 5729 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5730 | status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5731 | { |
| 5732 | const size_t SIZE = 256; |
| 5733 | char buffer[SIZE]; |
| 5734 | String8 result; |
| 5735 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5736 | snprintf(buffer, SIZE, " ID: %d\n", mId); |
| 5737 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5738 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 5739 | result.append(buffer); |
| 5740 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); |
| 5741 | result.append(buffer); |
| 5742 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); |
| 5743 | result.append(buffer); |
| 5744 | snprintf(buffer, SIZE, " Devices %08x\n", mDevice); |
| 5745 | result.append(buffer); |
| 5746 | snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); |
| 5747 | result.append(buffer); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 5748 | snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount); |
| 5749 | result.append(buffer); |
| 5750 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5751 | write(fd, result.string(), result.size()); |
| 5752 | |
| 5753 | return NO_ERROR; |
| 5754 | } |
| 5755 | |
| 5756 | // --- StreamDescriptor class implementation |
| 5757 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5758 | AudioPolicyManager::StreamDescriptor::StreamDescriptor() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5759 | : mIndexMin(0), mIndexMax(1), mCanBeMuted(true) |
| 5760 | { |
| 5761 | mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0); |
| 5762 | } |
| 5763 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5764 | int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5765 | { |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5766 | device = AudioPolicyManager::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5767 | // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT |
| 5768 | if (mIndexCur.indexOfKey(device) < 0) { |
| 5769 | device = AUDIO_DEVICE_OUT_DEFAULT; |
| 5770 | } |
| 5771 | return mIndexCur.valueFor(device); |
| 5772 | } |
| 5773 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5774 | void AudioPolicyManager::StreamDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5775 | { |
| 5776 | const size_t SIZE = 256; |
| 5777 | char buffer[SIZE]; |
| 5778 | String8 result; |
| 5779 | |
| 5780 | snprintf(buffer, SIZE, "%s %02d %02d ", |
| 5781 | mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax); |
| 5782 | result.append(buffer); |
| 5783 | for (size_t i = 0; i < mIndexCur.size(); i++) { |
| 5784 | snprintf(buffer, SIZE, "%04x : %02d, ", |
| 5785 | mIndexCur.keyAt(i), |
| 5786 | mIndexCur.valueAt(i)); |
| 5787 | result.append(buffer); |
| 5788 | } |
| 5789 | result.append("\n"); |
| 5790 | |
| 5791 | write(fd, result.string(), result.size()); |
| 5792 | } |
| 5793 | |
| 5794 | // --- EffectDescriptor class implementation |
| 5795 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5796 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5797 | { |
| 5798 | const size_t SIZE = 256; |
| 5799 | char buffer[SIZE]; |
| 5800 | String8 result; |
| 5801 | |
| 5802 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); |
| 5803 | result.append(buffer); |
| 5804 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); |
| 5805 | result.append(buffer); |
| 5806 | snprintf(buffer, SIZE, " Session: %d\n", mSession); |
| 5807 | result.append(buffer); |
| 5808 | snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); |
| 5809 | result.append(buffer); |
| 5810 | snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled"); |
| 5811 | result.append(buffer); |
| 5812 | write(fd, result.string(), result.size()); |
| 5813 | |
| 5814 | return NO_ERROR; |
| 5815 | } |
| 5816 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5817 | // --- HwModule class implementation |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5818 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5819 | AudioPolicyManager::HwModule::HwModule(const char *name) |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5820 | : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), |
| 5821 | mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5822 | { |
| 5823 | } |
| 5824 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5825 | AudioPolicyManager::HwModule::~HwModule() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5826 | { |
| 5827 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5828 | mOutputProfiles[i]->mSupportedDevices.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5829 | } |
| 5830 | for (size_t i = 0; i < mInputProfiles.size(); i++) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5831 | mInputProfiles[i]->mSupportedDevices.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5832 | } |
| 5833 | free((void *)mName); |
| 5834 | } |
| 5835 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5836 | status_t AudioPolicyManager::HwModule::loadInput(cnode *root) |
| 5837 | { |
| 5838 | cnode *node = root->first_child; |
| 5839 | |
| 5840 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this); |
| 5841 | |
| 5842 | while (node) { |
| 5843 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { |
| 5844 | profile->loadSamplingRates((char *)node->value); |
| 5845 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { |
| 5846 | profile->loadFormats((char *)node->value); |
| 5847 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5848 | profile->loadInChannels((char *)node->value); |
| 5849 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { |
| 5850 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, |
| 5851 | mDeclaredDevices); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5852 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { |
| 5853 | profile->mFlags = parseInputFlagNames((char *)node->value); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5854 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5855 | profile->loadGains(node); |
| 5856 | } |
| 5857 | node = node->next; |
| 5858 | } |
| 5859 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), |
| 5860 | "loadInput() invalid supported devices"); |
| 5861 | ALOGW_IF(profile->mChannelMasks.size() == 0, |
| 5862 | "loadInput() invalid supported channel masks"); |
| 5863 | ALOGW_IF(profile->mSamplingRates.size() == 0, |
| 5864 | "loadInput() invalid supported sampling rates"); |
| 5865 | ALOGW_IF(profile->mFormats.size() == 0, |
| 5866 | "loadInput() invalid supported formats"); |
| 5867 | if (!profile->mSupportedDevices.isEmpty() && |
| 5868 | (profile->mChannelMasks.size() != 0) && |
| 5869 | (profile->mSamplingRates.size() != 0) && |
| 5870 | (profile->mFormats.size() != 0)) { |
| 5871 | |
| 5872 | ALOGV("loadInput() adding input Supported Devices %04x", |
| 5873 | profile->mSupportedDevices.types()); |
| 5874 | |
| 5875 | mInputProfiles.add(profile); |
| 5876 | return NO_ERROR; |
| 5877 | } else { |
| 5878 | return BAD_VALUE; |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | status_t AudioPolicyManager::HwModule::loadOutput(cnode *root) |
| 5883 | { |
| 5884 | cnode *node = root->first_child; |
| 5885 | |
| 5886 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this); |
| 5887 | |
| 5888 | while (node) { |
| 5889 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { |
| 5890 | profile->loadSamplingRates((char *)node->value); |
| 5891 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { |
| 5892 | profile->loadFormats((char *)node->value); |
| 5893 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5894 | profile->loadOutChannels((char *)node->value); |
| 5895 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { |
| 5896 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, |
| 5897 | mDeclaredDevices); |
| 5898 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5899 | profile->mFlags = parseOutputFlagNames((char *)node->value); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5900 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5901 | profile->loadGains(node); |
| 5902 | } |
| 5903 | node = node->next; |
| 5904 | } |
| 5905 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), |
| 5906 | "loadOutput() invalid supported devices"); |
| 5907 | ALOGW_IF(profile->mChannelMasks.size() == 0, |
| 5908 | "loadOutput() invalid supported channel masks"); |
| 5909 | ALOGW_IF(profile->mSamplingRates.size() == 0, |
| 5910 | "loadOutput() invalid supported sampling rates"); |
| 5911 | ALOGW_IF(profile->mFormats.size() == 0, |
| 5912 | "loadOutput() invalid supported formats"); |
| 5913 | if (!profile->mSupportedDevices.isEmpty() && |
| 5914 | (profile->mChannelMasks.size() != 0) && |
| 5915 | (profile->mSamplingRates.size() != 0) && |
| 5916 | (profile->mFormats.size() != 0)) { |
| 5917 | |
| 5918 | ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x", |
| 5919 | profile->mSupportedDevices.types(), profile->mFlags); |
| 5920 | |
| 5921 | mOutputProfiles.add(profile); |
| 5922 | return NO_ERROR; |
| 5923 | } else { |
| 5924 | return BAD_VALUE; |
| 5925 | } |
| 5926 | } |
| 5927 | |
| 5928 | status_t AudioPolicyManager::HwModule::loadDevice(cnode *root) |
| 5929 | { |
| 5930 | cnode *node = root->first_child; |
| 5931 | |
| 5932 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 5933 | while (node) { |
| 5934 | if (strcmp(node->name, DEVICE_TYPE) == 0) { |
| 5935 | type = parseDeviceNames((char *)node->value); |
| 5936 | break; |
| 5937 | } |
| 5938 | node = node->next; |
| 5939 | } |
| 5940 | if (type == AUDIO_DEVICE_NONE || |
| 5941 | (!audio_is_input_device(type) && !audio_is_output_device(type))) { |
| 5942 | ALOGW("loadDevice() bad type %08x", type); |
| 5943 | return BAD_VALUE; |
| 5944 | } |
| 5945 | sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type); |
| 5946 | deviceDesc->mModule = this; |
| 5947 | |
| 5948 | node = root->first_child; |
| 5949 | while (node) { |
| 5950 | if (strcmp(node->name, DEVICE_ADDRESS) == 0) { |
| 5951 | deviceDesc->mAddress = String8((char *)node->value); |
| 5952 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5953 | if (audio_is_input_device(type)) { |
| 5954 | deviceDesc->loadInChannels((char *)node->value); |
| 5955 | } else { |
| 5956 | deviceDesc->loadOutChannels((char *)node->value); |
| 5957 | } |
| 5958 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5959 | deviceDesc->loadGains(node); |
| 5960 | } |
| 5961 | node = node->next; |
| 5962 | } |
| 5963 | |
| 5964 | ALOGV("loadDevice() adding device name %s type %08x address %s", |
| 5965 | deviceDesc->mName.string(), type, deviceDesc->mAddress.string()); |
| 5966 | |
| 5967 | mDeclaredDevices.add(deviceDesc); |
| 5968 | |
| 5969 | return NO_ERROR; |
| 5970 | } |
| 5971 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5972 | void AudioPolicyManager::HwModule::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5973 | { |
| 5974 | const size_t SIZE = 256; |
| 5975 | char buffer[SIZE]; |
| 5976 | String8 result; |
| 5977 | |
| 5978 | snprintf(buffer, SIZE, " - name: %s\n", mName); |
| 5979 | result.append(buffer); |
| 5980 | snprintf(buffer, SIZE, " - handle: %d\n", mHandle); |
| 5981 | result.append(buffer); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5982 | snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF); |
| 5983 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5984 | write(fd, result.string(), result.size()); |
| 5985 | if (mOutputProfiles.size()) { |
| 5986 | write(fd, " - outputs:\n", strlen(" - outputs:\n")); |
| 5987 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5988 | snprintf(buffer, SIZE, " output %zu:\n", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5989 | write(fd, buffer, strlen(buffer)); |
| 5990 | mOutputProfiles[i]->dump(fd); |
| 5991 | } |
| 5992 | } |
| 5993 | if (mInputProfiles.size()) { |
| 5994 | write(fd, " - inputs:\n", strlen(" - inputs:\n")); |
| 5995 | for (size_t i = 0; i < mInputProfiles.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5996 | snprintf(buffer, SIZE, " input %zu:\n", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5997 | write(fd, buffer, strlen(buffer)); |
| 5998 | mInputProfiles[i]->dump(fd); |
| 5999 | } |
| 6000 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6001 | if (mDeclaredDevices.size()) { |
| 6002 | write(fd, " - devices:\n", strlen(" - devices:\n")); |
| 6003 | for (size_t i = 0; i < mDeclaredDevices.size(); i++) { |
| 6004 | mDeclaredDevices[i]->dump(fd, 4, i); |
| 6005 | } |
| 6006 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6007 | } |
| 6008 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6009 | // --- AudioPort class implementation |
| 6010 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6011 | |
| 6012 | AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type, |
| 6013 | audio_port_role_t role, const sp<HwModule>& module) : |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 6014 | mName(name), mType(type), mRole(role), mModule(module), mFlags(0) |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6015 | { |
| 6016 | mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || |
| 6017 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); |
| 6018 | } |
| 6019 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6020 | void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const |
| 6021 | { |
| 6022 | port->role = mRole; |
| 6023 | port->type = mType; |
| 6024 | unsigned int i; |
| 6025 | 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] | 6026 | if (mSamplingRates[i] != 0) { |
| 6027 | port->sample_rates[i] = mSamplingRates[i]; |
| 6028 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6029 | } |
| 6030 | port->num_sample_rates = i; |
| 6031 | 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] | 6032 | if (mChannelMasks[i] != 0) { |
| 6033 | port->channel_masks[i] = mChannelMasks[i]; |
| 6034 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6035 | } |
| 6036 | port->num_channel_masks = i; |
| 6037 | 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] | 6038 | if (mFormats[i] != 0) { |
| 6039 | port->formats[i] = mFormats[i]; |
| 6040 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6041 | } |
| 6042 | port->num_formats = i; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 6043 | |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 6044 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 6045 | |
| 6046 | for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) { |
| 6047 | port->gains[i] = mGains[i]->mGain; |
| 6048 | } |
| 6049 | port->num_gains = i; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6050 | } |
| 6051 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6052 | void AudioPolicyManager::AudioPort::importAudioPort(const sp<AudioPort> port) { |
| 6053 | for (size_t k = 0 ; k < port->mSamplingRates.size() ; k++) { |
| 6054 | const uint32_t rate = port->mSamplingRates.itemAt(k); |
| 6055 | if (rate != 0) { // skip "dynamic" rates |
| 6056 | bool hasRate = false; |
| 6057 | for (size_t l = 0 ; l < mSamplingRates.size() ; l++) { |
| 6058 | if (rate == mSamplingRates.itemAt(l)) { |
| 6059 | hasRate = true; |
| 6060 | break; |
| 6061 | } |
| 6062 | } |
| 6063 | if (!hasRate) { // never import a sampling rate twice |
| 6064 | mSamplingRates.add(rate); |
| 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | for (size_t k = 0 ; k < port->mChannelMasks.size() ; k++) { |
| 6069 | const audio_channel_mask_t mask = port->mChannelMasks.itemAt(k); |
| 6070 | if (mask != 0) { // skip "dynamic" masks |
| 6071 | bool hasMask = false; |
| 6072 | for (size_t l = 0 ; l < mChannelMasks.size() ; l++) { |
| 6073 | if (mask == mChannelMasks.itemAt(l)) { |
| 6074 | hasMask = true; |
| 6075 | break; |
| 6076 | } |
| 6077 | } |
| 6078 | if (!hasMask) { // never import a channel mask twice |
| 6079 | mChannelMasks.add(mask); |
| 6080 | } |
| 6081 | } |
| 6082 | } |
| 6083 | for (size_t k = 0 ; k < port->mFormats.size() ; k++) { |
| 6084 | const audio_format_t format = port->mFormats.itemAt(k); |
| 6085 | if (format != 0) { // skip "dynamic" formats |
| 6086 | bool hasFormat = false; |
| 6087 | for (size_t l = 0 ; l < mFormats.size() ; l++) { |
| 6088 | if (format == mFormats.itemAt(l)) { |
| 6089 | hasFormat = true; |
| 6090 | break; |
| 6091 | } |
| 6092 | } |
| 6093 | if (!hasFormat) { // never import a channel mask twice |
| 6094 | mFormats.add(format); |
| 6095 | } |
| 6096 | } |
| 6097 | } |
Hochi Huang | 23fe3c0 | 2014-10-03 09:09:30 +0800 | [diff] [blame] | 6098 | for (size_t k = 0 ; k < port->mGains.size() ; k++) { |
| 6099 | sp<AudioGain> gain = port->mGains.itemAt(k); |
| 6100 | if (gain != 0) { |
| 6101 | bool hasGain = false; |
| 6102 | for (size_t l = 0 ; l < mGains.size() ; l++) { |
| 6103 | if (gain == mGains.itemAt(l)) { |
| 6104 | hasGain = true; |
| 6105 | break; |
| 6106 | } |
| 6107 | } |
| 6108 | if (!hasGain) { // never import a gain twice |
| 6109 | mGains.add(gain); |
| 6110 | } |
| 6111 | } |
| 6112 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6113 | } |
| 6114 | |
| 6115 | void AudioPolicyManager::AudioPort::clearCapabilities() { |
| 6116 | mChannelMasks.clear(); |
| 6117 | mFormats.clear(); |
| 6118 | mSamplingRates.clear(); |
Hochi Huang | 23fe3c0 | 2014-10-03 09:09:30 +0800 | [diff] [blame] | 6119 | mGains.clear(); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6120 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6121 | |
| 6122 | void AudioPolicyManager::AudioPort::loadSamplingRates(char *name) |
| 6123 | { |
| 6124 | char *str = strtok(name, "|"); |
| 6125 | |
| 6126 | // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling |
| 6127 | // rates should be read from the output stream after it is opened for the first time |
| 6128 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6129 | mSamplingRates.add(0); |
| 6130 | return; |
| 6131 | } |
| 6132 | |
| 6133 | while (str != NULL) { |
| 6134 | uint32_t rate = atoi(str); |
| 6135 | if (rate != 0) { |
| 6136 | ALOGV("loadSamplingRates() adding rate %d", rate); |
| 6137 | mSamplingRates.add(rate); |
| 6138 | } |
| 6139 | str = strtok(NULL, "|"); |
| 6140 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | void AudioPolicyManager::AudioPort::loadFormats(char *name) |
| 6144 | { |
| 6145 | char *str = strtok(name, "|"); |
| 6146 | |
| 6147 | // by convention, "0' in the first entry in mFormats indicates the supported formats |
| 6148 | // should be read from the output stream after it is opened for the first time |
| 6149 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6150 | mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 6151 | return; |
| 6152 | } |
| 6153 | |
| 6154 | while (str != NULL) { |
| 6155 | audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable, |
| 6156 | ARRAY_SIZE(sFormatNameToEnumTable), |
| 6157 | str); |
| 6158 | if (format != AUDIO_FORMAT_DEFAULT) { |
| 6159 | mFormats.add(format); |
| 6160 | } |
| 6161 | str = strtok(NULL, "|"); |
| 6162 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | void AudioPolicyManager::AudioPort::loadInChannels(char *name) |
| 6166 | { |
| 6167 | const char *str = strtok(name, "|"); |
| 6168 | |
| 6169 | ALOGV("loadInChannels() %s", name); |
| 6170 | |
| 6171 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6172 | mChannelMasks.add(0); |
| 6173 | return; |
| 6174 | } |
| 6175 | |
| 6176 | while (str != NULL) { |
| 6177 | audio_channel_mask_t channelMask = |
| 6178 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, |
| 6179 | ARRAY_SIZE(sInChannelsNameToEnumTable), |
| 6180 | str); |
| 6181 | if (channelMask != 0) { |
| 6182 | ALOGV("loadInChannels() adding channelMask %04x", channelMask); |
| 6183 | mChannelMasks.add(channelMask); |
| 6184 | } |
| 6185 | str = strtok(NULL, "|"); |
| 6186 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | void AudioPolicyManager::AudioPort::loadOutChannels(char *name) |
| 6190 | { |
| 6191 | const char *str = strtok(name, "|"); |
| 6192 | |
| 6193 | ALOGV("loadOutChannels() %s", name); |
| 6194 | |
| 6195 | // by convention, "0' in the first entry in mChannelMasks indicates the supported channel |
| 6196 | // masks should be read from the output stream after it is opened for the first time |
| 6197 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6198 | mChannelMasks.add(0); |
| 6199 | return; |
| 6200 | } |
| 6201 | |
| 6202 | while (str != NULL) { |
| 6203 | audio_channel_mask_t channelMask = |
| 6204 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, |
| 6205 | ARRAY_SIZE(sOutChannelsNameToEnumTable), |
| 6206 | str); |
| 6207 | if (channelMask != 0) { |
| 6208 | mChannelMasks.add(channelMask); |
| 6209 | } |
| 6210 | str = strtok(NULL, "|"); |
| 6211 | } |
| 6212 | return; |
| 6213 | } |
| 6214 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6215 | audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name) |
| 6216 | { |
| 6217 | const char *str = strtok(name, "|"); |
| 6218 | |
| 6219 | ALOGV("loadGainMode() %s", name); |
| 6220 | audio_gain_mode_t mode = 0; |
| 6221 | while (str != NULL) { |
| 6222 | mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable, |
| 6223 | ARRAY_SIZE(sGainModeNameToEnumTable), |
| 6224 | str); |
| 6225 | str = strtok(NULL, "|"); |
| 6226 | } |
| 6227 | return mode; |
| 6228 | } |
| 6229 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6230 | void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index) |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6231 | { |
| 6232 | cnode *node = root->first_child; |
| 6233 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6234 | sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6235 | |
| 6236 | while (node) { |
| 6237 | if (strcmp(node->name, GAIN_MODE) == 0) { |
| 6238 | gain->mGain.mode = loadGainMode((char *)node->value); |
| 6239 | } else if (strcmp(node->name, GAIN_CHANNELS) == 0) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6240 | if (mUseInChannelMask) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6241 | gain->mGain.channel_mask = |
| 6242 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, |
| 6243 | ARRAY_SIZE(sInChannelsNameToEnumTable), |
| 6244 | (char *)node->value); |
| 6245 | } else { |
| 6246 | gain->mGain.channel_mask = |
| 6247 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, |
| 6248 | ARRAY_SIZE(sOutChannelsNameToEnumTable), |
| 6249 | (char *)node->value); |
| 6250 | } |
| 6251 | } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) { |
| 6252 | gain->mGain.min_value = atoi((char *)node->value); |
| 6253 | } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) { |
| 6254 | gain->mGain.max_value = atoi((char *)node->value); |
| 6255 | } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) { |
| 6256 | gain->mGain.default_value = atoi((char *)node->value); |
| 6257 | } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) { |
| 6258 | gain->mGain.step_value = atoi((char *)node->value); |
| 6259 | } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) { |
| 6260 | gain->mGain.min_ramp_ms = atoi((char *)node->value); |
| 6261 | } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) { |
| 6262 | gain->mGain.max_ramp_ms = atoi((char *)node->value); |
| 6263 | } |
| 6264 | node = node->next; |
| 6265 | } |
| 6266 | |
| 6267 | ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d", |
| 6268 | gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value); |
| 6269 | |
| 6270 | if (gain->mGain.mode == 0) { |
| 6271 | return; |
| 6272 | } |
| 6273 | mGains.add(gain); |
| 6274 | } |
| 6275 | |
| 6276 | void AudioPolicyManager::AudioPort::loadGains(cnode *root) |
| 6277 | { |
| 6278 | cnode *node = root->first_child; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6279 | int index = 0; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6280 | while (node) { |
| 6281 | ALOGV("loadGains() loading gain %s", node->name); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6282 | loadGain(node, index++); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6283 | node = node->next; |
| 6284 | } |
| 6285 | } |
| 6286 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6287 | status_t AudioPolicyManager::AudioPort::checkExactSamplingRate(uint32_t samplingRate) const |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6288 | { |
| 6289 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6290 | if (mSamplingRates[i] == samplingRate) { |
| 6291 | return NO_ERROR; |
| 6292 | } |
| 6293 | } |
| 6294 | return BAD_VALUE; |
| 6295 | } |
| 6296 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6297 | status_t AudioPolicyManager::AudioPort::checkCompatibleSamplingRate(uint32_t samplingRate, |
| 6298 | uint32_t *updatedSamplingRate) const |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6299 | { |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6300 | // Search for the closest supported sampling rate that is above (preferred) |
| 6301 | // or below (acceptable) the desired sampling rate, within a permitted ratio. |
| 6302 | // The sampling rates do not need to be sorted in ascending order. |
| 6303 | ssize_t maxBelow = -1; |
| 6304 | ssize_t minAbove = -1; |
| 6305 | uint32_t candidate; |
| 6306 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
| 6307 | candidate = mSamplingRates[i]; |
| 6308 | if (candidate == samplingRate) { |
| 6309 | if (updatedSamplingRate != NULL) { |
| 6310 | *updatedSamplingRate = candidate; |
| 6311 | } |
| 6312 | return NO_ERROR; |
| 6313 | } |
| 6314 | // candidate < desired |
| 6315 | if (candidate < samplingRate) { |
| 6316 | if (maxBelow < 0 || candidate > mSamplingRates[maxBelow]) { |
| 6317 | maxBelow = i; |
| 6318 | } |
| 6319 | // candidate > desired |
| 6320 | } else { |
| 6321 | if (minAbove < 0 || candidate < mSamplingRates[minAbove]) { |
| 6322 | minAbove = i; |
| 6323 | } |
| 6324 | } |
| 6325 | } |
| 6326 | // This uses hard-coded knowledge about AudioFlinger resampling ratios. |
| 6327 | // TODO Move these assumptions out. |
| 6328 | static const uint32_t kMaxDownSampleRatio = 6; // beyond this aliasing occurs |
| 6329 | static const uint32_t kMaxUpSampleRatio = 256; // beyond this sample rate inaccuracies occur |
| 6330 | // due to approximation by an int32_t of the |
| 6331 | // phase increments |
| 6332 | // Prefer to down-sample from a higher sampling rate, as we get the desired frequency spectrum. |
| 6333 | if (minAbove >= 0) { |
| 6334 | candidate = mSamplingRates[minAbove]; |
| 6335 | if (candidate / kMaxDownSampleRatio <= samplingRate) { |
| 6336 | if (updatedSamplingRate != NULL) { |
| 6337 | *updatedSamplingRate = candidate; |
| 6338 | } |
| 6339 | return NO_ERROR; |
| 6340 | } |
| 6341 | } |
| 6342 | // But if we have to up-sample from a lower sampling rate, that's OK. |
| 6343 | if (maxBelow >= 0) { |
| 6344 | candidate = mSamplingRates[maxBelow]; |
| 6345 | if (candidate * kMaxUpSampleRatio >= samplingRate) { |
| 6346 | if (updatedSamplingRate != NULL) { |
| 6347 | *updatedSamplingRate = candidate; |
| 6348 | } |
| 6349 | return NO_ERROR; |
| 6350 | } |
| 6351 | } |
| 6352 | // leave updatedSamplingRate unmodified |
| 6353 | return BAD_VALUE; |
| 6354 | } |
| 6355 | |
| 6356 | status_t AudioPolicyManager::AudioPort::checkExactChannelMask(audio_channel_mask_t channelMask) const |
| 6357 | { |
| 6358 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6359 | if (mChannelMasks[i] == channelMask) { |
| 6360 | return NO_ERROR; |
| 6361 | } |
| 6362 | } |
| 6363 | return BAD_VALUE; |
| 6364 | } |
| 6365 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6366 | status_t AudioPolicyManager::AudioPort::checkCompatibleChannelMask(audio_channel_mask_t channelMask) |
| 6367 | const |
| 6368 | { |
| 6369 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; |
| 6370 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6371 | // FIXME Does not handle multi-channel automatic conversions yet |
| 6372 | audio_channel_mask_t supported = mChannelMasks[i]; |
| 6373 | if (supported == channelMask) { |
| 6374 | return NO_ERROR; |
| 6375 | } |
| 6376 | if (isRecordThread) { |
| 6377 | // This uses hard-coded knowledge that AudioFlinger can silently down-mix and up-mix. |
| 6378 | // FIXME Abstract this out to a table. |
| 6379 | if (((supported == AUDIO_CHANNEL_IN_FRONT_BACK || supported == AUDIO_CHANNEL_IN_STEREO) |
| 6380 | && channelMask == AUDIO_CHANNEL_IN_MONO) || |
| 6381 | (supported == AUDIO_CHANNEL_IN_MONO && (channelMask == AUDIO_CHANNEL_IN_FRONT_BACK |
| 6382 | || channelMask == AUDIO_CHANNEL_IN_STEREO))) { |
| 6383 | return NO_ERROR; |
| 6384 | } |
| 6385 | } |
| 6386 | } |
| 6387 | return BAD_VALUE; |
| 6388 | } |
| 6389 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6390 | status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const |
| 6391 | { |
| 6392 | for (size_t i = 0; i < mFormats.size(); i ++) { |
| 6393 | if (mFormats[i] == format) { |
| 6394 | return NO_ERROR; |
| 6395 | } |
| 6396 | } |
| 6397 | return BAD_VALUE; |
| 6398 | } |
| 6399 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6400 | |
| 6401 | uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const |
| 6402 | { |
| 6403 | // special case for uninitialized dynamic profile |
| 6404 | if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) { |
| 6405 | return 0; |
| 6406 | } |
| 6407 | |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6408 | // For direct outputs, pick minimum sampling rate: this helps ensuring that the |
| 6409 | // channel count / sampling rate combination chosen will be supported by the connected |
| 6410 | // sink |
| 6411 | if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) && |
| 6412 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) { |
| 6413 | uint32_t samplingRate = UINT_MAX; |
| 6414 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6415 | if ((mSamplingRates[i] < samplingRate) && (mSamplingRates[i] > 0)) { |
| 6416 | samplingRate = mSamplingRates[i]; |
| 6417 | } |
| 6418 | } |
| 6419 | return (samplingRate == UINT_MAX) ? 0 : samplingRate; |
| 6420 | } |
| 6421 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6422 | uint32_t samplingRate = 0; |
| 6423 | uint32_t maxRate = MAX_MIXER_SAMPLING_RATE; |
| 6424 | |
| 6425 | // For mixed output and inputs, use max mixer sampling rates. Do not |
| 6426 | // limit sampling rate otherwise |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6427 | if (mType != AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6428 | maxRate = UINT_MAX; |
| 6429 | } |
| 6430 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6431 | if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) { |
| 6432 | samplingRate = mSamplingRates[i]; |
| 6433 | } |
| 6434 | } |
| 6435 | return samplingRate; |
| 6436 | } |
| 6437 | |
| 6438 | audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const |
| 6439 | { |
| 6440 | // special case for uninitialized dynamic profile |
| 6441 | if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) { |
| 6442 | return AUDIO_CHANNEL_NONE; |
| 6443 | } |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6444 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE; |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6445 | |
| 6446 | // For direct outputs, pick minimum channel count: this helps ensuring that the |
| 6447 | // channel count / sampling rate combination chosen will be supported by the connected |
| 6448 | // sink |
| 6449 | if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) && |
| 6450 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) { |
| 6451 | uint32_t channelCount = UINT_MAX; |
| 6452 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6453 | uint32_t cnlCount; |
| 6454 | if (mUseInChannelMask) { |
| 6455 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); |
| 6456 | } else { |
| 6457 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); |
| 6458 | } |
| 6459 | if ((cnlCount < channelCount) && (cnlCount > 0)) { |
| 6460 | channelMask = mChannelMasks[i]; |
| 6461 | channelCount = cnlCount; |
| 6462 | } |
| 6463 | } |
| 6464 | return channelMask; |
| 6465 | } |
| 6466 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6467 | uint32_t channelCount = 0; |
| 6468 | uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; |
| 6469 | |
| 6470 | // For mixed output and inputs, use max mixer channel count. Do not |
| 6471 | // limit channel count otherwise |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6472 | if (mType != AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6473 | maxCount = UINT_MAX; |
| 6474 | } |
| 6475 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6476 | uint32_t cnlCount; |
| 6477 | if (mUseInChannelMask) { |
| 6478 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); |
| 6479 | } else { |
| 6480 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); |
| 6481 | } |
| 6482 | if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { |
| 6483 | channelMask = mChannelMasks[i]; |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6484 | channelCount = cnlCount; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6485 | } |
| 6486 | } |
| 6487 | return channelMask; |
| 6488 | } |
| 6489 | |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6490 | /* format in order of increasing preference */ |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6491 | const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = { |
| 6492 | AUDIO_FORMAT_DEFAULT, |
| 6493 | AUDIO_FORMAT_PCM_16_BIT, |
Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6494 | AUDIO_FORMAT_PCM_8_24_BIT, |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6495 | AUDIO_FORMAT_PCM_24_BIT_PACKED, |
Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6496 | AUDIO_FORMAT_PCM_32_BIT, |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6497 | AUDIO_FORMAT_PCM_FLOAT, |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6498 | }; |
| 6499 | |
| 6500 | int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1, |
| 6501 | audio_format_t format2) |
| 6502 | { |
| 6503 | // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any |
| 6504 | // compressed format and better than any PCM format. This is by design of pickFormat() |
| 6505 | if (!audio_is_linear_pcm(format1)) { |
| 6506 | if (!audio_is_linear_pcm(format2)) { |
| 6507 | return 0; |
| 6508 | } |
| 6509 | return 1; |
| 6510 | } |
| 6511 | if (!audio_is_linear_pcm(format2)) { |
| 6512 | return -1; |
| 6513 | } |
| 6514 | |
| 6515 | int index1 = -1, index2 = -1; |
| 6516 | for (size_t i = 0; |
| 6517 | (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1)); |
| 6518 | i ++) { |
| 6519 | if (sPcmFormatCompareTable[i] == format1) { |
| 6520 | index1 = i; |
| 6521 | } |
| 6522 | if (sPcmFormatCompareTable[i] == format2) { |
| 6523 | index2 = i; |
| 6524 | } |
| 6525 | } |
| 6526 | // format1 not found => index1 < 0 => format2 > format1 |
| 6527 | // format2 not found => index2 < 0 => format2 < format1 |
| 6528 | return index1 - index2; |
| 6529 | } |
| 6530 | |
| 6531 | audio_format_t AudioPolicyManager::AudioPort::pickFormat() const |
| 6532 | { |
| 6533 | // special case for uninitialized dynamic profile |
| 6534 | if (mFormats.size() == 1 && mFormats[0] == 0) { |
| 6535 | return AUDIO_FORMAT_DEFAULT; |
| 6536 | } |
| 6537 | |
| 6538 | audio_format_t format = AUDIO_FORMAT_DEFAULT; |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6539 | audio_format_t bestFormat = |
| 6540 | AudioPolicyManager::AudioPort::sPcmFormatCompareTable[ |
| 6541 | ARRAY_SIZE(AudioPolicyManager::AudioPort::sPcmFormatCompareTable) - 1]; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6542 | // For mixed output and inputs, use best mixer output format. Do not |
| 6543 | // limit format otherwise |
| 6544 | if ((mType != AUDIO_PORT_TYPE_MIX) || |
| 6545 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && |
Eric Laurent | d862237 | 2014-07-27 13:47:31 -0700 | [diff] [blame] | 6546 | (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) != 0)))) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6547 | bestFormat = AUDIO_FORMAT_INVALID; |
| 6548 | } |
| 6549 | |
| 6550 | for (size_t i = 0; i < mFormats.size(); i ++) { |
| 6551 | if ((compareFormats(mFormats[i], format) > 0) && |
| 6552 | (compareFormats(mFormats[i], bestFormat) <= 0)) { |
| 6553 | format = mFormats[i]; |
| 6554 | } |
| 6555 | } |
| 6556 | return format; |
| 6557 | } |
| 6558 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6559 | status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig, |
| 6560 | int index) const |
| 6561 | { |
| 6562 | if (index < 0 || (size_t)index >= mGains.size()) { |
| 6563 | return BAD_VALUE; |
| 6564 | } |
| 6565 | return mGains[index]->checkConfig(gainConfig); |
| 6566 | } |
| 6567 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6568 | void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const |
| 6569 | { |
| 6570 | const size_t SIZE = 256; |
| 6571 | char buffer[SIZE]; |
| 6572 | String8 result; |
| 6573 | |
| 6574 | if (mName.size() != 0) { |
| 6575 | snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string()); |
| 6576 | result.append(buffer); |
| 6577 | } |
| 6578 | |
| 6579 | if (mSamplingRates.size() != 0) { |
| 6580 | snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, ""); |
| 6581 | result.append(buffer); |
| 6582 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6583 | if (i == 0 && mSamplingRates[i] == 0) { |
| 6584 | snprintf(buffer, SIZE, "Dynamic"); |
| 6585 | } else { |
| 6586 | snprintf(buffer, SIZE, "%d", mSamplingRates[i]); |
| 6587 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6588 | result.append(buffer); |
| 6589 | result.append(i == (mSamplingRates.size() - 1) ? "" : ", "); |
| 6590 | } |
| 6591 | result.append("\n"); |
| 6592 | } |
| 6593 | |
| 6594 | if (mChannelMasks.size() != 0) { |
| 6595 | snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, ""); |
| 6596 | result.append(buffer); |
| 6597 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6598 | ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]); |
| 6599 | |
| 6600 | if (i == 0 && mChannelMasks[i] == 0) { |
| 6601 | snprintf(buffer, SIZE, "Dynamic"); |
| 6602 | } else { |
| 6603 | snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]); |
| 6604 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6605 | result.append(buffer); |
| 6606 | result.append(i == (mChannelMasks.size() - 1) ? "" : ", "); |
| 6607 | } |
| 6608 | result.append("\n"); |
| 6609 | } |
| 6610 | |
| 6611 | if (mFormats.size() != 0) { |
| 6612 | snprintf(buffer, SIZE, "%*s- formats: ", spaces, ""); |
| 6613 | result.append(buffer); |
| 6614 | for (size_t i = 0; i < mFormats.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6615 | const char *formatStr = enumToString(sFormatNameToEnumTable, |
| 6616 | ARRAY_SIZE(sFormatNameToEnumTable), |
| 6617 | mFormats[i]); |
| 6618 | if (i == 0 && strcmp(formatStr, "") == 0) { |
| 6619 | snprintf(buffer, SIZE, "Dynamic"); |
| 6620 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6621 | snprintf(buffer, SIZE, "%s", formatStr); |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6622 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6623 | result.append(buffer); |
| 6624 | result.append(i == (mFormats.size() - 1) ? "" : ", "); |
| 6625 | } |
| 6626 | result.append("\n"); |
| 6627 | } |
| 6628 | write(fd, result.string(), result.size()); |
| 6629 | if (mGains.size() != 0) { |
| 6630 | snprintf(buffer, SIZE, "%*s- gains:\n", spaces, ""); |
| 6631 | write(fd, buffer, strlen(buffer) + 1); |
| 6632 | result.append(buffer); |
| 6633 | for (size_t i = 0; i < mGains.size(); i++) { |
| 6634 | mGains[i]->dump(fd, spaces + 2, i); |
| 6635 | } |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | // --- AudioGain class implementation |
| 6640 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6641 | AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask) |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6642 | { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6643 | mIndex = index; |
| 6644 | mUseInChannelMask = useInChannelMask; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6645 | memset(&mGain, 0, sizeof(struct audio_gain)); |
| 6646 | } |
| 6647 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6648 | void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config) |
| 6649 | { |
| 6650 | config->index = mIndex; |
| 6651 | config->mode = mGain.mode; |
| 6652 | config->channel_mask = mGain.channel_mask; |
| 6653 | if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 6654 | config->values[0] = mGain.default_value; |
| 6655 | } else { |
| 6656 | uint32_t numValues; |
| 6657 | if (mUseInChannelMask) { |
| 6658 | numValues = audio_channel_count_from_in_mask(mGain.channel_mask); |
| 6659 | } else { |
| 6660 | numValues = audio_channel_count_from_out_mask(mGain.channel_mask); |
| 6661 | } |
| 6662 | for (size_t i = 0; i < numValues; i++) { |
| 6663 | config->values[i] = mGain.default_value; |
| 6664 | } |
| 6665 | } |
| 6666 | if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 6667 | config->ramp_duration_ms = mGain.min_ramp_ms; |
| 6668 | } |
| 6669 | } |
| 6670 | |
| 6671 | status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config) |
| 6672 | { |
| 6673 | if ((config->mode & ~mGain.mode) != 0) { |
| 6674 | return BAD_VALUE; |
| 6675 | } |
| 6676 | if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 6677 | if ((config->values[0] < mGain.min_value) || |
| 6678 | (config->values[0] > mGain.max_value)) { |
| 6679 | return BAD_VALUE; |
| 6680 | } |
| 6681 | } else { |
| 6682 | if ((config->channel_mask & ~mGain.channel_mask) != 0) { |
| 6683 | return BAD_VALUE; |
| 6684 | } |
| 6685 | uint32_t numValues; |
| 6686 | if (mUseInChannelMask) { |
| 6687 | numValues = audio_channel_count_from_in_mask(config->channel_mask); |
| 6688 | } else { |
| 6689 | numValues = audio_channel_count_from_out_mask(config->channel_mask); |
| 6690 | } |
| 6691 | for (size_t i = 0; i < numValues; i++) { |
| 6692 | if ((config->values[i] < mGain.min_value) || |
| 6693 | (config->values[i] > mGain.max_value)) { |
| 6694 | return BAD_VALUE; |
| 6695 | } |
| 6696 | } |
| 6697 | } |
| 6698 | if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 6699 | if ((config->ramp_duration_ms < mGain.min_ramp_ms) || |
| 6700 | (config->ramp_duration_ms > mGain.max_ramp_ms)) { |
| 6701 | return BAD_VALUE; |
| 6702 | } |
| 6703 | } |
| 6704 | return NO_ERROR; |
| 6705 | } |
| 6706 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6707 | void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const |
| 6708 | { |
| 6709 | const size_t SIZE = 256; |
| 6710 | char buffer[SIZE]; |
| 6711 | String8 result; |
| 6712 | |
| 6713 | snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1); |
| 6714 | result.append(buffer); |
| 6715 | snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode); |
| 6716 | result.append(buffer); |
| 6717 | snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask); |
| 6718 | result.append(buffer); |
| 6719 | snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value); |
| 6720 | result.append(buffer); |
| 6721 | snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value); |
| 6722 | result.append(buffer); |
| 6723 | snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value); |
| 6724 | result.append(buffer); |
| 6725 | snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value); |
| 6726 | result.append(buffer); |
| 6727 | snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms); |
| 6728 | result.append(buffer); |
| 6729 | snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms); |
| 6730 | result.append(buffer); |
| 6731 | |
| 6732 | write(fd, result.string(), result.size()); |
| 6733 | } |
| 6734 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6735 | // --- AudioPortConfig class implementation |
| 6736 | |
| 6737 | AudioPolicyManager::AudioPortConfig::AudioPortConfig() |
| 6738 | { |
| 6739 | mSamplingRate = 0; |
| 6740 | mChannelMask = AUDIO_CHANNEL_NONE; |
| 6741 | mFormat = AUDIO_FORMAT_INVALID; |
| 6742 | mGain.index = -1; |
| 6743 | } |
| 6744 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6745 | status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig( |
| 6746 | const struct audio_port_config *config, |
| 6747 | struct audio_port_config *backupConfig) |
| 6748 | { |
| 6749 | struct audio_port_config localBackupConfig; |
| 6750 | status_t status = NO_ERROR; |
| 6751 | |
| 6752 | localBackupConfig.config_mask = config->config_mask; |
| 6753 | toAudioPortConfig(&localBackupConfig); |
| 6754 | |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6755 | sp<AudioPort> audioport = getAudioPort(); |
| 6756 | if (audioport == 0) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6757 | status = NO_INIT; |
| 6758 | goto exit; |
| 6759 | } |
| 6760 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6761 | status = audioport->checkExactSamplingRate(config->sample_rate); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6762 | if (status != NO_ERROR) { |
| 6763 | goto exit; |
| 6764 | } |
| 6765 | mSamplingRate = config->sample_rate; |
| 6766 | } |
| 6767 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6768 | status = audioport->checkExactChannelMask(config->channel_mask); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6769 | if (status != NO_ERROR) { |
| 6770 | goto exit; |
| 6771 | } |
| 6772 | mChannelMask = config->channel_mask; |
| 6773 | } |
| 6774 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6775 | status = audioport->checkFormat(config->format); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6776 | if (status != NO_ERROR) { |
| 6777 | goto exit; |
| 6778 | } |
| 6779 | mFormat = config->format; |
| 6780 | } |
| 6781 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6782 | status = audioport->checkGain(&config->gain, config->gain.index); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6783 | if (status != NO_ERROR) { |
| 6784 | goto exit; |
| 6785 | } |
| 6786 | mGain = config->gain; |
| 6787 | } |
| 6788 | |
| 6789 | exit: |
| 6790 | if (status != NO_ERROR) { |
| 6791 | applyAudioPortConfig(&localBackupConfig); |
| 6792 | } |
| 6793 | if (backupConfig != NULL) { |
| 6794 | *backupConfig = localBackupConfig; |
| 6795 | } |
| 6796 | return status; |
| 6797 | } |
| 6798 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6799 | void AudioPolicyManager::AudioPortConfig::toAudioPortConfig( |
| 6800 | struct audio_port_config *dstConfig, |
| 6801 | const struct audio_port_config *srcConfig) const |
| 6802 | { |
| 6803 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 6804 | dstConfig->sample_rate = mSamplingRate; |
| 6805 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) { |
| 6806 | dstConfig->sample_rate = srcConfig->sample_rate; |
| 6807 | } |
| 6808 | } else { |
| 6809 | dstConfig->sample_rate = 0; |
| 6810 | } |
| 6811 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 6812 | dstConfig->channel_mask = mChannelMask; |
| 6813 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) { |
| 6814 | dstConfig->channel_mask = srcConfig->channel_mask; |
| 6815 | } |
| 6816 | } else { |
| 6817 | dstConfig->channel_mask = AUDIO_CHANNEL_NONE; |
| 6818 | } |
| 6819 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 6820 | dstConfig->format = mFormat; |
| 6821 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) { |
| 6822 | dstConfig->format = srcConfig->format; |
| 6823 | } |
| 6824 | } else { |
| 6825 | dstConfig->format = AUDIO_FORMAT_INVALID; |
| 6826 | } |
| 6827 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 6828 | dstConfig->gain = mGain; |
| 6829 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) { |
| 6830 | dstConfig->gain = srcConfig->gain; |
| 6831 | } |
| 6832 | } else { |
| 6833 | dstConfig->gain.index = -1; |
| 6834 | } |
| 6835 | if (dstConfig->gain.index != -1) { |
| 6836 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 6837 | } else { |
| 6838 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 6839 | } |
| 6840 | } |
| 6841 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6842 | // --- IOProfile class implementation |
| 6843 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6844 | AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role, |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6845 | const sp<HwModule>& module) |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6846 | : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6847 | { |
| 6848 | } |
| 6849 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6850 | AudioPolicyManager::IOProfile::~IOProfile() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6851 | { |
| 6852 | } |
| 6853 | |
| 6854 | // checks if the IO profile is compatible with specified parameters. |
| 6855 | // Sampling rate, format and channel mask must be specified in order to |
| 6856 | // get a valid a match |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6857 | bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6858 | uint32_t samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6859 | uint32_t *updatedSamplingRate, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6860 | audio_format_t format, |
| 6861 | audio_channel_mask_t channelMask, |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 6862 | uint32_t flags) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6863 | { |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6864 | const bool isPlaybackThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SOURCE; |
| 6865 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; |
| 6866 | ALOG_ASSERT(isPlaybackThread != isRecordThread); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6867 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6868 | if ((mSupportedDevices.types() & device) != device) { |
| 6869 | return false; |
| 6870 | } |
| 6871 | |
| 6872 | if (samplingRate == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6873 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6874 | } |
| 6875 | uint32_t myUpdatedSamplingRate = samplingRate; |
| 6876 | if (isPlaybackThread && checkExactSamplingRate(samplingRate) != NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6877 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6878 | } |
| 6879 | if (isRecordThread && checkCompatibleSamplingRate(samplingRate, &myUpdatedSamplingRate) != |
| 6880 | NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6881 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | if (!audio_is_valid_format(format) || checkFormat(format) != NO_ERROR) { |
| 6885 | return false; |
| 6886 | } |
| 6887 | |
| 6888 | if (isPlaybackThread && (!audio_is_output_channel(channelMask) || |
| 6889 | checkExactChannelMask(channelMask) != NO_ERROR)) { |
| 6890 | return false; |
| 6891 | } |
| 6892 | if (isRecordThread && (!audio_is_input_channel(channelMask) || |
| 6893 | checkCompatibleChannelMask(channelMask) != NO_ERROR)) { |
| 6894 | return false; |
| 6895 | } |
| 6896 | |
| 6897 | if (isPlaybackThread && (mFlags & flags) != flags) { |
| 6898 | return false; |
| 6899 | } |
| 6900 | // The only input flag that is allowed to be different is the fast flag. |
| 6901 | // An existing fast stream is compatible with a normal track request. |
| 6902 | // An existing normal stream is compatible with a fast track request, |
| 6903 | // but the fast request will be denied by AudioFlinger and converted to normal track. |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 6904 | if (isRecordThread && ((mFlags ^ flags) & |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6905 | ~AUDIO_INPUT_FLAG_FAST)) { |
| 6906 | return false; |
| 6907 | } |
| 6908 | |
| 6909 | if (updatedSamplingRate != NULL) { |
| 6910 | *updatedSamplingRate = myUpdatedSamplingRate; |
| 6911 | } |
| 6912 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6913 | } |
| 6914 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6915 | void AudioPolicyManager::IOProfile::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6916 | { |
| 6917 | const size_t SIZE = 256; |
| 6918 | char buffer[SIZE]; |
| 6919 | String8 result; |
| 6920 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6921 | AudioPort::dump(fd, 4); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6922 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6923 | snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags); |
| 6924 | result.append(buffer); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6925 | snprintf(buffer, SIZE, " - devices:\n"); |
| 6926 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6927 | write(fd, result.string(), result.size()); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6928 | for (size_t i = 0; i < mSupportedDevices.size(); i++) { |
| 6929 | mSupportedDevices[i]->dump(fd, 6, i); |
| 6930 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6931 | } |
| 6932 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6933 | void AudioPolicyManager::IOProfile::log() |
| 6934 | { |
| 6935 | const size_t SIZE = 256; |
| 6936 | char buffer[SIZE]; |
| 6937 | String8 result; |
| 6938 | |
| 6939 | ALOGV(" - sampling rates: "); |
| 6940 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
| 6941 | ALOGV(" %d", mSamplingRates[i]); |
| 6942 | } |
| 6943 | |
| 6944 | ALOGV(" - channel masks: "); |
| 6945 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
| 6946 | ALOGV(" 0x%04x", mChannelMasks[i]); |
| 6947 | } |
| 6948 | |
| 6949 | ALOGV(" - formats: "); |
| 6950 | for (size_t i = 0; i < mFormats.size(); i++) { |
| 6951 | ALOGV(" 0x%08x", mFormats[i]); |
| 6952 | } |
| 6953 | |
| 6954 | ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types()); |
| 6955 | ALOGV(" - flags: 0x%04x\n", mFlags); |
| 6956 | } |
| 6957 | |
| 6958 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6959 | // --- DeviceDescriptor implementation |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6960 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6961 | |
| 6962 | AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) : |
| 6963 | AudioPort(name, AUDIO_PORT_TYPE_DEVICE, |
| 6964 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : |
| 6965 | AUDIO_PORT_ROLE_SOURCE, |
| 6966 | NULL), |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6967 | mDeviceType(type), mAddress(""), mId(0) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6968 | { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6969 | if (mGains.size() > 0) { |
| 6970 | mGains[0]->getDefaultConfig(&mGain); |
| 6971 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6972 | } |
| 6973 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6974 | bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6975 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6976 | // Devices are considered equal if they: |
| 6977 | // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) |
| 6978 | // - have the same address or one device does not specify the address |
| 6979 | // - 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] | 6980 | return (mDeviceType == other->mDeviceType) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6981 | (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && |
Eric Laurent | 2f8a36f | 2014-03-26 19:05:55 -0700 | [diff] [blame] | 6982 | (mChannelMask == 0 || other->mChannelMask == 0 || |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6983 | mChannelMask == other->mChannelMask); |
| 6984 | } |
| 6985 | |
| 6986 | void AudioPolicyManager::DeviceVector::refreshTypes() |
| 6987 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6988 | mDeviceTypes = AUDIO_DEVICE_NONE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6989 | for(size_t i = 0; i < size(); i++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6990 | mDeviceTypes |= itemAt(i)->mDeviceType; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6991 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6992 | ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const |
| 6996 | { |
| 6997 | for(size_t i = 0; i < size(); i++) { |
| 6998 | if (item->equals(itemAt(i))) { |
| 6999 | return i; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7000 | } |
| 7001 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7002 | return -1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7003 | } |
| 7004 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7005 | ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7006 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7007 | ssize_t ret = indexOf(item); |
| 7008 | |
| 7009 | if (ret < 0) { |
| 7010 | ret = SortedVector::add(item); |
| 7011 | if (ret >= 0) { |
| 7012 | refreshTypes(); |
| 7013 | } |
| 7014 | } else { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7015 | ALOGW("DeviceVector::add device %08x already in", item->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7016 | ret = -1; |
| 7017 | } |
| 7018 | return ret; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7019 | } |
| 7020 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7021 | ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item) |
| 7022 | { |
| 7023 | size_t i; |
| 7024 | ssize_t ret = indexOf(item); |
| 7025 | |
| 7026 | if (ret < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7027 | ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7028 | } else { |
| 7029 | ret = SortedVector::removeAt(ret); |
| 7030 | if (ret >= 0) { |
| 7031 | refreshTypes(); |
| 7032 | } |
| 7033 | } |
| 7034 | return ret; |
| 7035 | } |
| 7036 | |
| 7037 | void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types) |
| 7038 | { |
| 7039 | DeviceVector deviceList; |
| 7040 | |
| 7041 | uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; |
| 7042 | types &= ~role_bit; |
| 7043 | |
| 7044 | while (types) { |
| 7045 | uint32_t i = 31 - __builtin_clz(types); |
| 7046 | uint32_t type = 1 << i; |
| 7047 | types &= ~type; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7048 | add(new DeviceDescriptor(String8(""), type | role_bit)); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7049 | } |
| 7050 | } |
| 7051 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7052 | void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name, |
| 7053 | const DeviceVector& declaredDevices) |
| 7054 | { |
| 7055 | char *devName = strtok(name, "|"); |
| 7056 | while (devName != NULL) { |
| 7057 | if (strlen(devName) != 0) { |
| 7058 | audio_devices_t type = stringToEnum(sDeviceNameToEnumTable, |
| 7059 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7060 | devName); |
| 7061 | if (type != AUDIO_DEVICE_NONE) { |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 7062 | sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(""), type); |
| 7063 | if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 7064 | dev->mAddress = String8("0"); |
| 7065 | } |
| 7066 | add(dev); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7067 | } else { |
| 7068 | sp<DeviceDescriptor> deviceDesc = |
| 7069 | declaredDevices.getDeviceFromName(String8(devName)); |
| 7070 | if (deviceDesc != 0) { |
| 7071 | add(deviceDesc); |
| 7072 | } |
| 7073 | } |
| 7074 | } |
| 7075 | devName = strtok(NULL, "|"); |
| 7076 | } |
| 7077 | } |
| 7078 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7079 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice( |
| 7080 | audio_devices_t type, String8 address) const |
| 7081 | { |
| 7082 | sp<DeviceDescriptor> device; |
| 7083 | for (size_t i = 0; i < size(); i++) { |
| 7084 | if (itemAt(i)->mDeviceType == type) { |
| 7085 | device = itemAt(i); |
| 7086 | if (itemAt(i)->mAddress = address) { |
| 7087 | break; |
| 7088 | } |
| 7089 | } |
| 7090 | } |
| 7091 | ALOGV("DeviceVector::getDevice() for type %d address %s found %p", |
| 7092 | type, address.string(), device.get()); |
| 7093 | return device; |
| 7094 | } |
| 7095 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7096 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId( |
| 7097 | audio_port_handle_t id) const |
| 7098 | { |
| 7099 | sp<DeviceDescriptor> device; |
| 7100 | for (size_t i = 0; i < size(); i++) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 7101 | 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] | 7102 | if (itemAt(i)->mId == id) { |
| 7103 | device = itemAt(i); |
| 7104 | break; |
| 7105 | } |
| 7106 | } |
| 7107 | return device; |
| 7108 | } |
| 7109 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7110 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType( |
| 7111 | audio_devices_t type) const |
| 7112 | { |
| 7113 | DeviceVector devices; |
| 7114 | for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { |
| 7115 | if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) { |
| 7116 | devices.add(itemAt(i)); |
| 7117 | type &= ~itemAt(i)->mDeviceType; |
| 7118 | ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", |
| 7119 | itemAt(i)->mDeviceType, itemAt(i).get()); |
| 7120 | } |
| 7121 | } |
| 7122 | return devices; |
| 7123 | } |
| 7124 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7125 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromTypeAddr( |
| 7126 | audio_devices_t type, String8 address) const |
| 7127 | { |
| 7128 | DeviceVector devices; |
| 7129 | //ALOGV(" looking for device=%x, addr=%s", type, address.string()); |
| 7130 | for (size_t i = 0; i < size(); i++) { |
| 7131 | //ALOGV(" at i=%d: device=%x, addr=%s", |
| 7132 | // i, itemAt(i)->mDeviceType, itemAt(i)->mAddress.string()); |
| 7133 | if (itemAt(i)->mDeviceType == type) { |
| 7134 | if (itemAt(i)->mAddress == address) { |
| 7135 | //ALOGV(" found matching address %s", address.string()); |
| 7136 | devices.add(itemAt(i)); |
| 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | return devices; |
| 7141 | } |
| 7142 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7143 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName( |
| 7144 | const String8& name) const |
| 7145 | { |
| 7146 | sp<DeviceDescriptor> device; |
| 7147 | for (size_t i = 0; i < size(); i++) { |
| 7148 | if (itemAt(i)->mName == name) { |
| 7149 | device = itemAt(i); |
| 7150 | break; |
| 7151 | } |
| 7152 | } |
| 7153 | return device; |
| 7154 | } |
| 7155 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7156 | void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig( |
| 7157 | struct audio_port_config *dstConfig, |
| 7158 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7159 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7160 | dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; |
| 7161 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 7162 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7163 | } |
| 7164 | |
| 7165 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 7166 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7167 | dstConfig->id = mId; |
| 7168 | dstConfig->role = audio_is_output_device(mDeviceType) ? |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7169 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7170 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7171 | dstConfig->ext.device.type = mDeviceType; |
| 7172 | dstConfig->ext.device.hw_module = mModule->mHandle; |
| 7173 | strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7174 | } |
| 7175 | |
| 7176 | void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const |
| 7177 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7178 | ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7179 | AudioPort::toAudioPort(port); |
| 7180 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7181 | toAudioPortConfig(&port->active_config); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7182 | port->ext.device.type = mDeviceType; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7183 | port->ext.device.hw_module = mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7184 | strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); |
| 7185 | } |
| 7186 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7187 | status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7188 | { |
| 7189 | const size_t SIZE = 256; |
| 7190 | char buffer[SIZE]; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7191 | String8 result; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7192 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7193 | snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); |
| 7194 | result.append(buffer); |
| 7195 | if (mId != 0) { |
| 7196 | snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); |
| 7197 | result.append(buffer); |
| 7198 | } |
| 7199 | snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", |
| 7200 | enumToString(sDeviceNameToEnumTable, |
| 7201 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7202 | mDeviceType)); |
| 7203 | result.append(buffer); |
| 7204 | if (mAddress.size() != 0) { |
| 7205 | snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); |
| 7206 | result.append(buffer); |
| 7207 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7208 | write(fd, result.string(), result.size()); |
| 7209 | AudioPort::dump(fd, spaces); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7210 | |
| 7211 | return NO_ERROR; |
| 7212 | } |
| 7213 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 7214 | status_t AudioPolicyManager::AudioPatch::dump(int fd, int spaces, int index) const |
| 7215 | { |
| 7216 | const size_t SIZE = 256; |
| 7217 | char buffer[SIZE]; |
| 7218 | String8 result; |
| 7219 | |
| 7220 | |
| 7221 | snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1); |
| 7222 | result.append(buffer); |
| 7223 | snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle); |
| 7224 | result.append(buffer); |
| 7225 | snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle); |
| 7226 | result.append(buffer); |
| 7227 | snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid); |
| 7228 | result.append(buffer); |
| 7229 | snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources); |
| 7230 | result.append(buffer); |
| 7231 | for (size_t i = 0; i < mPatch.num_sources; i++) { |
| 7232 | if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) { |
| 7233 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", |
| 7234 | mPatch.sources[i].id, enumToString(sDeviceNameToEnumTable, |
| 7235 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7236 | mPatch.sources[i].ext.device.type)); |
| 7237 | } else { |
| 7238 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", |
| 7239 | mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle); |
| 7240 | } |
| 7241 | result.append(buffer); |
| 7242 | } |
| 7243 | snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks); |
| 7244 | result.append(buffer); |
| 7245 | for (size_t i = 0; i < mPatch.num_sinks; i++) { |
| 7246 | if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) { |
| 7247 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", |
| 7248 | mPatch.sinks[i].id, enumToString(sDeviceNameToEnumTable, |
| 7249 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7250 | mPatch.sinks[i].ext.device.type)); |
| 7251 | } else { |
| 7252 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", |
| 7253 | mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle); |
| 7254 | } |
| 7255 | result.append(buffer); |
| 7256 | } |
| 7257 | |
| 7258 | write(fd, result.string(), result.size()); |
| 7259 | return NO_ERROR; |
| 7260 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7261 | |
| 7262 | // --- audio_policy.conf file parsing |
| 7263 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7264 | uint32_t AudioPolicyManager::parseOutputFlagNames(char *name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7265 | { |
| 7266 | uint32_t flag = 0; |
| 7267 | |
| 7268 | // it is OK to cast name to non const here as we are not going to use it after |
| 7269 | // strtok() modifies it |
| 7270 | char *flagName = strtok(name, "|"); |
| 7271 | while (flagName != NULL) { |
| 7272 | if (strlen(flagName) != 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7273 | flag |= stringToEnum(sOutputFlagNameToEnumTable, |
| 7274 | ARRAY_SIZE(sOutputFlagNameToEnumTable), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7275 | flagName); |
| 7276 | } |
| 7277 | flagName = strtok(NULL, "|"); |
| 7278 | } |
| 7279 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 7280 | // and all common behaviors are driven by checking only the direct flag |
| 7281 | // this should normally be set appropriately in the policy configuration file |
| 7282 | if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 7283 | flag |= AUDIO_OUTPUT_FLAG_DIRECT; |
| 7284 | } |
| 7285 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7286 | return flag; |
| 7287 | } |
| 7288 | |
| 7289 | uint32_t AudioPolicyManager::parseInputFlagNames(char *name) |
| 7290 | { |
| 7291 | uint32_t flag = 0; |
| 7292 | |
| 7293 | // it is OK to cast name to non const here as we are not going to use it after |
| 7294 | // strtok() modifies it |
| 7295 | char *flagName = strtok(name, "|"); |
| 7296 | while (flagName != NULL) { |
| 7297 | if (strlen(flagName) != 0) { |
| 7298 | flag |= stringToEnum(sInputFlagNameToEnumTable, |
| 7299 | ARRAY_SIZE(sInputFlagNameToEnumTable), |
| 7300 | flagName); |
| 7301 | } |
| 7302 | flagName = strtok(NULL, "|"); |
| 7303 | } |
| 7304 | return flag; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7305 | } |
| 7306 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7307 | audio_devices_t AudioPolicyManager::parseDeviceNames(char *name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7308 | { |
| 7309 | uint32_t device = 0; |
| 7310 | |
| 7311 | char *devName = strtok(name, "|"); |
| 7312 | while (devName != NULL) { |
| 7313 | if (strlen(devName) != 0) { |
| 7314 | device |= stringToEnum(sDeviceNameToEnumTable, |
| 7315 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7316 | devName); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7317 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7318 | devName = strtok(NULL, "|"); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7319 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7320 | return device; |
| 7321 | } |
| 7322 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7323 | void AudioPolicyManager::loadHwModule(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7324 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7325 | status_t status = NAME_NOT_FOUND; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7326 | cnode *node; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7327 | sp<HwModule> module = new HwModule(root->name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7328 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7329 | node = config_find(root, DEVICES_TAG); |
| 7330 | if (node != NULL) { |
| 7331 | node = node->first_child; |
| 7332 | while (node) { |
| 7333 | ALOGV("loadHwModule() loading device %s", node->name); |
| 7334 | status_t tmpStatus = module->loadDevice(node); |
| 7335 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7336 | status = tmpStatus; |
| 7337 | } |
| 7338 | node = node->next; |
| 7339 | } |
| 7340 | } |
| 7341 | node = config_find(root, OUTPUTS_TAG); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7342 | if (node != NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7343 | node = node->first_child; |
| 7344 | while (node) { |
| 7345 | ALOGV("loadHwModule() loading output %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7346 | status_t tmpStatus = module->loadOutput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7347 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7348 | status = tmpStatus; |
| 7349 | } |
| 7350 | node = node->next; |
| 7351 | } |
| 7352 | } |
| 7353 | node = config_find(root, INPUTS_TAG); |
| 7354 | if (node != NULL) { |
| 7355 | node = node->first_child; |
| 7356 | while (node) { |
| 7357 | ALOGV("loadHwModule() loading input %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7358 | status_t tmpStatus = module->loadInput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7359 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7360 | status = tmpStatus; |
| 7361 | } |
| 7362 | node = node->next; |
| 7363 | } |
| 7364 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7365 | loadGlobalConfig(root, module); |
| 7366 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7367 | if (status == NO_ERROR) { |
| 7368 | mHwModules.add(module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7369 | } |
| 7370 | } |
| 7371 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7372 | void AudioPolicyManager::loadHwModules(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7373 | { |
| 7374 | cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); |
| 7375 | if (node == NULL) { |
| 7376 | return; |
| 7377 | } |
| 7378 | |
| 7379 | node = node->first_child; |
| 7380 | while (node) { |
| 7381 | ALOGV("loadHwModules() loading module %s", node->name); |
| 7382 | loadHwModule(node); |
| 7383 | node = node->next; |
| 7384 | } |
| 7385 | } |
| 7386 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7387 | void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7388 | { |
| 7389 | cnode *node = config_find(root, GLOBAL_CONFIG_TAG); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 7390 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7391 | if (node == NULL) { |
| 7392 | return; |
| 7393 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7394 | DeviceVector declaredDevices; |
| 7395 | if (module != NULL) { |
| 7396 | declaredDevices = module->mDeclaredDevices; |
| 7397 | } |
| 7398 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7399 | node = node->first_child; |
| 7400 | while (node) { |
| 7401 | if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7402 | mAvailableOutputDevices.loadDevicesFromName((char *)node->value, |
| 7403 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7404 | ALOGV("loadGlobalConfig() Attached Output Devices %08x", |
| 7405 | mAvailableOutputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7406 | } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7407 | audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7408 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7409 | (char *)node->value); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7410 | if (device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7411 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7412 | } else { |
| 7413 | ALOGW("loadGlobalConfig() default device not specified"); |
| 7414 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7415 | ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7416 | } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7417 | mAvailableInputDevices.loadDevicesFromName((char *)node->value, |
| 7418 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7419 | ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7420 | } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) { |
| 7421 | mSpeakerDrcEnabled = stringToBool((char *)node->value); |
| 7422 | ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 7423 | } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) { |
| 7424 | uint32_t major, minor; |
| 7425 | sscanf((char *)node->value, "%u.%u", &major, &minor); |
| 7426 | module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor); |
| 7427 | ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u", |
| 7428 | module->mHalVersion, major, minor); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7429 | } |
| 7430 | node = node->next; |
| 7431 | } |
| 7432 | } |
| 7433 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7434 | status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7435 | { |
| 7436 | cnode *root; |
| 7437 | char *data; |
| 7438 | |
| 7439 | data = (char *)load_file(path, NULL); |
| 7440 | if (data == NULL) { |
| 7441 | return -ENODEV; |
| 7442 | } |
| 7443 | root = config_node("", ""); |
| 7444 | config_load(root, data); |
| 7445 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7446 | loadHwModules(root); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7447 | // legacy audio_policy.conf files have one global_configuration section |
| 7448 | loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7449 | config_free(root); |
| 7450 | free(root); |
| 7451 | free(data); |
| 7452 | |
| 7453 | ALOGI("loadAudioPolicyConfig() loaded %s\n", path); |
| 7454 | |
| 7455 | return NO_ERROR; |
| 7456 | } |
| 7457 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7458 | void AudioPolicyManager::defaultAudioPolicyConfig(void) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7459 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7460 | sp<HwModule> module; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7461 | sp<IOProfile> profile; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7462 | sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""), |
| 7463 | AUDIO_DEVICE_IN_BUILTIN_MIC); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7464 | mAvailableOutputDevices.add(mDefaultOutputDevice); |
| 7465 | mAvailableInputDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7466 | |
| 7467 | module = new HwModule("primary"); |
| 7468 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7469 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7470 | profile->mSamplingRates.add(44100); |
| 7471 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 7472 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7473 | profile->mSupportedDevices.add(mDefaultOutputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7474 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; |
| 7475 | module->mOutputProfiles.add(profile); |
| 7476 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7477 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7478 | profile->mSamplingRates.add(8000); |
| 7479 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 7480 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7481 | profile->mSupportedDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7482 | module->mInputProfiles.add(profile); |
| 7483 | |
| 7484 | mHwModules.add(module); |
| 7485 | } |
| 7486 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7487 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 7488 | { |
| 7489 | // flags to stream type mapping |
| 7490 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 7491 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 7492 | } |
| 7493 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 7494 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 7495 | } |
| 7496 | |
| 7497 | // usage to stream type mapping |
| 7498 | switch (attr->usage) { |
| 7499 | case AUDIO_USAGE_MEDIA: |
| 7500 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7501 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7502 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame^] | 7503 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7504 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7505 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7506 | return AUDIO_STREAM_SYSTEM; |
| 7507 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7508 | return AUDIO_STREAM_VOICE_CALL; |
| 7509 | |
| 7510 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7511 | return AUDIO_STREAM_DTMF; |
| 7512 | |
| 7513 | case AUDIO_USAGE_ALARM: |
| 7514 | return AUDIO_STREAM_ALARM; |
| 7515 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7516 | return AUDIO_STREAM_RING; |
| 7517 | |
| 7518 | case AUDIO_USAGE_NOTIFICATION: |
| 7519 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7520 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7521 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7522 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7523 | return AUDIO_STREAM_NOTIFICATION; |
| 7524 | |
| 7525 | case AUDIO_USAGE_UNKNOWN: |
| 7526 | default: |
| 7527 | return AUDIO_STREAM_MUSIC; |
| 7528 | } |
| 7529 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7530 | }; // namespace android |