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