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> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 28 | #include <media/PatchBuilder.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 29 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | // Note: the following macro is used for extremely verbose logging message. In |
| 34 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 35 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 36 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 37 | // turned on. Do not uncomment the #def below unless you really know what you |
| 38 | // are doing and want to see all of the extremely verbose messages. |
| 39 | //#define VERY_VERY_VERBOSE_LOGGING |
| 40 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 41 | #define ALOGVV ALOGV |
| 42 | #else |
| 43 | #define ALOGVV(a...) do { } while(0) |
| 44 | #endif |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | /* List connected audio ports and their attributes */ |
| 49 | status_t AudioFlinger::listAudioPorts(unsigned int *num_ports, |
| 50 | struct audio_port *ports) |
| 51 | { |
| 52 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 53 | return mPatchPanel.listAudioPorts(num_ports, ports); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Get supported attributes for a given audio port */ |
| 57 | status_t AudioFlinger::getAudioPort(struct audio_port *port) |
| 58 | { |
| 59 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 60 | return mPatchPanel.getAudioPort(port); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 63 | /* Connect a patch between several source and sink ports */ |
| 64 | status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch, |
| 65 | audio_patch_handle_t *handle) |
| 66 | { |
| 67 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 68 | return mPatchPanel.createAudioPatch(patch, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* Disconnect a patch */ |
| 72 | status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle) |
| 73 | { |
| 74 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 75 | return mPatchPanel.releaseAudioPatch(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 78 | /* List connected audio ports and they attributes */ |
| 79 | status_t AudioFlinger::listAudioPatches(unsigned int *num_patches, |
| 80 | struct audio_patch *patches) |
| 81 | { |
| 82 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 83 | return mPatchPanel.listAudioPatches(num_patches, patches); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 86 | status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const |
| 87 | { |
| 88 | const auto& iter = mPatchPanel.mPatches.find(mPatchHandle); |
| 89 | if (iter != mPatchPanel.mPatches.end()) { |
| 90 | return iter->second.getLatencyMs(latencyMs); |
| 91 | } else { |
| 92 | return BAD_VALUE; |
| 93 | } |
| 94 | } |
| 95 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 96 | /* List connected audio ports and their attributes */ |
| 97 | status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused, |
| 98 | struct audio_port *ports __unused) |
| 99 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 100 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 101 | return NO_ERROR; |
| 102 | } |
| 103 | |
| 104 | /* Get supported attributes for a given audio port */ |
| 105 | status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused) |
| 106 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 107 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 108 | return NO_ERROR; |
| 109 | } |
| 110 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 111 | /* Connect a patch between several source and sink ports */ |
| 112 | status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch, |
| 113 | audio_patch_handle_t *handle) |
| 114 | { |
Greg Kaiser | f27ce40 | 2016-03-14 13:43:14 -0700 | [diff] [blame] | 115 | if (handle == NULL || patch == NULL) { |
| 116 | return BAD_VALUE; |
| 117 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 118 | ALOGV("%s() num_sources %d num_sinks %d handle %d", |
| 119 | __func__, patch->num_sources, patch->num_sinks, *handle); |
| 120 | status_t status = NO_ERROR; |
| 121 | audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 122 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 123 | 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] | 124 | return BAD_VALUE; |
| 125 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 126 | // limit number of sources to 1 for now or 2 sources for special cross hw module case. |
| 127 | // only the audio policy manager can request a patch creation with 2 sources. |
| 128 | if (patch->num_sources > 2) { |
| 129 | return INVALID_OPERATION; |
| 130 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 131 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 132 | if (*handle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 133 | auto iter = mPatches.find(*handle); |
| 134 | if (iter != mPatches.end()) { |
| 135 | ALOGV("%s() removing patch handle %d", __func__, *handle); |
| 136 | Patch &removedPatch = iter->second; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 137 | // free resources owned by the removed patch if applicable |
| 138 | // 1) if a software patch is present, release the playback and capture threads and |
| 139 | // tracks created. This will also release the corresponding audio HAL patches |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 140 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 141 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 142 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 143 | // 2) if the new patch and old patch source or sink are devices from different |
| 144 | // hw modules, clear the audio HAL patches now because they will not be updated |
| 145 | // 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] | 146 | if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 147 | audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE; |
| 148 | const struct audio_patch &oldPatch = removedPatch.mAudioPatch; |
| 149 | if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 150 | (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 151 | oldPatch.sources[0].ext.device.hw_module != |
| 152 | patch->sources[0].ext.device.hw_module)) { |
| 153 | hwModule = oldPatch.sources[0].ext.device.hw_module; |
| 154 | } else if (patch->num_sinks == 0 || |
| 155 | (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 156 | (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 157 | oldPatch.sinks[0].ext.device.hw_module != |
| 158 | patch->sinks[0].ext.device.hw_module))) { |
| 159 | // Note on (patch->num_sinks == 0): this situation should not happen as |
| 160 | // these special patches are only created by the policy manager but just |
| 161 | // in case, systematically clear the HAL patch. |
| 162 | // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 163 | // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case. |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 164 | hwModule = oldPatch.sinks[0].ext.device.hw_module; |
| 165 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 166 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule); |
| 167 | if (hwDevice != 0) { |
| 168 | hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | mPatches.erase(iter); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 172 | removeSoftwarePatchFromInsertedModules(*handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 176 | Patch newPatch{*patch}; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 177 | audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 178 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 179 | switch (patch->sources[0].type) { |
| 180 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 181 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 182 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule); |
| 183 | if (!audioHwDevice) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 184 | status = BAD_VALUE; |
| 185 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 186 | } |
| 187 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 188 | // support only one sink if connection to a mix or across HW modules |
| 189 | if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX || |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 190 | (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE && |
| 191 | patch->sinks[i].ext.device.hw_module != srcModule)) && |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 192 | patch->num_sinks > 1) { |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 193 | ALOGW("%s() multiple sinks for mix or across modules not supported", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 194 | status = INVALID_OPERATION; |
| 195 | goto exit; |
| 196 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 197 | // reject connection to different sink types |
| 198 | if (patch->sinks[i].type != patch->sinks[0].type) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 199 | ALOGW("%s() different sink types in same patch not supported", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 200 | status = BAD_VALUE; |
| 201 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 202 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 205 | // manage patches requiring a software bridge |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 206 | // - special patch request with 2 sources (reuse one existing output mix) OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 207 | // - Device to device AND |
| 208 | // - source HW module != destination HW module OR |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 209 | // - audio HAL does not support audio patches creation |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 210 | if ((patch->num_sources == 2) || |
| 211 | ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) && |
| 212 | ((patch->sinks[0].ext.device.hw_module != srcModule) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 213 | !audioHwDevice->supportsAudioPatches()))) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 214 | if (patch->num_sources == 2) { |
| 215 | if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX || |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 216 | (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module != |
| 217 | patch->sources[1].ext.mix.hw_module)) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 218 | ALOGW("%s() invalid source combination", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 219 | status = INVALID_OPERATION; |
| 220 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 221 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 222 | |
| 223 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 224 | mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 225 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 226 | ALOGW("%s() cannot get playback thread", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 227 | status = INVALID_OPERATION; |
| 228 | goto exit; |
| 229 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 230 | // existing playback thread is reused, so it is not closed when patch is cleared |
| 231 | newPatch.mPlayback.setThread( |
| 232 | reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 233 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 234 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 235 | audio_devices_t device = patch->sinks[0].ext.device.type; |
| 236 | String8 address = String8(patch->sinks[0].ext.device.address); |
| 237 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 238 | audio_output_flags_t flags = |
| 239 | patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 240 | patch->sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 241 | sp<ThreadBase> thread = mAudioFlinger.openOutput_l( |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 242 | patch->sinks[0].ext.device.hw_module, |
| 243 | &output, |
| 244 | &config, |
| 245 | device, |
| 246 | address, |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 247 | flags); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 248 | ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get()); |
| 249 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 250 | status = NO_MEMORY; |
| 251 | goto exit; |
| 252 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 253 | newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get())); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 254 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 255 | audio_devices_t device = patch->sources[0].ext.device.type; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 256 | String8 address = String8(patch->sources[0].ext.device.address); |
| 257 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 258 | // open input stream with source device audio properties if provided or |
| 259 | // default to peer output stream properties otherwise. |
| 260 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 261 | config.sample_rate = patch->sources[0].sample_rate; |
| 262 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 263 | config.sample_rate = newPatch.mPlayback.thread()->sampleRate(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 264 | } |
| 265 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 266 | config.channel_mask = patch->sources[0].channel_mask; |
| 267 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 268 | config.channel_mask = audio_channel_in_mask_from_count( |
| 269 | newPatch.mPlayback.thread()->channelCount()); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 270 | } |
| 271 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 272 | config.format = patch->sources[0].format; |
| 273 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 274 | config.format = newPatch.mPlayback.thread()->format(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 275 | } |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 276 | audio_input_flags_t flags = |
| 277 | patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 278 | patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 279 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 280 | sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 281 | &input, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 282 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 283 | device, |
| 284 | address, |
| 285 | AUDIO_SOURCE_MIC, |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 286 | flags); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 287 | ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x", |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 288 | thread.get(), config.channel_mask); |
| 289 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 290 | status = NO_MEMORY; |
| 291 | goto exit; |
| 292 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 293 | newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get())); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 294 | status = newPatch.createConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 295 | if (status != NO_ERROR) { |
| 296 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 297 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 298 | if (audioHwDevice->isInsert()) { |
| 299 | insertedModule = audioHwDevice->handle(); |
| 300 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 301 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 302 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 303 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l( |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 304 | patch->sinks[0].ext.mix.handle); |
| 305 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 306 | thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 307 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 308 | ALOGW("%s() bad capture I/O handle %d", |
| 309 | __func__, patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 310 | status = BAD_VALUE; |
| 311 | goto exit; |
| 312 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 313 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 314 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 315 | } else { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 316 | sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice(); |
| 317 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 318 | patch->sources, |
| 319 | patch->num_sinks, |
| 320 | patch->sinks, |
| 321 | &halHandle); |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 322 | if (status == INVALID_OPERATION) goto exit; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 323 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 324 | } |
| 325 | } break; |
| 326 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 327 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 328 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 329 | if (index < 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 330 | ALOGW("%s() bad src hw module %d", __func__, srcModule); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 331 | status = BAD_VALUE; |
| 332 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 333 | } |
| 334 | // limit to connections between devices and output streams |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 335 | audio_devices_t type = AUDIO_DEVICE_NONE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 336 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 337 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 338 | ALOGW("%s() invalid sink type %d for mix source", |
| 339 | __func__, patch->sinks[i].type); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 340 | status = BAD_VALUE; |
| 341 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 342 | } |
| 343 | // limit to connections between sinks and sources on same HW module |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 344 | if (patch->sinks[i].ext.device.hw_module != srcModule) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 345 | status = BAD_VALUE; |
| 346 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 347 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 348 | type |= patch->sinks[i].ext.device.type; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 349 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 350 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 351 | mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 352 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 353 | thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 354 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 355 | ALOGW("%s() bad playback I/O handle %d", |
| 356 | __func__, patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 357 | status = BAD_VALUE; |
| 358 | goto exit; |
| 359 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 360 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 361 | if (thread == mAudioFlinger.primaryPlaybackThread_l()) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 362 | AudioParameter param = AudioParameter(); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 363 | param.addInt(String8(AudioParameter::keyRouting), (int)type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 364 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 365 | mAudioFlinger.broacastParametersToRecordThreads_l(param.toString()); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 368 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 369 | } break; |
| 370 | default: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 371 | status = BAD_VALUE; |
| 372 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 373 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 374 | exit: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 375 | ALOGV("%s() status %d", __func__, status); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 376 | if (status == NO_ERROR) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 377 | *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH); |
| 378 | newPatch.mHalHandle = halHandle; |
| 379 | mPatches.insert(std::make_pair(*handle, std::move(newPatch))); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 380 | if (insertedModule != AUDIO_MODULE_HANDLE_NONE) { |
| 381 | addSoftwarePatchToInsertedModules(insertedModule, *handle); |
| 382 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 383 | ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 384 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 385 | newPatch.clearConnections(this); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 386 | } |
| 387 | return status; |
| 388 | } |
| 389 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 390 | AudioFlinger::PatchPanel::Patch::~Patch() |
| 391 | { |
| 392 | ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d", |
| 393 | mRecord.handle(), mPlayback.handle()); |
| 394 | } |
| 395 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 396 | status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 397 | { |
| 398 | // create patch from source device to record thread input |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 399 | status_t status = panel->createAudioPatch( |
| 400 | PatchBuilder().addSource(mAudioPatch.sources[0]). |
| 401 | addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(), |
| 402 | mRecord.handlePtr()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 403 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 404 | *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 405 | return status; |
| 406 | } |
| 407 | |
| 408 | // create patch from playback thread output to sink device |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 409 | if (mAudioPatch.num_sinks != 0) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 410 | status = panel->createAudioPatch( |
| 411 | PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(), |
| 412 | mPlayback.handlePtr()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 413 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 414 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 415 | return status; |
| 416 | } |
| 417 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 418 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // use a pseudo LCM between input and output framecount |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 422 | size_t playbackFrameCount = mPlayback.thread()->frameCount(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 423 | int playbackShift = __builtin_ctz(playbackFrameCount); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 424 | size_t recordFramecount = mRecord.thread()->frameCount(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 425 | int shift = __builtin_ctz(recordFramecount); |
| 426 | if (playbackShift < shift) { |
| 427 | shift = playbackShift; |
| 428 | } |
| 429 | size_t frameCount = (playbackFrameCount * recordFramecount) >> shift; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 430 | ALOGV("%s() playframeCount %zu recordFramecount %zu frameCount %zu", |
| 431 | __func__, playbackFrameCount, recordFramecount, frameCount); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 432 | |
| 433 | // create a special record track to capture from record thread |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 434 | uint32_t channelCount = mPlayback.thread()->channelCount(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 435 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 436 | audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask(); |
| 437 | uint32_t sampleRate = mPlayback.thread()->sampleRate(); |
| 438 | audio_format_t format = mPlayback.thread()->format(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 439 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 440 | audio_format_t inputFormat = mRecord.thread()->format(); |
| 441 | if (!audio_is_linear_pcm(inputFormat)) { |
| 442 | // The playbackThread format will say PCM for IEC61937 packetized stream. |
| 443 | // Use recordThread format. |
| 444 | format = inputFormat; |
| 445 | } |
| 446 | audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 447 | mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 448 | sp<RecordThread::PatchRecord> tempRecordTrack = new (std::nothrow) RecordThread::PatchRecord( |
| 449 | mRecord.thread().get(), |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 450 | sampleRate, |
| 451 | inChannelMask, |
| 452 | format, |
| 453 | frameCount, |
| 454 | NULL, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 455 | (size_t)0 /* bufferSize */, |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 456 | inputFlags); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 457 | status = mRecord.checkTrack(tempRecordTrack.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 458 | if (status != NO_ERROR) { |
| 459 | return status; |
| 460 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 461 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 462 | audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 463 | mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE; |
| 464 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 465 | // create a special playback track to render to playback thread. |
| 466 | // this track is given the same buffer as the PatchRecord buffer |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 467 | sp<PlaybackThread::PatchTrack> tempPatchTrack = new (std::nothrow) PlaybackThread::PatchTrack( |
| 468 | mPlayback.thread().get(), |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 469 | mAudioPatch.sources[1].ext.mix.usecase.stream, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 470 | sampleRate, |
| 471 | outChannelMask, |
| 472 | format, |
| 473 | frameCount, |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 474 | tempRecordTrack->buffer(), |
| 475 | tempRecordTrack->bufferSize(), |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 476 | outputFlags); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 477 | status = mPlayback.checkTrack(tempPatchTrack.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 478 | if (status != NO_ERROR) { |
| 479 | return status; |
| 480 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 481 | |
| 482 | // tie playback and record tracks together |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 483 | mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack.get()); |
| 484 | mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 485 | |
| 486 | // start capture and playback |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 487 | mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE); |
| 488 | mPlayback.track()->start(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 489 | |
| 490 | return status; |
| 491 | } |
| 492 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 493 | void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 494 | { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 495 | ALOGV("%s() mRecord.handle %d mPlayback.handle %d", |
| 496 | __func__, mRecord.handle(), mPlayback.handle()); |
| 497 | mRecord.stopTrack(); |
| 498 | mPlayback.stopTrack(); |
| 499 | mRecord.closeConnections(panel); |
| 500 | mPlayback.closeConnections(panel); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 503 | status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const |
| 504 | { |
| 505 | if (!isSoftware()) return INVALID_OPERATION; |
| 506 | |
| 507 | auto recordTrack = mRecord.const_track(); |
| 508 | if (recordTrack.get() == nullptr) return INVALID_OPERATION; |
| 509 | |
| 510 | auto playbackTrack = mPlayback.const_track(); |
| 511 | if (playbackTrack.get() == nullptr) return INVALID_OPERATION; |
| 512 | |
| 513 | // Latency information for tracks may be called without obtaining |
| 514 | // the underlying thread lock. |
| 515 | // |
| 516 | // We use record server latency + playback track latency (generally smaller than the |
| 517 | // reverse due to internal biases). |
| 518 | // |
| 519 | // TODO: is this stable enough? Consider a PatchTrack synchronized version of this. |
| 520 | double recordServerLatencyMs; |
| 521 | if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) != OK) return INVALID_OPERATION; |
| 522 | |
| 523 | double playbackTrackLatencyMs; |
| 524 | if (playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) != OK) return INVALID_OPERATION; |
| 525 | |
| 526 | *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs; |
| 527 | return OK; |
| 528 | } |
| 529 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 530 | String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 531 | { |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 532 | // TODO: Consider table dump form for patches, just like tracks. |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 533 | String8 result = String8::format("Patch %d: thread %p => thread %p", |
| 534 | myHandle, mRecord.const_thread().get(), mPlayback.const_thread().get()); |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 535 | |
| 536 | // add latency if it exists |
| 537 | double latencyMs; |
| 538 | if (getLatencyMs(&latencyMs) == OK) { |
| 539 | result.appendFormat(" latency: %.2lf", latencyMs); |
| 540 | } |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 541 | return result; |
| 542 | } |
| 543 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 544 | /* Disconnect a patch */ |
| 545 | status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle) |
| 546 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 547 | ALOGV("%s handle %d", __func__, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 548 | status_t status = NO_ERROR; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 549 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 550 | auto iter = mPatches.find(handle); |
| 551 | if (iter == mPatches.end()) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 552 | return BAD_VALUE; |
| 553 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 554 | Patch &removedPatch = iter->second; |
| 555 | const struct audio_patch &patch = removedPatch.mAudioPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 556 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 557 | const struct audio_port_config &src = patch.sources[0]; |
| 558 | switch (src.type) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 559 | case AUDIO_PORT_TYPE_DEVICE: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 560 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module); |
| 561 | if (hwDevice == 0) { |
| 562 | 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] | 563 | status = BAD_VALUE; |
| 564 | break; |
| 565 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 566 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 567 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 568 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 572 | if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 573 | audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle; |
| 574 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 575 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 576 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 577 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 578 | ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 579 | status = BAD_VALUE; |
| 580 | break; |
| 581 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 582 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 583 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 584 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 585 | status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 586 | } |
| 587 | } break; |
| 588 | case AUDIO_PORT_TYPE_MIX: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 589 | if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) { |
| 590 | 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] | 591 | status = BAD_VALUE; |
| 592 | break; |
| 593 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 594 | audio_io_handle_t ioHandle = src.ext.mix.handle; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 595 | sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 596 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 597 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 598 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 599 | ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 600 | status = BAD_VALUE; |
| 601 | break; |
| 602 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 603 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 604 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 605 | } break; |
| 606 | default: |
| 607 | status = BAD_VALUE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 610 | mPatches.erase(iter); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 611 | removeSoftwarePatchFromInsertedModules(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 612 | return status; |
| 613 | } |
| 614 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 615 | /* List connected audio ports and they attributes */ |
| 616 | status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused, |
| 617 | struct audio_patch *patches __unused) |
| 618 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 619 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 620 | return NO_ERROR; |
| 621 | } |
| 622 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 623 | status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches( |
| 624 | audio_io_handle_t stream, |
| 625 | std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const |
| 626 | { |
| 627 | for (const auto& module : mInsertedModules) { |
| 628 | if (module.second.streams.count(stream)) { |
| 629 | for (const auto& patchHandle : module.second.sw_patches) { |
| 630 | const auto& patch_iter = mPatches.find(patchHandle); |
| 631 | if (patch_iter != mPatches.end()) { |
| 632 | const Patch &patch = patch_iter->second; |
| 633 | patches->emplace_back(*this, patchHandle, |
| 634 | patch.mPlayback.const_thread()->id(), |
| 635 | patch.mRecord.const_thread()->id()); |
| 636 | } else { |
| 637 | ALOGE("Stale patch handle in the cache: %d", patchHandle); |
| 638 | } |
| 639 | } |
| 640 | return OK; |
| 641 | } |
| 642 | } |
| 643 | // The stream is not associated with any of inserted modules. |
| 644 | return BAD_VALUE; |
| 645 | } |
| 646 | |
| 647 | void AudioFlinger::PatchPanel::notifyStreamOpened( |
| 648 | AudioHwDevice *audioHwDevice, audio_io_handle_t stream) |
| 649 | { |
| 650 | if (audioHwDevice->isInsert()) { |
| 651 | mInsertedModules[audioHwDevice->handle()].streams.insert(stream); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream) |
| 656 | { |
| 657 | for (auto& module : mInsertedModules) { |
| 658 | module.second.streams.erase(stream); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 663 | { |
| 664 | if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr; |
| 665 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module); |
| 666 | if (index < 0) { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 667 | ALOGW("%s() bad hw module %d", __func__, module); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 668 | return nullptr; |
| 669 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 670 | return mAudioFlinger.mAudioHwDevs.valueAt(index); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 673 | sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 674 | { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 675 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module); |
| 676 | return audioHwDevice ? audioHwDevice->hwDevice() : nullptr; |
| 677 | } |
| 678 | |
| 679 | void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules( |
| 680 | audio_module_handle_t module, audio_patch_handle_t handle) |
| 681 | { |
| 682 | mInsertedModules[module].sw_patches.insert(handle); |
| 683 | } |
| 684 | |
| 685 | void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules( |
| 686 | audio_patch_handle_t handle) |
| 687 | { |
| 688 | for (auto& module : mInsertedModules) { |
| 689 | module.second.sw_patches.erase(handle); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | void AudioFlinger::PatchPanel::dump(int fd) const |
| 694 | { |
| 695 | String8 patchPanelDump; |
| 696 | const char *indent = " "; |
| 697 | |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 698 | // Only dump software patches. |
| 699 | bool headerPrinted = false; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 700 | for (const auto& iter : mPatches) { |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 701 | if (iter.second.isSoftware()) { |
| 702 | if (!headerPrinted) { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 703 | patchPanelDump += "\nSoftware patches:\n"; |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 704 | headerPrinted = true; |
| 705 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 706 | patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame^] | 709 | |
| 710 | headerPrinted = false; |
| 711 | for (const auto& module : mInsertedModules) { |
| 712 | if (!module.second.streams.empty() || !module.second.sw_patches.empty()) { |
| 713 | if (!headerPrinted) { |
| 714 | patchPanelDump += "\nTracked inserted modules:\n"; |
| 715 | headerPrinted = true; |
| 716 | } |
| 717 | String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first); |
| 718 | for (const auto& stream : module.second.streams) { |
| 719 | moduleDump.appendFormat("%d ", stream); |
| 720 | } |
| 721 | moduleDump.append("; SW Patches: "); |
| 722 | for (const auto& patch : module.second.sw_patches) { |
| 723 | moduleDump.appendFormat("%d ", patch); |
| 724 | } |
| 725 | patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string()); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if (!patchPanelDump.isEmpty()) { |
| 730 | write(fd, patchPanelDump.string(), patchPanelDump.size()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 734 | } // namespace android |