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" |
| 27 | #include "ServiceUtilities.h" |
| 28 | #include <media/AudioParameter.h> |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | // Note: the following macro is used for extremely verbose logging message. In |
| 33 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 34 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 35 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 36 | // turned on. Do not uncomment the #def below unless you really know what you |
| 37 | // are doing and want to see all of the extremely verbose messages. |
| 38 | //#define VERY_VERY_VERBOSE_LOGGING |
| 39 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 40 | #define ALOGVV ALOGV |
| 41 | #else |
| 42 | #define ALOGVV(a...) do { } while(0) |
| 43 | #endif |
| 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | /* List connected audio ports and their attributes */ |
| 48 | status_t AudioFlinger::listAudioPorts(unsigned int *num_ports, |
| 49 | struct audio_port *ports) |
| 50 | { |
| 51 | Mutex::Autolock _l(mLock); |
| 52 | if (mPatchPanel != 0) { |
| 53 | return mPatchPanel->listAudioPorts(num_ports, ports); |
| 54 | } |
| 55 | return NO_INIT; |
| 56 | } |
| 57 | |
| 58 | /* Get supported attributes for a given audio port */ |
| 59 | status_t AudioFlinger::getAudioPort(struct audio_port *port) |
| 60 | { |
| 61 | Mutex::Autolock _l(mLock); |
| 62 | if (mPatchPanel != 0) { |
| 63 | return mPatchPanel->getAudioPort(port); |
| 64 | } |
| 65 | return NO_INIT; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* Connect a patch between several source and sink ports */ |
| 70 | status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch, |
| 71 | audio_patch_handle_t *handle) |
| 72 | { |
| 73 | Mutex::Autolock _l(mLock); |
| 74 | if (mPatchPanel != 0) { |
| 75 | return mPatchPanel->createAudioPatch(patch, handle); |
| 76 | } |
| 77 | return NO_INIT; |
| 78 | } |
| 79 | |
| 80 | /* Disconnect a patch */ |
| 81 | status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle) |
| 82 | { |
| 83 | Mutex::Autolock _l(mLock); |
| 84 | if (mPatchPanel != 0) { |
| 85 | return mPatchPanel->releaseAudioPatch(handle); |
| 86 | } |
| 87 | return NO_INIT; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* List connected audio ports and they attributes */ |
| 92 | status_t AudioFlinger::listAudioPatches(unsigned int *num_patches, |
| 93 | struct audio_patch *patches) |
| 94 | { |
| 95 | Mutex::Autolock _l(mLock); |
| 96 | if (mPatchPanel != 0) { |
| 97 | return mPatchPanel->listAudioPatches(num_patches, patches); |
| 98 | } |
| 99 | return NO_INIT; |
| 100 | } |
| 101 | |
| 102 | /* Set audio port configuration */ |
| 103 | status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config) |
| 104 | { |
| 105 | Mutex::Autolock _l(mLock); |
| 106 | if (mPatchPanel != 0) { |
| 107 | return mPatchPanel->setAudioPortConfig(config); |
| 108 | } |
| 109 | return NO_INIT; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | AudioFlinger::PatchPanel::PatchPanel(const sp<AudioFlinger>& audioFlinger) |
| 114 | : mAudioFlinger(audioFlinger) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | AudioFlinger::PatchPanel::~PatchPanel() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /* List connected audio ports and their attributes */ |
| 123 | status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused, |
| 124 | struct audio_port *ports __unused) |
| 125 | { |
| 126 | ALOGV("listAudioPorts"); |
| 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
| 130 | /* Get supported attributes for a given audio port */ |
| 131 | status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused) |
| 132 | { |
| 133 | ALOGV("getAudioPort"); |
| 134 | return NO_ERROR; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /* Connect a patch between several source and sink ports */ |
| 139 | status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch, |
| 140 | audio_patch_handle_t *handle) |
| 141 | { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 142 | status_t status = NO_ERROR; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 143 | audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 144 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
Greg Kaiser | f27ce40 | 2016-03-14 13:43:14 -0700 | [diff] [blame] | 145 | if (handle == NULL || patch == NULL) { |
| 146 | return BAD_VALUE; |
| 147 | } |
| 148 | ALOGV("createAudioPatch() num_sources %d num_sinks %d handle %d", |
| 149 | patch->num_sources, patch->num_sinks, *handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 150 | if (audioflinger == 0) { |
| 151 | return NO_INIT; |
| 152 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 153 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 154 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 155 | (patch->num_sinks == 0 && patch->num_sources != 2) || |
| 156 | patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 157 | return BAD_VALUE; |
| 158 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 159 | // limit number of sources to 1 for now or 2 sources for special cross hw module case. |
| 160 | // only the audio policy manager can request a patch creation with 2 sources. |
| 161 | if (patch->num_sources > 2) { |
| 162 | return INVALID_OPERATION; |
| 163 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 164 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 165 | if (*handle != AUDIO_PATCH_HANDLE_NONE) { |
| 166 | for (size_t index = 0; *handle != 0 && index < mPatches.size(); index++) { |
| 167 | if (*handle == mPatches[index]->mHandle) { |
| 168 | ALOGV("createAudioPatch() removing patch handle %d", *handle); |
| 169 | halHandle = mPatches[index]->mHalHandle; |
soon1.choi | d8cd477 | 2015-01-05 14:27:42 +0900 | [diff] [blame] | 170 | Patch *removedPatch = mPatches[index]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 171 | mPatches.removeAt(index); |
soon1.choi | d8cd477 | 2015-01-05 14:27:42 +0900 | [diff] [blame] | 172 | delete removedPatch; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 178 | Patch *newPatch = new Patch(patch); |
| 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; |
| 183 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 184 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 185 | ALOGW("createAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 186 | status = BAD_VALUE; |
| 187 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 188 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 189 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 190 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 191 | // support only one sink if connection to a mix or across HW modules |
| 192 | if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX || |
| 193 | patch->sinks[i].ext.mix.hw_module != srcModule) && |
| 194 | patch->num_sinks > 1) { |
| 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) { |
| 200 | ALOGW("createAudioPatch() different sink types in same patch not supported"); |
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 |
| 210 | // - audio HAL version < 3.0 |
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) || |
| 214 | (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0)))) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 215 | if (patch->num_sources == 2) { |
| 216 | if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX || |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 217 | (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module != |
| 218 | patch->sources[1].ext.mix.hw_module)) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 219 | ALOGW("createAudioPatch() invalid source combination"); |
| 220 | status = INVALID_OPERATION; |
| 221 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 222 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 223 | |
| 224 | sp<ThreadBase> thread = |
| 225 | audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle); |
| 226 | newPatch->mPlaybackThread = (MixerThread *)thread.get(); |
| 227 | if (thread == 0) { |
| 228 | ALOGW("createAudioPatch() cannot get playback thread"); |
| 229 | status = INVALID_OPERATION; |
| 230 | goto exit; |
| 231 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 232 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 233 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 234 | audio_devices_t device = patch->sinks[0].ext.device.type; |
| 235 | String8 address = String8(patch->sinks[0].ext.device.address); |
| 236 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 237 | newPatch->mPlaybackThread = audioflinger->openOutput_l( |
| 238 | patch->sinks[0].ext.device.hw_module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 239 | &output, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 240 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 241 | device, |
| 242 | address, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 243 | AUDIO_OUTPUT_FLAG_NONE); |
| 244 | ALOGV("audioflinger->openOutput_l() returned %p", |
| 245 | newPatch->mPlaybackThread.get()); |
| 246 | if (newPatch->mPlaybackThread == 0) { |
| 247 | status = NO_MEMORY; |
| 248 | goto exit; |
| 249 | } |
| 250 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 251 | audio_devices_t device = patch->sources[0].ext.device.type; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 252 | String8 address = String8(patch->sources[0].ext.device.address); |
| 253 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 254 | // open input stream with source device audio properties if provided or |
| 255 | // default to peer output stream properties otherwise. |
| 256 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 257 | config.sample_rate = patch->sources[0].sample_rate; |
| 258 | } else { |
| 259 | config.sample_rate = newPatch->mPlaybackThread->sampleRate(); |
| 260 | } |
| 261 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 262 | config.channel_mask = patch->sources[0].channel_mask; |
| 263 | } else { |
| 264 | config.channel_mask = |
| 265 | audio_channel_in_mask_from_count(newPatch->mPlaybackThread->channelCount()); |
| 266 | } |
| 267 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 268 | config.format = patch->sources[0].format; |
| 269 | } else { |
| 270 | config.format = newPatch->mPlaybackThread->format(); |
| 271 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 272 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 273 | newPatch->mRecordThread = audioflinger->openInput_l(srcModule, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 274 | &input, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 275 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 276 | device, |
| 277 | address, |
| 278 | AUDIO_SOURCE_MIC, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 279 | AUDIO_INPUT_FLAG_NONE); |
| 280 | ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x", |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 281 | newPatch->mRecordThread.get(), config.channel_mask); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 282 | if (newPatch->mRecordThread == 0) { |
| 283 | status = NO_MEMORY; |
| 284 | goto exit; |
| 285 | } |
| 286 | status = createPatchConnections(newPatch, patch); |
| 287 | if (status != NO_ERROR) { |
| 288 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 289 | } |
| 290 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 291 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 292 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
| 293 | patch->sinks[0].ext.mix.handle); |
| 294 | if (thread == 0) { |
| 295 | ALOGW("createAudioPatch() bad capture I/O handle %d", |
| 296 | patch->sinks[0].ext.mix.handle); |
| 297 | status = BAD_VALUE; |
| 298 | goto exit; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 299 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 300 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 301 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 302 | if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 303 | status = INVALID_OPERATION; |
| 304 | goto exit; |
| 305 | } |
| 306 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 307 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
| 308 | status = hwDevice->create_audio_patch(hwDevice, |
| 309 | patch->num_sources, |
| 310 | patch->sources, |
| 311 | patch->num_sinks, |
| 312 | patch->sinks, |
| 313 | &halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 314 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 315 | } |
| 316 | } break; |
| 317 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 318 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
| 319 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 320 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 321 | ALOGW("createAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 322 | status = BAD_VALUE; |
| 323 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 324 | } |
| 325 | // limit to connections between devices and output streams |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 326 | audio_devices_t type = AUDIO_DEVICE_NONE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 327 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 328 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 329 | ALOGW("createAudioPatch() invalid sink type %d for mix source", |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 330 | patch->sinks[i].type); |
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 sinks and sources on same HW module |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 335 | if (patch->sinks[i].ext.device.hw_module != srcModule) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 336 | status = BAD_VALUE; |
| 337 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 338 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 339 | type |= patch->sinks[i].ext.device.type; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 340 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 341 | sp<ThreadBase> thread = |
| 342 | audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
| 343 | if (thread == 0) { |
| 344 | ALOGW("createAudioPatch() bad playback I/O handle %d", |
| 345 | patch->sources[0].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 346 | status = BAD_VALUE; |
| 347 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 348 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 349 | if (thread == audioflinger->primaryPlaybackThread_l()) { |
| 350 | AudioParameter param = AudioParameter(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 351 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 352 | |
| 353 | audioflinger->broacastParametersToRecordThreads_l(param.toString()); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 356 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 357 | } break; |
| 358 | default: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 359 | status = BAD_VALUE; |
| 360 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 361 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 362 | exit: |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 363 | ALOGV("createAudioPatch() status %d", status); |
| 364 | if (status == NO_ERROR) { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 365 | *handle = (audio_patch_handle_t) audioflinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 366 | newPatch->mHandle = *handle; |
| 367 | newPatch->mHalHandle = halHandle; |
| 368 | mPatches.add(newPatch); |
| 369 | ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 370 | } else { |
| 371 | clearPatchConnections(newPatch); |
| 372 | delete newPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 373 | } |
| 374 | return status; |
| 375 | } |
| 376 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 377 | status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch, |
| 378 | const struct audio_patch *audioPatch) |
| 379 | { |
| 380 | // create patch from source device to record thread input |
| 381 | struct audio_patch subPatch; |
| 382 | subPatch.num_sources = 1; |
| 383 | subPatch.sources[0] = audioPatch->sources[0]; |
| 384 | subPatch.num_sinks = 1; |
| 385 | |
| 386 | patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]); |
| 387 | subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC; |
| 388 | |
| 389 | status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle); |
| 390 | if (status != NO_ERROR) { |
| 391 | patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 392 | return status; |
| 393 | } |
| 394 | |
| 395 | // create patch from playback thread output to sink device |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 396 | if (audioPatch->num_sinks != 0) { |
| 397 | patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]); |
| 398 | subPatch.sinks[0] = audioPatch->sinks[0]; |
| 399 | status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle); |
| 400 | if (status != NO_ERROR) { |
| 401 | patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 402 | return status; |
| 403 | } |
| 404 | } else { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 405 | patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // use a pseudo LCM between input and output framecount |
| 409 | size_t playbackFrameCount = patch->mPlaybackThread->frameCount(); |
| 410 | int playbackShift = __builtin_ctz(playbackFrameCount); |
| 411 | size_t recordFramecount = patch->mRecordThread->frameCount(); |
| 412 | int shift = __builtin_ctz(recordFramecount); |
| 413 | if (playbackShift < shift) { |
| 414 | shift = playbackShift; |
| 415 | } |
| 416 | size_t frameCount = (playbackFrameCount * recordFramecount) >> shift; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 417 | ALOGV("createPatchConnections() playframeCount %zu recordFramecount %zu frameCount %zu", |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 418 | playbackFrameCount, recordFramecount, frameCount); |
| 419 | |
| 420 | // create a special record track to capture from record thread |
| 421 | uint32_t channelCount = patch->mPlaybackThread->channelCount(); |
| 422 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
| 423 | audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask(); |
| 424 | uint32_t sampleRate = patch->mPlaybackThread->sampleRate(); |
| 425 | audio_format_t format = patch->mPlaybackThread->format(); |
| 426 | |
| 427 | patch->mPatchRecord = new RecordThread::PatchRecord( |
| 428 | patch->mRecordThread.get(), |
| 429 | sampleRate, |
| 430 | inChannelMask, |
| 431 | format, |
| 432 | frameCount, |
| 433 | NULL, |
| 434 | IAudioFlinger::TRACK_DEFAULT); |
| 435 | if (patch->mPatchRecord == 0) { |
| 436 | return NO_MEMORY; |
| 437 | } |
| 438 | status = patch->mPatchRecord->initCheck(); |
| 439 | if (status != NO_ERROR) { |
| 440 | return status; |
| 441 | } |
| 442 | patch->mRecordThread->addPatchRecord(patch->mPatchRecord); |
| 443 | |
| 444 | // create a special playback track to render to playback thread. |
| 445 | // this track is given the same buffer as the PatchRecord buffer |
| 446 | patch->mPatchTrack = new PlaybackThread::PatchTrack( |
| 447 | patch->mPlaybackThread.get(), |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 448 | audioPatch->sources[1].ext.mix.usecase.stream, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 449 | sampleRate, |
| 450 | outChannelMask, |
| 451 | format, |
| 452 | frameCount, |
| 453 | patch->mPatchRecord->buffer(), |
| 454 | IAudioFlinger::TRACK_DEFAULT); |
| 455 | if (patch->mPatchTrack == 0) { |
| 456 | return NO_MEMORY; |
| 457 | } |
| 458 | status = patch->mPatchTrack->initCheck(); |
| 459 | if (status != NO_ERROR) { |
| 460 | return status; |
| 461 | } |
| 462 | patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack); |
| 463 | |
| 464 | // tie playback and record tracks together |
| 465 | patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get()); |
| 466 | patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get()); |
| 467 | |
| 468 | // start capture and playback |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 469 | patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 470 | patch->mPatchTrack->start(); |
| 471 | |
| 472 | return status; |
| 473 | } |
| 474 | |
| 475 | void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch) |
| 476 | { |
| 477 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 478 | if (audioflinger == 0) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d", |
| 483 | patch->mRecordPatchHandle, patch->mPlaybackPatchHandle); |
| 484 | |
| 485 | if (patch->mPatchRecord != 0) { |
| 486 | patch->mPatchRecord->stop(); |
| 487 | } |
| 488 | if (patch->mPatchTrack != 0) { |
| 489 | patch->mPatchTrack->stop(); |
| 490 | } |
| 491 | if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 492 | releaseAudioPatch(patch->mRecordPatchHandle); |
| 493 | patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 494 | } |
| 495 | if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 496 | releaseAudioPatch(patch->mPlaybackPatchHandle); |
| 497 | patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 498 | } |
| 499 | if (patch->mRecordThread != 0) { |
| 500 | if (patch->mPatchRecord != 0) { |
| 501 | patch->mRecordThread->deletePatchRecord(patch->mPatchRecord); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 502 | } |
| 503 | audioflinger->closeInputInternal_l(patch->mRecordThread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 504 | } |
| 505 | if (patch->mPlaybackThread != 0) { |
| 506 | if (patch->mPatchTrack != 0) { |
| 507 | patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 508 | } |
| 509 | // if num sources == 2 we are reusing an existing playback thread so we do not close it |
| 510 | if (patch->mAudioPatch.num_sources != 2) { |
| 511 | audioflinger->closeOutputInternal_l(patch->mPlaybackThread); |
| 512 | } |
Eric Laurent | a0169a0 | 2015-07-06 18:32:01 -0700 | [diff] [blame] | 513 | } |
| 514 | if (patch->mRecordThread != 0) { |
| 515 | if (patch->mPatchRecord != 0) { |
| 516 | patch->mPatchRecord.clear(); |
| 517 | } |
| 518 | patch->mRecordThread.clear(); |
| 519 | } |
| 520 | if (patch->mPlaybackThread != 0) { |
| 521 | if (patch->mPatchTrack != 0) { |
| 522 | patch->mPatchTrack.clear(); |
| 523 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 524 | patch->mPlaybackThread.clear(); |
| 525 | } |
Eric Laurent | a0169a0 | 2015-07-06 18:32:01 -0700 | [diff] [blame] | 526 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 529 | /* Disconnect a patch */ |
| 530 | status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle) |
| 531 | { |
| 532 | ALOGV("releaseAudioPatch handle %d", handle); |
| 533 | status_t status = NO_ERROR; |
| 534 | size_t index; |
| 535 | |
| 536 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 537 | if (audioflinger == 0) { |
| 538 | return NO_INIT; |
| 539 | } |
| 540 | |
| 541 | for (index = 0; index < mPatches.size(); index++) { |
| 542 | if (handle == mPatches[index]->mHandle) { |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | if (index == mPatches.size()) { |
| 547 | return BAD_VALUE; |
| 548 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 549 | Patch *removedPatch = mPatches[index]; |
| 550 | mPatches.removeAt(index); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 551 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 552 | struct audio_patch *patch = &removedPatch->mAudioPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 553 | |
| 554 | switch (patch->sources[0].type) { |
| 555 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 556 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
| 557 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 558 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 559 | ALOGW("releaseAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 560 | status = BAD_VALUE; |
| 561 | break; |
| 562 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 563 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 564 | if (removedPatch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE || |
| 565 | removedPatch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 566 | clearPatchConnections(removedPatch); |
| 567 | break; |
| 568 | } |
| 569 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 570 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 571 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 572 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 573 | if (thread == 0) { |
| 574 | ALOGW("releaseAudioPatch() bad capture I/O handle %d", |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 575 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 576 | status = BAD_VALUE; |
| 577 | break; |
| 578 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 579 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle); |
| 580 | } else { |
| 581 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 582 | if (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) { |
| 583 | status = INVALID_OPERATION; |
| 584 | break; |
| 585 | } |
| 586 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
| 587 | status = hwDevice->release_audio_patch(hwDevice, removedPatch->mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 588 | } |
| 589 | } break; |
| 590 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 591 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
| 592 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 593 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 594 | ALOGW("releaseAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 595 | status = BAD_VALUE; |
| 596 | break; |
| 597 | } |
| 598 | sp<ThreadBase> thread = |
| 599 | audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
| 600 | if (thread == 0) { |
| 601 | ALOGW("releaseAudioPatch() bad playback I/O handle %d", |
| 602 | patch->sources[0].ext.mix.handle); |
| 603 | status = BAD_VALUE; |
| 604 | break; |
| 605 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 606 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 607 | } break; |
| 608 | default: |
| 609 | status = BAD_VALUE; |
| 610 | break; |
| 611 | } |
| 612 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 613 | delete removedPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 614 | return status; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | /* List connected audio ports and they attributes */ |
| 619 | status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused, |
| 620 | struct audio_patch *patches __unused) |
| 621 | { |
| 622 | ALOGV("listAudioPatches"); |
| 623 | return NO_ERROR; |
| 624 | } |
| 625 | |
| 626 | /* Set audio port configuration */ |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 627 | status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 628 | { |
| 629 | ALOGV("setAudioPortConfig"); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 630 | |
| 631 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 632 | if (audioflinger == 0) { |
| 633 | return NO_INIT; |
| 634 | } |
| 635 | |
| 636 | audio_module_handle_t module; |
| 637 | if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 638 | module = config->ext.device.hw_module; |
| 639 | } else { |
| 640 | module = config->ext.mix.hw_module; |
| 641 | } |
| 642 | |
| 643 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module); |
| 644 | if (index < 0) { |
| 645 | ALOGW("setAudioPortConfig() bad hw module %d", module); |
| 646 | return BAD_VALUE; |
| 647 | } |
| 648 | |
| 649 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 650 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 651 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
| 652 | return hwDevice->set_audio_port_config(hwDevice, config); |
| 653 | } else { |
| 654 | return INVALID_OPERATION; |
| 655 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 656 | return NO_ERROR; |
| 657 | } |
| 658 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 659 | } // namespace android |