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