Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2014, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger::PatchPanel" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "Configuration.h" |
| 23 | #include <utils/Log.h> |
| 24 | #include <audio_utils/primitives.h> |
| 25 | |
| 26 | #include "AudioFlinger.h" |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 27 | #include <media/AudioParameter.h> |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 28 | #include <media/AudioValidator.h> |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 29 | #include <media/DeviceDescriptorBase.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 30 | #include <media/PatchBuilder.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 31 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | // Note: the following macro is used for extremely verbose logging message. In |
| 36 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 37 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 38 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 39 | // turned on. Do not uncomment the #def below unless you really know what you |
| 40 | // are doing and want to see all of the extremely verbose messages. |
| 41 | //#define VERY_VERY_VERBOSE_LOGGING |
| 42 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 43 | #define ALOGVV ALOGV |
| 44 | #else |
| 45 | #define ALOGVV(a...) do { } while(0) |
| 46 | #endif |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | /* List connected audio ports and their attributes */ |
| 51 | status_t AudioFlinger::listAudioPorts(unsigned int *num_ports, |
| 52 | struct audio_port *ports) |
| 53 | { |
| 54 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 55 | return mPatchPanel.listAudioPorts(num_ports, ports); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Get supported attributes for a given audio port */ |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 59 | status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) { |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 60 | status_t status = AudioValidator::validateAudioPort(*port); |
| 61 | if (status != NO_ERROR) { |
| 62 | return status; |
| 63 | } |
| 64 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 65 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 66 | return mPatchPanel.getAudioPort(port); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 69 | /* Connect a patch between several source and sink ports */ |
| 70 | status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch, |
| 71 | audio_patch_handle_t *handle) |
| 72 | { |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 73 | status_t status = AudioValidator::validateAudioPatch(*patch); |
| 74 | if (status != NO_ERROR) { |
| 75 | return status; |
| 76 | } |
| 77 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 79 | return mPatchPanel.createAudioPatch(patch, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* Disconnect a patch */ |
| 83 | status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle) |
| 84 | { |
| 85 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 86 | return mPatchPanel.releaseAudioPatch(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 89 | /* List connected audio ports and they attributes */ |
| 90 | status_t AudioFlinger::listAudioPatches(unsigned int *num_patches, |
| 91 | struct audio_patch *patches) |
| 92 | { |
| 93 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 94 | return mPatchPanel.listAudioPatches(num_patches, patches); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 97 | status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const |
| 98 | { |
| 99 | const auto& iter = mPatchPanel.mPatches.find(mPatchHandle); |
| 100 | if (iter != mPatchPanel.mPatches.end()) { |
| 101 | return iter->second.getLatencyMs(latencyMs); |
| 102 | } else { |
| 103 | return BAD_VALUE; |
| 104 | } |
| 105 | } |
| 106 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 107 | /* List connected audio ports and their attributes */ |
| 108 | status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused, |
| 109 | struct audio_port *ports __unused) |
| 110 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 111 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 112 | return NO_ERROR; |
| 113 | } |
| 114 | |
| 115 | /* Get supported attributes for a given audio port */ |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 116 | status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 117 | { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 118 | if (port->type != AUDIO_PORT_TYPE_DEVICE) { |
| 119 | // Only query the HAL when the port is a device. |
| 120 | // TODO: implement getAudioPort for mix. |
| 121 | return INVALID_OPERATION; |
| 122 | } |
| 123 | AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module); |
| 124 | if (hwDevice == nullptr) { |
| 125 | ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module); |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | if (!hwDevice->supportsAudioPatches()) { |
| 129 | return INVALID_OPERATION; |
| 130 | } |
| 131 | return hwDevice->getAudioPort(port); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 134 | /* Connect a patch between several source and sink ports */ |
| 135 | status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch, |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 136 | audio_patch_handle_t *handle, |
| 137 | bool endpointPatch) |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 138 | { |
Greg Kaiser | f27ce40 | 2016-03-14 13:43:14 -0700 | [diff] [blame] | 139 | if (handle == NULL || patch == NULL) { |
| 140 | return BAD_VALUE; |
| 141 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 142 | ALOGV("%s() num_sources %d num_sinks %d handle %d", |
| 143 | __func__, patch->num_sources, patch->num_sinks, *handle); |
| 144 | status_t status = NO_ERROR; |
| 145 | audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 146 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 147 | if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 148 | return BAD_VALUE; |
| 149 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 150 | // limit number of sources to 1 for now or 2 sources for special cross hw module case. |
| 151 | // only the audio policy manager can request a patch creation with 2 sources. |
| 152 | if (patch->num_sources > 2) { |
| 153 | return INVALID_OPERATION; |
| 154 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 155 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 156 | if (*handle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 157 | auto iter = mPatches.find(*handle); |
| 158 | if (iter != mPatches.end()) { |
| 159 | ALOGV("%s() removing patch handle %d", __func__, *handle); |
| 160 | Patch &removedPatch = iter->second; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 161 | // free resources owned by the removed patch if applicable |
| 162 | // 1) if a software patch is present, release the playback and capture threads and |
| 163 | // tracks created. This will also release the corresponding audio HAL patches |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 164 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 165 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 166 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 167 | // 2) if the new patch and old patch source or sink are devices from different |
| 168 | // hw modules, clear the audio HAL patches now because they will not be updated |
| 169 | // by call to create_audio_patch() below which will happen on a different HW module |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 170 | if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 171 | audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE; |
| 172 | const struct audio_patch &oldPatch = removedPatch.mAudioPatch; |
| 173 | if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 174 | (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 175 | oldPatch.sources[0].ext.device.hw_module != |
| 176 | patch->sources[0].ext.device.hw_module)) { |
| 177 | hwModule = oldPatch.sources[0].ext.device.hw_module; |
| 178 | } else if (patch->num_sinks == 0 || |
| 179 | (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 180 | (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 181 | oldPatch.sinks[0].ext.device.hw_module != |
| 182 | patch->sinks[0].ext.device.hw_module))) { |
| 183 | // Note on (patch->num_sinks == 0): this situation should not happen as |
| 184 | // these special patches are only created by the policy manager but just |
| 185 | // in case, systematically clear the HAL patch. |
| 186 | // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 187 | // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case. |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 188 | hwModule = oldPatch.sinks[0].ext.device.hw_module; |
| 189 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 190 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule); |
| 191 | if (hwDevice != 0) { |
| 192 | hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 193 | } |
Eric Laurent | 9bcfa7c | 2019-11-21 15:45:04 -0800 | [diff] [blame] | 194 | halHandle = removedPatch.mHalHandle; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 195 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 196 | erasePatch(*handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 200 | Patch newPatch{*patch, endpointPatch}; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 201 | audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 202 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 203 | switch (patch->sources[0].type) { |
| 204 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 205 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 206 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule); |
| 207 | if (!audioHwDevice) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 208 | status = BAD_VALUE; |
| 209 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 210 | } |
| 211 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 212 | // support only one sink if connection to a mix or across HW modules |
| 213 | if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX || |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 214 | (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE && |
| 215 | patch->sinks[i].ext.device.hw_module != srcModule)) && |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 216 | patch->num_sinks > 1) { |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 217 | ALOGW("%s() multiple sinks for mix or across modules not supported", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 218 | status = INVALID_OPERATION; |
| 219 | goto exit; |
| 220 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 221 | // reject connection to different sink types |
| 222 | if (patch->sinks[i].type != patch->sinks[0].type) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 223 | ALOGW("%s() different sink types in same patch not supported", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 224 | status = BAD_VALUE; |
| 225 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 226 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 229 | // manage patches requiring a software bridge |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 230 | // - special patch request with 2 sources (reuse one existing output mix) OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 231 | // - Device to device AND |
| 232 | // - source HW module != destination HW module OR |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 233 | // - audio HAL does not support audio patches creation |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 234 | if ((patch->num_sources == 2) || |
| 235 | ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) && |
| 236 | ((patch->sinks[0].ext.device.hw_module != srcModule) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 237 | !audioHwDevice->supportsAudioPatches()))) { |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 238 | audio_devices_t outputDevice = patch->sinks[0].ext.device.type; |
| 239 | String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 240 | if (patch->num_sources == 2) { |
| 241 | if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX || |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 242 | (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module != |
| 243 | patch->sources[1].ext.mix.hw_module)) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 244 | ALOGW("%s() invalid source combination", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 245 | status = INVALID_OPERATION; |
| 246 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 247 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 248 | |
| 249 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 250 | mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 251 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 252 | ALOGW("%s() cannot get playback thread", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 253 | status = INVALID_OPERATION; |
| 254 | goto exit; |
| 255 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 256 | // existing playback thread is reused, so it is not closed when patch is cleared |
| 257 | newPatch.mPlayback.setThread( |
| 258 | reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 259 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 260 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 261 | audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 262 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | 67bae2c | 2018-07-16 15:44:35 -0700 | [diff] [blame] | 263 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 264 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 265 | config.sample_rate = patch->sinks[0].sample_rate; |
| 266 | } |
| 267 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 268 | config.channel_mask = patch->sinks[0].channel_mask; |
| 269 | } |
| 270 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 271 | config.format = patch->sinks[0].format; |
| 272 | } |
| 273 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) { |
| 274 | flags = patch->sinks[0].flags.output; |
| 275 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 276 | sp<ThreadBase> thread = mAudioFlinger.openOutput_l( |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 277 | patch->sinks[0].ext.device.hw_module, |
| 278 | &output, |
| 279 | &config, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 280 | &mixerConfig, |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 281 | outputDevice, |
| 282 | outputDeviceAddress, |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 283 | flags); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 284 | ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get()); |
| 285 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 286 | status = NO_MEMORY; |
| 287 | goto exit; |
| 288 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 289 | newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get())); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 290 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 291 | audio_devices_t device = patch->sources[0].ext.device.type; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 292 | String8 address = String8(patch->sources[0].ext.device.address); |
| 293 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 294 | // open input stream with source device audio properties if provided or |
| 295 | // default to peer output stream properties otherwise. |
| 296 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 297 | config.sample_rate = patch->sources[0].sample_rate; |
| 298 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 299 | config.sample_rate = newPatch.mPlayback.thread()->sampleRate(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 300 | } |
| 301 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 302 | config.channel_mask = patch->sources[0].channel_mask; |
| 303 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 304 | config.channel_mask = audio_channel_in_mask_from_count( |
| 305 | newPatch.mPlayback.thread()->channelCount()); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 306 | } |
| 307 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 308 | config.format = patch->sources[0].format; |
| 309 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 310 | config.format = newPatch.mPlayback.thread()->format(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 311 | } |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 312 | audio_input_flags_t flags = |
| 313 | patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 314 | patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 315 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 316 | audio_source_t source = AUDIO_SOURCE_MIC; |
| 317 | // For telephony patches, propagate voice communication use case to record side |
| 318 | if (patch->num_sources == 2 |
| 319 | && patch->sources[1].ext.mix.usecase.stream |
| 320 | == AUDIO_STREAM_VOICE_CALL) { |
| 321 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 322 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 323 | sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 324 | &input, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 325 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 326 | device, |
| 327 | address, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 328 | source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 329 | flags, |
| 330 | outputDevice, |
| 331 | outputDeviceAddress); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 332 | ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x", |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 333 | thread.get(), config.channel_mask); |
| 334 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 335 | status = NO_MEMORY; |
| 336 | goto exit; |
| 337 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 338 | newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get())); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 339 | status = newPatch.createConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 340 | if (status != NO_ERROR) { |
| 341 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 342 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 343 | if (audioHwDevice->isInsert()) { |
| 344 | insertedModule = audioHwDevice->handle(); |
| 345 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 346 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 347 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 348 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l( |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 349 | patch->sinks[0].ext.mix.handle); |
| 350 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 351 | thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 352 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 353 | ALOGW("%s() bad capture I/O handle %d", |
| 354 | __func__, patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 355 | status = BAD_VALUE; |
| 356 | goto exit; |
| 357 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 358 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 359 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 360 | if (status == NO_ERROR) { |
| 361 | newPatch.setThread(thread); |
| 362 | } |
| 363 | |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 364 | // remove stale audio patch with same input as sink if any |
| 365 | for (auto& iter : mPatches) { |
| 366 | if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 367 | erasePatch(iter.first); |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 371 | } else { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 372 | sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice(); |
| 373 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 374 | patch->sources, |
| 375 | patch->num_sinks, |
| 376 | patch->sinks, |
| 377 | &halHandle); |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 378 | if (status == INVALID_OPERATION) goto exit; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 379 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 380 | } |
| 381 | } break; |
| 382 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 383 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 384 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 385 | if (index < 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 386 | ALOGW("%s() bad src hw module %d", __func__, srcModule); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 387 | status = BAD_VALUE; |
| 388 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 389 | } |
| 390 | // limit to connections between devices and output streams |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 391 | DeviceDescriptorBaseVector devices; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 392 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 393 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 394 | ALOGW("%s() invalid sink type %d for mix source", |
| 395 | __func__, patch->sinks[i].type); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 396 | status = BAD_VALUE; |
| 397 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 398 | } |
| 399 | // limit to connections between sinks and sources on same HW module |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 400 | if (patch->sinks[i].ext.device.hw_module != srcModule) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 401 | status = BAD_VALUE; |
| 402 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 403 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 404 | sp<DeviceDescriptorBase> device = new DeviceDescriptorBase( |
| 405 | patch->sinks[i].ext.device.type); |
| 406 | device->setAddress(patch->sinks[i].ext.device.address); |
| 407 | device->applyAudioPortConfig(&patch->sinks[i]); |
| 408 | devices.push_back(device); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 409 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 410 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 411 | mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 412 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 413 | thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 414 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 415 | ALOGW("%s() bad playback I/O handle %d", |
| 416 | __func__, patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 417 | status = BAD_VALUE; |
| 418 | goto exit; |
| 419 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 420 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 421 | if (thread == mAudioFlinger.primaryPlaybackThread_l()) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 422 | mAudioFlinger.updateOutDevicesForRecordThreads_l(devices); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 425 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 426 | if (status == NO_ERROR) { |
| 427 | newPatch.setThread(thread); |
| 428 | } |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 429 | |
| 430 | // remove stale audio patch with same output as source if any |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 431 | // Prevent to remove endpoint patches (involved in a SwBridge) |
| 432 | // Prevent to remove AudioPatch used to route an output involved in an endpoint. |
| 433 | if (!endpointPatch) { |
| 434 | for (auto& iter : mPatches) { |
| 435 | if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() && |
| 436 | !iter.second.mIsEndpointPatch) { |
| 437 | erasePatch(iter.first); |
| 438 | break; |
| 439 | } |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 442 | } break; |
| 443 | default: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 444 | status = BAD_VALUE; |
| 445 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 446 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 447 | exit: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 448 | ALOGV("%s() status %d", __func__, status); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 449 | if (status == NO_ERROR) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 450 | *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH); |
| 451 | newPatch.mHalHandle = halHandle; |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 452 | mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 453 | if (insertedModule != AUDIO_MODULE_HANDLE_NONE) { |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 454 | addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 455 | } |
Eric Laurent | ef03eef | 2021-01-05 16:30:04 +0100 | [diff] [blame] | 456 | mPatches.insert(std::make_pair(*handle, std::move(newPatch))); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 457 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 458 | newPatch.clearConnections(this); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 459 | } |
| 460 | return status; |
| 461 | } |
| 462 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 463 | AudioFlinger::PatchPanel::Patch::~Patch() |
| 464 | { |
| 465 | ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d", |
| 466 | mRecord.handle(), mPlayback.handle()); |
| 467 | } |
| 468 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 469 | status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 470 | { |
| 471 | // create patch from source device to record thread input |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 472 | status_t status = panel->createAudioPatch( |
| 473 | PatchBuilder().addSource(mAudioPatch.sources[0]). |
| 474 | addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(), |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 475 | mRecord.handlePtr(), |
| 476 | true /*endpointPatch*/); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 477 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 478 | *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 479 | return status; |
| 480 | } |
| 481 | |
| 482 | // create patch from playback thread output to sink device |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 483 | if (mAudioPatch.num_sinks != 0) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 484 | status = panel->createAudioPatch( |
| 485 | PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(), |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 486 | mPlayback.handlePtr(), |
| 487 | true /*endpointPatch*/); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 488 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 489 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 490 | return status; |
| 491 | } |
| 492 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 493 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 496 | // create a special record track to capture from record thread |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 497 | uint32_t channelCount = mPlayback.thread()->channelCount(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 498 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 499 | audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask(); |
| 500 | uint32_t sampleRate = mPlayback.thread()->sampleRate(); |
| 501 | audio_format_t format = mPlayback.thread()->format(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 502 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 503 | audio_format_t inputFormat = mRecord.thread()->format(); |
| 504 | if (!audio_is_linear_pcm(inputFormat)) { |
| 505 | // The playbackThread format will say PCM for IEC61937 packetized stream. |
| 506 | // Use recordThread format. |
| 507 | format = inputFormat; |
| 508 | } |
| 509 | audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 510 | mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 511 | if (sampleRate == mRecord.thread()->sampleRate() && |
| 512 | inChannelMask == mRecord.thread()->channelMask() && |
| 513 | mRecord.thread()->fastTrackAvailable() && |
| 514 | mRecord.thread()->hasFastCapture()) { |
| 515 | // Create a fast track if the record thread has fast capture to get better performance. |
| 516 | // Only enable fast mode when there is no resample needed. |
| 517 | inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 518 | } else { |
| 519 | // Fast mode is not available in this case. |
| 520 | inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST); |
| 521 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 522 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 523 | audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 524 | mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 525 | audio_stream_type_t streamType = AUDIO_STREAM_PATCH; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 526 | audio_source_t source = AUDIO_SOURCE_DEFAULT; |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 527 | if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) { |
| 528 | // "reuse one existing output mix" case |
| 529 | streamType = mAudioPatch.sources[1].ext.mix.usecase.stream; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 530 | // For telephony patches, propagate voice communication use case to record side |
| 531 | if (streamType == AUDIO_STREAM_VOICE_CALL) { |
| 532 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 533 | } |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 534 | } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 535 | if (mPlayback.thread()->hasFastMixer()) { |
| 536 | // Create a fast track if the playback thread has fast mixer to get better performance. |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 537 | // Note: we should have matching channel mask, sample rate, and format by the logic above. |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 538 | outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 539 | } else { |
| 540 | outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST); |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 543 | sp<RecordThread::PatchRecord> tempRecordTrack; |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 544 | const bool usePassthruPatchRecord = |
| 545 | (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 546 | const size_t playbackFrameCount = mPlayback.thread()->frameCount(); |
| 547 | const size_t recordFrameCount = mRecord.thread()->frameCount(); |
| 548 | size_t frameCount = 0; |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 549 | if (usePassthruPatchRecord) { |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 550 | // PassthruPatchRecord producesBufferOnDemand, so use |
| 551 | // maximum of playback and record thread framecounts |
| 552 | frameCount = std::max(playbackFrameCount, recordFrameCount); |
| 553 | ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu", |
| 554 | __func__, playbackFrameCount, recordFrameCount, frameCount); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 555 | tempRecordTrack = new RecordThread::PassthruPatchRecord( |
| 556 | mRecord.thread().get(), |
| 557 | sampleRate, |
| 558 | inChannelMask, |
| 559 | format, |
| 560 | frameCount, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 561 | inputFlags, |
| 562 | source); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 563 | } else { |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 564 | // use a pseudo LCM between input and output framecount |
| 565 | int playbackShift = __builtin_ctz(playbackFrameCount); |
| 566 | int shift = __builtin_ctz(recordFrameCount); |
| 567 | if (playbackShift < shift) { |
| 568 | shift = playbackShift; |
| 569 | } |
| 570 | frameCount = (playbackFrameCount * recordFrameCount) >> shift; |
| 571 | ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu", |
| 572 | __func__, playbackFrameCount, recordFrameCount, frameCount); |
| 573 | |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 574 | tempRecordTrack = new RecordThread::PatchRecord( |
| 575 | mRecord.thread().get(), |
| 576 | sampleRate, |
| 577 | inChannelMask, |
| 578 | format, |
| 579 | frameCount, |
| 580 | nullptr, |
| 581 | (size_t)0 /* bufferSize */, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 582 | inputFlags, |
| 583 | {} /* timeout */, |
| 584 | source); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 585 | } |
| 586 | status = mRecord.checkTrack(tempRecordTrack.get()); |
| 587 | if (status != NO_ERROR) { |
| 588 | return status; |
| 589 | } |
| 590 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 591 | // create a special playback track to render to playback thread. |
| 592 | // this track is given the same buffer as the PatchRecord buffer |
Francois Gaffie | a0fd4c7 | 2021-05-05 10:49:17 +0200 | [diff] [blame] | 593 | |
| 594 | // Default behaviour is to start as soon as possible to have the lowest possible latency even if |
| 595 | // it might glitch. |
| 596 | // Disable this behavior for FM Tuner source if no fast capture/mixer available. |
| 597 | const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER; |
| 598 | const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1; |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 599 | sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack( |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 600 | mPlayback.thread().get(), |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 601 | streamType, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 602 | sampleRate, |
| 603 | outChannelMask, |
| 604 | format, |
| 605 | frameCount, |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 606 | tempRecordTrack->buffer(), |
| 607 | tempRecordTrack->bufferSize(), |
Francois Gaffie | a0fd4c7 | 2021-05-05 10:49:17 +0200 | [diff] [blame] | 608 | outputFlags, |
| 609 | {} /*timeout*/, |
| 610 | frameCountToBeReady); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 611 | status = mPlayback.checkTrack(tempPatchTrack.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 612 | if (status != NO_ERROR) { |
| 613 | return status; |
| 614 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 615 | |
| 616 | // tie playback and record tracks together |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 617 | // In the case of PassthruPatchRecord no I/O activity happens on RecordThread, |
| 618 | // everything is driven from PlaybackThread. Thus AudioBufferProvider methods |
| 619 | // of PassthruPatchRecord can only be called if the corresponding PatchTrack |
| 620 | // is alive. There is no need to hold a reference, and there is no need |
| 621 | // to clear it. In fact, since playback stopping is asynchronous, there is |
| 622 | // no proper time when clearing could be done. |
| 623 | mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord); |
| 624 | mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 625 | |
| 626 | // start capture and playback |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 627 | mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE); |
| 628 | mPlayback.track()->start(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 629 | |
| 630 | return status; |
| 631 | } |
| 632 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 633 | void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 634 | { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 635 | ALOGV("%s() mRecord.handle %d mPlayback.handle %d", |
| 636 | __func__, mRecord.handle(), mPlayback.handle()); |
| 637 | mRecord.stopTrack(); |
| 638 | mPlayback.stopTrack(); |
Mikhail Naganov | d3f301c | 2019-03-11 08:58:03 -0700 | [diff] [blame] | 639 | mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle. |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 640 | mRecord.closeConnections(panel); |
| 641 | mPlayback.closeConnections(panel); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 644 | status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const |
| 645 | { |
| 646 | if (!isSoftware()) return INVALID_OPERATION; |
| 647 | |
| 648 | auto recordTrack = mRecord.const_track(); |
| 649 | if (recordTrack.get() == nullptr) return INVALID_OPERATION; |
| 650 | |
| 651 | auto playbackTrack = mPlayback.const_track(); |
| 652 | if (playbackTrack.get() == nullptr) return INVALID_OPERATION; |
| 653 | |
| 654 | // Latency information for tracks may be called without obtaining |
| 655 | // the underlying thread lock. |
| 656 | // |
| 657 | // We use record server latency + playback track latency (generally smaller than the |
| 658 | // reverse due to internal biases). |
| 659 | // |
| 660 | // TODO: is this stable enough? Consider a PatchTrack synchronized version of this. |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 661 | |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 662 | // For PCM tracks get server latency. |
| 663 | if (audio_is_linear_pcm(recordTrack->format())) { |
| 664 | double recordServerLatencyMs, playbackTrackLatencyMs; |
| 665 | if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK |
| 666 | && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) { |
| 667 | *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs; |
| 668 | return OK; |
| 669 | } |
| 670 | } |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 671 | |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 672 | // See if kernel latencies are available. |
| 673 | // If so, do a frame diff and time difference computation to estimate |
| 674 | // the total patch latency. This requires that frame counts are reported by the |
| 675 | // HAL are matched properly in the case of record overruns and playback underruns. |
| 676 | ThreadBase::TrackBase::FrameTime recordFT{}, playFT{}; |
| 677 | recordTrack->getKernelFrameTime(&recordFT); |
| 678 | playbackTrack->getKernelFrameTime(&playFT); |
| 679 | if (recordFT.timeNs > 0 && playFT.timeNs > 0) { |
| 680 | const int64_t frameDiff = recordFT.frames - playFT.frames; |
| 681 | const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs; |
| 682 | |
| 683 | // It is possible that the patch track and patch record have a large time disparity because |
| 684 | // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp |
| 685 | // time difference based on how often we expect the timestamps to update in normal operation |
| 686 | // (typical should be no more than 50 ms). |
| 687 | // |
| 688 | // If the timestamps aren't sampled close enough, the patch latency is not |
| 689 | // considered valid. |
| 690 | // |
| 691 | // TODO: change this based on more experiments. |
| 692 | constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND; |
| 693 | if (std::abs(timeDiffNs) < maxValidTimeDiffNs) { |
| 694 | *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate() |
| 695 | - timeDiffNs * 1e-6; |
| 696 | return OK; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return INVALID_OPERATION; |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 703 | String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 704 | { |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 705 | // TODO: Consider table dump form for patches, just like tracks. |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 706 | String8 result = String8::format("Patch %d: %s (thread %p => thread %p)", |
| 707 | myHandle, isSoftware() ? "Software bridge between" : "No software bridge", |
| 708 | mRecord.const_thread().get(), mPlayback.const_thread().get()); |
| 709 | |
| 710 | bool hasSinkDevice = |
| 711 | mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE; |
| 712 | bool hasSourceDevice = |
| 713 | mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE; |
| 714 | result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(), |
| 715 | hasSinkDevice ? "num sinks" : |
| 716 | (hasSourceDevice ? "num sources" : "no devices"), |
| 717 | hasSinkDevice ? mAudioPatch.num_sinks : |
| 718 | (hasSourceDevice ? mAudioPatch.num_sources : 0), |
| 719 | hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type : |
| 720 | (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0)); |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 721 | |
| 722 | // add latency if it exists |
| 723 | double latencyMs; |
| 724 | if (getLatencyMs(&latencyMs) == OK) { |
Mikhail Naganov | b8b6097 | 2018-09-13 12:55:43 -0700 | [diff] [blame] | 725 | result.appendFormat(" latency: %.2lf ms", latencyMs); |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 726 | } |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 727 | return result; |
| 728 | } |
| 729 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 730 | /* Disconnect a patch */ |
| 731 | status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle) |
| 732 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 733 | ALOGV("%s handle %d", __func__, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 734 | status_t status = NO_ERROR; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 735 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 736 | auto iter = mPatches.find(handle); |
| 737 | if (iter == mPatches.end()) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 738 | return BAD_VALUE; |
| 739 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 740 | Patch &removedPatch = iter->second; |
| 741 | const struct audio_patch &patch = removedPatch.mAudioPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 742 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 743 | const struct audio_port_config &src = patch.sources[0]; |
| 744 | switch (src.type) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 745 | case AUDIO_PORT_TYPE_DEVICE: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 746 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module); |
| 747 | if (hwDevice == 0) { |
| 748 | ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 749 | status = BAD_VALUE; |
| 750 | break; |
| 751 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 752 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 753 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 754 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 758 | if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 759 | audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle; |
| 760 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 761 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 762 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 763 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 764 | ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 765 | status = BAD_VALUE; |
| 766 | break; |
| 767 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 768 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 769 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 770 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 771 | status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 772 | } |
| 773 | } break; |
| 774 | case AUDIO_PORT_TYPE_MIX: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 775 | if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) { |
| 776 | ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 777 | status = BAD_VALUE; |
| 778 | break; |
| 779 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 780 | audio_io_handle_t ioHandle = src.ext.mix.handle; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 781 | sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 782 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 783 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 784 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 785 | ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 786 | status = BAD_VALUE; |
| 787 | break; |
| 788 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 789 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 790 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 791 | } break; |
| 792 | default: |
| 793 | status = BAD_VALUE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 796 | erasePatch(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 797 | return status; |
| 798 | } |
| 799 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 800 | void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) { |
| 801 | mPatches.erase(handle); |
| 802 | removeSoftwarePatchFromInsertedModules(handle); |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 803 | mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 806 | /* List connected audio ports and they attributes */ |
| 807 | status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused, |
| 808 | struct audio_patch *patches __unused) |
| 809 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 810 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 811 | return NO_ERROR; |
| 812 | } |
| 813 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 814 | status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches( |
| 815 | audio_io_handle_t stream, |
| 816 | std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const |
| 817 | { |
| 818 | for (const auto& module : mInsertedModules) { |
| 819 | if (module.second.streams.count(stream)) { |
| 820 | for (const auto& patchHandle : module.second.sw_patches) { |
| 821 | const auto& patch_iter = mPatches.find(patchHandle); |
| 822 | if (patch_iter != mPatches.end()) { |
| 823 | const Patch &patch = patch_iter->second; |
| 824 | patches->emplace_back(*this, patchHandle, |
| 825 | patch.mPlayback.const_thread()->id(), |
| 826 | patch.mRecord.const_thread()->id()); |
| 827 | } else { |
| 828 | ALOGE("Stale patch handle in the cache: %d", patchHandle); |
| 829 | } |
| 830 | } |
| 831 | return OK; |
| 832 | } |
| 833 | } |
| 834 | // The stream is not associated with any of inserted modules. |
| 835 | return BAD_VALUE; |
| 836 | } |
| 837 | |
| 838 | void AudioFlinger::PatchPanel::notifyStreamOpened( |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 839 | AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch) |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 840 | { |
| 841 | if (audioHwDevice->isInsert()) { |
| 842 | mInsertedModules[audioHwDevice->handle()].streams.insert(stream); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 843 | if (patch != nullptr) { |
| 844 | std::vector <SoftwarePatch> swPatches; |
| 845 | getDownstreamSoftwarePatches(stream, &swPatches); |
| 846 | if (swPatches.size() > 0) { |
| 847 | auto iter = mPatches.find(swPatches[0].getPatchHandle()); |
| 848 | if (iter != mPatches.end()) { |
| 849 | *patch = iter->second.mAudioPatch; |
| 850 | } |
| 851 | } |
| 852 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream) |
| 857 | { |
| 858 | for (auto& module : mInsertedModules) { |
| 859 | module.second.streams.erase(stream); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 864 | { |
| 865 | if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr; |
| 866 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module); |
| 867 | if (index < 0) { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 868 | ALOGW("%s() bad hw module %d", __func__, module); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 869 | return nullptr; |
| 870 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 871 | return mAudioFlinger.mAudioHwDevs.valueAt(index); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 874 | sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 875 | { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 876 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module); |
| 877 | return audioHwDevice ? audioHwDevice->hwDevice() : nullptr; |
| 878 | } |
| 879 | |
| 880 | void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules( |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 881 | audio_module_handle_t module, audio_patch_handle_t handle, |
| 882 | const struct audio_patch *patch) |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 883 | { |
| 884 | mInsertedModules[module].sw_patches.insert(handle); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 885 | if (!mInsertedModules[module].streams.empty()) { |
| 886 | mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams); |
| 887 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules( |
| 891 | audio_patch_handle_t handle) |
| 892 | { |
| 893 | for (auto& module : mInsertedModules) { |
| 894 | module.second.sw_patches.erase(handle); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | void AudioFlinger::PatchPanel::dump(int fd) const |
| 899 | { |
| 900 | String8 patchPanelDump; |
| 901 | const char *indent = " "; |
| 902 | |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 903 | bool headerPrinted = false; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 904 | for (const auto& iter : mPatches) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 905 | if (!headerPrinted) { |
| 906 | patchPanelDump += "\nPatches:\n"; |
| 907 | headerPrinted = true; |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 908 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 909 | patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 910 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 911 | |
| 912 | headerPrinted = false; |
| 913 | for (const auto& module : mInsertedModules) { |
| 914 | if (!module.second.streams.empty() || !module.second.sw_patches.empty()) { |
| 915 | if (!headerPrinted) { |
| 916 | patchPanelDump += "\nTracked inserted modules:\n"; |
| 917 | headerPrinted = true; |
| 918 | } |
| 919 | String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first); |
| 920 | for (const auto& stream : module.second.streams) { |
| 921 | moduleDump.appendFormat("%d ", stream); |
| 922 | } |
| 923 | moduleDump.append("; SW Patches: "); |
| 924 | for (const auto& patch : module.second.sw_patches) { |
| 925 | moduleDump.appendFormat("%d ", patch); |
| 926 | } |
| 927 | patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string()); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (!patchPanelDump.isEmpty()) { |
| 932 | write(fd, patchPanelDump.string(), patchPanelDump.size()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 936 | } // namespace android |