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 | { |
| 142 | ALOGV("createAudioPatch() num_sources %d num_sinks %d handle %d", |
| 143 | patch->num_sources, patch->num_sinks, *handle); |
| 144 | status_t status = NO_ERROR; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 145 | audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 146 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 147 | if (audioflinger == 0) { |
| 148 | return NO_INIT; |
| 149 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 150 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 151 | if (handle == NULL || patch == NULL) { |
| 152 | return BAD_VALUE; |
| 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 | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 155 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 156 | return BAD_VALUE; |
| 157 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 158 | // limit number of sources to 1 for now or 2 sources for special cross hw module case. |
| 159 | // only the audio policy manager can request a patch creation with 2 sources. |
| 160 | if (patch->num_sources > 2) { |
| 161 | return INVALID_OPERATION; |
| 162 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 163 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 164 | if (*handle != AUDIO_PATCH_HANDLE_NONE) { |
| 165 | for (size_t index = 0; *handle != 0 && index < mPatches.size(); index++) { |
| 166 | if (*handle == mPatches[index]->mHandle) { |
| 167 | ALOGV("createAudioPatch() removing patch handle %d", *handle); |
| 168 | halHandle = mPatches[index]->mHalHandle; |
soon1.choi | d8cd477 | 2015-01-05 14:27:42 +0900 | [diff] [blame] | 169 | Patch *removedPatch = mPatches[index]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 170 | mPatches.removeAt(index); |
soon1.choi | d8cd477 | 2015-01-05 14:27:42 +0900 | [diff] [blame] | 171 | delete removedPatch; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 177 | Patch *newPatch = new Patch(patch); |
| 178 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 179 | switch (patch->sources[0].type) { |
| 180 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 181 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
| 182 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 183 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 184 | ALOGW("createAudioPatch() bad src hw module %d", srcModule); |
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 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 188 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 189 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 190 | // support only one sink if connection to a mix or across HW modules |
| 191 | if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX || |
| 192 | patch->sinks[i].ext.mix.hw_module != srcModule) && |
| 193 | patch->num_sinks > 1) { |
| 194 | status = INVALID_OPERATION; |
| 195 | goto exit; |
| 196 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 197 | // reject connection to different sink types |
| 198 | if (patch->sinks[i].type != patch->sinks[0].type) { |
| 199 | ALOGW("createAudioPatch() different sink types in same patch not supported"); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 200 | status = BAD_VALUE; |
| 201 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 202 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 205 | // manage patches requiring a software bridge |
| 206 | // - Device to device AND |
| 207 | // - source HW module != destination HW module OR |
| 208 | // - audio HAL version < 3.0 |
| 209 | // - special patch request with 2 sources (reuse one existing output mix) |
| 210 | if ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) && |
| 211 | ((patch->sinks[0].ext.device.hw_module != srcModule) || |
| 212 | (audioHwDevice->version() < AUDIO_DEVICE_API_VERSION_3_0) || |
| 213 | (patch->num_sources == 2))) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 214 | if (patch->num_sources == 2) { |
| 215 | if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX || |
| 216 | patch->sinks[0].ext.device.hw_module != |
| 217 | patch->sources[1].ext.mix.hw_module) { |
| 218 | ALOGW("createAudioPatch() invalid source combination"); |
| 219 | status = INVALID_OPERATION; |
| 220 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 221 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 222 | |
| 223 | sp<ThreadBase> thread = |
| 224 | audioflinger->checkPlaybackThread_l(patch->sources[1].ext.mix.handle); |
| 225 | newPatch->mPlaybackThread = (MixerThread *)thread.get(); |
| 226 | if (thread == 0) { |
| 227 | ALOGW("createAudioPatch() cannot get playback thread"); |
| 228 | status = INVALID_OPERATION; |
| 229 | goto exit; |
| 230 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 231 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 232 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 233 | audio_devices_t device = patch->sinks[0].ext.device.type; |
| 234 | String8 address = String8(patch->sinks[0].ext.device.address); |
| 235 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 236 | newPatch->mPlaybackThread = audioflinger->openOutput_l( |
| 237 | patch->sinks[0].ext.device.hw_module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 238 | &output, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 239 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 240 | device, |
| 241 | address, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 242 | AUDIO_OUTPUT_FLAG_NONE); |
| 243 | ALOGV("audioflinger->openOutput_l() returned %p", |
| 244 | newPatch->mPlaybackThread.get()); |
| 245 | if (newPatch->mPlaybackThread == 0) { |
| 246 | status = NO_MEMORY; |
| 247 | goto exit; |
| 248 | } |
| 249 | } |
| 250 | uint32_t channelCount = newPatch->mPlaybackThread->channelCount(); |
| 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 | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 254 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
| 255 | config.sample_rate = newPatch->mPlaybackThread->sampleRate(); |
| 256 | config.channel_mask = inChannelMask; |
| 257 | config.format = newPatch->mPlaybackThread->format(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 258 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 259 | newPatch->mRecordThread = audioflinger->openInput_l(srcModule, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 260 | &input, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 261 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 262 | device, |
| 263 | address, |
| 264 | AUDIO_SOURCE_MIC, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 265 | AUDIO_INPUT_FLAG_NONE); |
| 266 | ALOGV("audioflinger->openInput_l() returned %p inChannelMask %08x", |
| 267 | newPatch->mRecordThread.get(), inChannelMask); |
| 268 | if (newPatch->mRecordThread == 0) { |
| 269 | status = NO_MEMORY; |
| 270 | goto exit; |
| 271 | } |
| 272 | status = createPatchConnections(newPatch, patch); |
| 273 | if (status != NO_ERROR) { |
| 274 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 275 | } |
| 276 | } else { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 277 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 278 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 279 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 280 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 281 | if (thread == 0) { |
| 282 | ALOGW("createAudioPatch() bad capture I/O handle %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 283 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 284 | status = BAD_VALUE; |
| 285 | goto exit; |
| 286 | } |
| 287 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
| 288 | } else { |
| 289 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
| 290 | status = hwDevice->create_audio_patch(hwDevice, |
| 291 | patch->num_sources, |
| 292 | patch->sources, |
| 293 | patch->num_sinks, |
| 294 | patch->sinks, |
| 295 | &halHandle); |
| 296 | } |
| 297 | } else { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 298 | if (patch->sinks[0].type != AUDIO_PORT_TYPE_MIX) { |
| 299 | status = INVALID_OPERATION; |
| 300 | goto exit; |
| 301 | } |
| 302 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 303 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 304 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 305 | if (thread == 0) { |
| 306 | ALOGW("createAudioPatch() bad capture I/O handle %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 307 | patch->sinks[0].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 308 | status = BAD_VALUE; |
| 309 | goto exit; |
| 310 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 311 | char *address; |
| 312 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 313 | address = audio_device_address_to_parameter( |
| 314 | patch->sources[0].ext.device.type, |
| 315 | patch->sources[0].ext.device.address); |
| 316 | } else { |
| 317 | address = (char *)calloc(1, 1); |
| 318 | } |
| 319 | AudioParameter param = AudioParameter(String8(address)); |
| 320 | free(address); |
| 321 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 322 | (int)patch->sources[0].ext.device.type); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 323 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 324 | (int)patch->sinks[0].ext.mix.usecase.source); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 325 | ALOGV("createAudioPatch() AUDIO_PORT_TYPE_DEVICE setParameters %s", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 326 | param.toString().string()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 327 | status = thread->setParameters(param.toString()); |
| 328 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 329 | } |
| 330 | } break; |
| 331 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 332 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
| 333 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 334 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 335 | ALOGW("createAudioPatch() bad src hw module %d", 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 | } |
| 339 | // limit to connections between devices and output streams |
| 340 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 341 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 342 | ALOGW("createAudioPatch() invalid sink type %d for mix source", |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 343 | patch->sinks[i].type); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 344 | status = BAD_VALUE; |
| 345 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 346 | } |
| 347 | // limit to connections between sinks and sources on same HW module |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 348 | if (patch->sinks[i].ext.device.hw_module != srcModule) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 349 | status = BAD_VALUE; |
| 350 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 354 | sp<ThreadBase> thread = |
| 355 | audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
| 356 | if (thread == 0) { |
| 357 | ALOGW("createAudioPatch() bad playback I/O handle %d", |
| 358 | patch->sources[0].ext.mix.handle); |
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 | } |
| 362 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 363 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
| 364 | } else { |
| 365 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 366 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 367 | type |= patch->sinks[i].ext.device.type; |
| 368 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 369 | char *address; |
| 370 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 371 | //FIXME: we only support address on first sink with HAL version < 3.0 |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 372 | address = audio_device_address_to_parameter( |
| 373 | patch->sinks[0].ext.device.type, |
| 374 | patch->sinks[0].ext.device.address); |
| 375 | } else { |
| 376 | address = (char *)calloc(1, 1); |
| 377 | } |
| 378 | AudioParameter param = AudioParameter(String8(address)); |
| 379 | free(address); |
| 380 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 381 | status = thread->setParameters(param.toString()); |
| 382 | } |
| 383 | |
| 384 | } break; |
| 385 | default: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 386 | status = BAD_VALUE; |
| 387 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 388 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 389 | exit: |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 390 | ALOGV("createAudioPatch() status %d", status); |
| 391 | if (status == NO_ERROR) { |
| 392 | *handle = audioflinger->nextUniqueId(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 393 | newPatch->mHandle = *handle; |
| 394 | newPatch->mHalHandle = halHandle; |
| 395 | mPatches.add(newPatch); |
| 396 | ALOGV("createAudioPatch() added new patch handle %d halHandle %d", *handle, halHandle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 397 | } else { |
| 398 | clearPatchConnections(newPatch); |
| 399 | delete newPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 400 | } |
| 401 | return status; |
| 402 | } |
| 403 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 404 | status_t AudioFlinger::PatchPanel::createPatchConnections(Patch *patch, |
| 405 | const struct audio_patch *audioPatch) |
| 406 | { |
| 407 | // create patch from source device to record thread input |
| 408 | struct audio_patch subPatch; |
| 409 | subPatch.num_sources = 1; |
| 410 | subPatch.sources[0] = audioPatch->sources[0]; |
| 411 | subPatch.num_sinks = 1; |
| 412 | |
| 413 | patch->mRecordThread->getAudioPortConfig(&subPatch.sinks[0]); |
| 414 | subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC; |
| 415 | |
| 416 | status_t status = createAudioPatch(&subPatch, &patch->mRecordPatchHandle); |
| 417 | if (status != NO_ERROR) { |
| 418 | patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 419 | return status; |
| 420 | } |
| 421 | |
| 422 | // create patch from playback thread output to sink device |
| 423 | patch->mPlaybackThread->getAudioPortConfig(&subPatch.sources[0]); |
| 424 | subPatch.sinks[0] = audioPatch->sinks[0]; |
| 425 | status = createAudioPatch(&subPatch, &patch->mPlaybackPatchHandle); |
| 426 | if (status != NO_ERROR) { |
| 427 | patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 428 | return status; |
| 429 | } |
| 430 | |
| 431 | // use a pseudo LCM between input and output framecount |
| 432 | size_t playbackFrameCount = patch->mPlaybackThread->frameCount(); |
| 433 | int playbackShift = __builtin_ctz(playbackFrameCount); |
| 434 | size_t recordFramecount = patch->mRecordThread->frameCount(); |
| 435 | int shift = __builtin_ctz(recordFramecount); |
| 436 | if (playbackShift < shift) { |
| 437 | shift = playbackShift; |
| 438 | } |
| 439 | size_t frameCount = (playbackFrameCount * recordFramecount) >> shift; |
| 440 | ALOGV("createPatchConnections() playframeCount %d recordFramecount %d frameCount %d ", |
| 441 | playbackFrameCount, recordFramecount, frameCount); |
| 442 | |
| 443 | // create a special record track to capture from record thread |
| 444 | uint32_t channelCount = patch->mPlaybackThread->channelCount(); |
| 445 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
| 446 | audio_channel_mask_t outChannelMask = patch->mPlaybackThread->channelMask(); |
| 447 | uint32_t sampleRate = patch->mPlaybackThread->sampleRate(); |
| 448 | audio_format_t format = patch->mPlaybackThread->format(); |
| 449 | |
| 450 | patch->mPatchRecord = new RecordThread::PatchRecord( |
| 451 | patch->mRecordThread.get(), |
| 452 | sampleRate, |
| 453 | inChannelMask, |
| 454 | format, |
| 455 | frameCount, |
| 456 | NULL, |
| 457 | IAudioFlinger::TRACK_DEFAULT); |
| 458 | if (patch->mPatchRecord == 0) { |
| 459 | return NO_MEMORY; |
| 460 | } |
| 461 | status = patch->mPatchRecord->initCheck(); |
| 462 | if (status != NO_ERROR) { |
| 463 | return status; |
| 464 | } |
| 465 | patch->mRecordThread->addPatchRecord(patch->mPatchRecord); |
| 466 | |
| 467 | // create a special playback track to render to playback thread. |
| 468 | // this track is given the same buffer as the PatchRecord buffer |
| 469 | patch->mPatchTrack = new PlaybackThread::PatchTrack( |
| 470 | patch->mPlaybackThread.get(), |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 471 | audioPatch->sources[1].ext.mix.usecase.stream, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 472 | sampleRate, |
| 473 | outChannelMask, |
| 474 | format, |
| 475 | frameCount, |
| 476 | patch->mPatchRecord->buffer(), |
| 477 | IAudioFlinger::TRACK_DEFAULT); |
| 478 | if (patch->mPatchTrack == 0) { |
| 479 | return NO_MEMORY; |
| 480 | } |
| 481 | status = patch->mPatchTrack->initCheck(); |
| 482 | if (status != NO_ERROR) { |
| 483 | return status; |
| 484 | } |
| 485 | patch->mPlaybackThread->addPatchTrack(patch->mPatchTrack); |
| 486 | |
| 487 | // tie playback and record tracks together |
| 488 | patch->mPatchRecord->setPeerProxy(patch->mPatchTrack.get()); |
| 489 | patch->mPatchTrack->setPeerProxy(patch->mPatchRecord.get()); |
| 490 | |
| 491 | // start capture and playback |
| 492 | patch->mPatchRecord->start(AudioSystem::SYNC_EVENT_NONE, 0); |
| 493 | patch->mPatchTrack->start(); |
| 494 | |
| 495 | return status; |
| 496 | } |
| 497 | |
| 498 | void AudioFlinger::PatchPanel::clearPatchConnections(Patch *patch) |
| 499 | { |
| 500 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 501 | if (audioflinger == 0) { |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | ALOGV("clearPatchConnections() patch->mRecordPatchHandle %d patch->mPlaybackPatchHandle %d", |
| 506 | patch->mRecordPatchHandle, patch->mPlaybackPatchHandle); |
| 507 | |
| 508 | if (patch->mPatchRecord != 0) { |
| 509 | patch->mPatchRecord->stop(); |
| 510 | } |
| 511 | if (patch->mPatchTrack != 0) { |
| 512 | patch->mPatchTrack->stop(); |
| 513 | } |
| 514 | if (patch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 515 | releaseAudioPatch(patch->mRecordPatchHandle); |
| 516 | patch->mRecordPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 517 | } |
| 518 | if (patch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 519 | releaseAudioPatch(patch->mPlaybackPatchHandle); |
| 520 | patch->mPlaybackPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 521 | } |
| 522 | if (patch->mRecordThread != 0) { |
| 523 | if (patch->mPatchRecord != 0) { |
| 524 | patch->mRecordThread->deletePatchRecord(patch->mPatchRecord); |
| 525 | patch->mPatchRecord.clear(); |
| 526 | } |
| 527 | audioflinger->closeInputInternal_l(patch->mRecordThread); |
| 528 | patch->mRecordThread.clear(); |
| 529 | } |
| 530 | if (patch->mPlaybackThread != 0) { |
| 531 | if (patch->mPatchTrack != 0) { |
| 532 | patch->mPlaybackThread->deletePatchTrack(patch->mPatchTrack); |
| 533 | patch->mPatchTrack.clear(); |
| 534 | } |
| 535 | // if num sources == 2 we are reusing an existing playback thread so we do not close it |
| 536 | if (patch->mAudioPatch.num_sources != 2) { |
| 537 | audioflinger->closeOutputInternal_l(patch->mPlaybackThread); |
| 538 | } |
| 539 | patch->mPlaybackThread.clear(); |
| 540 | } |
| 541 | } |
| 542 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 543 | /* Disconnect a patch */ |
| 544 | status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle) |
| 545 | { |
| 546 | ALOGV("releaseAudioPatch handle %d", handle); |
| 547 | status_t status = NO_ERROR; |
| 548 | size_t index; |
| 549 | |
| 550 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 551 | if (audioflinger == 0) { |
| 552 | return NO_INIT; |
| 553 | } |
| 554 | |
| 555 | for (index = 0; index < mPatches.size(); index++) { |
| 556 | if (handle == mPatches[index]->mHandle) { |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | if (index == mPatches.size()) { |
| 561 | return BAD_VALUE; |
| 562 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 563 | Patch *removedPatch = mPatches[index]; |
| 564 | mPatches.removeAt(index); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 565 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 566 | struct audio_patch *patch = &removedPatch->mAudioPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 567 | |
| 568 | switch (patch->sources[0].type) { |
| 569 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 570 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
| 571 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 572 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 573 | ALOGW("releaseAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 574 | status = BAD_VALUE; |
| 575 | break; |
| 576 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 577 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 578 | if (removedPatch->mRecordPatchHandle != AUDIO_PATCH_HANDLE_NONE || |
| 579 | removedPatch->mPlaybackPatchHandle != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 580 | clearPatchConnections(removedPatch); |
| 581 | break; |
| 582 | } |
| 583 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 584 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 585 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 586 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 587 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
| 588 | patch->sinks[0].ext.mix.handle); |
| 589 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 590 | ALOGW("releaseAudioPatch() bad capture I/O handle %d", |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 591 | patch->sinks[0].ext.mix.handle); |
| 592 | status = BAD_VALUE; |
| 593 | break; |
| 594 | } |
Eric Laurent | f8fd8d6 | 2014-09-09 17:00:14 -0700 | [diff] [blame] | 595 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 596 | } else { |
| 597 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
Eric Laurent | f8fd8d6 | 2014-09-09 17:00:14 -0700 | [diff] [blame] | 598 | status = hwDevice->release_audio_patch(hwDevice, removedPatch->mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 599 | } |
| 600 | } else { |
| 601 | sp<ThreadBase> thread = audioflinger->checkRecordThread_l( |
| 602 | patch->sinks[0].ext.mix.handle); |
| 603 | if (thread == 0) { |
| 604 | ALOGW("releaseAudioPatch() bad capture I/O handle %d", |
| 605 | patch->sinks[0].ext.mix.handle); |
| 606 | status = BAD_VALUE; |
| 607 | break; |
| 608 | } |
| 609 | AudioParameter param; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 610 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
Eric Laurent | 24478d4 | 2014-06-04 20:02:57 -0700 | [diff] [blame] | 611 | ALOGV("releaseAudioPatch() AUDIO_PORT_TYPE_DEVICE setParameters %s", |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 612 | param.toString().string()); |
| 613 | status = thread->setParameters(param.toString()); |
| 614 | } |
| 615 | } break; |
| 616 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 617 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
| 618 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 619 | if (index < 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 620 | ALOGW("releaseAudioPatch() bad src hw module %d", srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 621 | status = BAD_VALUE; |
| 622 | break; |
| 623 | } |
| 624 | sp<ThreadBase> thread = |
| 625 | audioflinger->checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
| 626 | if (thread == 0) { |
| 627 | ALOGW("releaseAudioPatch() bad playback I/O handle %d", |
| 628 | patch->sources[0].ext.mix.handle); |
| 629 | status = BAD_VALUE; |
| 630 | break; |
| 631 | } |
| 632 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 633 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | f8fd8d6 | 2014-09-09 17:00:14 -0700 | [diff] [blame] | 634 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch->mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 635 | } else { |
| 636 | AudioParameter param; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 637 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 638 | status = thread->setParameters(param.toString()); |
| 639 | } |
| 640 | } break; |
| 641 | default: |
| 642 | status = BAD_VALUE; |
| 643 | break; |
| 644 | } |
| 645 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 646 | delete removedPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 647 | return status; |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /* List connected audio ports and they attributes */ |
| 652 | status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused, |
| 653 | struct audio_patch *patches __unused) |
| 654 | { |
| 655 | ALOGV("listAudioPatches"); |
| 656 | return NO_ERROR; |
| 657 | } |
| 658 | |
| 659 | /* Set audio port configuration */ |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 660 | status_t AudioFlinger::PatchPanel::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 661 | { |
| 662 | ALOGV("setAudioPortConfig"); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 663 | status_t status = NO_ERROR; |
| 664 | |
| 665 | sp<AudioFlinger> audioflinger = mAudioFlinger.promote(); |
| 666 | if (audioflinger == 0) { |
| 667 | return NO_INIT; |
| 668 | } |
| 669 | |
| 670 | audio_module_handle_t module; |
| 671 | if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 672 | module = config->ext.device.hw_module; |
| 673 | } else { |
| 674 | module = config->ext.mix.hw_module; |
| 675 | } |
| 676 | |
| 677 | ssize_t index = audioflinger->mAudioHwDevs.indexOfKey(module); |
| 678 | if (index < 0) { |
| 679 | ALOGW("setAudioPortConfig() bad hw module %d", module); |
| 680 | return BAD_VALUE; |
| 681 | } |
| 682 | |
| 683 | AudioHwDevice *audioHwDevice = audioflinger->mAudioHwDevs.valueAt(index); |
| 684 | if (audioHwDevice->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 685 | audio_hw_device_t *hwDevice = audioHwDevice->hwDevice(); |
| 686 | return hwDevice->set_audio_port_config(hwDevice, config); |
| 687 | } else { |
| 688 | return INVALID_OPERATION; |
| 689 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 690 | return NO_ERROR; |
| 691 | } |
| 692 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 693 | } // namespace android |