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