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