blob: adefd4bc52318e4d01d176840c721bb2425fdd3e [file] [log] [blame]
Eric Laurent951f4552014-05-20 10:48:17 -07001/*
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
45namespace android {
46
47/* List connected audio ports and their attributes */
48status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
49 struct audio_port *ports)
50{
51 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070052 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070053}
54
55/* Get supported attributes for a given audio port */
56status_t AudioFlinger::getAudioPort(struct audio_port *port)
57{
58 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070059 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070060}
61
Eric Laurent951f4552014-05-20 10:48:17 -070062/* Connect a patch between several source and sink ports */
63status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
64 audio_patch_handle_t *handle)
65{
66 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070067 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070068}
69
70/* Disconnect a patch */
71status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
72{
73 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070074 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070075}
76
Eric Laurent951f4552014-05-20 10:48:17 -070077/* List connected audio ports and they attributes */
78status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
79 struct audio_patch *patches)
80{
81 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070082 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070083}
84
85/* List connected audio ports and their attributes */
86status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
87 struct audio_port *ports __unused)
88{
Mikhail Naganovdea53042018-04-26 13:10:21 -070089 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070090 return NO_ERROR;
91}
92
93/* Get supported attributes for a given audio port */
94status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
95{
Mikhail Naganovdea53042018-04-26 13:10:21 -070096 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070097 return NO_ERROR;
98}
99
Eric Laurent951f4552014-05-20 10:48:17 -0700100/* Connect a patch between several source and sink ports */
101status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
102 audio_patch_handle_t *handle)
103{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700104 if (handle == NULL || patch == NULL) {
105 return BAD_VALUE;
106 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700107 ALOGV("%s() num_sources %d num_sinks %d handle %d",
108 __func__, patch->num_sources, patch->num_sinks, *handle);
109 status_t status = NO_ERROR;
110 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700111
Eric Laurent874c42872014-08-08 15:13:39 -0700112 if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700113 (patch->num_sinks == 0 && patch->num_sources != 2) ||
114 patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
Eric Laurent951f4552014-05-20 10:48:17 -0700115 return BAD_VALUE;
116 }
Eric Laurent874c42872014-08-08 15:13:39 -0700117 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
118 // only the audio policy manager can request a patch creation with 2 sources.
119 if (patch->num_sources > 2) {
120 return INVALID_OPERATION;
121 }
Eric Laurent951f4552014-05-20 10:48:17 -0700122
Eric Laurent83b88082014-06-20 18:31:16 -0700123 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700124 auto iter = mPatches.find(*handle);
125 if (iter != mPatches.end()) {
126 ALOGV("%s() removing patch handle %d", __func__, *handle);
127 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700128 // free resources owned by the removed patch if applicable
129 // 1) if a software patch is present, release the playback and capture threads and
130 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700131 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700132 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700133 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700134 // 2) if the new patch and old patch source or sink are devices from different
135 // hw modules, clear the audio HAL patches now because they will not be updated
136 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700137 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700138 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
139 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
140 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
141 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
142 oldPatch.sources[0].ext.device.hw_module !=
143 patch->sources[0].ext.device.hw_module)) {
144 hwModule = oldPatch.sources[0].ext.device.hw_module;
145 } else if (patch->num_sinks == 0 ||
146 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
147 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
148 oldPatch.sinks[0].ext.device.hw_module !=
149 patch->sinks[0].ext.device.hw_module))) {
150 // Note on (patch->num_sinks == 0): this situation should not happen as
151 // these special patches are only created by the policy manager but just
152 // in case, systematically clear the HAL patch.
153 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700154 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700155 hwModule = oldPatch.sinks[0].ext.device.hw_module;
156 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700157 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
158 if (hwDevice != 0) {
159 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700160 }
161 }
162 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700163 }
164 }
165
Mikhail Naganovdea53042018-04-26 13:10:21 -0700166 Patch newPatch{*patch};
Eric Laurent83b88082014-06-20 18:31:16 -0700167
Eric Laurent951f4552014-05-20 10:48:17 -0700168 switch (patch->sources[0].type) {
169 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700170 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700172 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700173 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700174 status = BAD_VALUE;
175 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700176 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 AudioHwDevice *audioHwDevice = mAudioFlinger.mAudioHwDevs.valueAt(index);
Eric Laurent951f4552014-05-20 10:48:17 -0700178 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700179 // support only one sink if connection to a mix or across HW modules
180 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700181 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
182 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700183 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700184 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700185 status = INVALID_OPERATION;
186 goto exit;
187 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700188 // reject connection to different sink types
189 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700190 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700191 status = BAD_VALUE;
192 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700193 }
Eric Laurent951f4552014-05-20 10:48:17 -0700194 }
195
Eric Laurent3bcf8592015-04-03 12:13:24 -0700196 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700197 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700198 // - Device to device AND
199 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700200 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700201 if ((patch->num_sources == 2) ||
202 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
203 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700204 !audioHwDevice->supportsAudioPatches()))) {
Eric Laurent83b88082014-06-20 18:31:16 -0700205 if (patch->num_sources == 2) {
206 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700207 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
208 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700209 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700210 status = INVALID_OPERATION;
211 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700212 }
Eric Laurent83b88082014-06-20 18:31:16 -0700213
214 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700215 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700216 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700217 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700218 status = INVALID_OPERATION;
219 goto exit;
220 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700221 // existing playback thread is reused, so it is not closed when patch is cleared
222 newPatch.mPlayback.setThread(
223 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700224 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700225 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
226 audio_devices_t device = patch->sinks[0].ext.device.type;
227 String8 address = String8(patch->sinks[0].ext.device.address);
228 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700229 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800230 patch->sinks[0].ext.device.hw_module,
231 &output,
232 &config,
233 device,
234 address,
235 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700236 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
237 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700238 status = NO_MEMORY;
239 goto exit;
240 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700241 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700242 }
Eric Laurent83b88082014-06-20 18:31:16 -0700243 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700244 String8 address = String8(patch->sources[0].ext.device.address);
245 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700246 // open input stream with source device audio properties if provided or
247 // default to peer output stream properties otherwise.
248 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
249 config.sample_rate = patch->sources[0].sample_rate;
250 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700251 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700252 }
253 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
254 config.channel_mask = patch->sources[0].channel_mask;
255 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700256 config.channel_mask = audio_channel_in_mask_from_count(
257 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700258 }
259 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
260 config.format = patch->sources[0].format;
261 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700262 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700263 }
Eric Laurentcf2c0212014-07-25 16:20:43 -0700264 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700265 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700266 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700267 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700268 device,
269 address,
270 AUDIO_SOURCE_MIC,
Eric Laurent83b88082014-06-20 18:31:16 -0700271 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700272 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700273 thread.get(), config.channel_mask);
274 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700275 status = NO_MEMORY;
276 goto exit;
277 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700278 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700279 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700280 if (status != NO_ERROR) {
281 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700282 }
283 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700284 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700285 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700286 patch->sinks[0].ext.mix.handle);
287 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700288 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800289 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700290 ALOGW("%s() bad capture I/O handle %d",
291 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800292 status = BAD_VALUE;
293 goto exit;
294 }
Eric Laurent83b88082014-06-20 18:31:16 -0700295 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700296 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700297 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700298 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
299 status = hwDevice->createAudioPatch(patch->num_sources,
300 patch->sources,
301 patch->num_sinks,
302 patch->sinks,
303 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700304 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700305 }
Eric Laurent951f4552014-05-20 10:48:17 -0700306 }
307 } break;
308 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700309 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700310 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700311 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700312 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700313 status = BAD_VALUE;
314 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700315 }
316 // limit to connections between devices and output streams
Eric Laurent054d9d32015-04-24 08:48:48 -0700317 audio_devices_t type = AUDIO_DEVICE_NONE;
Eric Laurent951f4552014-05-20 10:48:17 -0700318 for (unsigned int i = 0; i < patch->num_sinks; i++) {
319 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700320 ALOGW("%s() invalid sink type %d for mix source",
321 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700322 status = BAD_VALUE;
323 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700324 }
325 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700326 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700327 status = BAD_VALUE;
328 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700329 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700330 type |= patch->sinks[i].ext.device.type;
Eric Laurent951f4552014-05-20 10:48:17 -0700331 }
Eric Laurent951f4552014-05-20 10:48:17 -0700332 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700333 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700334 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700335 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800336 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700337 ALOGW("%s() bad playback I/O handle %d",
338 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800339 status = BAD_VALUE;
340 goto exit;
341 }
Eric Laurent951f4552014-05-20 10:48:17 -0700342 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700343 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
Eric Laurent054d9d32015-04-24 08:48:48 -0700344 AudioParameter param = AudioParameter();
Mikhail Naganov00260b52016-10-13 12:54:24 -0700345 param.addInt(String8(AudioParameter::keyRouting), (int)type);
Eric Laurent054d9d32015-04-24 08:48:48 -0700346
Mikhail Naganovdea53042018-04-26 13:10:21 -0700347 mAudioFlinger.broacastParametersToRecordThreads_l(param.toString());
Eric Laurent951f4552014-05-20 10:48:17 -0700348 }
349
Eric Laurent054d9d32015-04-24 08:48:48 -0700350 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700351 } break;
352 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700353 status = BAD_VALUE;
354 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700355 }
Eric Laurent83b88082014-06-20 18:31:16 -0700356exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700357 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700358 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700359 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
360 newPatch.mHalHandle = halHandle;
361 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
362 ALOGV("%s() added new patch handle %d halHandle %d", __func__, *handle, halHandle);
Eric Laurent83b88082014-06-20 18:31:16 -0700363 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700364 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700365 }
366 return status;
367}
368
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700369AudioFlinger::PatchPanel::Patch::~Patch()
370{
371 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
372 mRecord.handle(), mPlayback.handle());
373}
374
Mikhail Naganovdea53042018-04-26 13:10:21 -0700375status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700376{
377 // create patch from source device to record thread input
378 struct audio_patch subPatch;
379 subPatch.num_sources = 1;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 subPatch.sources[0] = mAudioPatch.sources[0];
Eric Laurent83b88082014-06-20 18:31:16 -0700381 subPatch.num_sinks = 1;
382
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700383 mRecord.thread()->getAudioPortConfig(&subPatch.sinks[0]);
Eric Laurent83b88082014-06-20 18:31:16 -0700384 subPatch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_MIC;
385
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700386 status_t status = panel->createAudioPatch(&subPatch, mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700387 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700388 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700389 return status;
390 }
391
392 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700393 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700394 mPlayback.thread()->getAudioPortConfig(&subPatch.sources[0]);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700395 subPatch.sinks[0] = mAudioPatch.sinks[0];
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700396 status = panel->createAudioPatch(&subPatch, mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700397 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700398 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700399 return status;
400 }
401 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700402 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700403 }
404
405 // use a pseudo LCM between input and output framecount
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700406 size_t playbackFrameCount = mPlayback.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700407 int playbackShift = __builtin_ctz(playbackFrameCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700408 size_t recordFramecount = mRecord.thread()->frameCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700409 int shift = __builtin_ctz(recordFramecount);
410 if (playbackShift < shift) {
411 shift = playbackShift;
412 }
413 size_t frameCount = (playbackFrameCount * recordFramecount) >> shift;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700414 ALOGV("%s() playframeCount %zu recordFramecount %zu frameCount %zu",
415 __func__, playbackFrameCount, recordFramecount, frameCount);
Eric Laurent83b88082014-06-20 18:31:16 -0700416
417 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700418 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700419 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700420 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
421 uint32_t sampleRate = mPlayback.thread()->sampleRate();
422 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700423
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700424 sp<RecordThread::PatchRecord> tempRecordTrack = new (std::nothrow) RecordThread::PatchRecord(
425 mRecord.thread().get(),
Eric Laurent83b88082014-06-20 18:31:16 -0700426 sampleRate,
427 inChannelMask,
428 format,
429 frameCount,
430 NULL,
Andy Hung8fe68032017-06-05 16:17:51 -0700431 (size_t)0 /* bufferSize */,
Eric Laurent05067782016-06-01 18:27:28 -0700432 AUDIO_INPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700433 status = mRecord.checkTrack(tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700434 if (status != NO_ERROR) {
435 return status;
436 }
Eric Laurent83b88082014-06-20 18:31:16 -0700437
438 // create a special playback track to render to playback thread.
439 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700440 sp<PlaybackThread::PatchTrack> tempPatchTrack = new (std::nothrow) PlaybackThread::PatchTrack(
441 mPlayback.thread().get(),
Mikhail Naganovdea53042018-04-26 13:10:21 -0700442 mAudioPatch.sources[1].ext.mix.usecase.stream,
Eric Laurent83b88082014-06-20 18:31:16 -0700443 sampleRate,
444 outChannelMask,
445 format,
446 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700447 tempRecordTrack->buffer(),
448 tempRecordTrack->bufferSize(),
Eric Laurent05067782016-06-01 18:27:28 -0700449 AUDIO_OUTPUT_FLAG_NONE);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700450 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700451 if (status != NO_ERROR) {
452 return status;
453 }
Eric Laurent83b88082014-06-20 18:31:16 -0700454
455 // tie playback and record tracks together
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700456 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack.get());
457 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700458
459 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700460 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
461 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700462
463 return status;
464}
465
Mikhail Naganovdea53042018-04-26 13:10:21 -0700466void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700467{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700468 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
469 __func__, mRecord.handle(), mPlayback.handle());
470 mRecord.stopTrack();
471 mPlayback.stopTrack();
472 mRecord.closeConnections(panel);
473 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700474}
475
Eric Laurent951f4552014-05-20 10:48:17 -0700476/* Disconnect a patch */
477status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
478{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700479 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700480 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700481
Mikhail Naganovdea53042018-04-26 13:10:21 -0700482 auto iter = mPatches.find(handle);
483 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700484 return BAD_VALUE;
485 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700486 Patch &removedPatch = iter->second;
487 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700488
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700489 const struct audio_port_config &src = patch.sources[0];
490 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700491 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700492 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
493 if (hwDevice == 0) {
494 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700495 status = BAD_VALUE;
496 break;
497 }
Eric Laurent83b88082014-06-20 18:31:16 -0700498
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700499 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700500 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700501 break;
502 }
503
Mikhail Naganovdea53042018-04-26 13:10:21 -0700504 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
505 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
506 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700507 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700508 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800509 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700510 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800511 status = BAD_VALUE;
512 break;
513 }
Eric Laurent951f4552014-05-20 10:48:17 -0700514 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700515 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700516 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700517 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700518 }
519 } break;
520 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700521 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
522 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700523 status = BAD_VALUE;
524 break;
525 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700526 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700527 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700528 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700529 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800530 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700531 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800532 status = BAD_VALUE;
533 break;
534 }
Eric Laurent951f4552014-05-20 10:48:17 -0700535 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700536 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700537 } break;
538 default:
539 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700540 }
541
Mikhail Naganovdea53042018-04-26 13:10:21 -0700542 mPatches.erase(iter);
Eric Laurent951f4552014-05-20 10:48:17 -0700543 return status;
544}
545
Eric Laurent951f4552014-05-20 10:48:17 -0700546/* List connected audio ports and they attributes */
547status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
548 struct audio_patch *patches __unused)
549{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700550 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700551 return NO_ERROR;
552}
553
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700554sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
555{
556 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
557 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
558 if (index < 0) {
559 return nullptr;
560 }
561 return mAudioFlinger.mAudioHwDevs.valueAt(index)->hwDevice();
562}
563
Glenn Kasten63238ef2015-03-02 15:50:29 -0800564} // namespace android