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