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 | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 46 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 47 | #include <soundtrigger/SoundTrigger.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 48 | #include "AudioPolicyManager.h" |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 49 | #include "audio_policy_conf.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 50 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 51 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 52 | |
| 53 | // ---------------------------------------------------------------------------- |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 54 | // Definitions for audio_policy.conf file parsing |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | struct StringToEnum { |
| 58 | const char *name; |
| 59 | uint32_t value; |
| 60 | }; |
| 61 | |
| 62 | #define STRING_TO_ENUM(string) { #string, string } |
| 63 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 64 | |
| 65 | const StringToEnum sDeviceNameToEnumTable[] = { |
| 66 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE), |
| 67 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER), |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 68 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER_SAFE), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 69 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET), |
| 70 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE), |
| 71 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO), |
| 72 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET), |
| 73 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT), |
| 74 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO), |
| 75 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP), |
| 76 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES), |
| 77 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), |
| 78 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP), |
| 79 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 80 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 81 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET), |
| 82 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET), |
| 83 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY), |
| 84 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE), |
| 85 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB), |
| 86 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 87 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_TELEPHONY_TX), |
| 88 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_LINE), |
| 89 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_HDMI_ARC), |
| 90 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPDIF), |
| 91 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_FM), |
Eric Laurent | e1d37b7 | 2014-07-29 10:26:26 -0700 | [diff] [blame] | 92 | STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_LINE), |
Eric Laurent | a57ab8d | 2014-07-30 10:01:42 -0500 | [diff] [blame] | 93 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AMBIENT), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 94 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC), |
| 95 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET), |
| 96 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ALL_SCO), |
| 97 | STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET), |
| 98 | STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 99 | STRING_TO_ENUM(AUDIO_DEVICE_IN_HDMI), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 100 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX), |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 101 | STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 102 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC), |
| 103 | STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX), |
| 104 | STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET), |
| 105 | STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET), |
| 106 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY), |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 107 | STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE), |
Eric Laurent | 1b77623 | 2014-05-19 17:26:41 -0700 | [diff] [blame] | 108 | STRING_TO_ENUM(AUDIO_DEVICE_IN_FM_TUNER), |
| 109 | STRING_TO_ENUM(AUDIO_DEVICE_IN_TV_TUNER), |
| 110 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LINE), |
| 111 | STRING_TO_ENUM(AUDIO_DEVICE_IN_SPDIF), |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 112 | STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP), |
Terry Heo | 7999a22 | 2014-06-27 15:23:36 +0900 | [diff] [blame] | 113 | STRING_TO_ENUM(AUDIO_DEVICE_IN_LOOPBACK), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 116 | const StringToEnum sOutputFlagNameToEnumTable[] = { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 117 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT), |
| 118 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY), |
| 119 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST), |
| 120 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER), |
| 121 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD), |
| 122 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING), |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 123 | STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 126 | const StringToEnum sInputFlagNameToEnumTable[] = { |
| 127 | STRING_TO_ENUM(AUDIO_INPUT_FLAG_FAST), |
| 128 | STRING_TO_ENUM(AUDIO_INPUT_FLAG_HW_HOTWORD), |
| 129 | }; |
| 130 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 131 | const StringToEnum sFormatNameToEnumTable[] = { |
| 132 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT), |
| 133 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT), |
| 134 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT), |
| 135 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT), |
| 136 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT), |
| 137 | STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED), |
| 138 | STRING_TO_ENUM(AUDIO_FORMAT_MP3), |
| 139 | STRING_TO_ENUM(AUDIO_FORMAT_AAC), |
aarti jadhav-gaikwad | 2829edc | 2014-06-18 15:25:26 +0530 | [diff] [blame] | 140 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_MAIN), |
| 141 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LC), |
| 142 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SSR), |
| 143 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LTP), |
| 144 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V1), |
| 145 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_SCALABLE), |
| 146 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ERLC), |
| 147 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_LD), |
| 148 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_HE_V2), |
| 149 | STRING_TO_ENUM(AUDIO_FORMAT_AAC_ELD), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 150 | STRING_TO_ENUM(AUDIO_FORMAT_VORBIS), |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 151 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1), |
| 152 | STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2), |
| 153 | STRING_TO_ENUM(AUDIO_FORMAT_OPUS), |
| 154 | STRING_TO_ENUM(AUDIO_FORMAT_AC3), |
| 155 | STRING_TO_ENUM(AUDIO_FORMAT_E_AC3), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | const StringToEnum sOutChannelsNameToEnumTable[] = { |
| 159 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO), |
| 160 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
Andy Hung | 3a0fe12 | 2014-07-29 17:56:46 -0700 | [diff] [blame] | 161 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_QUAD), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 163 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 164 | }; |
| 165 | |
| 166 | const StringToEnum sInChannelsNameToEnumTable[] = { |
| 167 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO), |
| 168 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO), |
| 169 | STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK), |
| 170 | }; |
| 171 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 172 | const StringToEnum sGainModeNameToEnumTable[] = { |
| 173 | STRING_TO_ENUM(AUDIO_GAIN_MODE_JOINT), |
| 174 | STRING_TO_ENUM(AUDIO_GAIN_MODE_CHANNELS), |
| 175 | STRING_TO_ENUM(AUDIO_GAIN_MODE_RAMP), |
| 176 | }; |
| 177 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 178 | |
| 179 | uint32_t AudioPolicyManager::stringToEnum(const struct StringToEnum *table, |
| 180 | size_t size, |
| 181 | const char *name) |
| 182 | { |
| 183 | for (size_t i = 0; i < size; i++) { |
| 184 | if (strcmp(table[i].name, name) == 0) { |
| 185 | ALOGV("stringToEnum() found %s", table[i].name); |
| 186 | return table[i].value; |
| 187 | } |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | const char *AudioPolicyManager::enumToString(const struct StringToEnum *table, |
| 193 | size_t size, |
| 194 | uint32_t value) |
| 195 | { |
| 196 | for (size_t i = 0; i < size; i++) { |
| 197 | if (table[i].value == value) { |
| 198 | return table[i].name; |
| 199 | } |
| 200 | } |
| 201 | return ""; |
| 202 | } |
| 203 | |
| 204 | bool AudioPolicyManager::stringToBool(const char *value) |
| 205 | { |
| 206 | return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0)); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | // ---------------------------------------------------------------------------- |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 211 | // AudioPolicyInterface implementation |
| 212 | // ---------------------------------------------------------------------------- |
| 213 | |
| 214 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 215 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 216 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 217 | const char *device_address) |
| 218 | { |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 219 | String8 address = (device_address == NULL) ? String8("") : String8(device_address); |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 220 | // handle legacy remote submix case where the address was not always specified |
| 221 | if (deviceDistinguishesOnAddress(device) && (address.length() == 0)) { |
| 222 | address = String8("0"); |
| 223 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 224 | |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 225 | ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", |
| 226 | device, state, address.string()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 227 | |
| 228 | // connect/disconnect only 1 device at a time |
| 229 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 230 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 231 | // handle output devices |
| 232 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 233 | SortedVector <audio_io_handle_t> outputs; |
| 234 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 235 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
| 236 | devDesc->mAddress = address; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 237 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 238 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 239 | // save a copy of the opened output descriptors before any output is opened or closed |
| 240 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 241 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 242 | switch (state) |
| 243 | { |
| 244 | // handle output device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 245 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 246 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 247 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 251 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | index = mAvailableOutputDevices.add(devDesc); |
| 254 | if (index >= 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 255 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 256 | if (module == 0) { |
| 257 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 258 | device); |
| 259 | mAvailableOutputDevices.remove(devDesc); |
| 260 | return INVALID_OPERATION; |
| 261 | } |
| 262 | mAvailableOutputDevices[index]->mId = nextUniqueId(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 263 | mAvailableOutputDevices[index]->mModule = module; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 264 | } else { |
| 265 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 268 | if (checkOutputsForDevice(devDesc, state, outputs, address) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 269 | mAvailableOutputDevices.remove(devDesc); |
| 270 | return INVALID_OPERATION; |
| 271 | } |
| 272 | // outputs should never be empty here |
| 273 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 274 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 275 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 276 | outputs.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 277 | break; |
| 278 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 279 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 280 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 281 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 282 | return INVALID_OPERATION; |
| 283 | } |
| 284 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 285 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 286 | |
| 287 | // Set Disconnect to HALs |
| 288 | AudioParameter param = AudioParameter(address); |
| 289 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 290 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 291 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 293 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 294 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 295 | checkOutputsForDevice(devDesc, state, outputs, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 296 | } break; |
| 297 | |
| 298 | default: |
| 299 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 300 | return BAD_VALUE; |
| 301 | } |
| 302 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 303 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 304 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | checkA2dpSuspend(); |
| 306 | checkOutputForAllStrategies(); |
| 307 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 308 | if (!outputs.isEmpty()) { |
| 309 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 310 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 311 | // close unused outputs after device disconnection or direct outputs that have been |
| 312 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 313 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 315 | (desc->mDirectOpenCount == 0))) { |
| 316 | closeOutput(outputs[i]); |
| 317 | } |
| 318 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 319 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 320 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 324 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 325 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 326 | updateCallRouting(newDevice); |
| 327 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 328 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 329 | audio_io_handle_t output = mOutputs.keyAt(i); |
| 330 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 331 | audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i), |
| 332 | true /*fromCache*/); |
| 333 | // do not force device change on duplicated output because if device is 0, it will |
| 334 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 335 | // a valid device selection on those outputs. |
| 336 | bool force = !mOutputs.valueAt(i)->isDuplicated() |
| 337 | && (!deviceDistinguishesOnAddress(device) |
| 338 | // always force when disconnecting (a non-duplicated device) |
| 339 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
| 340 | setOutputDevice(output, newDevice, force, 0); |
| 341 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 344 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 345 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 346 | } // end if is output device |
| 347 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 348 | // handle input devices |
| 349 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 350 | SortedVector <audio_io_handle_t> inputs; |
| 351 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 352 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
| 353 | devDesc->mAddress = address; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 354 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 355 | switch (state) |
| 356 | { |
| 357 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 358 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 359 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 361 | return INVALID_OPERATION; |
| 362 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 363 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 364 | if (module == NULL) { |
| 365 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 366 | device); |
| 367 | return INVALID_OPERATION; |
| 368 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 369 | if (checkInputsForDevice(device, state, inputs, address) != NO_ERROR) { |
| 370 | return INVALID_OPERATION; |
| 371 | } |
| 372 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 373 | index = mAvailableInputDevices.add(devDesc); |
| 374 | if (index >= 0) { |
| 375 | mAvailableInputDevices[index]->mId = nextUniqueId(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 376 | mAvailableInputDevices[index]->mModule = module; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 377 | } else { |
| 378 | return NO_MEMORY; |
| 379 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 380 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | |
| 382 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 383 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 384 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 385 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 386 | return INVALID_OPERATION; |
| 387 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 388 | |
| 389 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 390 | |
| 391 | // Set Disconnect to HALs |
| 392 | AudioParameter param = AudioParameter(address); |
| 393 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 394 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 395 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 396 | checkInputsForDevice(device, state, inputs, address); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 397 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 398 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 399 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 400 | |
| 401 | default: |
| 402 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 403 | return BAD_VALUE; |
| 404 | } |
| 405 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 406 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 407 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 408 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 409 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 410 | updateCallRouting(newDevice); |
| 411 | } |
| 412 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 413 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 414 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 415 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 416 | |
| 417 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 418 | return BAD_VALUE; |
| 419 | } |
| 420 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 421 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 422 | const char *device_address) |
| 423 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 424 | audio_policy_dev_state_t state = AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 425 | sp<DeviceDescriptor> devDesc = new DeviceDescriptor(String8(""), device); |
Eric Laurent | 2222601 | 2014-08-01 17:00:54 -0700 | [diff] [blame] | 426 | devDesc->mAddress = (device_address == NULL) ? String8("") : String8(device_address); |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 427 | // handle legacy remote submix case where the address was not always specified |
| 428 | if (deviceDistinguishesOnAddress(device) && (devDesc->mAddress.length() == 0)) { |
| 429 | devDesc->mAddress = String8("0"); |
| 430 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 431 | ssize_t index; |
| 432 | DeviceVector *deviceVector; |
| 433 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 434 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 435 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 436 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 437 | deviceVector = &mAvailableInputDevices; |
| 438 | } else { |
| 439 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 440 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 443 | index = deviceVector->indexOf(devDesc); |
| 444 | if (index >= 0) { |
| 445 | return AUDIO_POLICY_DEVICE_STATE_AVAILABLE; |
| 446 | } else { |
| 447 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 448 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 451 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 452 | { |
| 453 | bool createTxPatch = false; |
| 454 | struct audio_patch patch; |
| 455 | patch.num_sources = 1; |
| 456 | patch.num_sinks = 1; |
| 457 | status_t status; |
| 458 | audio_patch_handle_t afPatchHandle; |
| 459 | DeviceVector deviceList; |
| 460 | |
| 461 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 462 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 463 | |
| 464 | // release existing RX patch if any |
| 465 | if (mCallRxPatch != 0) { |
| 466 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 467 | mCallRxPatch.clear(); |
| 468 | } |
| 469 | // release TX patch if any |
| 470 | if (mCallTxPatch != 0) { |
| 471 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 472 | mCallTxPatch.clear(); |
| 473 | } |
| 474 | |
| 475 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 476 | // via setOutputDevice() on primary output. |
| 477 | // Otherwise, create two audio patches for TX and RX path. |
| 478 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 479 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 480 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 481 | // of it due to legacy implementation. If not, create a patch. |
| 482 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 483 | == AUDIO_DEVICE_NONE) { |
| 484 | createTxPatch = true; |
| 485 | } |
| 486 | } else { |
| 487 | // create RX path audio patch |
| 488 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 489 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 490 | "updateCallRouting() selected device not in output device list"); |
| 491 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 492 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 493 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 494 | "updateCallRouting() no telephony RX device"); |
| 495 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 496 | |
| 497 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 498 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 499 | |
| 500 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 501 | SortedVector<audio_io_handle_t> outputs = |
| 502 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 503 | audio_io_handle_t output = selectOutput(outputs, |
| 504 | AUDIO_OUTPUT_FLAG_NONE, |
| 505 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 506 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 507 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 508 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 509 | "updateCallRouting() RX device output is duplicated"); |
| 510 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 511 | patch.num_sources = 2; |
| 512 | } |
| 513 | |
| 514 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 515 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 516 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 517 | status); |
| 518 | if (status == NO_ERROR) { |
| 519 | mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 520 | &patch, mUidCached); |
| 521 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 522 | mCallRxPatch->mUid = mUidCached; |
| 523 | } |
| 524 | createTxPatch = true; |
| 525 | } |
| 526 | if (createTxPatch) { |
| 527 | |
| 528 | struct audio_patch patch; |
| 529 | patch.num_sources = 1; |
| 530 | patch.num_sinks = 1; |
| 531 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 532 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 533 | "updateCallRouting() selected device not in input device list"); |
| 534 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 535 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 536 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 537 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 538 | "updateCallRouting() no telephony TX device"); |
| 539 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 540 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 541 | |
| 542 | SortedVector<audio_io_handle_t> outputs = |
| 543 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 544 | audio_io_handle_t output = selectOutput(outputs, |
| 545 | AUDIO_OUTPUT_FLAG_NONE, |
| 546 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 547 | // request to reuse existing output stream if one is already opened to reach the TX |
| 548 | // path output device |
| 549 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 550 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 551 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 552 | "updateCallRouting() RX device output is duplicated"); |
| 553 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 554 | patch.num_sources = 2; |
| 555 | } |
| 556 | |
| 557 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 558 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 559 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 560 | status); |
| 561 | if (status == NO_ERROR) { |
| 562 | mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 563 | &patch, mUidCached); |
| 564 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 565 | mCallTxPatch->mUid = mUidCached; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 570 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | { |
| 572 | ALOGV("setPhoneState() state %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 573 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | ALOGW("setPhoneState() invalid state %d", state); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | if (state == mPhoneState ) { |
| 579 | ALOGW("setPhoneState() setting same state %d", state); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | // if leaving call state, handle special case of active streams |
| 584 | // pertaining to sonification strategy see handleIncallSonification() |
| 585 | if (isInCall()) { |
| 586 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 587 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 588 | if (stream == AUDIO_STREAM_PATCH) { |
| 589 | continue; |
| 590 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 591 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | // store previous phone state for management of sonification strategy below |
| 596 | int oldState = mPhoneState; |
| 597 | mPhoneState = state; |
| 598 | bool force = false; |
| 599 | |
| 600 | // are we entering or starting a call |
| 601 | if (!isStateInCall(oldState) && isStateInCall(state)) { |
| 602 | ALOGV(" Entering call in setPhoneState()"); |
| 603 | // force routing command to audio hardware when starting a call |
| 604 | // even if no device change is needed |
| 605 | force = true; |
| 606 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 607 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
| 608 | sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; |
| 609 | } |
| 610 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { |
| 611 | ALOGV(" Exiting call in setPhoneState()"); |
| 612 | // force routing command to audio hardware when exiting a call |
| 613 | // even if no device change is needed |
| 614 | force = true; |
| 615 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 616 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
| 617 | sVolumeProfiles[AUDIO_STREAM_DTMF][j]; |
| 618 | } |
| 619 | } else if (isStateInCall(state) && (state != oldState)) { |
| 620 | ALOGV(" Switching between telephony and VoIP in setPhoneState()"); |
| 621 | // force routing command to audio hardware when switching between telephony and VoIP |
| 622 | // even if no device change is needed |
| 623 | force = true; |
| 624 | } |
| 625 | |
| 626 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | checkA2dpSuspend(); |
| 628 | checkOutputForAllStrategies(); |
| 629 | updateDevicesAndOutputs(); |
| 630 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 631 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 633 | int delayMs = 0; |
| 634 | if (isStateInCall(state)) { |
| 635 | nsecs_t sysTime = systemTime(); |
| 636 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 637 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 638 | // mute media and sonification strategies and delay device switch by the largest |
| 639 | // latency of any output where either strategy is active. |
| 640 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
| 641 | if ((desc->isStrategyActive(STRATEGY_MEDIA, |
| 642 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 643 | sysTime) || |
| 644 | desc->isStrategyActive(STRATEGY_SONIFICATION, |
| 645 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 646 | sysTime)) && |
| 647 | (delayMs < (int)desc->mLatency*2)) { |
| 648 | delayMs = desc->mLatency*2; |
| 649 | } |
| 650 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); |
| 651 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 652 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
| 653 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); |
| 654 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 655 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 656 | } |
| 657 | } |
| 658 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 659 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 660 | // the device returned is not necessarily reachable via this output |
| 661 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 662 | // force routing command to audio hardware when ending call |
| 663 | // even if no device change is needed |
| 664 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 665 | rxDevice = hwOutputDesc->device(); |
| 666 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 667 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 668 | if (state == AUDIO_MODE_IN_CALL) { |
| 669 | updateCallRouting(rxDevice, delayMs); |
| 670 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 671 | if (mCallRxPatch != 0) { |
| 672 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 673 | mCallRxPatch.clear(); |
| 674 | } |
| 675 | if (mCallTxPatch != 0) { |
| 676 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 677 | mCallTxPatch.clear(); |
| 678 | } |
| 679 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 680 | } else { |
| 681 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 682 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 683 | // if entering in call state, handle special case of active streams |
| 684 | // pertaining to sonification strategy see handleIncallSonification() |
| 685 | if (isStateInCall(state)) { |
| 686 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 687 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 688 | if (stream == AUDIO_STREAM_PATCH) { |
| 689 | continue; |
| 690 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 691 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | // 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] | 696 | if (state == AUDIO_MODE_RINGTONE && |
| 697 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 698 | mLimitRingtoneVolume = true; |
| 699 | } else { |
| 700 | mLimitRingtoneVolume = false; |
| 701 | } |
| 702 | } |
| 703 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 704 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 705 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 706 | { |
| 707 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 708 | |
| 709 | bool forceVolumeReeval = false; |
| 710 | switch(usage) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 711 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 712 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 713 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 714 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 715 | return; |
| 716 | } |
| 717 | forceVolumeReeval = true; |
| 718 | mForceUse[usage] = config; |
| 719 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 720 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 721 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 722 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 723 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 724 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 725 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 726 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 727 | return; |
| 728 | } |
| 729 | mForceUse[usage] = config; |
| 730 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 731 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 732 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 733 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 734 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 735 | return; |
| 736 | } |
| 737 | mForceUse[usage] = config; |
| 738 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 739 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 740 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 741 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 742 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 743 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 744 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 745 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 746 | } |
| 747 | forceVolumeReeval = true; |
| 748 | mForceUse[usage] = config; |
| 749 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 750 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 751 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 752 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 753 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 754 | } |
| 755 | forceVolumeReeval = true; |
| 756 | mForceUse[usage] = config; |
| 757 | break; |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 758 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 759 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 760 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
| 761 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); |
| 762 | } |
| 763 | mForceUse[usage] = config; |
| 764 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 765 | default: |
| 766 | ALOGW("setForceUse() invalid usage %d", usage); |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | // check for device and output changes triggered by new force usage |
| 771 | checkA2dpSuspend(); |
| 772 | checkOutputForAllStrategies(); |
| 773 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 774 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 775 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 776 | updateCallRouting(newDevice); |
| 777 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 778 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 779 | audio_io_handle_t output = mOutputs.keyAt(i); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 780 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 781 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 782 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
| 783 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 784 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
| 785 | applyStreamVolumes(output, newDevice, 0, true); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | audio_io_handle_t activeInput = getActiveInput(); |
| 790 | if (activeInput != 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 791 | setInputDevice(activeInput, getNewInputDevice(activeInput)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | } |
| 795 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 796 | 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] | 797 | { |
| 798 | return mForceUse[usage]; |
| 799 | } |
| 800 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 801 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 802 | { |
| 803 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 804 | } |
| 805 | |
| 806 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 807 | // not explicitly request a direct output |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 808 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 809 | audio_devices_t device, |
| 810 | uint32_t samplingRate, |
| 811 | audio_format_t format, |
| 812 | audio_channel_mask_t channelMask, |
| 813 | audio_output_flags_t flags) |
| 814 | { |
| 815 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 816 | if (mHwModules[i]->mHandle == 0) { |
| 817 | continue; |
| 818 | } |
| 819 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 820 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 821 | bool found = profile->isCompatibleProfile(device, samplingRate, |
| 822 | NULL /*updatedSamplingRate*/, format, channelMask, |
| 823 | flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ? |
| 824 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 825 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { |
| 826 | return profile; |
| 827 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 833 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 834 | uint32_t samplingRate, |
| 835 | audio_format_t format, |
| 836 | audio_channel_mask_t channelMask, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 837 | audio_output_flags_t flags, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | const audio_offload_info_t *offloadInfo) |
| 839 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 840 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 841 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 842 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 843 | device, stream, samplingRate, format, channelMask, flags); |
| 844 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 845 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 846 | stream, samplingRate,format, channelMask, |
| 847 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 850 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 851 | audio_io_handle_t *output, |
| 852 | audio_session_t session, |
| 853 | audio_stream_type_t *stream, |
| 854 | uint32_t samplingRate, |
| 855 | audio_format_t format, |
| 856 | audio_channel_mask_t channelMask, |
| 857 | audio_output_flags_t flags, |
| 858 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 859 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 860 | audio_attributes_t attributes; |
| 861 | if (attr != NULL) { |
| 862 | if (!isValidAttributes(attr)) { |
| 863 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 864 | attr->usage, attr->content_type, attr->flags, |
| 865 | attr->tags); |
| 866 | return BAD_VALUE; |
| 867 | } |
| 868 | attributes = *attr; |
| 869 | } else { |
| 870 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 871 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 872 | return BAD_VALUE; |
| 873 | } |
| 874 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 875 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 876 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 877 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x", |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 878 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 879 | |
| 880 | // TODO this is where filtering for custom policies (rerouting, dynamic sources) will go |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 881 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 882 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 883 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 884 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 885 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 886 | } |
| 887 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 888 | 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] | 889 | device, samplingRate, format, channelMask, flags); |
| 890 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 891 | *stream = streamTypefromAttributesInt(&attributes); |
| 892 | *output = getOutputForDevice(device, session, *stream, |
| 893 | samplingRate, format, channelMask, |
| 894 | flags, offloadInfo); |
| 895 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 896 | return INVALID_OPERATION; |
| 897 | } |
| 898 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 902 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 903 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 904 | audio_stream_type_t stream, |
| 905 | uint32_t samplingRate, |
| 906 | audio_format_t format, |
| 907 | audio_channel_mask_t channelMask, |
| 908 | audio_output_flags_t flags, |
| 909 | const audio_offload_info_t *offloadInfo) |
| 910 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 911 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 912 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 913 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 914 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 915 | #ifdef AUDIO_POLICY_TEST |
| 916 | if (mCurOutput != 0) { |
| 917 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 918 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 919 | |
| 920 | if (mTestOutputs[mCurOutput] == 0) { |
| 921 | ALOGV("getOutput() opening test output"); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 922 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 924 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 925 | outputDesc->mFlags = |
| 926 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 928 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 929 | config.sample_rate = mTestSamplingRate; |
| 930 | config.channel_mask = mTestChannels; |
| 931 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 932 | if (offloadInfo != NULL) { |
| 933 | config.offload_info = *offloadInfo; |
| 934 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 935 | status = mpClientInterface->openOutput(0, |
| 936 | &mTestOutputs[mCurOutput], |
| 937 | &config, |
| 938 | &outputDesc->mDevice, |
| 939 | String8(""), |
| 940 | &outputDesc->mLatency, |
| 941 | outputDesc->mFlags); |
| 942 | if (status == NO_ERROR) { |
| 943 | outputDesc->mSamplingRate = config.sample_rate; |
| 944 | outputDesc->mFormat = config.format; |
| 945 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 946 | AudioParameter outputCmd = AudioParameter(); |
| 947 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 948 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 949 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 950 | } |
| 951 | } |
| 952 | return mTestOutputs[mCurOutput]; |
| 953 | } |
| 954 | #endif //AUDIO_POLICY_TEST |
| 955 | |
| 956 | // open a direct output if required by specified parameters |
| 957 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 958 | // and all common behaviors are driven by checking only the direct flag |
| 959 | // this should normally be set appropriately in the policy configuration file |
| 960 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 961 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 963 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 964 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 965 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 966 | // only allow deep buffering for music stream type |
| 967 | if (stream != AUDIO_STREAM_MUSIC) { |
| 968 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 969 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 971 | sp<IOProfile> profile; |
| 972 | |
| 973 | // 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] | 974 | // and not explicitly requested |
| 975 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 976 | audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 977 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 978 | goto non_direct_output; |
| 979 | } |
| 980 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 982 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 983 | // detects there is an active non offloadable effect. |
| 984 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 985 | // This may prevent offloading in rare situations where effects are left active by apps |
| 986 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 987 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 988 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 989 | !isNonOffloadableEffectEnabled()) { |
| 990 | profile = getProfileForDirectOutput(device, |
| 991 | samplingRate, |
| 992 | format, |
| 993 | channelMask, |
| 994 | (audio_output_flags_t)flags); |
| 995 | } |
| 996 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 997 | if (profile != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 998 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 999 | |
| 1000 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1001 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1002 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1003 | outputDesc = desc; |
| 1004 | // reuse direct output if currently open and configured with same parameters |
| 1005 | if ((samplingRate == outputDesc->mSamplingRate) && |
| 1006 | (format == outputDesc->mFormat) && |
| 1007 | (channelMask == outputDesc->mChannelMask)) { |
| 1008 | outputDesc->mDirectOpenCount++; |
| 1009 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 1010 | return mOutputs.keyAt(i); |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | // close direct output if currently open and configured with different parameters |
| 1015 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1016 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | } |
| 1018 | outputDesc = new AudioOutputDescriptor(profile); |
| 1019 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | outputDesc->mLatency = 0; |
| 1021 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1022 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1023 | config.sample_rate = samplingRate; |
| 1024 | config.channel_mask = channelMask; |
| 1025 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 1026 | if (offloadInfo != NULL) { |
| 1027 | config.offload_info = *offloadInfo; |
| 1028 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1029 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 1030 | &output, |
| 1031 | &config, |
| 1032 | &outputDesc->mDevice, |
| 1033 | String8(""), |
| 1034 | &outputDesc->mLatency, |
| 1035 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1036 | |
| 1037 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1038 | if (status != NO_ERROR || |
| 1039 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
| 1040 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || |
| 1041 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1042 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1043 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1044 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1045 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1046 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | mpClientInterface->closeOutput(output); |
| 1048 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1049 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1050 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1051 | outputDesc->mSamplingRate = config.sample_rate; |
| 1052 | outputDesc->mChannelMask = config.channel_mask; |
| 1053 | outputDesc->mFormat = config.format; |
| 1054 | outputDesc->mRefCount[stream] = 0; |
| 1055 | outputDesc->mStopTime[stream] = 0; |
| 1056 | outputDesc->mDirectOpenCount = 1; |
| 1057 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1058 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 1059 | addOutput(output, outputDesc); |
| 1060 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1061 | if (dstOutput == output) { |
| 1062 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 1063 | } |
| 1064 | mPreviousOutputs = mOutputs; |
| 1065 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1066 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1067 | return output; |
| 1068 | } |
| 1069 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1070 | non_direct_output: |
| 1071 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1072 | // ignoring channel mask due to downmix capability in mixer |
| 1073 | |
| 1074 | // open a non direct output |
| 1075 | |
| 1076 | // for non direct outputs, only PCM is supported |
| 1077 | if (audio_is_linear_pcm(format)) { |
| 1078 | // get which output is suitable for the specified stream. The actual |
| 1079 | // routing change will happen when startOutput() will be called |
| 1080 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1081 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1082 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1083 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1084 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1085 | } |
| 1086 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1087 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1088 | |
| 1089 | ALOGV("getOutput() returns output %d", output); |
| 1090 | |
| 1091 | return output; |
| 1092 | } |
| 1093 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1094 | 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] | 1095 | audio_output_flags_t flags, |
| 1096 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1097 | { |
| 1098 | // select one output among several that provide a path to a particular device or set of |
| 1099 | // devices (the list was previously build by getOutputsForDevice()). |
| 1100 | // The priority is as follows: |
| 1101 | // 1: the output with the highest number of requested policy flags |
| 1102 | // 2: the primary output |
| 1103 | // 3: the first output in the list |
| 1104 | |
| 1105 | if (outputs.size() == 0) { |
| 1106 | return 0; |
| 1107 | } |
| 1108 | if (outputs.size() == 1) { |
| 1109 | return outputs[0]; |
| 1110 | } |
| 1111 | |
| 1112 | int maxCommonFlags = 0; |
| 1113 | audio_io_handle_t outputFlags = 0; |
| 1114 | audio_io_handle_t outputPrimary = 0; |
| 1115 | |
| 1116 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1117 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1118 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1119 | // if a valid format is specified, skip output if not compatible |
| 1120 | if (format != AUDIO_FORMAT_INVALID) { |
| 1121 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1122 | if (format != outputDesc->mFormat) { |
| 1123 | continue; |
| 1124 | } |
| 1125 | } else if (!audio_is_linear_pcm(format)) { |
| 1126 | continue; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1130 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1131 | if (commonFlags > maxCommonFlags) { |
| 1132 | outputFlags = outputs[i]; |
| 1133 | maxCommonFlags = commonFlags; |
| 1134 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1135 | } |
| 1136 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 1137 | outputPrimary = outputs[i]; |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | if (outputFlags != 0) { |
| 1143 | return outputFlags; |
| 1144 | } |
| 1145 | if (outputPrimary != 0) { |
| 1146 | return outputPrimary; |
| 1147 | } |
| 1148 | |
| 1149 | return outputs[0]; |
| 1150 | } |
| 1151 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1152 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1153 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1154 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1155 | { |
| 1156 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
| 1157 | ssize_t index = mOutputs.indexOfKey(output); |
| 1158 | if (index < 0) { |
| 1159 | ALOGW("startOutput() unknown output %d", output); |
| 1160 | return BAD_VALUE; |
| 1161 | } |
| 1162 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1163 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1164 | uint32_t beaconMuteLatency = 0; |
| 1165 | if (stream == AUDIO_STREAM_TTS) { |
| 1166 | ALOGV("\t found BEACON stream"); |
| 1167 | if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
| 1168 | return INVALID_OPERATION; |
| 1169 | } else { |
| 1170 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1171 | } |
| 1172 | } else { |
| 1173 | // some playback other than beacon starts |
| 1174 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1175 | } |
| 1176 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1177 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1178 | |
| 1179 | // increment usage count for this stream on the requested output: |
| 1180 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1181 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1182 | outputDesc->changeRefCount(stream, 1); |
| 1183 | |
| 1184 | if (outputDesc->mRefCount[stream] == 1) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1185 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1186 | routing_strategy strategy = getStrategy(stream); |
| 1187 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1188 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1189 | (beaconMuteLatency > 0); |
| 1190 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1191 | bool force = false; |
| 1192 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1193 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1194 | if (desc != outputDesc) { |
| 1195 | // force a device change if any other output is managed by the same hw |
| 1196 | // module and has a current device selection that differs from selected device. |
| 1197 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1198 | // change the device currently selected by the other active output. |
| 1199 | if (outputDesc->sharesHwModuleWith(desc) && |
| 1200 | desc->device() != newDevice) { |
| 1201 | force = true; |
| 1202 | } |
| 1203 | // 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] | 1204 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1205 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1206 | uint32_t latency = desc->latency(); |
| 1207 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1208 | waitMs = latency; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); |
| 1213 | |
| 1214 | // handle special case for sonification while in call |
| 1215 | if (isInCall()) { |
| 1216 | handleIncallSonification(stream, true, false); |
| 1217 | } |
| 1218 | |
| 1219 | // apply volume rules for current stream and device if necessary |
| 1220 | checkAndSetVolume(stream, |
| 1221 | mStreams[stream].getVolumeIndex(newDevice), |
| 1222 | output, |
| 1223 | newDevice); |
| 1224 | |
| 1225 | // update the outputs if starting an output with a stream that can affect notification |
| 1226 | // routing |
| 1227 | handleNotificationRoutingForStream(stream); |
| 1228 | if (waitMs > muteWaitMs) { |
| 1229 | usleep((waitMs - muteWaitMs) * 2 * 1000); |
| 1230 | } |
| 1231 | } |
| 1232 | return NO_ERROR; |
| 1233 | } |
| 1234 | |
| 1235 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1236 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1237 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1238 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1239 | { |
| 1240 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1241 | ssize_t index = mOutputs.indexOfKey(output); |
| 1242 | if (index < 0) { |
| 1243 | ALOGW("stopOutput() unknown output %d", output); |
| 1244 | return BAD_VALUE; |
| 1245 | } |
| 1246 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1247 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1248 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1249 | // always handle stream stop, check which stream type is stopping |
| 1250 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1251 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | // handle special case for sonification while in call |
| 1253 | if (isInCall()) { |
| 1254 | handleIncallSonification(stream, false, false); |
| 1255 | } |
| 1256 | |
| 1257 | if (outputDesc->mRefCount[stream] > 0) { |
| 1258 | // decrement usage count of this stream on the output |
| 1259 | outputDesc->changeRefCount(stream, -1); |
| 1260 | // store time at which the stream was stopped - see isStreamActive() |
| 1261 | if (outputDesc->mRefCount[stream] == 0) { |
| 1262 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1263 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1264 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1265 | // the track stop() command is received and at that time the audio track buffer can |
| 1266 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1267 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1268 | // audio path (audio DSP, CODEC ...) |
| 1269 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); |
| 1270 | |
| 1271 | // force restoring the device selection on other active outputs if it differs from the |
| 1272 | // one being selected for this output |
| 1273 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1274 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1275 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1276 | if (curOutput != output && |
| 1277 | desc->isActive() && |
| 1278 | outputDesc->sharesHwModuleWith(desc) && |
| 1279 | (newDevice != desc->device())) { |
| 1280 | setOutputDevice(curOutput, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1281 | getNewOutputDevice(curOutput, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1282 | true, |
| 1283 | outputDesc->mLatency*2); |
| 1284 | } |
| 1285 | } |
| 1286 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1287 | handleNotificationRoutingForStream(stream); |
| 1288 | } |
| 1289 | return NO_ERROR; |
| 1290 | } else { |
| 1291 | ALOGW("stopOutput() refcount is already 0 for output %d", output); |
| 1292 | return INVALID_OPERATION; |
| 1293 | } |
| 1294 | } |
| 1295 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1296 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1297 | audio_stream_type_t stream __unused, |
| 1298 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | { |
| 1300 | ALOGV("releaseOutput() %d", output); |
| 1301 | ssize_t index = mOutputs.indexOfKey(output); |
| 1302 | if (index < 0) { |
| 1303 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1304 | return; |
| 1305 | } |
| 1306 | |
| 1307 | #ifdef AUDIO_POLICY_TEST |
| 1308 | int testIndex = testOutputIndex(output); |
| 1309 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1310 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1311 | if (outputDesc->isActive()) { |
| 1312 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1313 | mOutputs.removeItem(output); |
| 1314 | mTestOutputs[testIndex] = 0; |
| 1315 | } |
| 1316 | return; |
| 1317 | } |
| 1318 | #endif //AUDIO_POLICY_TEST |
| 1319 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1320 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1321 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | if (desc->mDirectOpenCount <= 0) { |
| 1323 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1324 | desc->mDirectOpenCount, output); |
| 1325 | return; |
| 1326 | } |
| 1327 | if (--desc->mDirectOpenCount == 0) { |
| 1328 | closeOutput(output); |
| 1329 | // If effects where present on the output, audioflinger moved them to the primary |
| 1330 | // output by default: move them back to the appropriate output. |
| 1331 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1332 | if (dstOutput != mPrimaryOutput) { |
| 1333 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); |
| 1334 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1335 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1341 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1342 | audio_io_handle_t *input, |
| 1343 | audio_session_t session, |
| 1344 | uint32_t samplingRate, |
| 1345 | audio_format_t format, |
| 1346 | audio_channel_mask_t channelMask, |
| 1347 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1348 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1349 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1350 | "session %d, flags %#x", |
| 1351 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1352 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1353 | audio_devices_t device = getDeviceForInputSource(attr->source); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1354 | |
| 1355 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1356 | ALOGW("getInputForAttr() could not find device for source %d", attr->source); |
| 1357 | return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | // adapt channel selection to input source |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1361 | switch (attr->source) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1362 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1363 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; |
| 1364 | break; |
| 1365 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1366 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1367 | break; |
| 1368 | case AUDIO_SOURCE_VOICE_CALL: |
| 1369 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1370 | break; |
| 1371 | default: |
| 1372 | break; |
| 1373 | } |
| 1374 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1375 | *input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1376 | bool isSoundTrigger = false; |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1377 | audio_source_t halInputSource = attr->source; |
| 1378 | if (attr->source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1379 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1380 | if (index >= 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1381 | *input = mSoundTriggerSessions.valueFor(session); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1382 | isSoundTrigger = true; |
| 1383 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1384 | ALOGV("SoundTrigger capture on session %d input %d", session, *input); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1385 | } else { |
| 1386 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1390 | sp<IOProfile> profile = getInputProfile(device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 | samplingRate, |
| 1392 | format, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1393 | channelMask, |
| 1394 | flags); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1395 | if (profile == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1396 | //retry without flags |
| 1397 | audio_input_flags_t log_flags = flags; |
| 1398 | flags = AUDIO_INPUT_FLAG_NONE; |
| 1399 | profile = getInputProfile(device, |
| 1400 | samplingRate, |
| 1401 | format, |
| 1402 | channelMask, |
| 1403 | flags); |
| 1404 | if (profile == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1405 | ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u," |
| 1406 | "format %#x, channelMask 0x%X, flags %#x", |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1407 | device, samplingRate, format, channelMask, log_flags); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1408 | return BAD_VALUE; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1409 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | if (profile->mModule->mHandle == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1413 | ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName); |
| 1414 | return NO_INIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1418 | config.sample_rate = samplingRate; |
| 1419 | config.channel_mask = channelMask; |
| 1420 | config.format = format; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1421 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1422 | // handle legacy remote submix case where the address was not always specified |
| 1423 | String8 address = deviceDistinguishesOnAddress(device) ? String8("0") : String8(""); |
| 1424 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1425 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1426 | input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1427 | &config, |
| 1428 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1429 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1430 | halInputSource, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1431 | flags); |
| 1432 | |
| 1433 | // only accept input with the exact requested set of parameters |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1434 | if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1435 | (samplingRate != config.sample_rate) || |
| 1436 | (format != config.format) || |
| 1437 | (channelMask != config.channel_mask)) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1438 | ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1439 | samplingRate, format, channelMask); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1440 | if (*input != AUDIO_IO_HANDLE_NONE) { |
| 1441 | mpClientInterface->closeInput(*input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1442 | } |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1443 | return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1446 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1447 | inputDesc->mInputSource = attr->source; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1448 | inputDesc->mRefCount = 0; |
| 1449 | inputDesc->mOpenRefCount = 1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1450 | inputDesc->mSamplingRate = samplingRate; |
| 1451 | inputDesc->mFormat = format; |
| 1452 | inputDesc->mChannelMask = channelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1453 | inputDesc->mDevice = device; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1454 | inputDesc->mSessions.add(session); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1455 | inputDesc->mIsSoundTrigger = isSoundTrigger; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1456 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1457 | addInput(*input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1458 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame^] | 1459 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1462 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
| 1463 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1464 | { |
| 1465 | ALOGV("startInput() input %d", input); |
| 1466 | ssize_t index = mInputs.indexOfKey(input); |
| 1467 | if (index < 0) { |
| 1468 | ALOGW("startInput() unknown input %d", input); |
| 1469 | return BAD_VALUE; |
| 1470 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1471 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1472 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1473 | index = inputDesc->mSessions.indexOf(session); |
| 1474 | if (index < 0) { |
| 1475 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1476 | return BAD_VALUE; |
| 1477 | } |
| 1478 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1479 | // virtual input devices are compatible with other input devices |
| 1480 | if (!isVirtualInputDevice(inputDesc->mDevice)) { |
| 1481 | |
| 1482 | // 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] | 1483 | audio_io_handle_t activeInput = getActiveInput(); |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1484 | if (activeInput != 0 && activeInput != input) { |
| 1485 | |
| 1486 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1487 | // otherwise the active input continues and the new input cannot be started. |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1488 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1490 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1491 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); |
| 1492 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1493 | } else { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1494 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1495 | return INVALID_OPERATION; |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1500 | if (inputDesc->mRefCount == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1501 | if (activeInputsCount() == 0) { |
| 1502 | SoundTrigger::setCaptureState(true); |
| 1503 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1504 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1505 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1506 | // Automatically enable the remote submix output when input is started. |
| 1507 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1508 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1509 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1510 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); |
| 1511 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1514 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
| 1515 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1516 | inputDesc->mRefCount++; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1517 | return NO_ERROR; |
| 1518 | } |
| 1519 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1520 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1521 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1522 | { |
| 1523 | ALOGV("stopInput() input %d", input); |
| 1524 | ssize_t index = mInputs.indexOfKey(input); |
| 1525 | if (index < 0) { |
| 1526 | ALOGW("stopInput() unknown input %d", input); |
| 1527 | return BAD_VALUE; |
| 1528 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1529 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1530 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1531 | index = inputDesc->mSessions.indexOf(session); |
| 1532 | if (index < 0) { |
| 1533 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1534 | return BAD_VALUE; |
| 1535 | } |
| 1536 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | if (inputDesc->mRefCount == 0) { |
| 1538 | ALOGW("stopInput() input %d already stopped", input); |
| 1539 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | inputDesc->mRefCount--; |
| 1543 | if (inputDesc->mRefCount == 0) { |
| 1544 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1545 | // automatically disable the remote submix output when input is stopped |
| 1546 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1547 | setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1548 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1551 | resetInputDevice(input); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1552 | |
| 1553 | if (activeInputsCount() == 0) { |
| 1554 | SoundTrigger::setCaptureState(false); |
| 1555 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1556 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1557 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1560 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1561 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1562 | { |
| 1563 | ALOGV("releaseInput() %d", input); |
| 1564 | ssize_t index = mInputs.indexOfKey(input); |
| 1565 | if (index < 0) { |
| 1566 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1567 | return; |
| 1568 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1569 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1570 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1571 | |
| 1572 | index = inputDesc->mSessions.indexOf(session); |
| 1573 | if (index < 0) { |
| 1574 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1575 | return; |
| 1576 | } |
| 1577 | inputDesc->mSessions.remove(session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1578 | if (inputDesc->mOpenRefCount == 0) { |
| 1579 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); |
| 1580 | return; |
| 1581 | } |
| 1582 | inputDesc->mOpenRefCount--; |
| 1583 | if (inputDesc->mOpenRefCount > 0) { |
| 1584 | ALOGV("releaseInput() exit > 0"); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1588 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1589 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1590 | ALOGV("releaseInput() exit"); |
| 1591 | } |
| 1592 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1593 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1594 | bool patchRemoved = false; |
| 1595 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1596 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1597 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
| 1598 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 1599 | if (patch_index >= 0) { |
| 1600 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1601 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1602 | mAudioPatches.removeItemsAt(patch_index); |
| 1603 | patchRemoved = true; |
| 1604 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1605 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1606 | } |
| 1607 | mInputs.clear(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1608 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1609 | |
| 1610 | if (patchRemoved) { |
| 1611 | mpClientInterface->onAudioPatchListUpdate(); |
| 1612 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1615 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1616 | int indexMin, |
| 1617 | int indexMax) |
| 1618 | { |
| 1619 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1620 | if (indexMin < 0 || indexMin >= indexMax) { |
| 1621 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1622 | return; |
| 1623 | } |
| 1624 | mStreams[stream].mIndexMin = indexMin; |
| 1625 | mStreams[stream].mIndexMax = indexMax; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1626 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1627 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1628 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin; |
| 1629 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax; |
| 1630 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1633 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1634 | int index, |
| 1635 | audio_devices_t device) |
| 1636 | { |
| 1637 | |
| 1638 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { |
| 1639 | return BAD_VALUE; |
| 1640 | } |
| 1641 | if (!audio_is_output_device(device)) { |
| 1642 | return BAD_VALUE; |
| 1643 | } |
| 1644 | |
| 1645 | // Force max volume if stream cannot be muted |
| 1646 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; |
| 1647 | |
| 1648 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1649 | stream, device, index); |
| 1650 | |
| 1651 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1652 | // clear all device specific values |
| 1653 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1654 | mStreams[stream].mIndexCur.clear(); |
| 1655 | } |
| 1656 | mStreams[stream].mIndexCur.add(device, index); |
| 1657 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1658 | // update volume on all outputs whose current device is also selected by the same |
| 1659 | // strategy as the device specified by the caller |
| 1660 | audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1661 | |
| 1662 | |
| 1663 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1664 | audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; |
| 1665 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1666 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index); |
| 1667 | accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); |
| 1668 | } |
| 1669 | if ((device != AUDIO_DEVICE_OUT_DEFAULT) && |
| 1670 | (device & (strategyDevice | accessibilityDevice)) == 0) { |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1671 | return NO_ERROR; |
| 1672 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1673 | status_t status = NO_ERROR; |
| 1674 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1675 | audio_devices_t curDevice = |
| 1676 | getDeviceForVolume(mOutputs.valueAt(i)->device()); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1677 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); |
| 1679 | if (volStatus != NO_ERROR) { |
| 1680 | status = volStatus; |
| 1681 | } |
| 1682 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1683 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) { |
| 1684 | status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, |
| 1685 | index, mOutputs.keyAt(i), curDevice); |
| 1686 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1687 | } |
| 1688 | return status; |
| 1689 | } |
| 1690 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1691 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1692 | int *index, |
| 1693 | audio_devices_t device) |
| 1694 | { |
| 1695 | if (index == NULL) { |
| 1696 | return BAD_VALUE; |
| 1697 | } |
| 1698 | if (!audio_is_output_device(device)) { |
| 1699 | return BAD_VALUE; |
| 1700 | } |
| 1701 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1702 | // the strategy the stream belongs to. |
| 1703 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1704 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1705 | } |
| 1706 | device = getDeviceForVolume(device); |
| 1707 | |
| 1708 | *index = mStreams[stream].getVolumeIndex(device); |
| 1709 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1710 | return NO_ERROR; |
| 1711 | } |
| 1712 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1713 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1714 | const SortedVector<audio_io_handle_t>& outputs) |
| 1715 | { |
| 1716 | // select one output among several suitable for global effects. |
| 1717 | // The priority is as follows: |
| 1718 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1719 | // AudioFlinger will invalidate the track and the offloaded output |
| 1720 | // will be closed causing the effect to be moved to a PCM output. |
| 1721 | // 2: A deep buffer output |
| 1722 | // 3: the first output in the list |
| 1723 | |
| 1724 | if (outputs.size() == 0) { |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | audio_io_handle_t outputOffloaded = 0; |
| 1729 | audio_io_handle_t outputDeepBuffer = 0; |
| 1730 | |
| 1731 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1732 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1733 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1734 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1735 | outputOffloaded = outputs[i]; |
| 1736 | } |
| 1737 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1738 | outputDeepBuffer = outputs[i]; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1743 | outputOffloaded, outputDeepBuffer); |
| 1744 | if (outputOffloaded != 0) { |
| 1745 | return outputOffloaded; |
| 1746 | } |
| 1747 | if (outputDeepBuffer != 0) { |
| 1748 | return outputDeepBuffer; |
| 1749 | } |
| 1750 | |
| 1751 | return outputs[0]; |
| 1752 | } |
| 1753 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1754 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1755 | { |
| 1756 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1757 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1758 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1760 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1761 | |
| 1762 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1763 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1764 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1765 | |
| 1766 | return output; |
| 1767 | } |
| 1768 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1769 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1770 | audio_io_handle_t io, |
| 1771 | uint32_t strategy, |
| 1772 | int session, |
| 1773 | int id) |
| 1774 | { |
| 1775 | ssize_t index = mOutputs.indexOfKey(io); |
| 1776 | if (index < 0) { |
| 1777 | index = mInputs.indexOfKey(io); |
| 1778 | if (index < 0) { |
| 1779 | ALOGW("registerEffect() unknown io %d", io); |
| 1780 | return INVALID_OPERATION; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { |
| 1785 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", |
| 1786 | desc->name, desc->memoryUsage); |
| 1787 | return INVALID_OPERATION; |
| 1788 | } |
| 1789 | mTotalEffectsMemory += desc->memoryUsage; |
| 1790 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", |
| 1791 | desc->name, io, strategy, session, id); |
| 1792 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); |
| 1793 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1794 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); |
| 1795 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); |
| 1796 | effectDesc->mIo = io; |
| 1797 | effectDesc->mStrategy = (routing_strategy)strategy; |
| 1798 | effectDesc->mSession = session; |
| 1799 | effectDesc->mEnabled = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1800 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1801 | mEffects.add(id, effectDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1802 | |
| 1803 | return NO_ERROR; |
| 1804 | } |
| 1805 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1806 | status_t AudioPolicyManager::unregisterEffect(int id) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1807 | { |
| 1808 | ssize_t index = mEffects.indexOfKey(id); |
| 1809 | if (index < 0) { |
| 1810 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1811 | return INVALID_OPERATION; |
| 1812 | } |
| 1813 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1814 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1816 | setEffectEnabled(effectDesc, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1817 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1818 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1819 | ALOGW("unregisterEffect() memory %d too big for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1820 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
| 1821 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1822 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1823 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1824 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1825 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1826 | |
| 1827 | mEffects.removeItem(id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1828 | |
| 1829 | return NO_ERROR; |
| 1830 | } |
| 1831 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1832 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1833 | { |
| 1834 | ssize_t index = mEffects.indexOfKey(id); |
| 1835 | if (index < 0) { |
| 1836 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1837 | return INVALID_OPERATION; |
| 1838 | } |
| 1839 | |
| 1840 | return setEffectEnabled(mEffects.valueAt(index), enabled); |
| 1841 | } |
| 1842 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1843 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1844 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1845 | if (enabled == effectDesc->mEnabled) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1846 | ALOGV("setEffectEnabled(%s) effect already %s", |
| 1847 | enabled?"true":"false", enabled?"enabled":"disabled"); |
| 1848 | return INVALID_OPERATION; |
| 1849 | } |
| 1850 | |
| 1851 | if (enabled) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1852 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1853 | 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] | 1854 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1855 | return INVALID_OPERATION; |
| 1856 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1857 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1858 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); |
| 1859 | } else { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1860 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1862 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); |
| 1863 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1864 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1865 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1866 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); |
| 1867 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1868 | effectDesc->mEnabled = enabled; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1869 | return NO_ERROR; |
| 1870 | } |
| 1871 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1872 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1873 | { |
| 1874 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1875 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 1876 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && |
| 1877 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1878 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1879 | effectDesc->mDesc.name, effectDesc->mSession); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | return true; |
| 1881 | } |
| 1882 | } |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1886 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1887 | { |
| 1888 | nsecs_t sysTime = systemTime(); |
| 1889 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1890 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1891 | if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1892 | return true; |
| 1893 | } |
| 1894 | } |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1898 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1899 | uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1900 | { |
| 1901 | nsecs_t sysTime = systemTime(); |
| 1902 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1903 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1904 | if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1905 | outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1906 | return true; |
| 1907 | } |
| 1908 | } |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1912 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1913 | { |
| 1914 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1915 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1916 | if ((inputDescriptor->mInputSource == (int)source || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1917 | (source == AUDIO_SOURCE_VOICE_RECOGNITION && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1918 | inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) |
| 1919 | && (inputDescriptor->mRefCount > 0)) { |
| 1920 | return true; |
| 1921 | } |
| 1922 | } |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
| 1926 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1927 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1928 | { |
| 1929 | const size_t SIZE = 256; |
| 1930 | char buffer[SIZE]; |
| 1931 | String8 result; |
| 1932 | |
| 1933 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 1934 | result.append(buffer); |
| 1935 | |
| 1936 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); |
| 1937 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1938 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); |
| 1939 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1940 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
| 1941 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1943 | 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] | 1944 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1945 | 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] | 1946 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1947 | 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] | 1948 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1949 | 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] | 1950 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1951 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
| 1952 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); |
| 1953 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1954 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1955 | snprintf(buffer, SIZE, " Available output devices:\n"); |
| 1956 | result.append(buffer); |
| 1957 | write(fd, result.string(), result.size()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1958 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1959 | mAvailableOutputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1960 | } |
| 1961 | snprintf(buffer, SIZE, "\n Available input devices:\n"); |
| 1962 | write(fd, buffer, strlen(buffer)); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1963 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 1964 | mAvailableInputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 1965 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1966 | |
| 1967 | snprintf(buffer, SIZE, "\nHW Modules dump:\n"); |
| 1968 | write(fd, buffer, strlen(buffer)); |
| 1969 | for (size_t i = 0; i < mHwModules.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1970 | snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1971 | write(fd, buffer, strlen(buffer)); |
| 1972 | mHwModules[i]->dump(fd); |
| 1973 | } |
| 1974 | |
| 1975 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); |
| 1976 | write(fd, buffer, strlen(buffer)); |
| 1977 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1978 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); |
| 1979 | write(fd, buffer, strlen(buffer)); |
| 1980 | mOutputs.valueAt(i)->dump(fd); |
| 1981 | } |
| 1982 | |
| 1983 | snprintf(buffer, SIZE, "\nInputs dump:\n"); |
| 1984 | write(fd, buffer, strlen(buffer)); |
| 1985 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1986 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); |
| 1987 | write(fd, buffer, strlen(buffer)); |
| 1988 | mInputs.valueAt(i)->dump(fd); |
| 1989 | } |
| 1990 | |
| 1991 | snprintf(buffer, SIZE, "\nStreams dump:\n"); |
| 1992 | write(fd, buffer, strlen(buffer)); |
| 1993 | snprintf(buffer, SIZE, |
| 1994 | " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n"); |
| 1995 | write(fd, buffer, strlen(buffer)); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 1996 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1997 | snprintf(buffer, SIZE, " %02zu ", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1998 | write(fd, buffer, strlen(buffer)); |
| 1999 | mStreams[i].dump(fd); |
| 2000 | } |
| 2001 | |
| 2002 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", |
| 2003 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); |
| 2004 | write(fd, buffer, strlen(buffer)); |
| 2005 | |
| 2006 | snprintf(buffer, SIZE, "Registered effects:\n"); |
| 2007 | write(fd, buffer, strlen(buffer)); |
| 2008 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2009 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); |
| 2010 | write(fd, buffer, strlen(buffer)); |
| 2011 | mEffects.valueAt(i)->dump(fd); |
| 2012 | } |
| 2013 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 2014 | snprintf(buffer, SIZE, "\nAudio Patches:\n"); |
| 2015 | write(fd, buffer, strlen(buffer)); |
| 2016 | for (size_t i = 0; i < mAudioPatches.size(); i++) { |
| 2017 | mAudioPatches[i]->dump(fd, 2, i); |
| 2018 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2019 | |
| 2020 | return NO_ERROR; |
| 2021 | } |
| 2022 | |
| 2023 | // This function checks for the parameters which can be offloaded. |
| 2024 | // This can be enhanced depending on the capability of the DSP and policy |
| 2025 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2026 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2027 | { |
| 2028 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2029 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2030 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2031 | offloadInfo.format, |
| 2032 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2033 | offloadInfo.has_video); |
| 2034 | |
| 2035 | // Check if offload has been disabled |
| 2036 | char propValue[PROPERTY_VALUE_MAX]; |
| 2037 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2038 | if (atoi(propValue) != 0) { |
| 2039 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2040 | return false; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | // Check if stream type is music, then only allow offload as of now. |
| 2045 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2046 | { |
| 2047 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2048 | return false; |
| 2049 | } |
| 2050 | |
| 2051 | //TODO: enable audio offloading with video when ready |
| 2052 | if (offloadInfo.has_video) |
| 2053 | { |
| 2054 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2055 | return false; |
| 2056 | } |
| 2057 | |
| 2058 | //If duration is less than minimum value defined in property, return false |
| 2059 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2060 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2061 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2062 | return false; |
| 2063 | } |
| 2064 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2065 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2066 | return false; |
| 2067 | } |
| 2068 | |
| 2069 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2070 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2071 | // detects there is an active non offloadable effect. |
| 2072 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2073 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2074 | // in the background. |
| 2075 | if (isNonOffloadableEffectEnabled()) { |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | // See if there is a profile to support this. |
| 2080 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2081 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2082 | offloadInfo.sample_rate, |
| 2083 | offloadInfo.format, |
| 2084 | offloadInfo.channel_mask, |
| 2085 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2086 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2087 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2090 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2091 | audio_port_type_t type, |
| 2092 | unsigned int *num_ports, |
| 2093 | struct audio_port *ports, |
| 2094 | unsigned int *generation) |
| 2095 | { |
| 2096 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2097 | generation == NULL) { |
| 2098 | return BAD_VALUE; |
| 2099 | } |
| 2100 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2101 | if (ports == NULL) { |
| 2102 | *num_ports = 0; |
| 2103 | } |
| 2104 | |
| 2105 | size_t portsWritten = 0; |
| 2106 | size_t portsMax = *num_ports; |
| 2107 | *num_ports = 0; |
| 2108 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2109 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2110 | for (size_t i = 0; |
| 2111 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2112 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2113 | } |
| 2114 | *num_ports += mAvailableOutputDevices.size(); |
| 2115 | } |
| 2116 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2117 | for (size_t i = 0; |
| 2118 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2119 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2120 | } |
| 2121 | *num_ports += mAvailableInputDevices.size(); |
| 2122 | } |
| 2123 | } |
| 2124 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2125 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2126 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2127 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2128 | } |
| 2129 | *num_ports += mInputs.size(); |
| 2130 | } |
| 2131 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2132 | size_t numOutputs = 0; |
| 2133 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2134 | if (!mOutputs[i]->isDuplicated()) { |
| 2135 | numOutputs++; |
| 2136 | if (portsWritten < portsMax) { |
| 2137 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2138 | } |
| 2139 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2140 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2141 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2145 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2146 | return NO_ERROR; |
| 2147 | } |
| 2148 | |
| 2149 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2150 | { |
| 2151 | return NO_ERROR; |
| 2152 | } |
| 2153 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2154 | sp<AudioPolicyManager::AudioOutputDescriptor> AudioPolicyManager::getOutputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2155 | audio_port_handle_t id) const |
| 2156 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2157 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2158 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2159 | outputDesc = mOutputs.valueAt(i); |
| 2160 | if (outputDesc->mId == id) { |
| 2161 | break; |
| 2162 | } |
| 2163 | } |
| 2164 | return outputDesc; |
| 2165 | } |
| 2166 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2167 | sp<AudioPolicyManager::AudioInputDescriptor> AudioPolicyManager::getInputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2168 | audio_port_handle_t id) const |
| 2169 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2170 | sp<AudioInputDescriptor> inputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2171 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2172 | inputDesc = mInputs.valueAt(i); |
| 2173 | if (inputDesc->mId == id) { |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | return inputDesc; |
| 2178 | } |
| 2179 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2180 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleForDevice( |
| 2181 | audio_devices_t device) const |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2182 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2183 | sp <HwModule> module; |
| 2184 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2185 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2186 | if (mHwModules[i]->mHandle == 0) { |
| 2187 | continue; |
| 2188 | } |
| 2189 | if (audio_is_output_device(device)) { |
| 2190 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2191 | { |
| 2192 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { |
| 2193 | return mHwModules[i]; |
| 2194 | } |
| 2195 | } |
| 2196 | } else { |
| 2197 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { |
| 2198 | if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() & |
| 2199 | device & ~AUDIO_DEVICE_BIT_IN) { |
| 2200 | return mHwModules[i]; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2205 | return module; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2208 | sp <AudioPolicyManager::HwModule> AudioPolicyManager::getModuleFromName(const char *name) const |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2209 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2210 | sp <HwModule> module; |
| 2211 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2212 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 2213 | { |
| 2214 | if (strcmp(mHwModules[i]->mName, name) == 0) { |
| 2215 | return mHwModules[i]; |
| 2216 | } |
| 2217 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2218 | return module; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2221 | audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices() |
| 2222 | { |
| 2223 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 2224 | audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types(); |
| 2225 | return devices & mAvailableOutputDevices.types(); |
| 2226 | } |
| 2227 | |
| 2228 | audio_devices_t AudioPolicyManager::availablePrimaryInputDevices() |
| 2229 | { |
| 2230 | audio_module_handle_t primaryHandle = |
| 2231 | mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle; |
| 2232 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
| 2233 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2234 | if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) { |
| 2235 | devices |= mAvailableInputDevices[i]->mDeviceType; |
| 2236 | } |
| 2237 | } |
| 2238 | return devices; |
| 2239 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2240 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2241 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2242 | audio_patch_handle_t *handle, |
| 2243 | uid_t uid) |
| 2244 | { |
| 2245 | ALOGV("createAudioPatch()"); |
| 2246 | |
| 2247 | if (handle == NULL || patch == NULL) { |
| 2248 | return BAD_VALUE; |
| 2249 | } |
| 2250 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2251 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2252 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2253 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2254 | return BAD_VALUE; |
| 2255 | } |
| 2256 | // only one source per audio patch supported for now |
| 2257 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2258 | return INVALID_OPERATION; |
| 2259 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2260 | |
| 2261 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2262 | return INVALID_OPERATION; |
| 2263 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2264 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2265 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2266 | return INVALID_OPERATION; |
| 2267 | } |
| 2268 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2269 | |
| 2270 | sp<AudioPatch> patchDesc; |
| 2271 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2272 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2273 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2274 | patch->sources[0].role, |
| 2275 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2276 | #if LOG_NDEBUG == 0 |
| 2277 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2278 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, |
| 2279 | patch->sinks[i].role, |
| 2280 | patch->sinks[i].type); |
| 2281 | } |
| 2282 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2283 | |
| 2284 | if (index >= 0) { |
| 2285 | patchDesc = mAudioPatches.valueAt(index); |
| 2286 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2287 | mUidCached, patchDesc->mUid, uid); |
| 2288 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2289 | return INVALID_OPERATION; |
| 2290 | } |
| 2291 | } else { |
| 2292 | *handle = 0; |
| 2293 | } |
| 2294 | |
| 2295 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2296 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2297 | if (outputDesc == NULL) { |
| 2298 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2299 | return BAD_VALUE; |
| 2300 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2301 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2302 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2303 | if (patchDesc != 0) { |
| 2304 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2305 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2306 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2307 | return BAD_VALUE; |
| 2308 | } |
| 2309 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2310 | DeviceVector devices; |
| 2311 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2312 | // Only support mix to devices connection |
| 2313 | // TODO add support for mix to mix connection |
| 2314 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2315 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2316 | return INVALID_OPERATION; |
| 2317 | } |
| 2318 | sp<DeviceDescriptor> devDesc = |
| 2319 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2320 | if (devDesc == 0) { |
| 2321 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2322 | return BAD_VALUE; |
| 2323 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2324 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2325 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
| 2326 | patch->sources[0].sample_rate, |
| 2327 | NULL, // updatedSamplingRate |
| 2328 | patch->sources[0].format, |
| 2329 | patch->sources[0].channel_mask, |
| 2330 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
| 2331 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2332 | devDesc->mDeviceType); |
| 2333 | return INVALID_OPERATION; |
| 2334 | } |
| 2335 | devices.add(devDesc); |
| 2336 | } |
| 2337 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2338 | return INVALID_OPERATION; |
| 2339 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2340 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2341 | // TODO: reconfigure output format and channels here |
| 2342 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2343 | devices.types(), outputDesc->mIoHandle); |
| 2344 | setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2345 | index = mAudioPatches.indexOfKey(*handle); |
| 2346 | if (index >= 0) { |
| 2347 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2348 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2349 | } |
| 2350 | patchDesc = mAudioPatches.valueAt(index); |
| 2351 | patchDesc->mUid = uid; |
| 2352 | ALOGV("createAudioPatch() success"); |
| 2353 | } else { |
| 2354 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2355 | return INVALID_OPERATION; |
| 2356 | } |
| 2357 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2358 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2359 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2360 | // only one sink supported when connecting an input device to a mix |
| 2361 | if (patch->num_sinks > 1) { |
| 2362 | return INVALID_OPERATION; |
| 2363 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2364 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2365 | if (inputDesc == NULL) { |
| 2366 | return BAD_VALUE; |
| 2367 | } |
| 2368 | if (patchDesc != 0) { |
| 2369 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2370 | return BAD_VALUE; |
| 2371 | } |
| 2372 | } |
| 2373 | sp<DeviceDescriptor> devDesc = |
| 2374 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2375 | if (devDesc == 0) { |
| 2376 | return BAD_VALUE; |
| 2377 | } |
| 2378 | |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2379 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 2380 | patch->sinks[0].sample_rate, |
| 2381 | NULL, /*updatedSampleRate*/ |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2382 | patch->sinks[0].format, |
| 2383 | patch->sinks[0].channel_mask, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2384 | // FIXME for the parameter type, |
| 2385 | // and the NONE |
| 2386 | (audio_output_flags_t) |
| 2387 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2388 | return INVALID_OPERATION; |
| 2389 | } |
| 2390 | // TODO: reconfigure output format and channels here |
| 2391 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2392 | devDesc->mDeviceType, inputDesc->mIoHandle); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2393 | setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2394 | index = mAudioPatches.indexOfKey(*handle); |
| 2395 | if (index >= 0) { |
| 2396 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2397 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2398 | } |
| 2399 | patchDesc = mAudioPatches.valueAt(index); |
| 2400 | patchDesc->mUid = uid; |
| 2401 | ALOGV("createAudioPatch() success"); |
| 2402 | } else { |
| 2403 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2404 | return INVALID_OPERATION; |
| 2405 | } |
| 2406 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2407 | // device to device connection |
| 2408 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2409 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2410 | return BAD_VALUE; |
| 2411 | } |
| 2412 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2413 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2414 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2415 | if (srcDeviceDesc == 0) { |
| 2416 | return BAD_VALUE; |
| 2417 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2418 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2419 | //update source and sink with our own data as the data passed in the patch may |
| 2420 | // be incomplete. |
| 2421 | struct audio_patch newPatch = *patch; |
| 2422 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2423 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2424 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2425 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2426 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2427 | return INVALID_OPERATION; |
| 2428 | } |
| 2429 | |
| 2430 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2431 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2432 | if (sinkDeviceDesc == 0) { |
| 2433 | return BAD_VALUE; |
| 2434 | } |
| 2435 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2436 | |
| 2437 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { |
| 2438 | // only one sink supported when connected devices across HW modules |
| 2439 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2440 | return INVALID_OPERATION; |
| 2441 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2442 | SortedVector<audio_io_handle_t> outputs = |
| 2443 | getOutputsForDevice(sinkDeviceDesc->mDeviceType, |
| 2444 | mOutputs); |
| 2445 | // if the sink device is reachable via an opened output stream, request to go via |
| 2446 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2447 | audio_io_handle_t output = selectOutput(outputs, |
| 2448 | AUDIO_OUTPUT_FLAG_NONE, |
| 2449 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2450 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2451 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2452 | if (outputDesc->isDuplicated()) { |
| 2453 | return INVALID_OPERATION; |
| 2454 | } |
| 2455 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
| 2456 | newPatch.num_sources = 2; |
| 2457 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2458 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2459 | } |
| 2460 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2461 | |
| 2462 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2463 | if (index >= 0) { |
| 2464 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2465 | } |
| 2466 | |
| 2467 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2468 | &afPatchHandle, |
| 2469 | 0); |
| 2470 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2471 | status, afPatchHandle); |
| 2472 | if (status == NO_ERROR) { |
| 2473 | if (index < 0) { |
| 2474 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 2475 | &newPatch, uid); |
| 2476 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2477 | } else { |
| 2478 | patchDesc->mPatch = newPatch; |
| 2479 | } |
| 2480 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2481 | *handle = patchDesc->mHandle; |
| 2482 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2483 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2484 | } else { |
| 2485 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2486 | status); |
| 2487 | return INVALID_OPERATION; |
| 2488 | } |
| 2489 | } else { |
| 2490 | return BAD_VALUE; |
| 2491 | } |
| 2492 | } else { |
| 2493 | return BAD_VALUE; |
| 2494 | } |
| 2495 | return NO_ERROR; |
| 2496 | } |
| 2497 | |
| 2498 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2499 | uid_t uid) |
| 2500 | { |
| 2501 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2502 | |
| 2503 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2504 | |
| 2505 | if (index < 0) { |
| 2506 | return BAD_VALUE; |
| 2507 | } |
| 2508 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2509 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2510 | mUidCached, patchDesc->mUid, uid); |
| 2511 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2512 | return INVALID_OPERATION; |
| 2513 | } |
| 2514 | |
| 2515 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2516 | patchDesc->mUid = mUidCached; |
| 2517 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2518 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2519 | if (outputDesc == NULL) { |
| 2520 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2521 | return BAD_VALUE; |
| 2522 | } |
| 2523 | |
| 2524 | setOutputDevice(outputDesc->mIoHandle, |
| 2525 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), |
| 2526 | true, |
| 2527 | 0, |
| 2528 | NULL); |
| 2529 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2530 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2531 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2532 | if (inputDesc == NULL) { |
| 2533 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2534 | return BAD_VALUE; |
| 2535 | } |
| 2536 | setInputDevice(inputDesc->mIoHandle, |
| 2537 | getNewInputDevice(inputDesc->mIoHandle), |
| 2538 | true, |
| 2539 | NULL); |
| 2540 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2541 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2542 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2543 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2544 | status, patchDesc->mAfPatchHandle); |
| 2545 | removeAudioPatch(patchDesc->mHandle); |
| 2546 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2547 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2548 | } else { |
| 2549 | return BAD_VALUE; |
| 2550 | } |
| 2551 | } else { |
| 2552 | return BAD_VALUE; |
| 2553 | } |
| 2554 | return NO_ERROR; |
| 2555 | } |
| 2556 | |
| 2557 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2558 | struct audio_patch *patches, |
| 2559 | unsigned int *generation) |
| 2560 | { |
| 2561 | if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || |
| 2562 | generation == NULL) { |
| 2563 | return BAD_VALUE; |
| 2564 | } |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2565 | ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2566 | *num_patches, patches, mAudioPatches.size()); |
| 2567 | if (patches == NULL) { |
| 2568 | *num_patches = 0; |
| 2569 | } |
| 2570 | |
| 2571 | size_t patchesWritten = 0; |
| 2572 | size_t patchesMax = *num_patches; |
| 2573 | for (size_t i = 0; |
| 2574 | i < mAudioPatches.size() && patchesWritten < patchesMax; i++) { |
| 2575 | patches[patchesWritten] = mAudioPatches[i]->mPatch; |
| 2576 | patches[patchesWritten++].id = mAudioPatches[i]->mHandle; |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2577 | ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2578 | i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks); |
| 2579 | } |
| 2580 | *num_patches = mAudioPatches.size(); |
| 2581 | |
| 2582 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2583 | ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2584 | return NO_ERROR; |
| 2585 | } |
| 2586 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2587 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2588 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2589 | ALOGV("setAudioPortConfig()"); |
| 2590 | |
| 2591 | if (config == NULL) { |
| 2592 | return BAD_VALUE; |
| 2593 | } |
| 2594 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2595 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2596 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2597 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2600 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2601 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2602 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2603 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2604 | if (outputDesc == NULL) { |
| 2605 | return BAD_VALUE; |
| 2606 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2607 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2608 | "setAudioPortConfig() called on duplicated output %d", |
| 2609 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2610 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2611 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2612 | sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2613 | if (inputDesc == NULL) { |
| 2614 | return BAD_VALUE; |
| 2615 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2616 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2617 | } else { |
| 2618 | return BAD_VALUE; |
| 2619 | } |
| 2620 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2621 | sp<DeviceDescriptor> deviceDesc; |
| 2622 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2623 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2624 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2625 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2626 | } else { |
| 2627 | return BAD_VALUE; |
| 2628 | } |
| 2629 | if (deviceDesc == NULL) { |
| 2630 | return BAD_VALUE; |
| 2631 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2632 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2633 | } else { |
| 2634 | return BAD_VALUE; |
| 2635 | } |
| 2636 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2637 | struct audio_port_config backupConfig; |
| 2638 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2639 | if (status == NO_ERROR) { |
| 2640 | struct audio_port_config newConfig; |
| 2641 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2642 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2643 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2644 | if (status != NO_ERROR) { |
| 2645 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2646 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2647 | |
| 2648 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2652 | { |
| 2653 | for (ssize_t i = 0; i < (ssize_t)mAudioPatches.size(); i++) { |
| 2654 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2655 | if (patchDesc->mUid == uid) { |
| 2656 | // releaseAudioPatch() removes the patch from mAudioPatches |
| 2657 | if (releaseAudioPatch(mAudioPatches.keyAt(i), uid) == NO_ERROR) { |
| 2658 | i--; |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2664 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2665 | audio_io_handle_t *ioHandle, |
| 2666 | audio_devices_t *device) |
| 2667 | { |
| 2668 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); |
| 2669 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); |
| 2670 | *device = getDeviceForInputSource(AUDIO_SOURCE_HOTWORD); |
| 2671 | |
| 2672 | mSoundTriggerSessions.add(*session, *ioHandle); |
| 2673 | |
| 2674 | return NO_ERROR; |
| 2675 | } |
| 2676 | |
| 2677 | status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session) |
| 2678 | { |
| 2679 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2680 | if (index < 0) { |
| 2681 | ALOGW("acquireSoundTriggerSession() session %d not registered", session); |
| 2682 | return BAD_VALUE; |
| 2683 | } |
| 2684 | |
| 2685 | mSoundTriggerSessions.removeItem(session); |
| 2686 | return NO_ERROR; |
| 2687 | } |
| 2688 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2689 | status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle, |
| 2690 | const sp<AudioPatch>& patch) |
| 2691 | { |
| 2692 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2693 | |
| 2694 | if (index >= 0) { |
| 2695 | ALOGW("addAudioPatch() patch %d already in", handle); |
| 2696 | return ALREADY_EXISTS; |
| 2697 | } |
| 2698 | mAudioPatches.add(handle, patch); |
| 2699 | ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d" |
| 2700 | "sink handle %d", |
| 2701 | handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks, |
| 2702 | patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id); |
| 2703 | return NO_ERROR; |
| 2704 | } |
| 2705 | |
| 2706 | status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle) |
| 2707 | { |
| 2708 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2709 | |
| 2710 | if (index < 0) { |
| 2711 | ALOGW("removeAudioPatch() patch %d not in", handle); |
| 2712 | return ALREADY_EXISTS; |
| 2713 | } |
| 2714 | ALOGV("removeAudioPatch() handle %d af handle %d", handle, |
| 2715 | mAudioPatches.valueAt(index)->mAfPatchHandle); |
| 2716 | mAudioPatches.removeItemsAt(index); |
| 2717 | return NO_ERROR; |
| 2718 | } |
| 2719 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2720 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2721 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2722 | // ---------------------------------------------------------------------------- |
| 2723 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2724 | uint32_t AudioPolicyManager::nextUniqueId() |
| 2725 | { |
| 2726 | return android_atomic_inc(&mNextUniqueId); |
| 2727 | } |
| 2728 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2729 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 2730 | { |
| 2731 | return android_atomic_inc(&mAudioPortGeneration); |
| 2732 | } |
| 2733 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2734 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2735 | : |
| 2736 | #ifdef AUDIO_POLICY_TEST |
| 2737 | Thread(false), |
| 2738 | #endif //AUDIO_POLICY_TEST |
| 2739 | mPrimaryOutput((audio_io_handle_t)0), |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2740 | mPhoneState(AUDIO_MODE_NORMAL), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2741 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
| 2742 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2743 | mA2dpSuspended(false), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | mSpeakerDrcEnabled(false), mNextUniqueId(1), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2745 | mAudioPortGeneration(1), |
| 2746 | mBeaconMuteRefCount(0), |
| 2747 | mBeaconPlayingRefCount(0), |
| 2748 | mBeaconMuted(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2749 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2750 | mUidCached = getuid(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2751 | mpClientInterface = clientInterface; |
| 2752 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2753 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { |
| 2754 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2757 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), AUDIO_DEVICE_OUT_SPEAKER); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2758 | if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { |
| 2759 | if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { |
| 2760 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 2761 | defaultAudioPolicyConfig(); |
| 2762 | } |
| 2763 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2764 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | |
| 2766 | // must be done after reading the policy |
| 2767 | initializeVolumeCurves(); |
| 2768 | |
| 2769 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2770 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 2771 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2772 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2773 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); |
| 2774 | if (mHwModules[i]->mHandle == 0) { |
| 2775 | ALOGW("could not open HW module %s", mHwModules[i]->mName); |
| 2776 | continue; |
| 2777 | } |
| 2778 | // open all output streams needed to access attached devices |
| 2779 | // except for direct output streams that are only opened when they are actually |
| 2780 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2781 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2782 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2783 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2784 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2785 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2786 | if (outProfile->mSupportedDevices.isEmpty()) { |
| 2787 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); |
| 2788 | continue; |
| 2789 | } |
| 2790 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2791 | if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 2792 | continue; |
| 2793 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2794 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); |
| 2795 | if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) { |
| 2796 | profileType = mDefaultOutputDevice->mDeviceType; |
| 2797 | } else { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2798 | // chose first device present in mSupportedDevices also part of |
| 2799 | // outputDeviceTypes |
| 2800 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2801 | profileType = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2802 | if ((profileType & outputDeviceTypes) != 0) { |
| 2803 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2804 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2805 | } |
| 2806 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2807 | if ((profileType & outputDeviceTypes) == 0) { |
| 2808 | continue; |
| 2809 | } |
| 2810 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); |
| 2811 | |
| 2812 | outputDesc->mDevice = profileType; |
| 2813 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2814 | config.sample_rate = outputDesc->mSamplingRate; |
| 2815 | config.channel_mask = outputDesc->mChannelMask; |
| 2816 | config.format = outputDesc->mFormat; |
| 2817 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2818 | status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, |
| 2819 | &output, |
| 2820 | &config, |
| 2821 | &outputDesc->mDevice, |
| 2822 | String8(""), |
| 2823 | &outputDesc->mLatency, |
| 2824 | outputDesc->mFlags); |
| 2825 | |
| 2826 | if (status != NO_ERROR) { |
| 2827 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 2828 | outputDesc->mDevice, |
| 2829 | mHwModules[i]->mName); |
| 2830 | } else { |
| 2831 | outputDesc->mSamplingRate = config.sample_rate; |
| 2832 | outputDesc->mChannelMask = config.channel_mask; |
| 2833 | outputDesc->mFormat = config.format; |
| 2834 | |
| 2835 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2836 | audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2837 | ssize_t index = |
| 2838 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); |
| 2839 | // give a valid ID to an attached device once confirmed it is reachable |
| 2840 | if ((index >= 0) && (mAvailableOutputDevices[index]->mId == 0)) { |
| 2841 | mAvailableOutputDevices[index]->mId = nextUniqueId(); |
| 2842 | mAvailableOutputDevices[index]->mModule = mHwModules[i]; |
| 2843 | } |
| 2844 | } |
| 2845 | if (mPrimaryOutput == 0 && |
| 2846 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 2847 | mPrimaryOutput = output; |
| 2848 | } |
| 2849 | addOutput(output, outputDesc); |
| 2850 | setOutputDevice(output, |
| 2851 | outputDesc->mDevice, |
| 2852 | true); |
| 2853 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2854 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2855 | // open input streams needed to access attached devices to validate |
| 2856 | // mAvailableInputDevices list |
| 2857 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 2858 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2859 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2860 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2861 | if (inProfile->mSupportedDevices.isEmpty()) { |
| 2862 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); |
| 2863 | continue; |
| 2864 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2865 | // chose first device present in mSupportedDevices also part of |
| 2866 | // inputDeviceTypes |
| 2867 | audio_devices_t profileType = AUDIO_DEVICE_NONE; |
| 2868 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 2869 | profileType = inProfile->mSupportedDevices[k]->mDeviceType; |
| 2870 | if (profileType & inputDeviceTypes) { |
| 2871 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2872 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2873 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2874 | if ((profileType & inputDeviceTypes) == 0) { |
| 2875 | continue; |
| 2876 | } |
| 2877 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); |
| 2878 | |
| 2879 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; |
| 2880 | inputDesc->mDevice = profileType; |
| 2881 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2882 | // find the address |
| 2883 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 2884 | // the inputs vector must be of size 1, but we don't want to crash here |
| 2885 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 2886 | : String8(""); |
| 2887 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 2888 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 2889 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2890 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2891 | config.sample_rate = inputDesc->mSamplingRate; |
| 2892 | config.channel_mask = inputDesc->mChannelMask; |
| 2893 | config.format = inputDesc->mFormat; |
| 2894 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 2895 | status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, |
| 2896 | &input, |
| 2897 | &config, |
| 2898 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2899 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2900 | AUDIO_SOURCE_MIC, |
| 2901 | AUDIO_INPUT_FLAG_NONE); |
| 2902 | |
| 2903 | if (status == NO_ERROR) { |
| 2904 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 2905 | audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; |
| 2906 | ssize_t index = |
| 2907 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); |
| 2908 | // give a valid ID to an attached device once confirmed it is reachable |
| 2909 | if ((index >= 0) && (mAvailableInputDevices[index]->mId == 0)) { |
| 2910 | mAvailableInputDevices[index]->mId = nextUniqueId(); |
| 2911 | mAvailableInputDevices[index]->mModule = mHwModules[i]; |
| 2912 | } |
| 2913 | } |
| 2914 | mpClientInterface->closeInput(input); |
| 2915 | } else { |
| 2916 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 2917 | inputDesc->mDevice, |
| 2918 | mHwModules[i]->mName); |
| 2919 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2920 | } |
| 2921 | } |
| 2922 | // make sure all attached devices have been allocated a unique ID |
| 2923 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
| 2924 | if (mAvailableOutputDevices[i]->mId == 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2925 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2926 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 2927 | continue; |
| 2928 | } |
| 2929 | i++; |
| 2930 | } |
| 2931 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
| 2932 | if (mAvailableInputDevices[i]->mId == 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2933 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2934 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 2935 | continue; |
| 2936 | } |
| 2937 | i++; |
| 2938 | } |
| 2939 | // make sure default device is reachable |
| 2940 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2941 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2942 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2943 | |
| 2944 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 2945 | |
| 2946 | updateDevicesAndOutputs(); |
| 2947 | |
| 2948 | #ifdef AUDIO_POLICY_TEST |
| 2949 | if (mPrimaryOutput != 0) { |
| 2950 | AudioParameter outputCmd = AudioParameter(); |
| 2951 | outputCmd.addInt(String8("set_id"), 0); |
| 2952 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 2953 | |
| 2954 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 2955 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2956 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2957 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2958 | mTestLatencyMs = 0; |
| 2959 | mCurOutput = 0; |
| 2960 | mDirectOutput = false; |
| 2961 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 2962 | mTestOutputs[i] = 0; |
| 2963 | } |
| 2964 | |
| 2965 | const size_t SIZE = 256; |
| 2966 | char buffer[SIZE]; |
| 2967 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 2968 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 2969 | } |
| 2970 | #endif //AUDIO_POLICY_TEST |
| 2971 | } |
| 2972 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2973 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2974 | { |
| 2975 | #ifdef AUDIO_POLICY_TEST |
| 2976 | exit(); |
| 2977 | #endif //AUDIO_POLICY_TEST |
| 2978 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2979 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2980 | } |
| 2981 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2982 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2983 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2984 | mAvailableOutputDevices.clear(); |
| 2985 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2986 | mOutputs.clear(); |
| 2987 | mInputs.clear(); |
| 2988 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2991 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2992 | { |
| 2993 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; |
| 2994 | } |
| 2995 | |
| 2996 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2997 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2998 | { |
| 2999 | ALOGV("entering threadLoop()"); |
| 3000 | while (!exitPending()) |
| 3001 | { |
| 3002 | String8 command; |
| 3003 | int valueInt; |
| 3004 | String8 value; |
| 3005 | |
| 3006 | Mutex::Autolock _l(mLock); |
| 3007 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3008 | |
| 3009 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3010 | AudioParameter param = AudioParameter(command); |
| 3011 | |
| 3012 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3013 | valueInt != 0) { |
| 3014 | ALOGV("Test command %s received", command.string()); |
| 3015 | String8 target; |
| 3016 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3017 | target = "Manager"; |
| 3018 | } |
| 3019 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3020 | param.remove(String8("test_cmd_policy_output")); |
| 3021 | mCurOutput = valueInt; |
| 3022 | } |
| 3023 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3024 | param.remove(String8("test_cmd_policy_direct")); |
| 3025 | if (value == "false") { |
| 3026 | mDirectOutput = false; |
| 3027 | } else if (value == "true") { |
| 3028 | mDirectOutput = true; |
| 3029 | } |
| 3030 | } |
| 3031 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3032 | param.remove(String8("test_cmd_policy_input")); |
| 3033 | mTestInput = valueInt; |
| 3034 | } |
| 3035 | |
| 3036 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3037 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3038 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3039 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3040 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3041 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3042 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3043 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3044 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3045 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3046 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3047 | if (target == "Manager") { |
| 3048 | mTestFormat = format; |
| 3049 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3050 | AudioParameter outputParam = AudioParameter(); |
| 3051 | outputParam.addInt(String8("format"), format); |
| 3052 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3053 | } |
| 3054 | } |
| 3055 | } |
| 3056 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3057 | param.remove(String8("test_cmd_policy_channels")); |
| 3058 | int channels = 0; |
| 3059 | |
| 3060 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3061 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3062 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3063 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3064 | } |
| 3065 | if (channels != 0) { |
| 3066 | if (target == "Manager") { |
| 3067 | mTestChannels = channels; |
| 3068 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3069 | AudioParameter outputParam = AudioParameter(); |
| 3070 | outputParam.addInt(String8("channels"), channels); |
| 3071 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3076 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3077 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3078 | int samplingRate = valueInt; |
| 3079 | if (target == "Manager") { |
| 3080 | mTestSamplingRate = samplingRate; |
| 3081 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3082 | AudioParameter outputParam = AudioParameter(); |
| 3083 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3084 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3085 | } |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3090 | param.remove(String8("test_cmd_policy_reopen")); |
| 3091 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3092 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3093 | mpClientInterface->closeOutput(mPrimaryOutput); |
| 3094 | |
| 3095 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; |
| 3096 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3097 | mOutputs.removeItem(mPrimaryOutput); |
| 3098 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3099 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3100 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3101 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3102 | config.sample_rate = outputDesc->mSamplingRate; |
| 3103 | config.channel_mask = outputDesc->mChannelMask; |
| 3104 | config.format = outputDesc->mFormat; |
| 3105 | status_t status = mpClientInterface->openOutput(moduleHandle, |
| 3106 | &mPrimaryOutput, |
| 3107 | &config, |
| 3108 | &outputDesc->mDevice, |
| 3109 | String8(""), |
| 3110 | &outputDesc->mLatency, |
| 3111 | outputDesc->mFlags); |
| 3112 | if (status != NO_ERROR) { |
| 3113 | ALOGE("Failed to reopen hardware output stream, " |
| 3114 | "samplingRate: %d, format %d, channels %d", |
| 3115 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3116 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3117 | outputDesc->mSamplingRate = config.sample_rate; |
| 3118 | outputDesc->mChannelMask = config.channel_mask; |
| 3119 | outputDesc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3120 | AudioParameter outputCmd = AudioParameter(); |
| 3121 | outputCmd.addInt(String8("set_id"), 0); |
| 3122 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 3123 | addOutput(mPrimaryOutput, outputDesc); |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | |
| 3128 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3129 | } |
| 3130 | } |
| 3131 | return false; |
| 3132 | } |
| 3133 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3134 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3135 | { |
| 3136 | { |
| 3137 | AutoMutex _l(mLock); |
| 3138 | requestExit(); |
| 3139 | mWaitWorkCV.signal(); |
| 3140 | } |
| 3141 | requestExitAndWait(); |
| 3142 | } |
| 3143 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3144 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3145 | { |
| 3146 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3147 | if (output == mTestOutputs[i]) return i; |
| 3148 | } |
| 3149 | return 0; |
| 3150 | } |
| 3151 | #endif //AUDIO_POLICY_TEST |
| 3152 | |
| 3153 | // --- |
| 3154 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3155 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3156 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3157 | outputDesc->mIoHandle = output; |
| 3158 | outputDesc->mId = nextUniqueId(); |
| 3159 | mOutputs.add(output, outputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3160 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3163 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3164 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3165 | inputDesc->mIoHandle = input; |
| 3166 | inputDesc->mId = nextUniqueId(); |
| 3167 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3168 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3169 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3170 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3171 | void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/, |
| 3172 | const String8 address /*in*/, |
| 3173 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
| 3174 | // look for a match on the given address on the addresses of the outputs: |
| 3175 | // find the address by finding the patch that maps to this output |
| 3176 | ssize_t patchIdx = mAudioPatches.indexOfKey(desc->mPatchHandle); |
| 3177 | //ALOGV(" inspecting output %d (patch %d) for supported device=0x%x", |
| 3178 | // outputIdx, patchIdx, desc->mProfile->mSupportedDevices.types()); |
| 3179 | if (patchIdx >= 0) { |
| 3180 | const sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patchIdx); |
| 3181 | const int numSinks = patchDesc->mPatch.num_sinks; |
| 3182 | for (ssize_t j=0; j < numSinks; j++) { |
| 3183 | if (patchDesc->mPatch.sinks[j].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3184 | const char* patchAddr = |
| 3185 | patchDesc->mPatch.sinks[j].ext.device.address; |
| 3186 | if (strncmp(patchAddr, |
| 3187 | address.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3188 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3189 | desc->mIoHandle, patchDesc->mPatch.sinks[j].ext.device.address); |
| 3190 | outputs.add(desc->mIoHandle); |
| 3191 | break; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3198 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3199 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3200 | SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3201 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3202 | { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3203 | audio_devices_t device = devDesc->mDeviceType; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3204 | sp<AudioOutputDescriptor> desc; |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3205 | // erase all current sample rates, formats and channel masks |
| 3206 | devDesc->clearCapabilities(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3207 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3208 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3209 | // first list already open outputs that can be routed to this device |
| 3210 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3211 | desc = mOutputs.valueAt(i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3212 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3213 | if (!deviceDistinguishesOnAddress(device)) { |
| 3214 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3215 | outputs.add(mOutputs.keyAt(i)); |
| 3216 | } else { |
| 3217 | ALOGV(" checking address match due to device 0x%x", device); |
| 3218 | findIoHandlesByAddress(desc, address, outputs); |
| 3219 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3220 | } |
| 3221 | } |
| 3222 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3223 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3224 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3225 | { |
| 3226 | if (mHwModules[i]->mHandle == 0) { |
| 3227 | continue; |
| 3228 | } |
| 3229 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3230 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3231 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3232 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3233 | profiles.add(mHwModules[i]->mOutputProfiles[j]); |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3238 | ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size()); |
| 3239 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3240 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3241 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3242 | return BAD_VALUE; |
| 3243 | } |
| 3244 | |
| 3245 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3246 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3247 | 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] | 3248 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3249 | |
| 3250 | // nothing to do if one output is already opened for this profile |
| 3251 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3252 | for (j = 0; j < outputs.size(); j++) { |
| 3253 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3254 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3255 | // matching profile: save the sample rates, format and channel masks supported |
| 3256 | // by the profile in our device descriptor |
| 3257 | devDesc->importAudioPort(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3258 | break; |
| 3259 | } |
| 3260 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3261 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3262 | continue; |
| 3263 | } |
| 3264 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3265 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3266 | device, address.string(), profile.get()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3267 | desc = new AudioOutputDescriptor(profile); |
| 3268 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3269 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3270 | config.sample_rate = desc->mSamplingRate; |
| 3271 | config.channel_mask = desc->mChannelMask; |
| 3272 | config.format = desc->mFormat; |
| 3273 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3274 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3275 | config.offload_info.format = desc->mFormat; |
| 3276 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3277 | status_t status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3278 | &output, |
| 3279 | &config, |
| 3280 | &desc->mDevice, |
| 3281 | address, |
| 3282 | &desc->mLatency, |
| 3283 | desc->mFlags); |
| 3284 | if (status == NO_ERROR) { |
| 3285 | desc->mSamplingRate = config.sample_rate; |
| 3286 | desc->mChannelMask = config.channel_mask; |
| 3287 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3288 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3289 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3290 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3291 | char *param = audio_device_address_to_parameter(device, address); |
| 3292 | mpClientInterface->setParameters(output, String8(param)); |
| 3293 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3296 | // Here is where we step through and resolve any "dynamic" fields |
| 3297 | String8 reply; |
| 3298 | char *value; |
| 3299 | if (profile->mSamplingRates[0] == 0) { |
| 3300 | reply = mpClientInterface->getParameters(output, |
| 3301 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3302 | ALOGV("checkOutputsForDevice() supported sampling rates %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3303 | reply.string()); |
| 3304 | value = strpbrk((char *)reply.string(), "="); |
| 3305 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3306 | profile->loadSamplingRates(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3307 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3308 | } |
| 3309 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3310 | reply = mpClientInterface->getParameters(output, |
| 3311 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3312 | ALOGV("checkOutputsForDevice() supported formats %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3313 | reply.string()); |
| 3314 | value = strpbrk((char *)reply.string(), "="); |
| 3315 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3316 | profile->loadFormats(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3317 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3318 | } |
| 3319 | if (profile->mChannelMasks[0] == 0) { |
| 3320 | reply = mpClientInterface->getParameters(output, |
| 3321 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3322 | ALOGV("checkOutputsForDevice() supported channel masks %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3323 | reply.string()); |
| 3324 | value = strpbrk((char *)reply.string(), "="); |
| 3325 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3326 | profile->loadOutChannels(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3327 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3328 | } |
| 3329 | if (((profile->mSamplingRates[0] == 0) && |
| 3330 | (profile->mSamplingRates.size() < 2)) || |
| 3331 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && |
| 3332 | (profile->mFormats.size() < 2)) || |
| 3333 | ((profile->mChannelMasks[0] == 0) && |
| 3334 | (profile->mChannelMasks.size() < 2))) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3335 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3336 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3337 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3338 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || |
| 3339 | profile->mChannelMasks[0] == 0) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3340 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3341 | config.sample_rate = profile->pickSamplingRate(); |
| 3342 | config.channel_mask = profile->pickChannelMask(); |
| 3343 | config.format = profile->pickFormat(); |
| 3344 | config.offload_info.sample_rate = config.sample_rate; |
| 3345 | config.offload_info.channel_mask = config.channel_mask; |
| 3346 | config.offload_info.format = config.format; |
| 3347 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3348 | &output, |
| 3349 | &config, |
| 3350 | &desc->mDevice, |
| 3351 | address, |
| 3352 | &desc->mLatency, |
| 3353 | desc->mFlags); |
| 3354 | if (status == NO_ERROR) { |
| 3355 | desc->mSamplingRate = config.sample_rate; |
| 3356 | desc->mChannelMask = config.channel_mask; |
| 3357 | desc->mFormat = config.format; |
| 3358 | } else { |
| 3359 | output = AUDIO_IO_HANDLE_NONE; |
| 3360 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3363 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3364 | addOutput(output, desc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3365 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3366 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3367 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3368 | // set initial stream volume for device |
| 3369 | applyStreamVolumes(output, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3370 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3371 | //TODO: configure audio effect output stage here |
| 3372 | |
| 3373 | // open a duplicating output thread for the new output and the primary output |
| 3374 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, |
| 3375 | mPrimaryOutput); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3376 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3377 | // add duplicated output descriptor |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3378 | sp<AudioOutputDescriptor> dupOutputDesc = |
| 3379 | new AudioOutputDescriptor(NULL); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3380 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); |
| 3381 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); |
| 3382 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3383 | dupOutputDesc->mFormat = desc->mFormat; |
| 3384 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3385 | dupOutputDesc->mLatency = desc->mLatency; |
| 3386 | addOutput(duplicatedOutput, dupOutputDesc); |
| 3387 | applyStreamVolumes(duplicatedOutput, device, 0, true); |
| 3388 | } else { |
| 3389 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 3390 | mPrimaryOutput, output); |
| 3391 | mpClientInterface->closeOutput(output); |
| 3392 | mOutputs.removeItem(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3393 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3394 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3395 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3396 | } |
| 3397 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3398 | } else { |
| 3399 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3400 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3401 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3402 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3403 | profiles.removeAt(profile_index); |
| 3404 | profile_index--; |
| 3405 | } else { |
| 3406 | outputs.add(output); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3407 | devDesc->importAudioPort(profile); |
| 3408 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3409 | if (deviceDistinguishesOnAddress(device)) { |
| 3410 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3411 | device, address.string()); |
| 3412 | setOutputDevice(output, device, true/*force*/, 0/*delay*/, |
| 3413 | NULL/*patch handle*/, address.string()); |
| 3414 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3415 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | if (profiles.isEmpty()) { |
| 3420 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3421 | return BAD_VALUE; |
| 3422 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3423 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3424 | // check if one opened output is not needed any more after disconnecting one device |
| 3425 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3426 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3427 | if (!desc->isDuplicated()) { |
| 3428 | if (!(desc->mProfile->mSupportedDevices.types() |
| 3429 | & mAvailableOutputDevices.types())) { |
| 3430 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3431 | mOutputs.keyAt(i)); |
| 3432 | outputs.add(mOutputs.keyAt(i)); |
| 3433 | } else if (deviceDistinguishesOnAddress(device) && |
| 3434 | // exact match on device |
| 3435 | (desc->mProfile->mSupportedDevices.types() == device)) { |
| 3436 | findIoHandlesByAddress(desc, address, outputs); |
| 3437 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3438 | } |
| 3439 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3440 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3441 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3442 | { |
| 3443 | if (mHwModules[i]->mHandle == 0) { |
| 3444 | continue; |
| 3445 | } |
| 3446 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3447 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3448 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3449 | if (profile->mSupportedDevices.types() & device) { |
| 3450 | ALOGV("checkOutputsForDevice(): " |
| 3451 | "clearing direct output profile %zu on module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3452 | if (profile->mSamplingRates[0] == 0) { |
| 3453 | profile->mSamplingRates.clear(); |
| 3454 | profile->mSamplingRates.add(0); |
| 3455 | } |
| 3456 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3457 | profile->mFormats.clear(); |
| 3458 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3459 | } |
| 3460 | if (profile->mChannelMasks[0] == 0) { |
| 3461 | profile->mChannelMasks.clear(); |
| 3462 | profile->mChannelMasks.add(0); |
| 3463 | } |
| 3464 | } |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | return NO_ERROR; |
| 3469 | } |
| 3470 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3471 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, |
| 3472 | audio_policy_dev_state_t state, |
| 3473 | SortedVector<audio_io_handle_t>& inputs, |
| 3474 | const String8 address) |
| 3475 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3476 | sp<AudioInputDescriptor> desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3477 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3478 | // first list already open inputs that can be routed to this device |
| 3479 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3480 | desc = mInputs.valueAt(input_index); |
| 3481 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3482 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3483 | inputs.add(mInputs.keyAt(input_index)); |
| 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3488 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3489 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3490 | { |
| 3491 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3492 | continue; |
| 3493 | } |
| 3494 | for (size_t profile_index = 0; |
| 3495 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3496 | profile_index++) |
| 3497 | { |
| 3498 | if (mHwModules[module_idx]->mInputProfiles[profile_index]->mSupportedDevices.types() |
| 3499 | & (device & ~AUDIO_DEVICE_BIT_IN)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3500 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3501 | profile_index, module_idx); |
| 3502 | profiles.add(mHwModules[module_idx]->mInputProfiles[profile_index]); |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3508 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3509 | return BAD_VALUE; |
| 3510 | } |
| 3511 | |
| 3512 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3513 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3514 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3515 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3516 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3517 | // nothing to do if one input is already opened for this profile |
| 3518 | size_t input_index; |
| 3519 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3520 | desc = mInputs.valueAt(input_index); |
| 3521 | if (desc->mProfile == profile) { |
| 3522 | break; |
| 3523 | } |
| 3524 | } |
| 3525 | if (input_index != mInputs.size()) { |
| 3526 | continue; |
| 3527 | } |
| 3528 | |
| 3529 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3530 | desc = new AudioInputDescriptor(profile); |
| 3531 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3532 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3533 | config.sample_rate = desc->mSamplingRate; |
| 3534 | config.channel_mask = desc->mChannelMask; |
| 3535 | config.format = desc->mFormat; |
| 3536 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 3537 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
| 3538 | &input, |
| 3539 | &config, |
| 3540 | &desc->mDevice, |
| 3541 | address, |
| 3542 | AUDIO_SOURCE_MIC, |
| 3543 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3544 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3545 | if (status == NO_ERROR) { |
| 3546 | desc->mSamplingRate = config.sample_rate; |
| 3547 | desc->mChannelMask = config.channel_mask; |
| 3548 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3549 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3550 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3551 | char *param = audio_device_address_to_parameter(device, address); |
| 3552 | mpClientInterface->setParameters(input, String8(param)); |
| 3553 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | // Here is where we step through and resolve any "dynamic" fields |
| 3557 | String8 reply; |
| 3558 | char *value; |
| 3559 | if (profile->mSamplingRates[0] == 0) { |
| 3560 | reply = mpClientInterface->getParameters(input, |
| 3561 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 3562 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", |
| 3563 | reply.string()); |
| 3564 | value = strpbrk((char *)reply.string(), "="); |
| 3565 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3566 | profile->loadSamplingRates(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3570 | reply = mpClientInterface->getParameters(input, |
| 3571 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 3572 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); |
| 3573 | value = strpbrk((char *)reply.string(), "="); |
| 3574 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3575 | profile->loadFormats(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | if (profile->mChannelMasks[0] == 0) { |
| 3579 | reply = mpClientInterface->getParameters(input, |
| 3580 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 3581 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", |
| 3582 | reply.string()); |
| 3583 | value = strpbrk((char *)reply.string(), "="); |
| 3584 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3585 | profile->loadInChannels(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || |
| 3589 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || |
| 3590 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { |
| 3591 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3592 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3593 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | if (input != 0) { |
| 3597 | addInput(input, desc); |
| 3598 | } |
| 3599 | } // endif input != 0 |
| 3600 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3601 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3602 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3603 | profiles.removeAt(profile_index); |
| 3604 | profile_index--; |
| 3605 | } else { |
| 3606 | inputs.add(input); |
| 3607 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3608 | } |
| 3609 | } // end scan profiles |
| 3610 | |
| 3611 | if (profiles.isEmpty()) { |
| 3612 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3613 | return BAD_VALUE; |
| 3614 | } |
| 3615 | } else { |
| 3616 | // Disconnect |
| 3617 | // check if one opened input is not needed any more after disconnecting one device |
| 3618 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3619 | desc = mInputs.valueAt(input_index); |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3620 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() & |
| 3621 | ~AUDIO_DEVICE_BIT_IN)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3622 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3623 | mInputs.keyAt(input_index)); |
| 3624 | inputs.add(mInputs.keyAt(input_index)); |
| 3625 | } |
| 3626 | } |
| 3627 | // Clear any profiles associated with the disconnected device. |
| 3628 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3629 | if (mHwModules[module_index]->mHandle == 0) { |
| 3630 | continue; |
| 3631 | } |
| 3632 | for (size_t profile_index = 0; |
| 3633 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3634 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3635 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3636 | if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3637 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3638 | profile_index, module_index); |
| 3639 | if (profile->mSamplingRates[0] == 0) { |
| 3640 | profile->mSamplingRates.clear(); |
| 3641 | profile->mSamplingRates.add(0); |
| 3642 | } |
| 3643 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3644 | profile->mFormats.clear(); |
| 3645 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3646 | } |
| 3647 | if (profile->mChannelMasks[0] == 0) { |
| 3648 | profile->mChannelMasks.clear(); |
| 3649 | profile->mChannelMasks.add(0); |
| 3650 | } |
| 3651 | } |
| 3652 | } |
| 3653 | } |
| 3654 | } // end disconnect |
| 3655 | |
| 3656 | return NO_ERROR; |
| 3657 | } |
| 3658 | |
| 3659 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3660 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3661 | { |
| 3662 | ALOGV("closeOutput(%d)", output); |
| 3663 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3664 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3665 | if (outputDesc == NULL) { |
| 3666 | ALOGW("closeOutput() unknown output %d", output); |
| 3667 | return; |
| 3668 | } |
| 3669 | |
| 3670 | // look for duplicated outputs connected to the output being removed. |
| 3671 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3672 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3673 | if (dupOutputDesc->isDuplicated() && |
| 3674 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3675 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3676 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3677 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 3678 | outputDesc2 = dupOutputDesc->mOutput2; |
| 3679 | } else { |
| 3680 | outputDesc2 = dupOutputDesc->mOutput1; |
| 3681 | } |
| 3682 | // As all active tracks on duplicated output will be deleted, |
| 3683 | // and as they were also referenced on the other output, the reference |
| 3684 | // count for their stream type must be adjusted accordingly on |
| 3685 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3686 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3687 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3688 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3689 | } |
| 3690 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 3691 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 3692 | |
| 3693 | mpClientInterface->closeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3694 | mOutputs.removeItem(duplicatedOutput); |
| 3695 | } |
| 3696 | } |
| 3697 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3698 | nextAudioPortGeneration(); |
| 3699 | |
| 3700 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3701 | if (index >= 0) { |
| 3702 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3703 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3704 | mAudioPatches.removeItemsAt(index); |
| 3705 | mpClientInterface->onAudioPatchListUpdate(); |
| 3706 | } |
| 3707 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3708 | AudioParameter param; |
| 3709 | param.add(String8("closing"), String8("true")); |
| 3710 | mpClientInterface->setParameters(output, param.toString()); |
| 3711 | |
| 3712 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3713 | mOutputs.removeItem(output); |
| 3714 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3715 | } |
| 3716 | |
| 3717 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 3718 | { |
| 3719 | ALOGV("closeInput(%d)", input); |
| 3720 | |
| 3721 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 3722 | if (inputDesc == NULL) { |
| 3723 | ALOGW("closeInput() unknown input %d", input); |
| 3724 | return; |
| 3725 | } |
| 3726 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3727 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3728 | |
| 3729 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3730 | if (index >= 0) { |
| 3731 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3732 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3733 | mAudioPatches.removeItemsAt(index); |
| 3734 | mpClientInterface->onAudioPatchListUpdate(); |
| 3735 | } |
| 3736 | |
| 3737 | mpClientInterface->closeInput(input); |
| 3738 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3739 | } |
| 3740 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3741 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3742 | DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3743 | { |
| 3744 | SortedVector<audio_io_handle_t> outputs; |
| 3745 | |
| 3746 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 3747 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 3748 | ALOGVV("output %d isDuplicated=%d device=%04x", |
| 3749 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); |
| 3750 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 3751 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 3752 | outputs.add(openOutputs.keyAt(i)); |
| 3753 | } |
| 3754 | } |
| 3755 | return outputs; |
| 3756 | } |
| 3757 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3758 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3759 | SortedVector<audio_io_handle_t>& outputs2) |
| 3760 | { |
| 3761 | if (outputs1.size() != outputs2.size()) { |
| 3762 | return false; |
| 3763 | } |
| 3764 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 3765 | if (outputs1[i] != outputs2[i]) { |
| 3766 | return false; |
| 3767 | } |
| 3768 | } |
| 3769 | return true; |
| 3770 | } |
| 3771 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3772 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3773 | { |
| 3774 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3775 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3776 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 3777 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 3778 | |
| 3779 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 3780 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 3781 | strategy, srcOutputs[0], dstOutputs[0]); |
| 3782 | // mute strategy while moving tracks from one output to another |
| 3783 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3784 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3785 | if (desc->isStrategyActive(strategy)) { |
| 3786 | setStrategyMute(strategy, true, srcOutputs[i]); |
| 3787 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); |
| 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | // Move effects associated to this strategy from previous output to new output |
| 3792 | if (strategy == STRATEGY_MEDIA) { |
| 3793 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 3794 | SortedVector<audio_io_handle_t> moved; |
| 3795 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3796 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 3797 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 3798 | effectDesc->mIo != fxOutput) { |
| 3799 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3800 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 3801 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3802 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3803 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3804 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3805 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3806 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3807 | } |
| 3808 | } |
| 3809 | } |
| 3810 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3811 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3812 | if (i == AUDIO_STREAM_PATCH) { |
| 3813 | continue; |
| 3814 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3815 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 3816 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | } |
| 3821 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3822 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3823 | { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3824 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3825 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3826 | checkOutputForStrategy(STRATEGY_PHONE); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3827 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 3828 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3829 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 3830 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3831 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3832 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 3833 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3834 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3835 | } |
| 3836 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3837 | audio_io_handle_t AudioPolicyManager::getA2dpOutput() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3838 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3839 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3840 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3841 | if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 3842 | return mOutputs.keyAt(i); |
| 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | return 0; |
| 3847 | } |
| 3848 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3849 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3850 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3851 | audio_io_handle_t a2dpOutput = getA2dpOutput(); |
| 3852 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3853 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3854 | return; |
| 3855 | } |
| 3856 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3857 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3858 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 3859 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 3860 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3861 | // suspend A2DP output if: |
| 3862 | // (NOT already suspended) && |
| 3863 | // ((SCO device is connected && |
| 3864 | // (forced usage for communication || for record is SCO))) || |
| 3865 | // (phone state is ringing || in call) |
| 3866 | // |
| 3867 | // restore A2DP output if: |
| 3868 | // (Already suspended) && |
| 3869 | // ((SCO device is NOT connected || |
| 3870 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 3871 | // (phone state is NOT ringing && NOT in call) |
| 3872 | // |
| 3873 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3874 | if ((!isScoConnected || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3875 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && |
| 3876 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 3877 | ((mPhoneState != AUDIO_MODE_IN_CALL) && |
| 3878 | (mPhoneState != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | |
| 3880 | mpClientInterface->restoreOutput(a2dpOutput); |
| 3881 | mA2dpSuspended = false; |
| 3882 | } |
| 3883 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3884 | if ((isScoConnected && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3885 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 3886 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 3887 | ((mPhoneState == AUDIO_MODE_IN_CALL) || |
| 3888 | (mPhoneState == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3889 | |
| 3890 | mpClientInterface->suspendOutput(a2dpOutput); |
| 3891 | mA2dpSuspended = true; |
| 3892 | } |
| 3893 | } |
| 3894 | } |
| 3895 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3896 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3897 | { |
| 3898 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 3899 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3900 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3901 | |
| 3902 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3903 | if (index >= 0) { |
| 3904 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3905 | if (patchDesc->mUid != mUidCached) { |
| 3906 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
| 3907 | outputDesc->device(), outputDesc->mPatchHandle); |
| 3908 | return outputDesc->device(); |
| 3909 | } |
| 3910 | } |
| 3911 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3912 | // 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] | 3913 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | // use device for strategy enforced audible |
| 3915 | // 2: we are in call or the strategy phone is active on the output: |
| 3916 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3917 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 3918 | // use the device for strategy enforced audible |
| 3919 | // 4: the strategy sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3920 | // use device for strategy sonification |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3921 | // 5: the strategy "respectful" sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3922 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3923 | // 6: the strategy accessibility is active on the output: |
| 3924 | // use device for strategy accessibility |
| 3925 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3926 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3927 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3928 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3929 | // 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] | 3930 | // use device for strategy t-t-s |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3931 | if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) && |
| 3932 | mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3933 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 3934 | } else if (isInCall() || |
| 3935 | outputDesc->isStrategyActive(STRATEGY_PHONE)) { |
| 3936 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3937 | } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { |
| 3938 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3939 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { |
| 3940 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
| 3941 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { |
| 3942 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3943 | } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) { |
| 3944 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3945 | } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { |
| 3946 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
| 3947 | } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { |
| 3948 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3949 | } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
| 3950 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3951 | } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) { |
| 3952 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3955 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 3956 | return device; |
| 3957 | } |
| 3958 | |
| 3959 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
| 3960 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3961 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3962 | |
| 3963 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3964 | if (index >= 0) { |
| 3965 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3966 | if (patchDesc->mUid != mUidCached) { |
| 3967 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
| 3968 | inputDesc->mDevice, inputDesc->mPatchHandle); |
| 3969 | return inputDesc->mDevice; |
| 3970 | } |
| 3971 | } |
| 3972 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3973 | audio_devices_t device = getDeviceForInputSource(inputDesc->mInputSource); |
| 3974 | |
| 3975 | ALOGV("getNewInputDevice() selected device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3976 | return device; |
| 3977 | } |
| 3978 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3979 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3980 | return (uint32_t)getStrategy(stream); |
| 3981 | } |
| 3982 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3983 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3984 | // By checking the range of stream before calling getStrategy, we avoid |
| 3985 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 3986 | // 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] | 3987 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3988 | return AUDIO_DEVICE_NONE; |
| 3989 | } |
| 3990 | audio_devices_t devices; |
| 3991 | AudioPolicyManager::routing_strategy strategy = getStrategy(stream); |
| 3992 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3993 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); |
| 3994 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3995 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3996 | if (outputDesc->isStrategyActive(strategy)) { |
| 3997 | devices = outputDesc->device(); |
| 3998 | break; |
| 3999 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4000 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4001 | |
| 4002 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4003 | and doesn't really need to.*/ |
| 4004 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4005 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4006 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4007 | } |
| 4008 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4009 | return devices; |
| 4010 | } |
| 4011 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4012 | AudioPolicyManager::routing_strategy AudioPolicyManager::getStrategy( |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4013 | audio_stream_type_t stream) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4014 | |
| 4015 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH"); |
| 4016 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | // stream to strategy mapping |
| 4018 | switch (stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4019 | case AUDIO_STREAM_VOICE_CALL: |
| 4020 | case AUDIO_STREAM_BLUETOOTH_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4021 | return STRATEGY_PHONE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4022 | case AUDIO_STREAM_RING: |
| 4023 | case AUDIO_STREAM_ALARM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4024 | return STRATEGY_SONIFICATION; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4025 | case AUDIO_STREAM_NOTIFICATION: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4026 | return STRATEGY_SONIFICATION_RESPECTFUL; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4027 | case AUDIO_STREAM_DTMF: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4028 | return STRATEGY_DTMF; |
| 4029 | default: |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4030 | ALOGE("unknown stream type %d", stream); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4031 | case AUDIO_STREAM_SYSTEM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4032 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 4033 | // while key clicks are played produces a poor result |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4034 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4035 | return STRATEGY_MEDIA; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4036 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4037 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4038 | case AUDIO_STREAM_TTS: |
| 4039 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4040 | case AUDIO_STREAM_ACCESSIBILITY: |
| 4041 | return STRATEGY_ACCESSIBILITY; |
| 4042 | case AUDIO_STREAM_REROUTING: |
| 4043 | return STRATEGY_REROUTING; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4044 | } |
| 4045 | } |
| 4046 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4047 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4048 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4049 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4050 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4051 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4052 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4053 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4054 | } |
| 4055 | |
| 4056 | // usage to strategy mapping |
| 4057 | switch (attr->usage) { |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4058 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 4059 | if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) { |
| 4060 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4061 | } |
| 4062 | if (isInCall()) { |
| 4063 | return (uint32_t) STRATEGY_PHONE; |
| 4064 | } |
Eric Laurent | 0f78eab | 2014-11-25 11:01:34 -0800 | [diff] [blame] | 4065 | return (uint32_t) STRATEGY_ACCESSIBILITY; |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4066 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4067 | case AUDIO_USAGE_MEDIA: |
| 4068 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4069 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 4070 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 4071 | return (uint32_t) STRATEGY_MEDIA; |
| 4072 | |
| 4073 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 4074 | return (uint32_t) STRATEGY_PHONE; |
| 4075 | |
| 4076 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 4077 | return (uint32_t) STRATEGY_DTMF; |
| 4078 | |
| 4079 | case AUDIO_USAGE_ALARM: |
| 4080 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 4081 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4082 | |
| 4083 | case AUDIO_USAGE_NOTIFICATION: |
| 4084 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 4085 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 4086 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 4087 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 4088 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; |
| 4089 | |
| 4090 | case AUDIO_USAGE_UNKNOWN: |
| 4091 | default: |
| 4092 | return (uint32_t) STRATEGY_MEDIA; |
| 4093 | } |
| 4094 | } |
| 4095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4096 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4097 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4098 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4099 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4100 | updateDevicesAndOutputs(); |
| 4101 | break; |
| 4102 | default: |
| 4103 | break; |
| 4104 | } |
| 4105 | } |
| 4106 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4107 | bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) { |
| 4108 | for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) { |
| 4109 | if (s == (size_t) streamToIgnore) { |
| 4110 | continue; |
| 4111 | } |
| 4112 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4113 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 4114 | if (outputDesc->mRefCount[s] != 0) { |
| 4115 | return true; |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | return false; |
| 4120 | } |
| 4121 | |
| 4122 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
| 4123 | switch(event) { |
| 4124 | case STARTING_OUTPUT: |
| 4125 | mBeaconMuteRefCount++; |
| 4126 | break; |
| 4127 | case STOPPING_OUTPUT: |
| 4128 | if (mBeaconMuteRefCount > 0) { |
| 4129 | mBeaconMuteRefCount--; |
| 4130 | } |
| 4131 | break; |
| 4132 | case STARTING_BEACON: |
| 4133 | mBeaconPlayingRefCount++; |
| 4134 | break; |
| 4135 | case STOPPING_BEACON: |
| 4136 | if (mBeaconPlayingRefCount > 0) { |
| 4137 | mBeaconPlayingRefCount--; |
| 4138 | } |
| 4139 | break; |
| 4140 | } |
| 4141 | |
| 4142 | if (mBeaconMuteRefCount > 0) { |
| 4143 | // any playback causes beacon to be muted |
| 4144 | return setBeaconMute(true); |
| 4145 | } else { |
| 4146 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4147 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4152 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4153 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4154 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4155 | if (mBeaconMuted != mute) { |
| 4156 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4157 | ALOGV("\t muting %d", mute); |
| 4158 | uint32_t maxLatency = 0; |
| 4159 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4160 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4161 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
| 4162 | desc->mIoHandle, |
| 4163 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4164 | const uint32_t latency = desc->latency() * 2; |
| 4165 | if (latency > maxLatency) { |
| 4166 | maxLatency = latency; |
| 4167 | } |
| 4168 | } |
| 4169 | mBeaconMuted = mute; |
| 4170 | return maxLatency; |
| 4171 | } |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4175 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | bool fromCache) |
| 4177 | { |
| 4178 | uint32_t device = AUDIO_DEVICE_NONE; |
| 4179 | |
| 4180 | if (fromCache) { |
| 4181 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4182 | strategy, mDeviceForStrategy[strategy]); |
| 4183 | return mDeviceForStrategy[strategy]; |
| 4184 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4185 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | switch (strategy) { |
| 4187 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4188 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 4189 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4190 | if (!device) { |
| 4191 | ALOGE("getDeviceForStrategy() no device found for "\ |
| 4192 | "STRATEGY_TRANSMITTED_THROUGH_SPEAKER"); |
| 4193 | } |
| 4194 | break; |
| 4195 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4196 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 4197 | if (isInCall()) { |
| 4198 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4199 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4200 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 4201 | // while media is playing on a remote device, use the the sonification behavior. |
| 4202 | // Note that we test this usecase before testing if media is playing because |
| 4203 | // the isStreamActive() method only informs about the activity of a stream, not |
| 4204 | // if it's for local playback. Note also that we use the same delay between both tests |
| 4205 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4206 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4207 | //other acoustic safety mechanisms for notification |
| 4208 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4209 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4210 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4211 | // while media is playing (or has recently played), use the same device |
| 4212 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4213 | } else { |
| 4214 | // when media is not playing anymore, fall back on the sonification behavior |
| 4215 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4216 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4217 | //other acoustic safety mechanisms for notification |
| 4218 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4219 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | break; |
| 4223 | |
| 4224 | case STRATEGY_DTMF: |
| 4225 | if (!isInCall()) { |
| 4226 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 4227 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4228 | break; |
| 4229 | } |
| 4230 | // when in call, DTMF and PHONE strategies follow the same rules |
| 4231 | // FALL THROUGH |
| 4232 | |
| 4233 | case STRATEGY_PHONE: |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4234 | // Force use of only devices on primary output if: |
| 4235 | // - in call AND |
| 4236 | // - cannot route from voice call RX OR |
| 4237 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 4238 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 4239 | audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4240 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 4241 | if (((mAvailableInputDevices.types() & |
| 4242 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 4243 | (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 4244 | (hwOutputDesc->getAudioPort()->mModule->mHalVersion < |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4245 | AUDIO_DEVICE_API_VERSION_3_0))) { |
| 4246 | availableOutputDeviceTypes = availablePrimaryOutputDevices(); |
| 4247 | } |
| 4248 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4249 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 4250 | // of priority |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4251 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4252 | case AUDIO_POLICY_FORCE_BT_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4253 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4254 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4255 | if (device) break; |
| 4256 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4257 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
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_BLUETOOTH_SCO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4260 | if (device) break; |
| 4261 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4262 | // FALL THROUGH |
| 4263 | |
| 4264 | default: // FORCE_NONE |
| 4265 | // 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] | 4266 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4267 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4268 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4269 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4270 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4271 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4272 | if (device) break; |
| 4273 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4274 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4275 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4276 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | if (device) break; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4278 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 4279 | if (device) break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4280 | if (mPhoneState != AUDIO_MODE_IN_CALL) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4281 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4282 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4283 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4284 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4285 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4287 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4288 | if (device) break; |
| 4289 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4290 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4292 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | if (device == AUDIO_DEVICE_NONE) { |
| 4294 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 4295 | } |
| 4296 | break; |
| 4297 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4298 | case AUDIO_POLICY_FORCE_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 4300 | // A2DP speaker when forcing to speaker output |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4301 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4302 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4303 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4304 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4305 | if (device) break; |
| 4306 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4307 | if (mPhoneState != AUDIO_MODE_IN_CALL) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4308 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4309 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4310 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4312 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4313 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4314 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4315 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4316 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4317 | if (device) break; |
| 4318 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4319 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4320 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4321 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4322 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4323 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4324 | if (device == AUDIO_DEVICE_NONE) { |
| 4325 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 4326 | } |
| 4327 | break; |
| 4328 | } |
| 4329 | break; |
| 4330 | |
| 4331 | case STRATEGY_SONIFICATION: |
| 4332 | |
| 4333 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 4334 | // handleIncallSonification(). |
| 4335 | if (isInCall()) { |
| 4336 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); |
| 4337 | break; |
| 4338 | } |
| 4339 | // FALL THROUGH |
| 4340 | |
| 4341 | case STRATEGY_ENFORCED_AUDIBLE: |
| 4342 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 4343 | // except: |
| 4344 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 4345 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 4346 | |
| 4347 | if ((strategy == STRATEGY_SONIFICATION) || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4348 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4349 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4350 | if (device == AUDIO_DEVICE_NONE) { |
| 4351 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 4352 | } |
| 4353 | } |
| 4354 | // The second device used for sonification is the same as the device used by media strategy |
| 4355 | // FALL THROUGH |
| 4356 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4357 | // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
| 4358 | case STRATEGY_ACCESSIBILITY: |
Eric Laurent | 066ceec | 2014-11-25 12:35:01 -0800 | [diff] [blame] | 4359 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 4360 | // do not route accessibility prompts to a digital output currently configured with a |
| 4361 | // compressed format as they would likely not be mixed and dropped. |
| 4362 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4363 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4364 | audio_devices_t devices = desc->device() & |
| 4365 | (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); |
| 4366 | if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && |
| 4367 | devices != AUDIO_DEVICE_NONE) { |
| 4368 | availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices; |
| 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | // FALL THROUGH |
| 4373 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4374 | case STRATEGY_REROUTING: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4375 | case STRATEGY_MEDIA: { |
| 4376 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 4377 | if (strategy != STRATEGY_SONIFICATION) { |
| 4378 | // no sonification on remote submix (e.g. WFD) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4379 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4380 | } |
| 4381 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4382 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4383 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4384 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4386 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4387 | } |
| 4388 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4389 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4390 | } |
| 4391 | } |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4392 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 4393 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { |
| 4394 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4395 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4396 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4397 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4398 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4399 | if ((device2 == AUDIO_DEVICE_NONE)) { |
| 4400 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4401 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4402 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4403 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4404 | } |
| 4405 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4406 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4407 | } |
| 4408 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4409 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4410 | } |
| 4411 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4412 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4413 | } |
| 4414 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 4415 | // no sonification on aux digital (e.g. HDMI) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4416 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4417 | } |
| 4418 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4419 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4420 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4421 | } |
| 4422 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4423 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4424 | } |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4425 | int device3 = AUDIO_DEVICE_NONE; |
| 4426 | if (strategy == STRATEGY_MEDIA) { |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4427 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 4428 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4429 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4430 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4431 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4432 | |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4433 | device2 |= device3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4434 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 4435 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 4436 | device |= device2; |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4437 | |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4438 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 4439 | if ((strategy == STRATEGY_MEDIA) && |
| 4440 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == |
| 4441 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 4442 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4443 | } |
| 4444 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4445 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4446 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4447 | if (device == AUDIO_DEVICE_NONE) { |
| 4448 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); |
| 4449 | } |
| 4450 | } break; |
| 4451 | |
| 4452 | default: |
| 4453 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 4454 | break; |
| 4455 | } |
| 4456 | |
| 4457 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 4458 | return device; |
| 4459 | } |
| 4460 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4461 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4462 | { |
| 4463 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4464 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4465 | } |
| 4466 | mPreviousOutputs = mOutputs; |
| 4467 | } |
| 4468 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4469 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4470 | audio_devices_t prevDevice, |
| 4471 | uint32_t delayMs) |
| 4472 | { |
| 4473 | // mute/unmute strategies using an incompatible device combination |
| 4474 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4475 | // if unmuting, unmute only after the specified delay |
| 4476 | if (outputDesc->isDuplicated()) { |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
| 4480 | uint32_t muteWaitMs = 0; |
| 4481 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4482 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4483 | |
| 4484 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4485 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 4486 | curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4487 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4488 | bool doMute = false; |
| 4489 | |
| 4490 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4491 | doMute = true; |
| 4492 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4493 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4494 | doMute = true; |
| 4495 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4496 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4497 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4498 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4499 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4500 | // skip output if it does not share any device with current output |
| 4501 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4502 | == AUDIO_DEVICE_NONE) { |
| 4503 | continue; |
| 4504 | } |
| 4505 | audio_io_handle_t curOutput = mOutputs.keyAt(j); |
| 4506 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", |
| 4507 | mute ? "muting" : "unmuting", i, curDevice, curOutput); |
| 4508 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); |
| 4509 | if (desc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4510 | if (mute) { |
| 4511 | // FIXME: should not need to double latency if volume could be applied |
| 4512 | // immediately by the audioflinger mixer. We must account for the delay |
| 4513 | // between now and the next time the audioflinger thread for this output |
| 4514 | // will process a buffer (which corresponds to one buffer size, |
| 4515 | // usually 1/2 or 1/4 of the latency). |
| 4516 | if (muteWaitMs < desc->latency() * 2) { |
| 4517 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4518 | } |
| 4519 | } |
| 4520 | } |
| 4521 | } |
| 4522 | } |
| 4523 | } |
| 4524 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4525 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4526 | // different per device volumes |
| 4527 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4528 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4529 | muteWaitMs = outputDesc->latency() * 2; |
| 4530 | } |
| 4531 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4532 | if (outputDesc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4533 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4534 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4535 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4536 | muteWaitMs *2, device); |
| 4537 | } |
| 4538 | } |
| 4539 | } |
| 4540 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4541 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4542 | if (muteWaitMs > delayMs) { |
| 4543 | muteWaitMs -= delayMs; |
| 4544 | usleep(muteWaitMs * 1000); |
| 4545 | return muteWaitMs; |
| 4546 | } |
| 4547 | return 0; |
| 4548 | } |
| 4549 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4550 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4551 | audio_devices_t device, |
| 4552 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4553 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4554 | audio_patch_handle_t *patchHandle, |
| 4555 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4556 | { |
| 4557 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4558 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4559 | AudioParameter param; |
| 4560 | uint32_t muteWaitMs; |
| 4561 | |
| 4562 | if (outputDesc->isDuplicated()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4563 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); |
| 4564 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4565 | return muteWaitMs; |
| 4566 | } |
| 4567 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4568 | // output profile |
| 4569 | if ((device != AUDIO_DEVICE_NONE) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4570 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4571 | return 0; |
| 4572 | } |
| 4573 | |
| 4574 | // filter devices according to output selected |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4575 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4576 | |
| 4577 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4578 | |
| 4579 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); |
| 4580 | |
| 4581 | if (device != AUDIO_DEVICE_NONE) { |
| 4582 | outputDesc->mDevice = device; |
| 4583 | } |
| 4584 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4585 | |
| 4586 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4587 | // the requested device is AUDIO_DEVICE_NONE |
| 4588 | // OR the requested device is the same as current device |
| 4589 | // AND force is not specified |
| 4590 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4591 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4592 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force && |
| 4593 | outputDesc->mPatchHandle != 0) { |
| 4594 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", |
| 4595 | device, output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | return muteWaitMs; |
| 4597 | } |
| 4598 | |
| 4599 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4600 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4601 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4602 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4603 | resetOutputDevice(output, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4604 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4605 | DeviceVector deviceList = (address == NULL) ? |
| 4606 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4607 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4608 | if (!deviceList.isEmpty()) { |
| 4609 | struct audio_patch patch; |
| 4610 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4611 | patch.num_sources = 1; |
| 4612 | patch.num_sinks = 0; |
| 4613 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4614 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4615 | patch.num_sinks++; |
| 4616 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4617 | ssize_t index; |
| 4618 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4619 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4620 | } else { |
| 4621 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4622 | } |
| 4623 | sp< AudioPatch> patchDesc; |
| 4624 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4625 | if (index >= 0) { |
| 4626 | patchDesc = mAudioPatches.valueAt(index); |
| 4627 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4628 | } |
| 4629 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4630 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4631 | &afPatchHandle, |
| 4632 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4633 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4634 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4635 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4636 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4637 | if (index < 0) { |
| 4638 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4639 | &patch, mUidCached); |
| 4640 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4641 | } else { |
| 4642 | patchDesc->mPatch = patch; |
| 4643 | } |
| 4644 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4645 | patchDesc->mUid = mUidCached; |
| 4646 | if (patchHandle) { |
| 4647 | *patchHandle = patchDesc->mHandle; |
| 4648 | } |
| 4649 | outputDesc->mPatchHandle = patchDesc->mHandle; |
| 4650 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4651 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4652 | } |
| 4653 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4654 | |
| 4655 | // inform all input as well |
| 4656 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4657 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
| 4658 | if (!isVirtualInputDevice(inputDescriptor->mDevice)) { |
| 4659 | AudioParameter inputCmd = AudioParameter(); |
| 4660 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4661 | inputDescriptor->mIoHandle, device); |
| 4662 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4663 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4664 | inputCmd.toString(), |
| 4665 | delayMs); |
| 4666 | } |
| 4667 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4668 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 | |
| 4670 | // update stream volumes according to new device |
| 4671 | applyStreamVolumes(output, device, delayMs); |
| 4672 | |
| 4673 | return muteWaitMs; |
| 4674 | } |
| 4675 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4676 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4677 | int delayMs, |
| 4678 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4679 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4680 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4681 | ssize_t index; |
| 4682 | if (patchHandle) { |
| 4683 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4684 | } else { |
| 4685 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4686 | } |
| 4687 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4688 | return INVALID_OPERATION; |
| 4689 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4690 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4691 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4692 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
| 4693 | outputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4694 | removeAudioPatch(patchDesc->mHandle); |
| 4695 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4696 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4697 | return status; |
| 4698 | } |
| 4699 | |
| 4700 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4701 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4702 | bool force, |
| 4703 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4704 | { |
| 4705 | status_t status = NO_ERROR; |
| 4706 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4707 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4708 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4709 | inputDesc->mDevice = device; |
| 4710 | |
| 4711 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4712 | if (!deviceList.isEmpty()) { |
| 4713 | struct audio_patch patch; |
| 4714 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4715 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4716 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4717 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
| 4718 | !inputDesc->mIsSoundTrigger) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4719 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4720 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4721 | patch.num_sinks = 1; |
| 4722 | //only one input device for now |
| 4723 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4724 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4725 | ssize_t index; |
| 4726 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4727 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4728 | } else { |
| 4729 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4730 | } |
| 4731 | sp< AudioPatch> patchDesc; |
| 4732 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4733 | if (index >= 0) { |
| 4734 | patchDesc = mAudioPatches.valueAt(index); |
| 4735 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4736 | } |
| 4737 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4738 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4739 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4740 | 0); |
| 4741 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4742 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4743 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4744 | if (index < 0) { |
| 4745 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4746 | &patch, mUidCached); |
| 4747 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4748 | } else { |
| 4749 | patchDesc->mPatch = patch; |
| 4750 | } |
| 4751 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4752 | patchDesc->mUid = mUidCached; |
| 4753 | if (patchHandle) { |
| 4754 | *patchHandle = patchDesc->mHandle; |
| 4755 | } |
| 4756 | inputDesc->mPatchHandle = patchDesc->mHandle; |
| 4757 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4758 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | return status; |
| 4763 | } |
| 4764 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4765 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4766 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4767 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4768 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4769 | ssize_t index; |
| 4770 | if (patchHandle) { |
| 4771 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4772 | } else { |
| 4773 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4774 | } |
| 4775 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4776 | return INVALID_OPERATION; |
| 4777 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4778 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4779 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4780 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
| 4781 | inputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4782 | removeAudioPatch(patchDesc->mHandle); |
| 4783 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4784 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4785 | return status; |
| 4786 | } |
| 4787 | |
| 4788 | sp<AudioPolicyManager::IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4789 | uint32_t& samplingRate, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4790 | audio_format_t format, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4791 | audio_channel_mask_t channelMask, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4792 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4793 | { |
| 4794 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4795 | // profile supporting all requested parameters. |
| 4796 | |
| 4797 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4798 | { |
| 4799 | if (mHwModules[i]->mHandle == 0) { |
| 4800 | continue; |
| 4801 | } |
| 4802 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4803 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4804 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4805 | // profile->log(); |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4806 | if (profile->isCompatibleProfile(device, samplingRate, |
| 4807 | &samplingRate /*updatedSamplingRate*/, |
| 4808 | format, channelMask, (audio_output_flags_t) flags)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4809 | return profile; |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | return NULL; |
| 4814 | } |
| 4815 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4816 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4817 | { |
| 4818 | uint32_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4819 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & |
| 4820 | ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4821 | switch (inputSource) { |
| 4822 | case AUDIO_SOURCE_VOICE_UPLINK: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4823 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4824 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4825 | break; |
| 4826 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4827 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4828 | |
| 4829 | case AUDIO_SOURCE_DEFAULT: |
| 4830 | case AUDIO_SOURCE_MIC: |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4831 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 4832 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4833 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4834 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4835 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4836 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4837 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4838 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4839 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4840 | break; |
| 4841 | |
| 4842 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 4843 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 4844 | // to voice call path. |
| 4845 | if ((mPhoneState == AUDIO_MODE_IN_CALL) && |
| 4846 | (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 4847 | availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN; |
| 4848 | } |
| 4849 | |
| 4850 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4851 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 4852 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4853 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 4854 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 4855 | break; |
| 4856 | } |
| 4857 | // FALL THROUGH |
| 4858 | |
| 4859 | default: // FORCE_NONE |
| 4860 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 4861 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 4862 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4863 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 4864 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4865 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4866 | } |
| 4867 | break; |
| 4868 | |
| 4869 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 4870 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 4871 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 4872 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4873 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4874 | } |
| 4875 | break; |
| 4876 | } |
| 4877 | break; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 4878 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4879 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 4880 | case AUDIO_SOURCE_HOTWORD: |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4881 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4882 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4884 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4886 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 4887 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4888 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4889 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4890 | } |
| 4891 | break; |
| 4892 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4893 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4894 | device = AUDIO_DEVICE_IN_BACK_MIC; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4895 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4896 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 4897 | } |
| 4898 | break; |
| 4899 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 4900 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4901 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4902 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 4903 | } |
| 4904 | break; |
| 4905 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4906 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4907 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 4908 | } |
| 4909 | break; |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4910 | case AUDIO_SOURCE_FM_TUNER: |
| 4911 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 4912 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 4913 | } |
| 4914 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4915 | default: |
| 4916 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 4917 | break; |
| 4918 | } |
| 4919 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 4920 | return device; |
| 4921 | } |
| 4922 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4923 | bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4924 | { |
| 4925 | if ((device & AUDIO_DEVICE_BIT_IN) != 0) { |
| 4926 | device &= ~AUDIO_DEVICE_BIT_IN; |
| 4927 | if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) |
| 4928 | return true; |
| 4929 | } |
| 4930 | return false; |
| 4931 | } |
| 4932 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4933 | bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) { |
| 4934 | return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL) != 0); |
| 4935 | } |
| 4936 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4937 | audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4938 | { |
| 4939 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4940 | const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4941 | if ((input_descriptor->mRefCount > 0) |
| 4942 | && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { |
| 4943 | return mInputs.keyAt(i); |
| 4944 | } |
| 4945 | } |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4949 | uint32_t AudioPolicyManager::activeInputsCount() const |
| 4950 | { |
| 4951 | uint32_t count = 0; |
| 4952 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4953 | const sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 4954 | if (desc->mRefCount > 0) { |
| 4955 | return count++; |
| 4956 | } |
| 4957 | } |
| 4958 | return count; |
| 4959 | } |
| 4960 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4961 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4962 | audio_devices_t AudioPolicyManager::getDeviceForVolume(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4963 | { |
| 4964 | if (device == AUDIO_DEVICE_NONE) { |
| 4965 | // this happens when forcing a route update and no track is active on an output. |
| 4966 | // In this case the returned category is not important. |
| 4967 | device = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4968 | } else if (popcount(device) > 1) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4969 | // Multiple device selection is either: |
| 4970 | // - speaker + one other device: give priority to speaker in this case. |
| 4971 | // - one A2DP device + another device: happens with duplicated output. In this case |
| 4972 | // retain the device on the A2DP output as the other must not correspond to an active |
| 4973 | // selection if not the speaker. |
Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame] | 4974 | // - 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] | 4975 | if (device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4976 | device = AUDIO_DEVICE_OUT_SPEAKER; |
Jungshik Jang | a1f9917 | 2014-09-05 21:25:48 +0900 | [diff] [blame] | 4977 | } else if (device & AUDIO_DEVICE_OUT_HDMI_ARC) { |
| 4978 | device = AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4979 | } else if (device & AUDIO_DEVICE_OUT_AUX_LINE) { |
| 4980 | device = AUDIO_DEVICE_OUT_AUX_LINE; |
| 4981 | } else if (device & AUDIO_DEVICE_OUT_SPDIF) { |
| 4982 | device = AUDIO_DEVICE_OUT_SPDIF; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | } else { |
| 4984 | device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP); |
| 4985 | } |
| 4986 | } |
| 4987 | |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4988 | /*SPEAKER_SAFE is an alias of SPEAKER for purposes of volume control*/ |
| 4989 | if (device == AUDIO_DEVICE_OUT_SPEAKER_SAFE) |
| 4990 | device = AUDIO_DEVICE_OUT_SPEAKER; |
| 4991 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4992 | ALOGW_IF(popcount(device) != 1, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | "getDeviceForVolume() invalid device combination: %08x", |
| 4994 | device); |
| 4995 | |
| 4996 | return device; |
| 4997 | } |
| 4998 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4999 | AudioPolicyManager::device_category AudioPolicyManager::getDeviceCategory(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5000 | { |
| 5001 | switch(getDeviceForVolume(device)) { |
| 5002 | case AUDIO_DEVICE_OUT_EARPIECE: |
| 5003 | return DEVICE_CATEGORY_EARPIECE; |
| 5004 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 5005 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 5006 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: |
| 5007 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: |
| 5008 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 5009 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 5010 | return DEVICE_CATEGORY_HEADSET; |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5011 | case AUDIO_DEVICE_OUT_LINE: |
| 5012 | case AUDIO_DEVICE_OUT_AUX_DIGITAL: |
| 5013 | /*USB? Remote submix?*/ |
| 5014 | return DEVICE_CATEGORY_EXT_MEDIA; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5015 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 5016 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 5017 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5018 | case AUDIO_DEVICE_OUT_USB_ACCESSORY: |
| 5019 | case AUDIO_DEVICE_OUT_USB_DEVICE: |
| 5020 | case AUDIO_DEVICE_OUT_REMOTE_SUBMIX: |
| 5021 | default: |
| 5022 | return DEVICE_CATEGORY_SPEAKER; |
| 5023 | } |
| 5024 | } |
| 5025 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5026 | /* static */ |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5027 | float AudioPolicyManager::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5028 | int indexInUi) |
| 5029 | { |
| 5030 | device_category deviceCategory = getDeviceCategory(device); |
| 5031 | const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory]; |
| 5032 | |
| 5033 | // the volume index in the UI is relative to the min and max volume indices for this stream type |
| 5034 | int nbSteps = 1 + curve[VOLMAX].mIndex - |
| 5035 | curve[VOLMIN].mIndex; |
| 5036 | int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) / |
| 5037 | (streamDesc.mIndexMax - streamDesc.mIndexMin); |
| 5038 | |
| 5039 | // find what part of the curve this index volume belongs to, or if it's out of bounds |
| 5040 | int segment = 0; |
| 5041 | if (volIdx < curve[VOLMIN].mIndex) { // out of bounds |
| 5042 | return 0.0f; |
| 5043 | } else if (volIdx < curve[VOLKNEE1].mIndex) { |
| 5044 | segment = 0; |
| 5045 | } else if (volIdx < curve[VOLKNEE2].mIndex) { |
| 5046 | segment = 1; |
| 5047 | } else if (volIdx <= curve[VOLMAX].mIndex) { |
| 5048 | segment = 2; |
| 5049 | } else { // out of bounds |
| 5050 | return 1.0f; |
| 5051 | } |
| 5052 | |
| 5053 | // linear interpolation in the attenuation table in dB |
| 5054 | float decibels = curve[segment].mDBAttenuation + |
| 5055 | ((float)(volIdx - curve[segment].mIndex)) * |
| 5056 | ( (curve[segment+1].mDBAttenuation - |
| 5057 | curve[segment].mDBAttenuation) / |
| 5058 | ((float)(curve[segment+1].mIndex - |
| 5059 | curve[segment].mIndex)) ); |
| 5060 | |
| 5061 | float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 ) |
| 5062 | |
| 5063 | ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f", |
| 5064 | curve[segment].mIndex, volIdx, |
| 5065 | curve[segment+1].mIndex, |
| 5066 | curve[segment].mDBAttenuation, |
| 5067 | decibels, |
| 5068 | curve[segment+1].mDBAttenuation, |
| 5069 | amplification); |
| 5070 | |
| 5071 | return amplification; |
| 5072 | } |
| 5073 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5074 | const AudioPolicyManager::VolumeCurvePoint |
| 5075 | AudioPolicyManager::sDefaultVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5076 | {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f} |
| 5077 | }; |
| 5078 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5079 | const AudioPolicyManager::VolumeCurvePoint |
| 5080 | AudioPolicyManager::sDefaultMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5081 | {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f} |
| 5082 | }; |
| 5083 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5084 | const AudioPolicyManager::VolumeCurvePoint |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5085 | AudioPolicyManager::sExtMediaSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5086 | {1, -58.0f}, {20, -40.0f}, {60, -21.0f}, {100, -10.0f} |
| 5087 | }; |
| 5088 | |
| 5089 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5090 | AudioPolicyManager::sSpeakerMediaVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5091 | {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f} |
| 5092 | }; |
| 5093 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5094 | const AudioPolicyManager::VolumeCurvePoint |
Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 5095 | AudioPolicyManager::sSpeakerMediaVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Jean-Michel Trivi | 98c6043 | 2014-07-09 08:51:34 -0700 | [diff] [blame] | 5096 | {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] | 5097 | }; |
| 5098 | |
| 5099 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5100 | AudioPolicyManager::sSpeakerSonificationVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5101 | {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f} |
| 5102 | }; |
| 5103 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5104 | const AudioPolicyManager::VolumeCurvePoint |
| 5105 | AudioPolicyManager::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5106 | {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f} |
| 5107 | }; |
| 5108 | |
| 5109 | // AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks |
| 5110 | // AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets. |
| 5111 | // AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java). |
| 5112 | // The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset. |
| 5113 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5114 | const AudioPolicyManager::VolumeCurvePoint |
| 5115 | AudioPolicyManager::sDefaultSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5116 | {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f} |
| 5117 | }; |
| 5118 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5119 | const AudioPolicyManager::VolumeCurvePoint |
| 5120 | AudioPolicyManager::sDefaultSystemVolumeCurveDrc[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5121 | {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f} |
| 5122 | }; |
| 5123 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5124 | const AudioPolicyManager::VolumeCurvePoint |
| 5125 | AudioPolicyManager::sHeadsetSystemVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5126 | {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f} |
| 5127 | }; |
| 5128 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5129 | const AudioPolicyManager::VolumeCurvePoint |
| 5130 | AudioPolicyManager::sDefaultVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5131 | {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f} |
| 5132 | }; |
| 5133 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5134 | const AudioPolicyManager::VolumeCurvePoint |
| 5135 | AudioPolicyManager::sSpeakerVoiceVolumeCurve[AudioPolicyManager::VOLCNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5136 | {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f} |
| 5137 | }; |
| 5138 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5139 | const AudioPolicyManager::VolumeCurvePoint |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5140 | AudioPolicyManager::sLinearVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5141 | {0, -96.0f}, {33, -68.0f}, {66, -34.0f}, {100, 0.0f} |
| 5142 | }; |
| 5143 | |
| 5144 | const AudioPolicyManager::VolumeCurvePoint |
| 5145 | AudioPolicyManager::sSilentVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5146 | {0, -96.0f}, {1, -96.0f}, {2, -96.0f}, {100, -96.0f} |
| 5147 | }; |
| 5148 | |
| 5149 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5150 | AudioPolicyManager::sFullScaleVolumeCurve[AudioPolicyManager::VOLCNT] = { |
| 5151 | {0, 0.0f}, {1, 0.0f}, {2, 0.0f}, {100, 0.0f} |
| 5152 | }; |
| 5153 | |
| 5154 | const AudioPolicyManager::VolumeCurvePoint |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5155 | *AudioPolicyManager::sVolumeProfiles[AUDIO_STREAM_CNT] |
| 5156 | [AudioPolicyManager::DEVICE_CATEGORY_CNT] = { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5157 | { // AUDIO_STREAM_VOICE_CALL |
| 5158 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5159 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5160 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5161 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5162 | }, |
| 5163 | { // AUDIO_STREAM_SYSTEM |
| 5164 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5165 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5166 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5167 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5168 | }, |
| 5169 | { // AUDIO_STREAM_RING |
| 5170 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5171 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5172 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5173 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5174 | }, |
| 5175 | { // AUDIO_STREAM_MUSIC |
| 5176 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5177 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5178 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5179 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5180 | }, |
| 5181 | { // AUDIO_STREAM_ALARM |
| 5182 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5183 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5184 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5185 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5186 | }, |
| 5187 | { // AUDIO_STREAM_NOTIFICATION |
| 5188 | sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5189 | sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5190 | sDefaultVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5191 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5192 | }, |
| 5193 | { // AUDIO_STREAM_BLUETOOTH_SCO |
| 5194 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5195 | sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5196 | sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5197 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5198 | }, |
| 5199 | { // AUDIO_STREAM_ENFORCED_AUDIBLE |
| 5200 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5201 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5202 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5203 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | }, |
| 5205 | { // AUDIO_STREAM_DTMF |
| 5206 | sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5207 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 5208 | sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5209 | sExtMediaSystemVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5210 | }, |
| 5211 | { // AUDIO_STREAM_TTS |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5212 | // "Transmitted Through Speaker": always silent except on DEVICE_CATEGORY_SPEAKER |
| 5213 | sSilentVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5214 | sLinearVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5215 | sSilentVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5216 | sSilentVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5217 | }, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5218 | { // AUDIO_STREAM_ACCESSIBILITY |
| 5219 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5220 | sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5221 | sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5222 | sDefaultMediaVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5223 | }, |
| 5224 | { // AUDIO_STREAM_REROUTING |
| 5225 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5226 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5227 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5228 | sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5229 | }, |
| 5230 | { // AUDIO_STREAM_PATCH |
| 5231 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_HEADSET |
| 5232 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_SPEAKER |
| 5233 | sFullScaleVolumeCurve, // DEVICE_CATEGORY_EARPIECE |
| 5234 | sFullScaleVolumeCurve // DEVICE_CATEGORY_EXT_MEDIA |
| 5235 | }, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5236 | }; |
| 5237 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5238 | void AudioPolicyManager::initializeVolumeCurves() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5239 | { |
| 5240 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
| 5241 | for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) { |
| 5242 | mStreams[i].mVolumeCurve[j] = |
| 5243 | sVolumeProfiles[i][j]; |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | // Check availability of DRC on speaker path: if available, override some of the speaker curves |
| 5248 | if (mSpeakerDrcEnabled) { |
| 5249 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5250 | sDefaultSystemVolumeCurveDrc; |
| 5251 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5252 | sSpeakerSonificationVolumeCurveDrc; |
| 5253 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5254 | sSpeakerSonificationVolumeCurveDrc; |
| 5255 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5256 | sSpeakerSonificationVolumeCurveDrc; |
Jean-Michel Trivi | ccd8e4a | 2014-06-05 15:33:20 -0700 | [diff] [blame] | 5257 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5258 | sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5259 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] = |
| 5260 | sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5264 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | int index, |
| 5266 | audio_io_handle_t output, |
| 5267 | audio_devices_t device) |
| 5268 | { |
| 5269 | float volume = 1.0; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5270 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5271 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 5272 | |
| 5273 | if (device == AUDIO_DEVICE_NONE) { |
| 5274 | device = outputDesc->device(); |
| 5275 | } |
| 5276 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5277 | volume = volIndexToAmpl(device, streamDesc, index); |
| 5278 | |
| 5279 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5280 | // to avoid sound level bursts in user's ears: |
| 5281 | // - always attenuate ring tones and notifications volume by 6dB |
| 5282 | // - if music is playing, always limit the volume to current music volume, |
| 5283 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5284 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5285 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5286 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5287 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 5288 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 5289 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5290 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5291 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5292 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5293 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5294 | streamDesc.mCanBeMuted) { |
| 5295 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; |
| 5296 | // when the phone is ringing we must consider that music could have been paused just before |
| 5297 | // by the music application and behave as if music was active if the last music track was |
| 5298 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5299 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5300 | mLimitRingtoneVolume) { |
| 5301 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5302 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, |
| 5303 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5304 | output, |
| 5305 | musicDevice); |
| 5306 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? |
| 5307 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; |
| 5308 | if (volume > minVol) { |
| 5309 | volume = minVol; |
| 5310 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); |
| 5311 | } |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | return volume; |
| 5316 | } |
| 5317 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5318 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5319 | int index, |
| 5320 | audio_io_handle_t output, |
| 5321 | audio_devices_t device, |
| 5322 | int delayMs, |
| 5323 | bool force) |
| 5324 | { |
| 5325 | |
| 5326 | // do not change actual stream volume if the stream is muted |
| 5327 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 5328 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
| 5329 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 5330 | return NO_ERROR; |
| 5331 | } |
| 5332 | |
| 5333 | // 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] | 5334 | if ((stream == AUDIO_STREAM_VOICE_CALL && |
| 5335 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5336 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && |
| 5337 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5338 | 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] | 5339 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5340 | return INVALID_OPERATION; |
| 5341 | } |
| 5342 | |
| 5343 | float volume = computeVolume(stream, index, output, device); |
| 5344 | // We actually change the volume if: |
| 5345 | // - the float value returned by computeVolume() changed |
| 5346 | // - the force flag is set |
| 5347 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
| 5348 | force) { |
| 5349 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 5350 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 5351 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is |
| 5352 | // enabled |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5353 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
| 5354 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5355 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5356 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5357 | } |
| 5358 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5359 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5360 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5361 | float voiceVolume; |
| 5362 | // 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] | 5363 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5364 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 5365 | } else { |
| 5366 | voiceVolume = 1.0; |
| 5367 | } |
| 5368 | |
| 5369 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { |
| 5370 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5371 | mLastVoiceVolume = voiceVolume; |
| 5372 | } |
| 5373 | } |
| 5374 | |
| 5375 | return NO_ERROR; |
| 5376 | } |
| 5377 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5378 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5379 | audio_devices_t device, |
| 5380 | int delayMs, |
| 5381 | bool force) |
| 5382 | { |
| 5383 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); |
| 5384 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5385 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5386 | if (stream == AUDIO_STREAM_PATCH) { |
| 5387 | continue; |
| 5388 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5389 | checkAndSetVolume((audio_stream_type_t)stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5390 | mStreams[stream].getVolumeIndex(device), |
| 5391 | output, |
| 5392 | device, |
| 5393 | delayMs, |
| 5394 | force); |
| 5395 | } |
| 5396 | } |
| 5397 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5398 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5399 | bool on, |
| 5400 | audio_io_handle_t output, |
| 5401 | int delayMs, |
| 5402 | audio_devices_t device) |
| 5403 | { |
| 5404 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5405 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5406 | if (stream == AUDIO_STREAM_PATCH) { |
| 5407 | continue; |
| 5408 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5409 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5410 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5411 | } |
| 5412 | } |
| 5413 | } |
| 5414 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5415 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5416 | bool on, |
| 5417 | audio_io_handle_t output, |
| 5418 | int delayMs, |
| 5419 | audio_devices_t device) |
| 5420 | { |
| 5421 | StreamDescriptor &streamDesc = mStreams[stream]; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5422 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5423 | if (device == AUDIO_DEVICE_NONE) { |
| 5424 | device = outputDesc->device(); |
| 5425 | } |
| 5426 | |
| 5427 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", |
| 5428 | stream, on, output, outputDesc->mMuteCount[stream], device); |
| 5429 | |
| 5430 | if (on) { |
| 5431 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5432 | if (streamDesc.mCanBeMuted && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5433 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 5434 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5435 | checkAndSetVolume(stream, 0, output, device, delayMs); |
| 5436 | } |
| 5437 | } |
| 5438 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5439 | outputDesc->mMuteCount[stream]++; |
| 5440 | } else { |
| 5441 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5442 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5443 | return; |
| 5444 | } |
| 5445 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5446 | checkAndSetVolume(stream, |
| 5447 | streamDesc.getVolumeIndex(device), |
| 5448 | output, |
| 5449 | device, |
| 5450 | delayMs); |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5455 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5456 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5457 | { |
| 5458 | // if the stream pertains to sonification strategy and we are in call we must |
| 5459 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5460 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5461 | // interfere with the device used for phone strategy |
| 5462 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5463 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5464 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5465 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5466 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5467 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5468 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5469 | stream, starting, outputDesc->mDevice, stateChange); |
| 5470 | if (outputDesc->mRefCount[stream]) { |
| 5471 | int muteCount = 1; |
| 5472 | if (stateChange) { |
| 5473 | muteCount = outputDesc->mRefCount[stream]; |
| 5474 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5475 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5476 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5477 | for (int i = 0; i < muteCount; i++) { |
| 5478 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5479 | } |
| 5480 | } else { |
| 5481 | ALOGV("handleIncallSonification() high visibility"); |
| 5482 | if (outputDesc->device() & |
| 5483 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5484 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5485 | for (int i = 0; i < muteCount; i++) { |
| 5486 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5487 | } |
| 5488 | } |
| 5489 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5490 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5491 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5492 | } else { |
| 5493 | mpClientInterface->stopTone(); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | } |
| 5498 | } |
| 5499 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5500 | bool AudioPolicyManager::isInCall() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5501 | { |
| 5502 | return isStateInCall(mPhoneState); |
| 5503 | } |
| 5504 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5505 | bool AudioPolicyManager::isStateInCall(int state) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5506 | return ((state == AUDIO_MODE_IN_CALL) || |
| 5507 | (state == AUDIO_MODE_IN_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5508 | } |
| 5509 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5510 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5511 | { |
| 5512 | return MAX_EFFECTS_CPU_LOAD; |
| 5513 | } |
| 5514 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5515 | uint32_t AudioPolicyManager::getMaxEffectsMemory() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5516 | { |
| 5517 | return MAX_EFFECTS_MEMORY; |
| 5518 | } |
| 5519 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5520 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5521 | // --- AudioOutputDescriptor class implementation |
| 5522 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5523 | AudioPolicyManager::AudioOutputDescriptor::AudioOutputDescriptor( |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5524 | const sp<IOProfile>& profile) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5525 | : mId(0), mIoHandle(0), mLatency(0), |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5526 | mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5527 | mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0) |
| 5528 | { |
| 5529 | // clear usage count for all stream types |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5530 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5531 | mRefCount[i] = 0; |
| 5532 | mCurVolume[i] = -1.0; |
| 5533 | mMuteCount[i] = 0; |
| 5534 | mStopTime[i] = 0; |
| 5535 | } |
| 5536 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5537 | mStrategyMutedByDevice[i] = false; |
| 5538 | } |
| 5539 | if (profile != NULL) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5540 | mFlags = (audio_output_flags_t)profile->mFlags; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5541 | mSamplingRate = profile->pickSamplingRate(); |
| 5542 | mFormat = profile->pickFormat(); |
| 5543 | mChannelMask = profile->pickChannelMask(); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5544 | if (profile->mGains.size() > 0) { |
| 5545 | profile->mGains[0]->getDefaultConfig(&mGain); |
| 5546 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5550 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::device() const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5551 | { |
| 5552 | if (isDuplicated()) { |
| 5553 | return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice); |
| 5554 | } else { |
| 5555 | return mDevice; |
| 5556 | } |
| 5557 | } |
| 5558 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5559 | uint32_t AudioPolicyManager::AudioOutputDescriptor::latency() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5560 | { |
| 5561 | if (isDuplicated()) { |
| 5562 | return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency; |
| 5563 | } else { |
| 5564 | return mLatency; |
| 5565 | } |
| 5566 | } |
| 5567 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5568 | bool AudioPolicyManager::AudioOutputDescriptor::sharesHwModuleWith( |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5569 | const sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5570 | { |
| 5571 | if (isDuplicated()) { |
| 5572 | return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc); |
| 5573 | } else if (outputDesc->isDuplicated()){ |
| 5574 | return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2); |
| 5575 | } else { |
| 5576 | return (mProfile->mModule == outputDesc->mProfile->mModule); |
| 5577 | } |
| 5578 | } |
| 5579 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5580 | void AudioPolicyManager::AudioOutputDescriptor::changeRefCount(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5581 | int delta) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5582 | { |
| 5583 | // forward usage count change to attached outputs |
| 5584 | if (isDuplicated()) { |
| 5585 | mOutput1->changeRefCount(stream, delta); |
| 5586 | mOutput2->changeRefCount(stream, delta); |
| 5587 | } |
| 5588 | if ((delta + (int)mRefCount[stream]) < 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5589 | ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", |
| 5590 | delta, stream, mRefCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5591 | mRefCount[stream] = 0; |
| 5592 | return; |
| 5593 | } |
| 5594 | mRefCount[stream] += delta; |
| 5595 | ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]); |
| 5596 | } |
| 5597 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5598 | audio_devices_t AudioPolicyManager::AudioOutputDescriptor::supportedDevices() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5599 | { |
| 5600 | if (isDuplicated()) { |
| 5601 | return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices()); |
| 5602 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5603 | return mProfile->mSupportedDevices.types() ; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5607 | bool AudioPolicyManager::AudioOutputDescriptor::isActive(uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5608 | { |
| 5609 | return isStrategyActive(NUM_STRATEGIES, inPastMs); |
| 5610 | } |
| 5611 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5612 | bool AudioPolicyManager::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5613 | uint32_t inPastMs, |
| 5614 | nsecs_t sysTime) const |
| 5615 | { |
| 5616 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5617 | sysTime = systemTime(); |
| 5618 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5619 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5620 | if (i == AUDIO_STREAM_PATCH) { |
| 5621 | continue; |
| 5622 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5623 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5624 | (NUM_STRATEGIES == strategy)) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5625 | isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5626 | return true; |
| 5627 | } |
| 5628 | } |
| 5629 | return false; |
| 5630 | } |
| 5631 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5632 | bool AudioPolicyManager::AudioOutputDescriptor::isStreamActive(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5633 | uint32_t inPastMs, |
| 5634 | nsecs_t sysTime) const |
| 5635 | { |
| 5636 | if (mRefCount[stream] != 0) { |
| 5637 | return true; |
| 5638 | } |
| 5639 | if (inPastMs == 0) { |
| 5640 | return false; |
| 5641 | } |
| 5642 | if (sysTime == 0) { |
| 5643 | sysTime = systemTime(); |
| 5644 | } |
| 5645 | if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) { |
| 5646 | return true; |
| 5647 | } |
| 5648 | return false; |
| 5649 | } |
| 5650 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5651 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPortConfig( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5652 | struct audio_port_config *dstConfig, |
| 5653 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5654 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5655 | ALOG_ASSERT(!isDuplicated(), "toAudioPortConfig() called on duplicated output %d", mIoHandle); |
| 5656 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5657 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 5658 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; |
| 5659 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5660 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5661 | } |
| 5662 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 5663 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5664 | dstConfig->id = mId; |
| 5665 | dstConfig->role = AUDIO_PORT_ROLE_SOURCE; |
| 5666 | dstConfig->type = AUDIO_PORT_TYPE_MIX; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5667 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; |
| 5668 | dstConfig->ext.mix.handle = mIoHandle; |
| 5669 | dstConfig->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | void AudioPolicyManager::AudioOutputDescriptor::toAudioPort( |
| 5673 | struct audio_port *port) const |
| 5674 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5675 | ALOG_ASSERT(!isDuplicated(), "toAudioPort() called on duplicated output %d", mIoHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5676 | mProfile->toAudioPort(port); |
| 5677 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5678 | toAudioPortConfig(&port->active_config); |
| 5679 | port->ext.mix.hw_module = mProfile->mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5680 | port->ext.mix.handle = mIoHandle; |
| 5681 | port->ext.mix.latency_class = |
| 5682 | mFlags & AUDIO_OUTPUT_FLAG_FAST ? AUDIO_LATENCY_LOW : AUDIO_LATENCY_NORMAL; |
| 5683 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5684 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5685 | status_t AudioPolicyManager::AudioOutputDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5686 | { |
| 5687 | const size_t SIZE = 256; |
| 5688 | char buffer[SIZE]; |
| 5689 | String8 result; |
| 5690 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5691 | snprintf(buffer, SIZE, " ID: %d\n", mId); |
| 5692 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5693 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 5694 | result.append(buffer); |
| 5695 | snprintf(buffer, SIZE, " Format: %08x\n", mFormat); |
| 5696 | result.append(buffer); |
| 5697 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); |
| 5698 | result.append(buffer); |
| 5699 | snprintf(buffer, SIZE, " Latency: %d\n", mLatency); |
| 5700 | result.append(buffer); |
| 5701 | snprintf(buffer, SIZE, " Flags %08x\n", mFlags); |
| 5702 | result.append(buffer); |
| 5703 | snprintf(buffer, SIZE, " Devices %08x\n", device()); |
| 5704 | result.append(buffer); |
| 5705 | snprintf(buffer, SIZE, " Stream volume refCount muteCount\n"); |
| 5706 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5707 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
| 5708 | snprintf(buffer, SIZE, " %02d %.03f %02d %02d\n", |
| 5709 | i, mCurVolume[i], mRefCount[i], mMuteCount[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5710 | result.append(buffer); |
| 5711 | } |
| 5712 | write(fd, result.string(), result.size()); |
| 5713 | |
| 5714 | return NO_ERROR; |
| 5715 | } |
| 5716 | |
| 5717 | // --- AudioInputDescriptor class implementation |
| 5718 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5719 | AudioPolicyManager::AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5720 | : mId(0), mIoHandle(0), |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5721 | mDevice(AUDIO_DEVICE_NONE), mPatchHandle(0), mRefCount(0), |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5722 | mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5723 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5724 | if (profile != NULL) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 5725 | mSamplingRate = profile->pickSamplingRate(); |
| 5726 | mFormat = profile->pickFormat(); |
| 5727 | mChannelMask = profile->pickChannelMask(); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5728 | if (profile->mGains.size() > 0) { |
| 5729 | profile->mGains[0]->getDefaultConfig(&mGain); |
| 5730 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5731 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5732 | } |
| 5733 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5734 | void AudioPolicyManager::AudioInputDescriptor::toAudioPortConfig( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5735 | struct audio_port_config *dstConfig, |
| 5736 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5737 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5738 | ALOG_ASSERT(mProfile != 0, |
| 5739 | "toAudioPortConfig() called on input with null profile %d", mIoHandle); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5740 | dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 5741 | AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN; |
| 5742 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5743 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 5747 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5748 | dstConfig->id = mId; |
| 5749 | dstConfig->role = AUDIO_PORT_ROLE_SINK; |
| 5750 | dstConfig->type = AUDIO_PORT_TYPE_MIX; |
Eric Laurent | 62aaabb | 2014-06-02 10:40:54 -0700 | [diff] [blame] | 5751 | dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle; |
| 5752 | dstConfig->ext.mix.handle = mIoHandle; |
| 5753 | dstConfig->ext.mix.usecase.source = mInputSource; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | void AudioPolicyManager::AudioInputDescriptor::toAudioPort( |
| 5757 | struct audio_port *port) const |
| 5758 | { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5759 | ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle); |
| 5760 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5761 | mProfile->toAudioPort(port); |
| 5762 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5763 | toAudioPortConfig(&port->active_config); |
| 5764 | port->ext.mix.hw_module = mProfile->mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5765 | port->ext.mix.handle = mIoHandle; |
| 5766 | port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL; |
| 5767 | } |
| 5768 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5769 | status_t AudioPolicyManager::AudioInputDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5770 | { |
| 5771 | const size_t SIZE = 256; |
| 5772 | char buffer[SIZE]; |
| 5773 | String8 result; |
| 5774 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 5775 | snprintf(buffer, SIZE, " ID: %d\n", mId); |
| 5776 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5777 | snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate); |
| 5778 | result.append(buffer); |
| 5779 | snprintf(buffer, SIZE, " Format: %d\n", mFormat); |
| 5780 | result.append(buffer); |
| 5781 | snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask); |
| 5782 | result.append(buffer); |
| 5783 | snprintf(buffer, SIZE, " Devices %08x\n", mDevice); |
| 5784 | result.append(buffer); |
| 5785 | snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount); |
| 5786 | result.append(buffer); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 5787 | snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount); |
| 5788 | result.append(buffer); |
| 5789 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5790 | write(fd, result.string(), result.size()); |
| 5791 | |
| 5792 | return NO_ERROR; |
| 5793 | } |
| 5794 | |
| 5795 | // --- StreamDescriptor class implementation |
| 5796 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5797 | AudioPolicyManager::StreamDescriptor::StreamDescriptor() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5798 | : mIndexMin(0), mIndexMax(1), mCanBeMuted(true) |
| 5799 | { |
| 5800 | mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0); |
| 5801 | } |
| 5802 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5803 | int AudioPolicyManager::StreamDescriptor::getVolumeIndex(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5804 | { |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5805 | device = AudioPolicyManager::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5806 | // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT |
| 5807 | if (mIndexCur.indexOfKey(device) < 0) { |
| 5808 | device = AUDIO_DEVICE_OUT_DEFAULT; |
| 5809 | } |
| 5810 | return mIndexCur.valueFor(device); |
| 5811 | } |
| 5812 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5813 | void AudioPolicyManager::StreamDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5814 | { |
| 5815 | const size_t SIZE = 256; |
| 5816 | char buffer[SIZE]; |
| 5817 | String8 result; |
| 5818 | |
| 5819 | snprintf(buffer, SIZE, "%s %02d %02d ", |
| 5820 | mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax); |
| 5821 | result.append(buffer); |
| 5822 | for (size_t i = 0; i < mIndexCur.size(); i++) { |
| 5823 | snprintf(buffer, SIZE, "%04x : %02d, ", |
| 5824 | mIndexCur.keyAt(i), |
| 5825 | mIndexCur.valueAt(i)); |
| 5826 | result.append(buffer); |
| 5827 | } |
| 5828 | result.append("\n"); |
| 5829 | |
| 5830 | write(fd, result.string(), result.size()); |
| 5831 | } |
| 5832 | |
| 5833 | // --- EffectDescriptor class implementation |
| 5834 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5835 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5836 | { |
| 5837 | const size_t SIZE = 256; |
| 5838 | char buffer[SIZE]; |
| 5839 | String8 result; |
| 5840 | |
| 5841 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); |
| 5842 | result.append(buffer); |
| 5843 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); |
| 5844 | result.append(buffer); |
| 5845 | snprintf(buffer, SIZE, " Session: %d\n", mSession); |
| 5846 | result.append(buffer); |
| 5847 | snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); |
| 5848 | result.append(buffer); |
| 5849 | snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled"); |
| 5850 | result.append(buffer); |
| 5851 | write(fd, result.string(), result.size()); |
| 5852 | |
| 5853 | return NO_ERROR; |
| 5854 | } |
| 5855 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5856 | // --- HwModule class implementation |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5857 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5858 | AudioPolicyManager::HwModule::HwModule(const char *name) |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5859 | : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), |
| 5860 | mHalVersion(AUDIO_DEVICE_API_VERSION_MIN), mHandle(0) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5861 | { |
| 5862 | } |
| 5863 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5864 | AudioPolicyManager::HwModule::~HwModule() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5865 | { |
| 5866 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5867 | mOutputProfiles[i]->mSupportedDevices.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5868 | } |
| 5869 | for (size_t i = 0; i < mInputProfiles.size(); i++) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5870 | mInputProfiles[i]->mSupportedDevices.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5871 | } |
| 5872 | free((void *)mName); |
| 5873 | } |
| 5874 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5875 | status_t AudioPolicyManager::HwModule::loadInput(cnode *root) |
| 5876 | { |
| 5877 | cnode *node = root->first_child; |
| 5878 | |
| 5879 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SINK, this); |
| 5880 | |
| 5881 | while (node) { |
| 5882 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { |
| 5883 | profile->loadSamplingRates((char *)node->value); |
| 5884 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { |
| 5885 | profile->loadFormats((char *)node->value); |
| 5886 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5887 | profile->loadInChannels((char *)node->value); |
| 5888 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { |
| 5889 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, |
| 5890 | mDeclaredDevices); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5891 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { |
| 5892 | profile->mFlags = parseInputFlagNames((char *)node->value); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5893 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5894 | profile->loadGains(node); |
| 5895 | } |
| 5896 | node = node->next; |
| 5897 | } |
| 5898 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), |
| 5899 | "loadInput() invalid supported devices"); |
| 5900 | ALOGW_IF(profile->mChannelMasks.size() == 0, |
| 5901 | "loadInput() invalid supported channel masks"); |
| 5902 | ALOGW_IF(profile->mSamplingRates.size() == 0, |
| 5903 | "loadInput() invalid supported sampling rates"); |
| 5904 | ALOGW_IF(profile->mFormats.size() == 0, |
| 5905 | "loadInput() invalid supported formats"); |
| 5906 | if (!profile->mSupportedDevices.isEmpty() && |
| 5907 | (profile->mChannelMasks.size() != 0) && |
| 5908 | (profile->mSamplingRates.size() != 0) && |
| 5909 | (profile->mFormats.size() != 0)) { |
| 5910 | |
| 5911 | ALOGV("loadInput() adding input Supported Devices %04x", |
| 5912 | profile->mSupportedDevices.types()); |
| 5913 | |
| 5914 | mInputProfiles.add(profile); |
| 5915 | return NO_ERROR; |
| 5916 | } else { |
| 5917 | return BAD_VALUE; |
| 5918 | } |
| 5919 | } |
| 5920 | |
| 5921 | status_t AudioPolicyManager::HwModule::loadOutput(cnode *root) |
| 5922 | { |
| 5923 | cnode *node = root->first_child; |
| 5924 | |
| 5925 | sp<IOProfile> profile = new IOProfile(String8(root->name), AUDIO_PORT_ROLE_SOURCE, this); |
| 5926 | |
| 5927 | while (node) { |
| 5928 | if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) { |
| 5929 | profile->loadSamplingRates((char *)node->value); |
| 5930 | } else if (strcmp(node->name, FORMATS_TAG) == 0) { |
| 5931 | profile->loadFormats((char *)node->value); |
| 5932 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5933 | profile->loadOutChannels((char *)node->value); |
| 5934 | } else if (strcmp(node->name, DEVICES_TAG) == 0) { |
| 5935 | profile->mSupportedDevices.loadDevicesFromName((char *)node->value, |
| 5936 | mDeclaredDevices); |
| 5937 | } else if (strcmp(node->name, FLAGS_TAG) == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 5938 | profile->mFlags = parseOutputFlagNames((char *)node->value); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5939 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5940 | profile->loadGains(node); |
| 5941 | } |
| 5942 | node = node->next; |
| 5943 | } |
| 5944 | ALOGW_IF(profile->mSupportedDevices.isEmpty(), |
| 5945 | "loadOutput() invalid supported devices"); |
| 5946 | ALOGW_IF(profile->mChannelMasks.size() == 0, |
| 5947 | "loadOutput() invalid supported channel masks"); |
| 5948 | ALOGW_IF(profile->mSamplingRates.size() == 0, |
| 5949 | "loadOutput() invalid supported sampling rates"); |
| 5950 | ALOGW_IF(profile->mFormats.size() == 0, |
| 5951 | "loadOutput() invalid supported formats"); |
| 5952 | if (!profile->mSupportedDevices.isEmpty() && |
| 5953 | (profile->mChannelMasks.size() != 0) && |
| 5954 | (profile->mSamplingRates.size() != 0) && |
| 5955 | (profile->mFormats.size() != 0)) { |
| 5956 | |
| 5957 | ALOGV("loadOutput() adding output Supported Devices %04x, mFlags %04x", |
| 5958 | profile->mSupportedDevices.types(), profile->mFlags); |
| 5959 | |
| 5960 | mOutputProfiles.add(profile); |
| 5961 | return NO_ERROR; |
| 5962 | } else { |
| 5963 | return BAD_VALUE; |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | status_t AudioPolicyManager::HwModule::loadDevice(cnode *root) |
| 5968 | { |
| 5969 | cnode *node = root->first_child; |
| 5970 | |
| 5971 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 5972 | while (node) { |
| 5973 | if (strcmp(node->name, DEVICE_TYPE) == 0) { |
| 5974 | type = parseDeviceNames((char *)node->value); |
| 5975 | break; |
| 5976 | } |
| 5977 | node = node->next; |
| 5978 | } |
| 5979 | if (type == AUDIO_DEVICE_NONE || |
| 5980 | (!audio_is_input_device(type) && !audio_is_output_device(type))) { |
| 5981 | ALOGW("loadDevice() bad type %08x", type); |
| 5982 | return BAD_VALUE; |
| 5983 | } |
| 5984 | sp<DeviceDescriptor> deviceDesc = new DeviceDescriptor(String8(root->name), type); |
| 5985 | deviceDesc->mModule = this; |
| 5986 | |
| 5987 | node = root->first_child; |
| 5988 | while (node) { |
| 5989 | if (strcmp(node->name, DEVICE_ADDRESS) == 0) { |
| 5990 | deviceDesc->mAddress = String8((char *)node->value); |
| 5991 | } else if (strcmp(node->name, CHANNELS_TAG) == 0) { |
| 5992 | if (audio_is_input_device(type)) { |
| 5993 | deviceDesc->loadInChannels((char *)node->value); |
| 5994 | } else { |
| 5995 | deviceDesc->loadOutChannels((char *)node->value); |
| 5996 | } |
| 5997 | } else if (strcmp(node->name, GAINS_TAG) == 0) { |
| 5998 | deviceDesc->loadGains(node); |
| 5999 | } |
| 6000 | node = node->next; |
| 6001 | } |
| 6002 | |
| 6003 | ALOGV("loadDevice() adding device name %s type %08x address %s", |
| 6004 | deviceDesc->mName.string(), type, deviceDesc->mAddress.string()); |
| 6005 | |
| 6006 | mDeclaredDevices.add(deviceDesc); |
| 6007 | |
| 6008 | return NO_ERROR; |
| 6009 | } |
| 6010 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6011 | void AudioPolicyManager::HwModule::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6012 | { |
| 6013 | const size_t SIZE = 256; |
| 6014 | char buffer[SIZE]; |
| 6015 | String8 result; |
| 6016 | |
| 6017 | snprintf(buffer, SIZE, " - name: %s\n", mName); |
| 6018 | result.append(buffer); |
| 6019 | snprintf(buffer, SIZE, " - handle: %d\n", mHandle); |
| 6020 | result.append(buffer); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 6021 | snprintf(buffer, SIZE, " - version: %u.%u\n", mHalVersion >> 8, mHalVersion & 0xFF); |
| 6022 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6023 | write(fd, result.string(), result.size()); |
| 6024 | if (mOutputProfiles.size()) { |
| 6025 | write(fd, " - outputs:\n", strlen(" - outputs:\n")); |
| 6026 | for (size_t i = 0; i < mOutputProfiles.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6027 | snprintf(buffer, SIZE, " output %zu:\n", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6028 | write(fd, buffer, strlen(buffer)); |
| 6029 | mOutputProfiles[i]->dump(fd); |
| 6030 | } |
| 6031 | } |
| 6032 | if (mInputProfiles.size()) { |
| 6033 | write(fd, " - inputs:\n", strlen(" - inputs:\n")); |
| 6034 | for (size_t i = 0; i < mInputProfiles.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6035 | snprintf(buffer, SIZE, " input %zu:\n", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6036 | write(fd, buffer, strlen(buffer)); |
| 6037 | mInputProfiles[i]->dump(fd); |
| 6038 | } |
| 6039 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6040 | if (mDeclaredDevices.size()) { |
| 6041 | write(fd, " - devices:\n", strlen(" - devices:\n")); |
| 6042 | for (size_t i = 0; i < mDeclaredDevices.size(); i++) { |
| 6043 | mDeclaredDevices[i]->dump(fd, 4, i); |
| 6044 | } |
| 6045 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6046 | } |
| 6047 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6048 | // --- AudioPort class implementation |
| 6049 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6050 | |
| 6051 | AudioPolicyManager::AudioPort::AudioPort(const String8& name, audio_port_type_t type, |
| 6052 | audio_port_role_t role, const sp<HwModule>& module) : |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 6053 | mName(name), mType(type), mRole(role), mModule(module), mFlags(0) |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6054 | { |
| 6055 | mUseInChannelMask = ((type == AUDIO_PORT_TYPE_DEVICE) && (role == AUDIO_PORT_ROLE_SOURCE)) || |
| 6056 | ((type == AUDIO_PORT_TYPE_MIX) && (role == AUDIO_PORT_ROLE_SINK)); |
| 6057 | } |
| 6058 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6059 | void AudioPolicyManager::AudioPort::toAudioPort(struct audio_port *port) const |
| 6060 | { |
| 6061 | port->role = mRole; |
| 6062 | port->type = mType; |
| 6063 | unsigned int i; |
| 6064 | 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] | 6065 | if (mSamplingRates[i] != 0) { |
| 6066 | port->sample_rates[i] = mSamplingRates[i]; |
| 6067 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6068 | } |
| 6069 | port->num_sample_rates = i; |
| 6070 | 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] | 6071 | if (mChannelMasks[i] != 0) { |
| 6072 | port->channel_masks[i] = mChannelMasks[i]; |
| 6073 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6074 | } |
| 6075 | port->num_channel_masks = i; |
| 6076 | 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] | 6077 | if (mFormats[i] != 0) { |
| 6078 | port->formats[i] = mFormats[i]; |
| 6079 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6080 | } |
| 6081 | port->num_formats = i; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 6082 | |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 6083 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 6084 | |
| 6085 | for (i = 0; i < mGains.size() && i < AUDIO_PORT_MAX_GAINS; i++) { |
| 6086 | port->gains[i] = mGains[i]->mGain; |
| 6087 | } |
| 6088 | port->num_gains = i; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6089 | } |
| 6090 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6091 | void AudioPolicyManager::AudioPort::importAudioPort(const sp<AudioPort> port) { |
| 6092 | for (size_t k = 0 ; k < port->mSamplingRates.size() ; k++) { |
| 6093 | const uint32_t rate = port->mSamplingRates.itemAt(k); |
| 6094 | if (rate != 0) { // skip "dynamic" rates |
| 6095 | bool hasRate = false; |
| 6096 | for (size_t l = 0 ; l < mSamplingRates.size() ; l++) { |
| 6097 | if (rate == mSamplingRates.itemAt(l)) { |
| 6098 | hasRate = true; |
| 6099 | break; |
| 6100 | } |
| 6101 | } |
| 6102 | if (!hasRate) { // never import a sampling rate twice |
| 6103 | mSamplingRates.add(rate); |
| 6104 | } |
| 6105 | } |
| 6106 | } |
| 6107 | for (size_t k = 0 ; k < port->mChannelMasks.size() ; k++) { |
| 6108 | const audio_channel_mask_t mask = port->mChannelMasks.itemAt(k); |
| 6109 | if (mask != 0) { // skip "dynamic" masks |
| 6110 | bool hasMask = false; |
| 6111 | for (size_t l = 0 ; l < mChannelMasks.size() ; l++) { |
| 6112 | if (mask == mChannelMasks.itemAt(l)) { |
| 6113 | hasMask = true; |
| 6114 | break; |
| 6115 | } |
| 6116 | } |
| 6117 | if (!hasMask) { // never import a channel mask twice |
| 6118 | mChannelMasks.add(mask); |
| 6119 | } |
| 6120 | } |
| 6121 | } |
| 6122 | for (size_t k = 0 ; k < port->mFormats.size() ; k++) { |
| 6123 | const audio_format_t format = port->mFormats.itemAt(k); |
| 6124 | if (format != 0) { // skip "dynamic" formats |
| 6125 | bool hasFormat = false; |
| 6126 | for (size_t l = 0 ; l < mFormats.size() ; l++) { |
| 6127 | if (format == mFormats.itemAt(l)) { |
| 6128 | hasFormat = true; |
| 6129 | break; |
| 6130 | } |
| 6131 | } |
| 6132 | if (!hasFormat) { // never import a channel mask twice |
| 6133 | mFormats.add(format); |
| 6134 | } |
| 6135 | } |
| 6136 | } |
Hochi Huang | 23fe3c0 | 2014-10-03 09:09:30 +0800 | [diff] [blame] | 6137 | for (size_t k = 0 ; k < port->mGains.size() ; k++) { |
| 6138 | sp<AudioGain> gain = port->mGains.itemAt(k); |
| 6139 | if (gain != 0) { |
| 6140 | bool hasGain = false; |
| 6141 | for (size_t l = 0 ; l < mGains.size() ; l++) { |
| 6142 | if (gain == mGains.itemAt(l)) { |
| 6143 | hasGain = true; |
| 6144 | break; |
| 6145 | } |
| 6146 | } |
| 6147 | if (!hasGain) { // never import a gain twice |
| 6148 | mGains.add(gain); |
| 6149 | } |
| 6150 | } |
| 6151 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | void AudioPolicyManager::AudioPort::clearCapabilities() { |
| 6155 | mChannelMasks.clear(); |
| 6156 | mFormats.clear(); |
| 6157 | mSamplingRates.clear(); |
Hochi Huang | 23fe3c0 | 2014-10-03 09:09:30 +0800 | [diff] [blame] | 6158 | mGains.clear(); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6159 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6160 | |
| 6161 | void AudioPolicyManager::AudioPort::loadSamplingRates(char *name) |
| 6162 | { |
| 6163 | char *str = strtok(name, "|"); |
| 6164 | |
| 6165 | // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling |
| 6166 | // rates should be read from the output stream after it is opened for the first time |
| 6167 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6168 | mSamplingRates.add(0); |
| 6169 | return; |
| 6170 | } |
| 6171 | |
| 6172 | while (str != NULL) { |
| 6173 | uint32_t rate = atoi(str); |
| 6174 | if (rate != 0) { |
| 6175 | ALOGV("loadSamplingRates() adding rate %d", rate); |
| 6176 | mSamplingRates.add(rate); |
| 6177 | } |
| 6178 | str = strtok(NULL, "|"); |
| 6179 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | void AudioPolicyManager::AudioPort::loadFormats(char *name) |
| 6183 | { |
| 6184 | char *str = strtok(name, "|"); |
| 6185 | |
| 6186 | // by convention, "0' in the first entry in mFormats indicates the supported formats |
| 6187 | // should be read from the output stream after it is opened for the first time |
| 6188 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6189 | mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 6190 | return; |
| 6191 | } |
| 6192 | |
| 6193 | while (str != NULL) { |
| 6194 | audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable, |
| 6195 | ARRAY_SIZE(sFormatNameToEnumTable), |
| 6196 | str); |
| 6197 | if (format != AUDIO_FORMAT_DEFAULT) { |
| 6198 | mFormats.add(format); |
| 6199 | } |
| 6200 | str = strtok(NULL, "|"); |
| 6201 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6202 | } |
| 6203 | |
| 6204 | void AudioPolicyManager::AudioPort::loadInChannels(char *name) |
| 6205 | { |
| 6206 | const char *str = strtok(name, "|"); |
| 6207 | |
| 6208 | ALOGV("loadInChannels() %s", name); |
| 6209 | |
| 6210 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6211 | mChannelMasks.add(0); |
| 6212 | return; |
| 6213 | } |
| 6214 | |
| 6215 | while (str != NULL) { |
| 6216 | audio_channel_mask_t channelMask = |
| 6217 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, |
| 6218 | ARRAY_SIZE(sInChannelsNameToEnumTable), |
| 6219 | str); |
| 6220 | if (channelMask != 0) { |
| 6221 | ALOGV("loadInChannels() adding channelMask %04x", channelMask); |
| 6222 | mChannelMasks.add(channelMask); |
| 6223 | } |
| 6224 | str = strtok(NULL, "|"); |
| 6225 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | void AudioPolicyManager::AudioPort::loadOutChannels(char *name) |
| 6229 | { |
| 6230 | const char *str = strtok(name, "|"); |
| 6231 | |
| 6232 | ALOGV("loadOutChannels() %s", name); |
| 6233 | |
| 6234 | // by convention, "0' in the first entry in mChannelMasks indicates the supported channel |
| 6235 | // masks should be read from the output stream after it is opened for the first time |
| 6236 | if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) { |
| 6237 | mChannelMasks.add(0); |
| 6238 | return; |
| 6239 | } |
| 6240 | |
| 6241 | while (str != NULL) { |
| 6242 | audio_channel_mask_t channelMask = |
| 6243 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, |
| 6244 | ARRAY_SIZE(sOutChannelsNameToEnumTable), |
| 6245 | str); |
| 6246 | if (channelMask != 0) { |
| 6247 | mChannelMasks.add(channelMask); |
| 6248 | } |
| 6249 | str = strtok(NULL, "|"); |
| 6250 | } |
| 6251 | return; |
| 6252 | } |
| 6253 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6254 | audio_gain_mode_t AudioPolicyManager::AudioPort::loadGainMode(char *name) |
| 6255 | { |
| 6256 | const char *str = strtok(name, "|"); |
| 6257 | |
| 6258 | ALOGV("loadGainMode() %s", name); |
| 6259 | audio_gain_mode_t mode = 0; |
| 6260 | while (str != NULL) { |
| 6261 | mode |= (audio_gain_mode_t)stringToEnum(sGainModeNameToEnumTable, |
| 6262 | ARRAY_SIZE(sGainModeNameToEnumTable), |
| 6263 | str); |
| 6264 | str = strtok(NULL, "|"); |
| 6265 | } |
| 6266 | return mode; |
| 6267 | } |
| 6268 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6269 | void AudioPolicyManager::AudioPort::loadGain(cnode *root, int index) |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6270 | { |
| 6271 | cnode *node = root->first_child; |
| 6272 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6273 | sp<AudioGain> gain = new AudioGain(index, mUseInChannelMask); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6274 | |
| 6275 | while (node) { |
| 6276 | if (strcmp(node->name, GAIN_MODE) == 0) { |
| 6277 | gain->mGain.mode = loadGainMode((char *)node->value); |
| 6278 | } else if (strcmp(node->name, GAIN_CHANNELS) == 0) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6279 | if (mUseInChannelMask) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6280 | gain->mGain.channel_mask = |
| 6281 | (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable, |
| 6282 | ARRAY_SIZE(sInChannelsNameToEnumTable), |
| 6283 | (char *)node->value); |
| 6284 | } else { |
| 6285 | gain->mGain.channel_mask = |
| 6286 | (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable, |
| 6287 | ARRAY_SIZE(sOutChannelsNameToEnumTable), |
| 6288 | (char *)node->value); |
| 6289 | } |
| 6290 | } else if (strcmp(node->name, GAIN_MIN_VALUE) == 0) { |
| 6291 | gain->mGain.min_value = atoi((char *)node->value); |
| 6292 | } else if (strcmp(node->name, GAIN_MAX_VALUE) == 0) { |
| 6293 | gain->mGain.max_value = atoi((char *)node->value); |
| 6294 | } else if (strcmp(node->name, GAIN_DEFAULT_VALUE) == 0) { |
| 6295 | gain->mGain.default_value = atoi((char *)node->value); |
| 6296 | } else if (strcmp(node->name, GAIN_STEP_VALUE) == 0) { |
| 6297 | gain->mGain.step_value = atoi((char *)node->value); |
| 6298 | } else if (strcmp(node->name, GAIN_MIN_RAMP_MS) == 0) { |
| 6299 | gain->mGain.min_ramp_ms = atoi((char *)node->value); |
| 6300 | } else if (strcmp(node->name, GAIN_MAX_RAMP_MS) == 0) { |
| 6301 | gain->mGain.max_ramp_ms = atoi((char *)node->value); |
| 6302 | } |
| 6303 | node = node->next; |
| 6304 | } |
| 6305 | |
| 6306 | ALOGV("loadGain() adding new gain mode %08x channel mask %08x min mB %d max mB %d", |
| 6307 | gain->mGain.mode, gain->mGain.channel_mask, gain->mGain.min_value, gain->mGain.max_value); |
| 6308 | |
| 6309 | if (gain->mGain.mode == 0) { |
| 6310 | return; |
| 6311 | } |
| 6312 | mGains.add(gain); |
| 6313 | } |
| 6314 | |
| 6315 | void AudioPolicyManager::AudioPort::loadGains(cnode *root) |
| 6316 | { |
| 6317 | cnode *node = root->first_child; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6318 | int index = 0; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6319 | while (node) { |
| 6320 | ALOGV("loadGains() loading gain %s", node->name); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6321 | loadGain(node, index++); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6322 | node = node->next; |
| 6323 | } |
| 6324 | } |
| 6325 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6326 | status_t AudioPolicyManager::AudioPort::checkExactSamplingRate(uint32_t samplingRate) const |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6327 | { |
| 6328 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6329 | if (mSamplingRates[i] == samplingRate) { |
| 6330 | return NO_ERROR; |
| 6331 | } |
| 6332 | } |
| 6333 | return BAD_VALUE; |
| 6334 | } |
| 6335 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6336 | status_t AudioPolicyManager::AudioPort::checkCompatibleSamplingRate(uint32_t samplingRate, |
| 6337 | uint32_t *updatedSamplingRate) const |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6338 | { |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6339 | // Search for the closest supported sampling rate that is above (preferred) |
| 6340 | // or below (acceptable) the desired sampling rate, within a permitted ratio. |
| 6341 | // The sampling rates do not need to be sorted in ascending order. |
| 6342 | ssize_t maxBelow = -1; |
| 6343 | ssize_t minAbove = -1; |
| 6344 | uint32_t candidate; |
| 6345 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
| 6346 | candidate = mSamplingRates[i]; |
| 6347 | if (candidate == samplingRate) { |
| 6348 | if (updatedSamplingRate != NULL) { |
| 6349 | *updatedSamplingRate = candidate; |
| 6350 | } |
| 6351 | return NO_ERROR; |
| 6352 | } |
| 6353 | // candidate < desired |
| 6354 | if (candidate < samplingRate) { |
| 6355 | if (maxBelow < 0 || candidate > mSamplingRates[maxBelow]) { |
| 6356 | maxBelow = i; |
| 6357 | } |
| 6358 | // candidate > desired |
| 6359 | } else { |
| 6360 | if (minAbove < 0 || candidate < mSamplingRates[minAbove]) { |
| 6361 | minAbove = i; |
| 6362 | } |
| 6363 | } |
| 6364 | } |
| 6365 | // This uses hard-coded knowledge about AudioFlinger resampling ratios. |
| 6366 | // TODO Move these assumptions out. |
| 6367 | static const uint32_t kMaxDownSampleRatio = 6; // beyond this aliasing occurs |
| 6368 | static const uint32_t kMaxUpSampleRatio = 256; // beyond this sample rate inaccuracies occur |
| 6369 | // due to approximation by an int32_t of the |
| 6370 | // phase increments |
| 6371 | // Prefer to down-sample from a higher sampling rate, as we get the desired frequency spectrum. |
| 6372 | if (minAbove >= 0) { |
| 6373 | candidate = mSamplingRates[minAbove]; |
| 6374 | if (candidate / kMaxDownSampleRatio <= samplingRate) { |
| 6375 | if (updatedSamplingRate != NULL) { |
| 6376 | *updatedSamplingRate = candidate; |
| 6377 | } |
| 6378 | return NO_ERROR; |
| 6379 | } |
| 6380 | } |
| 6381 | // But if we have to up-sample from a lower sampling rate, that's OK. |
| 6382 | if (maxBelow >= 0) { |
| 6383 | candidate = mSamplingRates[maxBelow]; |
| 6384 | if (candidate * kMaxUpSampleRatio >= samplingRate) { |
| 6385 | if (updatedSamplingRate != NULL) { |
| 6386 | *updatedSamplingRate = candidate; |
| 6387 | } |
| 6388 | return NO_ERROR; |
| 6389 | } |
| 6390 | } |
| 6391 | // leave updatedSamplingRate unmodified |
| 6392 | return BAD_VALUE; |
| 6393 | } |
| 6394 | |
| 6395 | status_t AudioPolicyManager::AudioPort::checkExactChannelMask(audio_channel_mask_t channelMask) const |
| 6396 | { |
| 6397 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6398 | if (mChannelMasks[i] == channelMask) { |
| 6399 | return NO_ERROR; |
| 6400 | } |
| 6401 | } |
| 6402 | return BAD_VALUE; |
| 6403 | } |
| 6404 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6405 | status_t AudioPolicyManager::AudioPort::checkCompatibleChannelMask(audio_channel_mask_t channelMask) |
| 6406 | const |
| 6407 | { |
| 6408 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; |
| 6409 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6410 | // FIXME Does not handle multi-channel automatic conversions yet |
| 6411 | audio_channel_mask_t supported = mChannelMasks[i]; |
| 6412 | if (supported == channelMask) { |
| 6413 | return NO_ERROR; |
| 6414 | } |
| 6415 | if (isRecordThread) { |
| 6416 | // This uses hard-coded knowledge that AudioFlinger can silently down-mix and up-mix. |
| 6417 | // FIXME Abstract this out to a table. |
| 6418 | if (((supported == AUDIO_CHANNEL_IN_FRONT_BACK || supported == AUDIO_CHANNEL_IN_STEREO) |
| 6419 | && channelMask == AUDIO_CHANNEL_IN_MONO) || |
| 6420 | (supported == AUDIO_CHANNEL_IN_MONO && (channelMask == AUDIO_CHANNEL_IN_FRONT_BACK |
| 6421 | || channelMask == AUDIO_CHANNEL_IN_STEREO))) { |
| 6422 | return NO_ERROR; |
| 6423 | } |
| 6424 | } |
| 6425 | } |
| 6426 | return BAD_VALUE; |
| 6427 | } |
| 6428 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6429 | status_t AudioPolicyManager::AudioPort::checkFormat(audio_format_t format) const |
| 6430 | { |
| 6431 | for (size_t i = 0; i < mFormats.size(); i ++) { |
| 6432 | if (mFormats[i] == format) { |
| 6433 | return NO_ERROR; |
| 6434 | } |
| 6435 | } |
| 6436 | return BAD_VALUE; |
| 6437 | } |
| 6438 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6439 | |
| 6440 | uint32_t AudioPolicyManager::AudioPort::pickSamplingRate() const |
| 6441 | { |
| 6442 | // special case for uninitialized dynamic profile |
| 6443 | if (mSamplingRates.size() == 1 && mSamplingRates[0] == 0) { |
| 6444 | return 0; |
| 6445 | } |
| 6446 | |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6447 | // For direct outputs, pick minimum sampling rate: this helps ensuring that the |
| 6448 | // channel count / sampling rate combination chosen will be supported by the connected |
| 6449 | // sink |
| 6450 | if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) && |
| 6451 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) { |
| 6452 | uint32_t samplingRate = UINT_MAX; |
| 6453 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6454 | if ((mSamplingRates[i] < samplingRate) && (mSamplingRates[i] > 0)) { |
| 6455 | samplingRate = mSamplingRates[i]; |
| 6456 | } |
| 6457 | } |
| 6458 | return (samplingRate == UINT_MAX) ? 0 : samplingRate; |
| 6459 | } |
| 6460 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6461 | uint32_t samplingRate = 0; |
| 6462 | uint32_t maxRate = MAX_MIXER_SAMPLING_RATE; |
| 6463 | |
| 6464 | // For mixed output and inputs, use max mixer sampling rates. Do not |
| 6465 | // limit sampling rate otherwise |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6466 | if (mType != AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6467 | maxRate = UINT_MAX; |
| 6468 | } |
| 6469 | for (size_t i = 0; i < mSamplingRates.size(); i ++) { |
| 6470 | if ((mSamplingRates[i] > samplingRate) && (mSamplingRates[i] <= maxRate)) { |
| 6471 | samplingRate = mSamplingRates[i]; |
| 6472 | } |
| 6473 | } |
| 6474 | return samplingRate; |
| 6475 | } |
| 6476 | |
| 6477 | audio_channel_mask_t AudioPolicyManager::AudioPort::pickChannelMask() const |
| 6478 | { |
| 6479 | // special case for uninitialized dynamic profile |
| 6480 | if (mChannelMasks.size() == 1 && mChannelMasks[0] == 0) { |
| 6481 | return AUDIO_CHANNEL_NONE; |
| 6482 | } |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6483 | audio_channel_mask_t channelMask = AUDIO_CHANNEL_NONE; |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6484 | |
| 6485 | // For direct outputs, pick minimum channel count: this helps ensuring that the |
| 6486 | // channel count / sampling rate combination chosen will be supported by the connected |
| 6487 | // sink |
| 6488 | if ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) && |
| 6489 | (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD))) { |
| 6490 | uint32_t channelCount = UINT_MAX; |
| 6491 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6492 | uint32_t cnlCount; |
| 6493 | if (mUseInChannelMask) { |
| 6494 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); |
| 6495 | } else { |
| 6496 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); |
| 6497 | } |
| 6498 | if ((cnlCount < channelCount) && (cnlCount > 0)) { |
| 6499 | channelMask = mChannelMasks[i]; |
| 6500 | channelCount = cnlCount; |
| 6501 | } |
| 6502 | } |
| 6503 | return channelMask; |
| 6504 | } |
| 6505 | |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6506 | uint32_t channelCount = 0; |
| 6507 | uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; |
| 6508 | |
| 6509 | // For mixed output and inputs, use max mixer channel count. Do not |
| 6510 | // limit channel count otherwise |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6511 | if (mType != AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6512 | maxCount = UINT_MAX; |
| 6513 | } |
| 6514 | for (size_t i = 0; i < mChannelMasks.size(); i ++) { |
| 6515 | uint32_t cnlCount; |
| 6516 | if (mUseInChannelMask) { |
| 6517 | cnlCount = audio_channel_count_from_in_mask(mChannelMasks[i]); |
| 6518 | } else { |
| 6519 | cnlCount = audio_channel_count_from_out_mask(mChannelMasks[i]); |
| 6520 | } |
| 6521 | if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { |
| 6522 | channelMask = mChannelMasks[i]; |
Eric Laurent | 828bcff | 2014-09-07 12:26:06 -0700 | [diff] [blame] | 6523 | channelCount = cnlCount; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6524 | } |
| 6525 | } |
| 6526 | return channelMask; |
| 6527 | } |
| 6528 | |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6529 | /* format in order of increasing preference */ |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6530 | const audio_format_t AudioPolicyManager::AudioPort::sPcmFormatCompareTable[] = { |
| 6531 | AUDIO_FORMAT_DEFAULT, |
| 6532 | AUDIO_FORMAT_PCM_16_BIT, |
Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6533 | AUDIO_FORMAT_PCM_8_24_BIT, |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6534 | AUDIO_FORMAT_PCM_24_BIT_PACKED, |
Eric Laurent | a204994 | 2014-07-21 17:49:25 -0700 | [diff] [blame] | 6535 | AUDIO_FORMAT_PCM_32_BIT, |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6536 | AUDIO_FORMAT_PCM_FLOAT, |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6537 | }; |
| 6538 | |
| 6539 | int AudioPolicyManager::AudioPort::compareFormats(audio_format_t format1, |
| 6540 | audio_format_t format2) |
| 6541 | { |
| 6542 | // NOTE: AUDIO_FORMAT_INVALID is also considered not PCM and will be compared equal to any |
| 6543 | // compressed format and better than any PCM format. This is by design of pickFormat() |
| 6544 | if (!audio_is_linear_pcm(format1)) { |
| 6545 | if (!audio_is_linear_pcm(format2)) { |
| 6546 | return 0; |
| 6547 | } |
| 6548 | return 1; |
| 6549 | } |
| 6550 | if (!audio_is_linear_pcm(format2)) { |
| 6551 | return -1; |
| 6552 | } |
| 6553 | |
| 6554 | int index1 = -1, index2 = -1; |
| 6555 | for (size_t i = 0; |
| 6556 | (i < ARRAY_SIZE(sPcmFormatCompareTable)) && ((index1 == -1) || (index2 == -1)); |
| 6557 | i ++) { |
| 6558 | if (sPcmFormatCompareTable[i] == format1) { |
| 6559 | index1 = i; |
| 6560 | } |
| 6561 | if (sPcmFormatCompareTable[i] == format2) { |
| 6562 | index2 = i; |
| 6563 | } |
| 6564 | } |
| 6565 | // format1 not found => index1 < 0 => format2 > format1 |
| 6566 | // format2 not found => index2 < 0 => format2 < format1 |
| 6567 | return index1 - index2; |
| 6568 | } |
| 6569 | |
| 6570 | audio_format_t AudioPolicyManager::AudioPort::pickFormat() const |
| 6571 | { |
| 6572 | // special case for uninitialized dynamic profile |
| 6573 | if (mFormats.size() == 1 && mFormats[0] == 0) { |
| 6574 | return AUDIO_FORMAT_DEFAULT; |
| 6575 | } |
| 6576 | |
| 6577 | audio_format_t format = AUDIO_FORMAT_DEFAULT; |
Andy Hung | 9a60538 | 2014-07-28 16:16:31 -0700 | [diff] [blame] | 6578 | audio_format_t bestFormat = |
| 6579 | AudioPolicyManager::AudioPort::sPcmFormatCompareTable[ |
| 6580 | ARRAY_SIZE(AudioPolicyManager::AudioPort::sPcmFormatCompareTable) - 1]; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6581 | // For mixed output and inputs, use best mixer output format. Do not |
| 6582 | // limit format otherwise |
| 6583 | if ((mType != AUDIO_PORT_TYPE_MIX) || |
| 6584 | ((mRole == AUDIO_PORT_ROLE_SOURCE) && |
Eric Laurent | d862237 | 2014-07-27 13:47:31 -0700 | [diff] [blame] | 6585 | (((mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) != 0)))) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6586 | bestFormat = AUDIO_FORMAT_INVALID; |
| 6587 | } |
| 6588 | |
| 6589 | for (size_t i = 0; i < mFormats.size(); i ++) { |
| 6590 | if ((compareFormats(mFormats[i], format) > 0) && |
| 6591 | (compareFormats(mFormats[i], bestFormat) <= 0)) { |
| 6592 | format = mFormats[i]; |
| 6593 | } |
| 6594 | } |
| 6595 | return format; |
| 6596 | } |
| 6597 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6598 | status_t AudioPolicyManager::AudioPort::checkGain(const struct audio_gain_config *gainConfig, |
| 6599 | int index) const |
| 6600 | { |
| 6601 | if (index < 0 || (size_t)index >= mGains.size()) { |
| 6602 | return BAD_VALUE; |
| 6603 | } |
| 6604 | return mGains[index]->checkConfig(gainConfig); |
| 6605 | } |
| 6606 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6607 | void AudioPolicyManager::AudioPort::dump(int fd, int spaces) const |
| 6608 | { |
| 6609 | const size_t SIZE = 256; |
| 6610 | char buffer[SIZE]; |
| 6611 | String8 result; |
| 6612 | |
| 6613 | if (mName.size() != 0) { |
| 6614 | snprintf(buffer, SIZE, "%*s- name: %s\n", spaces, "", mName.string()); |
| 6615 | result.append(buffer); |
| 6616 | } |
| 6617 | |
| 6618 | if (mSamplingRates.size() != 0) { |
| 6619 | snprintf(buffer, SIZE, "%*s- sampling rates: ", spaces, ""); |
| 6620 | result.append(buffer); |
| 6621 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6622 | if (i == 0 && mSamplingRates[i] == 0) { |
| 6623 | snprintf(buffer, SIZE, "Dynamic"); |
| 6624 | } else { |
| 6625 | snprintf(buffer, SIZE, "%d", mSamplingRates[i]); |
| 6626 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6627 | result.append(buffer); |
| 6628 | result.append(i == (mSamplingRates.size() - 1) ? "" : ", "); |
| 6629 | } |
| 6630 | result.append("\n"); |
| 6631 | } |
| 6632 | |
| 6633 | if (mChannelMasks.size() != 0) { |
| 6634 | snprintf(buffer, SIZE, "%*s- channel masks: ", spaces, ""); |
| 6635 | result.append(buffer); |
| 6636 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6637 | ALOGV("AudioPort::dump mChannelMasks %zu %08x", i, mChannelMasks[i]); |
| 6638 | |
| 6639 | if (i == 0 && mChannelMasks[i] == 0) { |
| 6640 | snprintf(buffer, SIZE, "Dynamic"); |
| 6641 | } else { |
| 6642 | snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]); |
| 6643 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6644 | result.append(buffer); |
| 6645 | result.append(i == (mChannelMasks.size() - 1) ? "" : ", "); |
| 6646 | } |
| 6647 | result.append("\n"); |
| 6648 | } |
| 6649 | |
| 6650 | if (mFormats.size() != 0) { |
| 6651 | snprintf(buffer, SIZE, "%*s- formats: ", spaces, ""); |
| 6652 | result.append(buffer); |
| 6653 | for (size_t i = 0; i < mFormats.size(); i++) { |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6654 | const char *formatStr = enumToString(sFormatNameToEnumTable, |
| 6655 | ARRAY_SIZE(sFormatNameToEnumTable), |
| 6656 | mFormats[i]); |
| 6657 | if (i == 0 && strcmp(formatStr, "") == 0) { |
| 6658 | snprintf(buffer, SIZE, "Dynamic"); |
| 6659 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6660 | snprintf(buffer, SIZE, "%s", formatStr); |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6661 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6662 | result.append(buffer); |
| 6663 | result.append(i == (mFormats.size() - 1) ? "" : ", "); |
| 6664 | } |
| 6665 | result.append("\n"); |
| 6666 | } |
| 6667 | write(fd, result.string(), result.size()); |
| 6668 | if (mGains.size() != 0) { |
| 6669 | snprintf(buffer, SIZE, "%*s- gains:\n", spaces, ""); |
| 6670 | write(fd, buffer, strlen(buffer) + 1); |
| 6671 | result.append(buffer); |
| 6672 | for (size_t i = 0; i < mGains.size(); i++) { |
| 6673 | mGains[i]->dump(fd, spaces + 2, i); |
| 6674 | } |
| 6675 | } |
| 6676 | } |
| 6677 | |
| 6678 | // --- AudioGain class implementation |
| 6679 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6680 | AudioPolicyManager::AudioGain::AudioGain(int index, bool useInChannelMask) |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6681 | { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6682 | mIndex = index; |
| 6683 | mUseInChannelMask = useInChannelMask; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6684 | memset(&mGain, 0, sizeof(struct audio_gain)); |
| 6685 | } |
| 6686 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6687 | void AudioPolicyManager::AudioGain::getDefaultConfig(struct audio_gain_config *config) |
| 6688 | { |
| 6689 | config->index = mIndex; |
| 6690 | config->mode = mGain.mode; |
| 6691 | config->channel_mask = mGain.channel_mask; |
| 6692 | if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 6693 | config->values[0] = mGain.default_value; |
| 6694 | } else { |
| 6695 | uint32_t numValues; |
| 6696 | if (mUseInChannelMask) { |
| 6697 | numValues = audio_channel_count_from_in_mask(mGain.channel_mask); |
| 6698 | } else { |
| 6699 | numValues = audio_channel_count_from_out_mask(mGain.channel_mask); |
| 6700 | } |
| 6701 | for (size_t i = 0; i < numValues; i++) { |
| 6702 | config->values[i] = mGain.default_value; |
| 6703 | } |
| 6704 | } |
| 6705 | if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 6706 | config->ramp_duration_ms = mGain.min_ramp_ms; |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | status_t AudioPolicyManager::AudioGain::checkConfig(const struct audio_gain_config *config) |
| 6711 | { |
| 6712 | if ((config->mode & ~mGain.mode) != 0) { |
| 6713 | return BAD_VALUE; |
| 6714 | } |
| 6715 | if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { |
| 6716 | if ((config->values[0] < mGain.min_value) || |
| 6717 | (config->values[0] > mGain.max_value)) { |
| 6718 | return BAD_VALUE; |
| 6719 | } |
| 6720 | } else { |
| 6721 | if ((config->channel_mask & ~mGain.channel_mask) != 0) { |
| 6722 | return BAD_VALUE; |
| 6723 | } |
| 6724 | uint32_t numValues; |
| 6725 | if (mUseInChannelMask) { |
| 6726 | numValues = audio_channel_count_from_in_mask(config->channel_mask); |
| 6727 | } else { |
| 6728 | numValues = audio_channel_count_from_out_mask(config->channel_mask); |
| 6729 | } |
| 6730 | for (size_t i = 0; i < numValues; i++) { |
| 6731 | if ((config->values[i] < mGain.min_value) || |
| 6732 | (config->values[i] > mGain.max_value)) { |
| 6733 | return BAD_VALUE; |
| 6734 | } |
| 6735 | } |
| 6736 | } |
| 6737 | if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { |
| 6738 | if ((config->ramp_duration_ms < mGain.min_ramp_ms) || |
| 6739 | (config->ramp_duration_ms > mGain.max_ramp_ms)) { |
| 6740 | return BAD_VALUE; |
| 6741 | } |
| 6742 | } |
| 6743 | return NO_ERROR; |
| 6744 | } |
| 6745 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6746 | void AudioPolicyManager::AudioGain::dump(int fd, int spaces, int index) const |
| 6747 | { |
| 6748 | const size_t SIZE = 256; |
| 6749 | char buffer[SIZE]; |
| 6750 | String8 result; |
| 6751 | |
| 6752 | snprintf(buffer, SIZE, "%*sGain %d:\n", spaces, "", index+1); |
| 6753 | result.append(buffer); |
| 6754 | snprintf(buffer, SIZE, "%*s- mode: %08x\n", spaces, "", mGain.mode); |
| 6755 | result.append(buffer); |
| 6756 | snprintf(buffer, SIZE, "%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask); |
| 6757 | result.append(buffer); |
| 6758 | snprintf(buffer, SIZE, "%*s- min_value: %d mB\n", spaces, "", mGain.min_value); |
| 6759 | result.append(buffer); |
| 6760 | snprintf(buffer, SIZE, "%*s- max_value: %d mB\n", spaces, "", mGain.max_value); |
| 6761 | result.append(buffer); |
| 6762 | snprintf(buffer, SIZE, "%*s- default_value: %d mB\n", spaces, "", mGain.default_value); |
| 6763 | result.append(buffer); |
| 6764 | snprintf(buffer, SIZE, "%*s- step_value: %d mB\n", spaces, "", mGain.step_value); |
| 6765 | result.append(buffer); |
| 6766 | snprintf(buffer, SIZE, "%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms); |
| 6767 | result.append(buffer); |
| 6768 | snprintf(buffer, SIZE, "%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms); |
| 6769 | result.append(buffer); |
| 6770 | |
| 6771 | write(fd, result.string(), result.size()); |
| 6772 | } |
| 6773 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6774 | // --- AudioPortConfig class implementation |
| 6775 | |
| 6776 | AudioPolicyManager::AudioPortConfig::AudioPortConfig() |
| 6777 | { |
| 6778 | mSamplingRate = 0; |
| 6779 | mChannelMask = AUDIO_CHANNEL_NONE; |
| 6780 | mFormat = AUDIO_FORMAT_INVALID; |
| 6781 | mGain.index = -1; |
| 6782 | } |
| 6783 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6784 | status_t AudioPolicyManager::AudioPortConfig::applyAudioPortConfig( |
| 6785 | const struct audio_port_config *config, |
| 6786 | struct audio_port_config *backupConfig) |
| 6787 | { |
| 6788 | struct audio_port_config localBackupConfig; |
| 6789 | status_t status = NO_ERROR; |
| 6790 | |
| 6791 | localBackupConfig.config_mask = config->config_mask; |
| 6792 | toAudioPortConfig(&localBackupConfig); |
| 6793 | |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6794 | sp<AudioPort> audioport = getAudioPort(); |
| 6795 | if (audioport == 0) { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6796 | status = NO_INIT; |
| 6797 | goto exit; |
| 6798 | } |
| 6799 | if (config->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6800 | status = audioport->checkExactSamplingRate(config->sample_rate); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6801 | if (status != NO_ERROR) { |
| 6802 | goto exit; |
| 6803 | } |
| 6804 | mSamplingRate = config->sample_rate; |
| 6805 | } |
| 6806 | if (config->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6807 | status = audioport->checkExactChannelMask(config->channel_mask); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6808 | if (status != NO_ERROR) { |
| 6809 | goto exit; |
| 6810 | } |
| 6811 | mChannelMask = config->channel_mask; |
| 6812 | } |
| 6813 | if (config->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6814 | status = audioport->checkFormat(config->format); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6815 | if (status != NO_ERROR) { |
| 6816 | goto exit; |
| 6817 | } |
| 6818 | mFormat = config->format; |
| 6819 | } |
| 6820 | if (config->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 6821 | status = audioport->checkGain(&config->gain, config->gain.index); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 6822 | if (status != NO_ERROR) { |
| 6823 | goto exit; |
| 6824 | } |
| 6825 | mGain = config->gain; |
| 6826 | } |
| 6827 | |
| 6828 | exit: |
| 6829 | if (status != NO_ERROR) { |
| 6830 | applyAudioPortConfig(&localBackupConfig); |
| 6831 | } |
| 6832 | if (backupConfig != NULL) { |
| 6833 | *backupConfig = localBackupConfig; |
| 6834 | } |
| 6835 | return status; |
| 6836 | } |
| 6837 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6838 | void AudioPolicyManager::AudioPortConfig::toAudioPortConfig( |
| 6839 | struct audio_port_config *dstConfig, |
| 6840 | const struct audio_port_config *srcConfig) const |
| 6841 | { |
| 6842 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 6843 | dstConfig->sample_rate = mSamplingRate; |
| 6844 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE)) { |
| 6845 | dstConfig->sample_rate = srcConfig->sample_rate; |
| 6846 | } |
| 6847 | } else { |
| 6848 | dstConfig->sample_rate = 0; |
| 6849 | } |
| 6850 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 6851 | dstConfig->channel_mask = mChannelMask; |
| 6852 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK)) { |
| 6853 | dstConfig->channel_mask = srcConfig->channel_mask; |
| 6854 | } |
| 6855 | } else { |
| 6856 | dstConfig->channel_mask = AUDIO_CHANNEL_NONE; |
| 6857 | } |
| 6858 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 6859 | dstConfig->format = mFormat; |
| 6860 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_FORMAT)) { |
| 6861 | dstConfig->format = srcConfig->format; |
| 6862 | } |
| 6863 | } else { |
| 6864 | dstConfig->format = AUDIO_FORMAT_INVALID; |
| 6865 | } |
| 6866 | if (dstConfig->config_mask & AUDIO_PORT_CONFIG_GAIN) { |
| 6867 | dstConfig->gain = mGain; |
| 6868 | if ((srcConfig != NULL) && (srcConfig->config_mask & AUDIO_PORT_CONFIG_GAIN)) { |
| 6869 | dstConfig->gain = srcConfig->gain; |
| 6870 | } |
| 6871 | } else { |
| 6872 | dstConfig->gain.index = -1; |
| 6873 | } |
| 6874 | if (dstConfig->gain.index != -1) { |
| 6875 | dstConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN; |
| 6876 | } else { |
| 6877 | dstConfig->config_mask &= ~AUDIO_PORT_CONFIG_GAIN; |
| 6878 | } |
| 6879 | } |
| 6880 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6881 | // --- IOProfile class implementation |
| 6882 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6883 | AudioPolicyManager::IOProfile::IOProfile(const String8& name, audio_port_role_t role, |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6884 | const sp<HwModule>& module) |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 6885 | : AudioPort(name, AUDIO_PORT_TYPE_MIX, role, module) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6886 | { |
| 6887 | } |
| 6888 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6889 | AudioPolicyManager::IOProfile::~IOProfile() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6890 | { |
| 6891 | } |
| 6892 | |
| 6893 | // checks if the IO profile is compatible with specified parameters. |
| 6894 | // Sampling rate, format and channel mask must be specified in order to |
| 6895 | // get a valid a match |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6896 | bool AudioPolicyManager::IOProfile::isCompatibleProfile(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6897 | uint32_t samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6898 | uint32_t *updatedSamplingRate, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6899 | audio_format_t format, |
| 6900 | audio_channel_mask_t channelMask, |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 6901 | uint32_t flags) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6902 | { |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6903 | const bool isPlaybackThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SOURCE; |
| 6904 | const bool isRecordThread = mType == AUDIO_PORT_TYPE_MIX && mRole == AUDIO_PORT_ROLE_SINK; |
| 6905 | ALOG_ASSERT(isPlaybackThread != isRecordThread); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6906 | |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6907 | if ((mSupportedDevices.types() & device) != device) { |
| 6908 | return false; |
| 6909 | } |
| 6910 | |
| 6911 | if (samplingRate == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6912 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6913 | } |
| 6914 | uint32_t myUpdatedSamplingRate = samplingRate; |
| 6915 | if (isPlaybackThread && checkExactSamplingRate(samplingRate) != NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6916 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6917 | } |
| 6918 | if (isRecordThread && checkCompatibleSamplingRate(samplingRate, &myUpdatedSamplingRate) != |
| 6919 | NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6920 | return false; |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | if (!audio_is_valid_format(format) || checkFormat(format) != NO_ERROR) { |
| 6924 | return false; |
| 6925 | } |
| 6926 | |
| 6927 | if (isPlaybackThread && (!audio_is_output_channel(channelMask) || |
| 6928 | checkExactChannelMask(channelMask) != NO_ERROR)) { |
| 6929 | return false; |
| 6930 | } |
| 6931 | if (isRecordThread && (!audio_is_input_channel(channelMask) || |
| 6932 | checkCompatibleChannelMask(channelMask) != NO_ERROR)) { |
| 6933 | return false; |
| 6934 | } |
| 6935 | |
| 6936 | if (isPlaybackThread && (mFlags & flags) != flags) { |
| 6937 | return false; |
| 6938 | } |
| 6939 | // The only input flag that is allowed to be different is the fast flag. |
| 6940 | // An existing fast stream is compatible with a normal track request. |
| 6941 | // An existing normal stream is compatible with a fast track request, |
| 6942 | // 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] | 6943 | if (isRecordThread && ((mFlags ^ flags) & |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 6944 | ~AUDIO_INPUT_FLAG_FAST)) { |
| 6945 | return false; |
| 6946 | } |
| 6947 | |
| 6948 | if (updatedSamplingRate != NULL) { |
| 6949 | *updatedSamplingRate = myUpdatedSamplingRate; |
| 6950 | } |
| 6951 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6952 | } |
| 6953 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6954 | void AudioPolicyManager::IOProfile::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6955 | { |
| 6956 | const size_t SIZE = 256; |
| 6957 | char buffer[SIZE]; |
| 6958 | String8 result; |
| 6959 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6960 | AudioPort::dump(fd, 4); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6961 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6962 | snprintf(buffer, SIZE, " - flags: 0x%04x\n", mFlags); |
| 6963 | result.append(buffer); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6964 | snprintf(buffer, SIZE, " - devices:\n"); |
| 6965 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6966 | write(fd, result.string(), result.size()); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 6967 | for (size_t i = 0; i < mSupportedDevices.size(); i++) { |
| 6968 | mSupportedDevices[i]->dump(fd, 6, i); |
| 6969 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6970 | } |
| 6971 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6972 | void AudioPolicyManager::IOProfile::log() |
| 6973 | { |
| 6974 | const size_t SIZE = 256; |
| 6975 | char buffer[SIZE]; |
| 6976 | String8 result; |
| 6977 | |
| 6978 | ALOGV(" - sampling rates: "); |
| 6979 | for (size_t i = 0; i < mSamplingRates.size(); i++) { |
| 6980 | ALOGV(" %d", mSamplingRates[i]); |
| 6981 | } |
| 6982 | |
| 6983 | ALOGV(" - channel masks: "); |
| 6984 | for (size_t i = 0; i < mChannelMasks.size(); i++) { |
| 6985 | ALOGV(" 0x%04x", mChannelMasks[i]); |
| 6986 | } |
| 6987 | |
| 6988 | ALOGV(" - formats: "); |
| 6989 | for (size_t i = 0; i < mFormats.size(); i++) { |
| 6990 | ALOGV(" 0x%08x", mFormats[i]); |
| 6991 | } |
| 6992 | |
| 6993 | ALOGV(" - devices: 0x%04x\n", mSupportedDevices.types()); |
| 6994 | ALOGV(" - flags: 0x%04x\n", mFlags); |
| 6995 | } |
| 6996 | |
| 6997 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6998 | // --- DeviceDescriptor implementation |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6999 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7000 | |
| 7001 | AudioPolicyManager::DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) : |
| 7002 | AudioPort(name, AUDIO_PORT_TYPE_DEVICE, |
| 7003 | audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK : |
| 7004 | AUDIO_PORT_ROLE_SOURCE, |
| 7005 | NULL), |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 7006 | mDeviceType(type), mAddress(""), mId(0) |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7007 | { |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 7008 | if (mGains.size() > 0) { |
| 7009 | mGains[0]->getDefaultConfig(&mGain); |
| 7010 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7011 | } |
| 7012 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7013 | bool AudioPolicyManager::DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7014 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7015 | // Devices are considered equal if they: |
| 7016 | // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE) |
| 7017 | // - have the same address or one device does not specify the address |
| 7018 | // - 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] | 7019 | return (mDeviceType == other->mDeviceType) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7020 | (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) && |
Eric Laurent | 2f8a36f | 2014-03-26 19:05:55 -0700 | [diff] [blame] | 7021 | (mChannelMask == 0 || other->mChannelMask == 0 || |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7022 | mChannelMask == other->mChannelMask); |
| 7023 | } |
| 7024 | |
| 7025 | void AudioPolicyManager::DeviceVector::refreshTypes() |
| 7026 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7027 | mDeviceTypes = AUDIO_DEVICE_NONE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7028 | for(size_t i = 0; i < size(); i++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7029 | mDeviceTypes |= itemAt(i)->mDeviceType; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7030 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7031 | ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7032 | } |
| 7033 | |
| 7034 | ssize_t AudioPolicyManager::DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const |
| 7035 | { |
| 7036 | for(size_t i = 0; i < size(); i++) { |
| 7037 | if (item->equals(itemAt(i))) { |
| 7038 | return i; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7039 | } |
| 7040 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7041 | return -1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7042 | } |
| 7043 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7044 | ssize_t AudioPolicyManager::DeviceVector::add(const sp<DeviceDescriptor>& item) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7045 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7046 | ssize_t ret = indexOf(item); |
| 7047 | |
| 7048 | if (ret < 0) { |
| 7049 | ret = SortedVector::add(item); |
| 7050 | if (ret >= 0) { |
| 7051 | refreshTypes(); |
| 7052 | } |
| 7053 | } else { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7054 | ALOGW("DeviceVector::add device %08x already in", item->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7055 | ret = -1; |
| 7056 | } |
| 7057 | return ret; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7058 | } |
| 7059 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7060 | ssize_t AudioPolicyManager::DeviceVector::remove(const sp<DeviceDescriptor>& item) |
| 7061 | { |
| 7062 | size_t i; |
| 7063 | ssize_t ret = indexOf(item); |
| 7064 | |
| 7065 | if (ret < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7066 | ALOGW("DeviceVector::remove device %08x not in", item->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7067 | } else { |
| 7068 | ret = SortedVector::removeAt(ret); |
| 7069 | if (ret >= 0) { |
| 7070 | refreshTypes(); |
| 7071 | } |
| 7072 | } |
| 7073 | return ret; |
| 7074 | } |
| 7075 | |
| 7076 | void AudioPolicyManager::DeviceVector::loadDevicesFromType(audio_devices_t types) |
| 7077 | { |
| 7078 | DeviceVector deviceList; |
| 7079 | |
| 7080 | uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types; |
| 7081 | types &= ~role_bit; |
| 7082 | |
| 7083 | while (types) { |
| 7084 | uint32_t i = 31 - __builtin_clz(types); |
| 7085 | uint32_t type = 1 << i; |
| 7086 | types &= ~type; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7087 | add(new DeviceDescriptor(String8(""), type | role_bit)); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7088 | } |
| 7089 | } |
| 7090 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7091 | void AudioPolicyManager::DeviceVector::loadDevicesFromName(char *name, |
| 7092 | const DeviceVector& declaredDevices) |
| 7093 | { |
| 7094 | char *devName = strtok(name, "|"); |
| 7095 | while (devName != NULL) { |
| 7096 | if (strlen(devName) != 0) { |
| 7097 | audio_devices_t type = stringToEnum(sDeviceNameToEnumTable, |
| 7098 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7099 | devName); |
| 7100 | if (type != AUDIO_DEVICE_NONE) { |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 7101 | sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(""), type); |
| 7102 | if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
| 7103 | dev->mAddress = String8("0"); |
| 7104 | } |
| 7105 | add(dev); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7106 | } else { |
| 7107 | sp<DeviceDescriptor> deviceDesc = |
| 7108 | declaredDevices.getDeviceFromName(String8(devName)); |
| 7109 | if (deviceDesc != 0) { |
| 7110 | add(deviceDesc); |
| 7111 | } |
| 7112 | } |
| 7113 | } |
| 7114 | devName = strtok(NULL, "|"); |
| 7115 | } |
| 7116 | } |
| 7117 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7118 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDevice( |
| 7119 | audio_devices_t type, String8 address) const |
| 7120 | { |
| 7121 | sp<DeviceDescriptor> device; |
| 7122 | for (size_t i = 0; i < size(); i++) { |
| 7123 | if (itemAt(i)->mDeviceType == type) { |
| 7124 | device = itemAt(i); |
| 7125 | if (itemAt(i)->mAddress = address) { |
| 7126 | break; |
| 7127 | } |
| 7128 | } |
| 7129 | } |
| 7130 | ALOGV("DeviceVector::getDevice() for type %d address %s found %p", |
| 7131 | type, address.string(), device.get()); |
| 7132 | return device; |
| 7133 | } |
| 7134 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7135 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromId( |
| 7136 | audio_port_handle_t id) const |
| 7137 | { |
| 7138 | sp<DeviceDescriptor> device; |
| 7139 | for (size_t i = 0; i < size(); i++) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 7140 | 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] | 7141 | if (itemAt(i)->mId == id) { |
| 7142 | device = itemAt(i); |
| 7143 | break; |
| 7144 | } |
| 7145 | } |
| 7146 | return device; |
| 7147 | } |
| 7148 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7149 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromType( |
| 7150 | audio_devices_t type) const |
| 7151 | { |
| 7152 | DeviceVector devices; |
| 7153 | for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) { |
| 7154 | if (itemAt(i)->mDeviceType & type & ~AUDIO_DEVICE_BIT_IN) { |
| 7155 | devices.add(itemAt(i)); |
| 7156 | type &= ~itemAt(i)->mDeviceType; |
| 7157 | ALOGV("DeviceVector::getDevicesFromType() for type %x found %p", |
| 7158 | itemAt(i)->mDeviceType, itemAt(i).get()); |
| 7159 | } |
| 7160 | } |
| 7161 | return devices; |
| 7162 | } |
| 7163 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 7164 | AudioPolicyManager::DeviceVector AudioPolicyManager::DeviceVector::getDevicesFromTypeAddr( |
| 7165 | audio_devices_t type, String8 address) const |
| 7166 | { |
| 7167 | DeviceVector devices; |
| 7168 | //ALOGV(" looking for device=%x, addr=%s", type, address.string()); |
| 7169 | for (size_t i = 0; i < size(); i++) { |
| 7170 | //ALOGV(" at i=%d: device=%x, addr=%s", |
| 7171 | // i, itemAt(i)->mDeviceType, itemAt(i)->mAddress.string()); |
| 7172 | if (itemAt(i)->mDeviceType == type) { |
| 7173 | if (itemAt(i)->mAddress == address) { |
| 7174 | //ALOGV(" found matching address %s", address.string()); |
| 7175 | devices.add(itemAt(i)); |
| 7176 | } |
| 7177 | } |
| 7178 | } |
| 7179 | return devices; |
| 7180 | } |
| 7181 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7182 | sp<AudioPolicyManager::DeviceDescriptor> AudioPolicyManager::DeviceVector::getDeviceFromName( |
| 7183 | const String8& name) const |
| 7184 | { |
| 7185 | sp<DeviceDescriptor> device; |
| 7186 | for (size_t i = 0; i < size(); i++) { |
| 7187 | if (itemAt(i)->mName == name) { |
| 7188 | device = itemAt(i); |
| 7189 | break; |
| 7190 | } |
| 7191 | } |
| 7192 | return device; |
| 7193 | } |
| 7194 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7195 | void AudioPolicyManager::DeviceDescriptor::toAudioPortConfig( |
| 7196 | struct audio_port_config *dstConfig, |
| 7197 | const struct audio_port_config *srcConfig) const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7198 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7199 | dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN; |
| 7200 | if (srcConfig != NULL) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 7201 | dstConfig->config_mask |= srcConfig->config_mask; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7202 | } |
| 7203 | |
| 7204 | AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig); |
| 7205 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7206 | dstConfig->id = mId; |
| 7207 | dstConfig->role = audio_is_output_device(mDeviceType) ? |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7208 | AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7209 | dstConfig->type = AUDIO_PORT_TYPE_DEVICE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7210 | dstConfig->ext.device.type = mDeviceType; |
| 7211 | dstConfig->ext.device.hw_module = mModule->mHandle; |
| 7212 | strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | void AudioPolicyManager::DeviceDescriptor::toAudioPort(struct audio_port *port) const |
| 7216 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7217 | ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7218 | AudioPort::toAudioPort(port); |
| 7219 | port->id = mId; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7220 | toAudioPortConfig(&port->active_config); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7221 | port->ext.device.type = mDeviceType; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7222 | port->ext.device.hw_module = mModule->mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7223 | strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN); |
| 7224 | } |
| 7225 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7226 | status_t AudioPolicyManager::DeviceDescriptor::dump(int fd, int spaces, int index) const |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7227 | { |
| 7228 | const size_t SIZE = 256; |
| 7229 | char buffer[SIZE]; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7230 | String8 result; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7231 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7232 | snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1); |
| 7233 | result.append(buffer); |
| 7234 | if (mId != 0) { |
| 7235 | snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId); |
| 7236 | result.append(buffer); |
| 7237 | } |
| 7238 | snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", |
| 7239 | enumToString(sDeviceNameToEnumTable, |
| 7240 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7241 | mDeviceType)); |
| 7242 | result.append(buffer); |
| 7243 | if (mAddress.size() != 0) { |
| 7244 | snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string()); |
| 7245 | result.append(buffer); |
| 7246 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7247 | write(fd, result.string(), result.size()); |
| 7248 | AudioPort::dump(fd, spaces); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7249 | |
| 7250 | return NO_ERROR; |
| 7251 | } |
| 7252 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 7253 | status_t AudioPolicyManager::AudioPatch::dump(int fd, int spaces, int index) const |
| 7254 | { |
| 7255 | const size_t SIZE = 256; |
| 7256 | char buffer[SIZE]; |
| 7257 | String8 result; |
| 7258 | |
| 7259 | |
| 7260 | snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1); |
| 7261 | result.append(buffer); |
| 7262 | snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle); |
| 7263 | result.append(buffer); |
| 7264 | snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle); |
| 7265 | result.append(buffer); |
| 7266 | snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid); |
| 7267 | result.append(buffer); |
| 7268 | snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources); |
| 7269 | result.append(buffer); |
| 7270 | for (size_t i = 0; i < mPatch.num_sources; i++) { |
| 7271 | if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) { |
| 7272 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", |
| 7273 | mPatch.sources[i].id, enumToString(sDeviceNameToEnumTable, |
| 7274 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7275 | mPatch.sources[i].ext.device.type)); |
| 7276 | } else { |
| 7277 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", |
| 7278 | mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle); |
| 7279 | } |
| 7280 | result.append(buffer); |
| 7281 | } |
| 7282 | snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks); |
| 7283 | result.append(buffer); |
| 7284 | for (size_t i = 0; i < mPatch.num_sinks; i++) { |
| 7285 | if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) { |
| 7286 | snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "", |
| 7287 | mPatch.sinks[i].id, enumToString(sDeviceNameToEnumTable, |
| 7288 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7289 | mPatch.sinks[i].ext.device.type)); |
| 7290 | } else { |
| 7291 | snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "", |
| 7292 | mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle); |
| 7293 | } |
| 7294 | result.append(buffer); |
| 7295 | } |
| 7296 | |
| 7297 | write(fd, result.string(), result.size()); |
| 7298 | return NO_ERROR; |
| 7299 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7300 | |
| 7301 | // --- audio_policy.conf file parsing |
| 7302 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7303 | uint32_t AudioPolicyManager::parseOutputFlagNames(char *name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7304 | { |
| 7305 | uint32_t flag = 0; |
| 7306 | |
| 7307 | // it is OK to cast name to non const here as we are not going to use it after |
| 7308 | // strtok() modifies it |
| 7309 | char *flagName = strtok(name, "|"); |
| 7310 | while (flagName != NULL) { |
| 7311 | if (strlen(flagName) != 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7312 | flag |= stringToEnum(sOutputFlagNameToEnumTable, |
| 7313 | ARRAY_SIZE(sOutputFlagNameToEnumTable), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7314 | flagName); |
| 7315 | } |
| 7316 | flagName = strtok(NULL, "|"); |
| 7317 | } |
| 7318 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 7319 | // and all common behaviors are driven by checking only the direct flag |
| 7320 | // this should normally be set appropriately in the policy configuration file |
| 7321 | if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 7322 | flag |= AUDIO_OUTPUT_FLAG_DIRECT; |
| 7323 | } |
| 7324 | |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 7325 | return flag; |
| 7326 | } |
| 7327 | |
| 7328 | uint32_t AudioPolicyManager::parseInputFlagNames(char *name) |
| 7329 | { |
| 7330 | uint32_t flag = 0; |
| 7331 | |
| 7332 | // it is OK to cast name to non const here as we are not going to use it after |
| 7333 | // strtok() modifies it |
| 7334 | char *flagName = strtok(name, "|"); |
| 7335 | while (flagName != NULL) { |
| 7336 | if (strlen(flagName) != 0) { |
| 7337 | flag |= stringToEnum(sInputFlagNameToEnumTable, |
| 7338 | ARRAY_SIZE(sInputFlagNameToEnumTable), |
| 7339 | flagName); |
| 7340 | } |
| 7341 | flagName = strtok(NULL, "|"); |
| 7342 | } |
| 7343 | return flag; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7344 | } |
| 7345 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7346 | audio_devices_t AudioPolicyManager::parseDeviceNames(char *name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7347 | { |
| 7348 | uint32_t device = 0; |
| 7349 | |
| 7350 | char *devName = strtok(name, "|"); |
| 7351 | while (devName != NULL) { |
| 7352 | if (strlen(devName) != 0) { |
| 7353 | device |= stringToEnum(sDeviceNameToEnumTable, |
| 7354 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7355 | devName); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7356 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7357 | devName = strtok(NULL, "|"); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7358 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7359 | return device; |
| 7360 | } |
| 7361 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7362 | void AudioPolicyManager::loadHwModule(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7363 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7364 | status_t status = NAME_NOT_FOUND; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7365 | cnode *node; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7366 | sp<HwModule> module = new HwModule(root->name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7367 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7368 | node = config_find(root, DEVICES_TAG); |
| 7369 | if (node != NULL) { |
| 7370 | node = node->first_child; |
| 7371 | while (node) { |
| 7372 | ALOGV("loadHwModule() loading device %s", node->name); |
| 7373 | status_t tmpStatus = module->loadDevice(node); |
| 7374 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7375 | status = tmpStatus; |
| 7376 | } |
| 7377 | node = node->next; |
| 7378 | } |
| 7379 | } |
| 7380 | node = config_find(root, OUTPUTS_TAG); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7381 | if (node != NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7382 | node = node->first_child; |
| 7383 | while (node) { |
| 7384 | ALOGV("loadHwModule() loading output %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7385 | status_t tmpStatus = module->loadOutput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7386 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7387 | status = tmpStatus; |
| 7388 | } |
| 7389 | node = node->next; |
| 7390 | } |
| 7391 | } |
| 7392 | node = config_find(root, INPUTS_TAG); |
| 7393 | if (node != NULL) { |
| 7394 | node = node->first_child; |
| 7395 | while (node) { |
| 7396 | ALOGV("loadHwModule() loading input %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7397 | status_t tmpStatus = module->loadInput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7398 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 7399 | status = tmpStatus; |
| 7400 | } |
| 7401 | node = node->next; |
| 7402 | } |
| 7403 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7404 | loadGlobalConfig(root, module); |
| 7405 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7406 | if (status == NO_ERROR) { |
| 7407 | mHwModules.add(module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7408 | } |
| 7409 | } |
| 7410 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7411 | void AudioPolicyManager::loadHwModules(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7412 | { |
| 7413 | cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); |
| 7414 | if (node == NULL) { |
| 7415 | return; |
| 7416 | } |
| 7417 | |
| 7418 | node = node->first_child; |
| 7419 | while (node) { |
| 7420 | ALOGV("loadHwModules() loading module %s", node->name); |
| 7421 | loadHwModule(node); |
| 7422 | node = node->next; |
| 7423 | } |
| 7424 | } |
| 7425 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7426 | void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7427 | { |
| 7428 | cnode *node = config_find(root, GLOBAL_CONFIG_TAG); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 7429 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7430 | if (node == NULL) { |
| 7431 | return; |
| 7432 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7433 | DeviceVector declaredDevices; |
| 7434 | if (module != NULL) { |
| 7435 | declaredDevices = module->mDeclaredDevices; |
| 7436 | } |
| 7437 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7438 | node = node->first_child; |
| 7439 | while (node) { |
| 7440 | if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7441 | mAvailableOutputDevices.loadDevicesFromName((char *)node->value, |
| 7442 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7443 | ALOGV("loadGlobalConfig() Attached Output Devices %08x", |
| 7444 | mAvailableOutputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7445 | } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7446 | audio_devices_t device = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7447 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 7448 | (char *)node->value); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7449 | if (device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7450 | mDefaultOutputDevice = new DeviceDescriptor(String8(""), device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7451 | } else { |
| 7452 | ALOGW("loadGlobalConfig() default device not specified"); |
| 7453 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7454 | ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7455 | } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7456 | mAvailableInputDevices.loadDevicesFromName((char *)node->value, |
| 7457 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7458 | ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7459 | } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) { |
| 7460 | mSpeakerDrcEnabled = stringToBool((char *)node->value); |
| 7461 | ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 7462 | } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) { |
| 7463 | uint32_t major, minor; |
| 7464 | sscanf((char *)node->value, "%u.%u", &major, &minor); |
| 7465 | module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor); |
| 7466 | ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u", |
| 7467 | module->mHalVersion, major, minor); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7468 | } |
| 7469 | node = node->next; |
| 7470 | } |
| 7471 | } |
| 7472 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7473 | status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7474 | { |
| 7475 | cnode *root; |
| 7476 | char *data; |
| 7477 | |
| 7478 | data = (char *)load_file(path, NULL); |
| 7479 | if (data == NULL) { |
| 7480 | return -ENODEV; |
| 7481 | } |
| 7482 | root = config_node("", ""); |
| 7483 | config_load(root, data); |
| 7484 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7485 | loadHwModules(root); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7486 | // legacy audio_policy.conf files have one global_configuration section |
| 7487 | loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7488 | config_free(root); |
| 7489 | free(root); |
| 7490 | free(data); |
| 7491 | |
| 7492 | ALOGI("loadAudioPolicyConfig() loaded %s\n", path); |
| 7493 | |
| 7494 | return NO_ERROR; |
| 7495 | } |
| 7496 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7497 | void AudioPolicyManager::defaultAudioPolicyConfig(void) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7498 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7499 | sp<HwModule> module; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7500 | sp<IOProfile> profile; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7501 | sp<DeviceDescriptor> defaultInputDevice = new DeviceDescriptor(String8(""), |
| 7502 | AUDIO_DEVICE_IN_BUILTIN_MIC); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7503 | mAvailableOutputDevices.add(mDefaultOutputDevice); |
| 7504 | mAvailableInputDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7505 | |
| 7506 | module = new HwModule("primary"); |
| 7507 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7508 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7509 | profile->mSamplingRates.add(44100); |
| 7510 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 7511 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7512 | profile->mSupportedDevices.add(mDefaultOutputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7513 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; |
| 7514 | module->mOutputProfiles.add(profile); |
| 7515 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 7516 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7517 | profile->mSamplingRates.add(8000); |
| 7518 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 7519 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 7520 | profile->mSupportedDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7521 | module->mInputProfiles.add(profile); |
| 7522 | |
| 7523 | mHwModules.add(module); |
| 7524 | } |
| 7525 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7526 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 7527 | { |
| 7528 | // flags to stream type mapping |
| 7529 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 7530 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 7531 | } |
| 7532 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 7533 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 7534 | } |
| 7535 | |
| 7536 | // usage to stream type mapping |
| 7537 | switch (attr->usage) { |
| 7538 | case AUDIO_USAGE_MEDIA: |
| 7539 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7540 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7541 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 7542 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7543 | if (isStreamActive(AUDIO_STREAM_ALARM)) { |
| 7544 | return AUDIO_STREAM_ALARM; |
| 7545 | } |
| 7546 | if (isStreamActive(AUDIO_STREAM_RING)) { |
| 7547 | return AUDIO_STREAM_RING; |
| 7548 | } |
| 7549 | if (isInCall()) { |
| 7550 | return AUDIO_STREAM_VOICE_CALL; |
| 7551 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 7552 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 7553 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7554 | return AUDIO_STREAM_SYSTEM; |
| 7555 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7556 | return AUDIO_STREAM_VOICE_CALL; |
| 7557 | |
| 7558 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7559 | return AUDIO_STREAM_DTMF; |
| 7560 | |
| 7561 | case AUDIO_USAGE_ALARM: |
| 7562 | return AUDIO_STREAM_ALARM; |
| 7563 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7564 | return AUDIO_STREAM_RING; |
| 7565 | |
| 7566 | case AUDIO_USAGE_NOTIFICATION: |
| 7567 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7568 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7569 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7570 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7571 | return AUDIO_STREAM_NOTIFICATION; |
| 7572 | |
| 7573 | case AUDIO_USAGE_UNKNOWN: |
| 7574 | default: |
| 7575 | return AUDIO_STREAM_MUSIC; |
| 7576 | } |
| 7577 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7578 | |
| 7579 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) { |
| 7580 | // has flags that map to a strategy? |
| 7581 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7582 | return true; |
| 7583 | } |
| 7584 | |
| 7585 | // has known usage? |
| 7586 | switch (paa->usage) { |
| 7587 | case AUDIO_USAGE_UNKNOWN: |
| 7588 | case AUDIO_USAGE_MEDIA: |
| 7589 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7590 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7591 | case AUDIO_USAGE_ALARM: |
| 7592 | case AUDIO_USAGE_NOTIFICATION: |
| 7593 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7594 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7595 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7596 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7597 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7598 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7599 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7600 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7601 | case AUDIO_USAGE_GAME: |
| 7602 | break; |
| 7603 | default: |
| 7604 | return false; |
| 7605 | } |
| 7606 | return true; |
| 7607 | } |
| 7608 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7609 | }; // namespace android |