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