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