blob: 030c929e0b9e497ca12d412c8033ea413692da89 [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"
Eric Laurent951f4552014-05-20 10:48:17 -070027#include <media/AudioParameter.h>
jiabinc52b1ff2019-10-31 17:20:42 -070028#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070029#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070030#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070031
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
47namespace android {
48
49/* List connected audio ports and their attributes */
50status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
51 struct audio_port *ports)
52{
53 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070054 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070055}
56
57/* Get supported attributes for a given audio port */
58status_t AudioFlinger::getAudioPort(struct audio_port *port)
59{
60 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070061 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070062}
63
Eric Laurent951f4552014-05-20 10:48:17 -070064/* Connect a patch between several source and sink ports */
65status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
66 audio_patch_handle_t *handle)
67{
68 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070069 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070070}
71
72/* Disconnect a patch */
73status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
74{
75 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070076 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070077}
78
Eric Laurent951f4552014-05-20 10:48:17 -070079/* List connected audio ports and they attributes */
80status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
81 struct audio_patch *patches)
82{
83 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070084 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070085}
86
Mikhail Naganovadca70f2018-07-09 12:49:25 -070087status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const
88{
89 const auto& iter = mPatchPanel.mPatches.find(mPatchHandle);
90 if (iter != mPatchPanel.mPatches.end()) {
91 return iter->second.getLatencyMs(latencyMs);
92 } else {
93 return BAD_VALUE;
94 }
95}
96
Eric Laurent951f4552014-05-20 10:48:17 -070097/* List connected audio ports and their attributes */
98status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
99 struct audio_port *ports __unused)
100{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700101 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700102 return NO_ERROR;
103}
104
105/* Get supported attributes for a given audio port */
106status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port *port __unused)
107{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700108 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700109 return NO_ERROR;
110}
111
Eric Laurent951f4552014-05-20 10:48:17 -0700112/* Connect a patch between several source and sink ports */
113status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200114 audio_patch_handle_t *handle,
115 bool endpointPatch)
Eric Laurent951f4552014-05-20 10:48:17 -0700116{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700117 if (handle == NULL || patch == NULL) {
118 return BAD_VALUE;
119 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700120 ALOGV("%s() num_sources %d num_sinks %d handle %d",
121 __func__, patch->num_sources, patch->num_sinks, *handle);
122 status_t status = NO_ERROR;
123 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700124
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700125 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700126 return BAD_VALUE;
127 }
Eric Laurent874c42872014-08-08 15:13:39 -0700128 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
129 // only the audio policy manager can request a patch creation with 2 sources.
130 if (patch->num_sources > 2) {
131 return INVALID_OPERATION;
132 }
Eric Laurent951f4552014-05-20 10:48:17 -0700133
Eric Laurent83b88082014-06-20 18:31:16 -0700134 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700135 auto iter = mPatches.find(*handle);
136 if (iter != mPatches.end()) {
137 ALOGV("%s() removing patch handle %d", __func__, *handle);
138 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700139 // free resources owned by the removed patch if applicable
140 // 1) if a software patch is present, release the playback and capture threads and
141 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700142 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700143 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700144 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700145 // 2) if the new patch and old patch source or sink are devices from different
146 // hw modules, clear the audio HAL patches now because they will not be updated
147 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700148 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700149 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
150 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
151 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
152 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
153 oldPatch.sources[0].ext.device.hw_module !=
154 patch->sources[0].ext.device.hw_module)) {
155 hwModule = oldPatch.sources[0].ext.device.hw_module;
156 } else if (patch->num_sinks == 0 ||
157 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
158 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
159 oldPatch.sinks[0].ext.device.hw_module !=
160 patch->sinks[0].ext.device.hw_module))) {
161 // Note on (patch->num_sinks == 0): this situation should not happen as
162 // these special patches are only created by the policy manager but just
163 // in case, systematically clear the HAL patch.
164 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700165 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700166 hwModule = oldPatch.sinks[0].ext.device.hw_module;
167 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700168 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
169 if (hwDevice != 0) {
170 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800172 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700173 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800174 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700175 }
176 }
177
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200178 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700179 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700180
Eric Laurent951f4552014-05-20 10:48:17 -0700181 switch (patch->sources[0].type) {
182 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700183 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700184 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
185 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700186 status = BAD_VALUE;
187 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700188 }
189 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700190 // support only one sink if connection to a mix or across HW modules
191 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700192 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
193 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700194 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700195 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700196 status = INVALID_OPERATION;
197 goto exit;
198 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700199 // reject connection to different sink types
200 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700201 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700202 status = BAD_VALUE;
203 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700204 }
Eric Laurent951f4552014-05-20 10:48:17 -0700205 }
206
Eric Laurent3bcf8592015-04-03 12:13:24 -0700207 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700208 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700209 // - Device to device AND
210 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700211 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700212 if ((patch->num_sources == 2) ||
213 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
214 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700215 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800216 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
217 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700218 if (patch->num_sources == 2) {
219 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700220 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
221 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700222 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700223 status = INVALID_OPERATION;
224 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700225 }
Eric Laurent83b88082014-06-20 18:31:16 -0700226
227 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700228 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700229 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700230 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700231 status = INVALID_OPERATION;
232 goto exit;
233 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700234 // existing playback thread is reused, so it is not closed when patch is cleared
235 newPatch.mPlayback.setThread(
236 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700237 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700238 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700239 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700240 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
241 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
242 config.sample_rate = patch->sinks[0].sample_rate;
243 }
244 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
245 config.channel_mask = patch->sinks[0].channel_mask;
246 }
247 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
248 config.format = patch->sinks[0].format;
249 }
250 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
251 flags = patch->sinks[0].flags.output;
252 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700253 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800254 patch->sinks[0].ext.device.hw_module,
255 &output,
256 &config,
juyuchen2224c5a2019-01-21 12:00:58 +0800257 outputDevice,
258 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700259 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700260 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
261 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700262 status = NO_MEMORY;
263 goto exit;
264 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700265 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700266 }
Eric Laurent83b88082014-06-20 18:31:16 -0700267 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700268 String8 address = String8(patch->sources[0].ext.device.address);
269 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700270 // open input stream with source device audio properties if provided or
271 // default to peer output stream properties otherwise.
272 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
273 config.sample_rate = patch->sources[0].sample_rate;
274 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700275 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700276 }
277 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
278 config.channel_mask = patch->sources[0].channel_mask;
279 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700280 config.channel_mask = audio_channel_in_mask_from_count(
281 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700282 }
283 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
284 config.format = patch->sources[0].format;
285 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700286 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700287 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700288 audio_input_flags_t flags =
289 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
290 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700291 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700292 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700293 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700294 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 device,
296 address,
297 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800298 flags,
299 outputDevice,
300 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700301 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700302 thread.get(), config.channel_mask);
303 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700304 status = NO_MEMORY;
305 goto exit;
306 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700307 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700308 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700309 if (status != NO_ERROR) {
310 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700311 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700312 if (audioHwDevice->isInsert()) {
313 insertedModule = audioHwDevice->handle();
314 }
Eric Laurent951f4552014-05-20 10:48:17 -0700315 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700316 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700317 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700318 patch->sinks[0].ext.mix.handle);
319 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700320 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800321 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700322 ALOGW("%s() bad capture I/O handle %d",
323 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800324 status = BAD_VALUE;
325 goto exit;
326 }
Eric Laurent83b88082014-06-20 18:31:16 -0700327 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700328 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800329 if (status == NO_ERROR) {
330 newPatch.setThread(thread);
331 }
332
Eric Laurent526aa572019-01-15 10:54:58 -0800333 // remove stale audio patch with same input as sink if any
334 for (auto& iter : mPatches) {
335 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800336 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800337 break;
338 }
339 }
Eric Laurent83b88082014-06-20 18:31:16 -0700340 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700341 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
342 status = hwDevice->createAudioPatch(patch->num_sources,
343 patch->sources,
344 patch->num_sinks,
345 patch->sinks,
346 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700347 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700348 }
Eric Laurent951f4552014-05-20 10:48:17 -0700349 }
350 } break;
351 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700352 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700353 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700354 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700355 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700356 status = BAD_VALUE;
357 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700358 }
359 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700360 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700361 for (unsigned int i = 0; i < patch->num_sinks; i++) {
362 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700363 ALOGW("%s() invalid sink type %d for mix source",
364 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700365 status = BAD_VALUE;
366 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700367 }
368 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700369 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700370 status = BAD_VALUE;
371 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700372 }
jiabinc52b1ff2019-10-31 17:20:42 -0700373 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
374 patch->sinks[i].ext.device.type);
375 device->setAddress(patch->sinks[i].ext.device.address);
376 device->applyAudioPortConfig(&patch->sinks[i]);
377 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700378 }
Eric Laurent951f4552014-05-20 10:48:17 -0700379 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700381 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700382 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800383 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700384 ALOGW("%s() bad playback I/O handle %d",
385 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800386 status = BAD_VALUE;
387 goto exit;
388 }
Eric Laurent951f4552014-05-20 10:48:17 -0700389 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700390 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700391 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700392 }
393
Eric Laurent054d9d32015-04-24 08:48:48 -0700394 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800395 if (status == NO_ERROR) {
396 newPatch.setThread(thread);
397 }
Eric Laurent526aa572019-01-15 10:54:58 -0800398
399 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200400 // Prevent to remove endpoint patches (involved in a SwBridge)
401 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
402 if (!endpointPatch) {
403 for (auto& iter : mPatches) {
404 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
405 !iter.second.mIsEndpointPatch) {
406 erasePatch(iter.first);
407 break;
408 }
Eric Laurent526aa572019-01-15 10:54:58 -0800409 }
410 }
Eric Laurent951f4552014-05-20 10:48:17 -0700411 } break;
412 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700413 status = BAD_VALUE;
414 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700415 }
Eric Laurent83b88082014-06-20 18:31:16 -0700416exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700417 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700418 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700419 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
420 newPatch.mHalHandle = halHandle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800421 mAudioFlinger.mDeviceEffectManager.createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700422 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent029e33e2020-12-23 18:19:44 +0100423 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700424 }
Eric Laurent6d604a52021-01-05 16:30:04 +0100425 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700426 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700427 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700428 }
429 return status;
430}
431
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700432AudioFlinger::PatchPanel::Patch::~Patch()
433{
434 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
435 mRecord.handle(), mPlayback.handle());
436}
437
Mikhail Naganovdea53042018-04-26 13:10:21 -0700438status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700439{
440 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700441 status_t status = panel->createAudioPatch(
442 PatchBuilder().addSource(mAudioPatch.sources[0]).
443 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200444 mRecord.handlePtr(),
445 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700446 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700447 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700448 return status;
449 }
450
451 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700452 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700453 status = panel->createAudioPatch(
454 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200455 mPlayback.handlePtr(),
456 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700457 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700458 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700459 return status;
460 }
461 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700462 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700463 }
464
Eric Laurent83b88082014-06-20 18:31:16 -0700465 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700466 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700467 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700468 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
469 uint32_t sampleRate = mPlayback.thread()->sampleRate();
470 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700471
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700472 audio_format_t inputFormat = mRecord.thread()->format();
473 if (!audio_is_linear_pcm(inputFormat)) {
474 // The playbackThread format will say PCM for IEC61937 packetized stream.
475 // Use recordThread format.
476 format = inputFormat;
477 }
478 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
479 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700480 if (sampleRate == mRecord.thread()->sampleRate() &&
481 inChannelMask == mRecord.thread()->channelMask() &&
482 mRecord.thread()->fastTrackAvailable() &&
483 mRecord.thread()->hasFastCapture()) {
484 // Create a fast track if the record thread has fast capture to get better performance.
485 // Only enable fast mode when there is no resample needed.
486 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
487 } else {
488 // Fast mode is not available in this case.
489 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
490 }
Eric Laurent83b88082014-06-20 18:31:16 -0700491
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700492 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
493 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700494 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
495 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
496 // "reuse one existing output mix" case
497 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
498 }
jiabin01c8f562018-07-19 17:47:28 -0700499 if (mPlayback.thread()->hasFastMixer()) {
500 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700501 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700502 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700503 } else {
504 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700505 }
506
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700507 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800508 const bool usePassthruPatchRecord =
509 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100510 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
511 const size_t recordFrameCount = mRecord.thread()->frameCount();
512 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800513 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100514 // PassthruPatchRecord producesBufferOnDemand, so use
515 // maximum of playback and record thread framecounts
516 frameCount = std::max(playbackFrameCount, recordFrameCount);
517 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
518 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700519 tempRecordTrack = new RecordThread::PassthruPatchRecord(
520 mRecord.thread().get(),
521 sampleRate,
522 inChannelMask,
523 format,
524 frameCount,
525 inputFlags);
526 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100527 // use a pseudo LCM between input and output framecount
528 int playbackShift = __builtin_ctz(playbackFrameCount);
529 int shift = __builtin_ctz(recordFrameCount);
530 if (playbackShift < shift) {
531 shift = playbackShift;
532 }
533 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
534 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
535 __func__, playbackFrameCount, recordFrameCount, frameCount);
536
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700537 tempRecordTrack = new RecordThread::PatchRecord(
538 mRecord.thread().get(),
539 sampleRate,
540 inChannelMask,
541 format,
542 frameCount,
543 nullptr,
544 (size_t)0 /* bufferSize */,
545 inputFlags);
546 }
547 status = mRecord.checkTrack(tempRecordTrack.get());
548 if (status != NO_ERROR) {
549 return status;
550 }
551
Eric Laurent83b88082014-06-20 18:31:16 -0700552 // create a special playback track to render to playback thread.
553 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700554 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700555 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700556 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700557 sampleRate,
558 outChannelMask,
559 format,
560 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700561 tempRecordTrack->buffer(),
562 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700563 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700564 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700565 if (status != NO_ERROR) {
566 return status;
567 }
Eric Laurent83b88082014-06-20 18:31:16 -0700568
569 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800570 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
571 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
572 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
573 // is alive. There is no need to hold a reference, and there is no need
574 // to clear it. In fact, since playback stopping is asynchronous, there is
575 // no proper time when clearing could be done.
576 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
577 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700578
579 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700580 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
581 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700582
583 return status;
584}
585
Mikhail Naganovdea53042018-04-26 13:10:21 -0700586void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700587{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700588 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
589 __func__, mRecord.handle(), mPlayback.handle());
590 mRecord.stopTrack();
591 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700592 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700593 mRecord.closeConnections(panel);
594 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700595}
596
Andy Hungc3ab7732018-06-01 13:46:29 -0700597status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
598{
599 if (!isSoftware()) return INVALID_OPERATION;
600
601 auto recordTrack = mRecord.const_track();
602 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
603
604 auto playbackTrack = mPlayback.const_track();
605 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
606
607 // Latency information for tracks may be called without obtaining
608 // the underlying thread lock.
609 //
610 // We use record server latency + playback track latency (generally smaller than the
611 // reverse due to internal biases).
612 //
613 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700614
Andy Hung30282562018-08-08 18:27:03 -0700615 // For PCM tracks get server latency.
616 if (audio_is_linear_pcm(recordTrack->format())) {
617 double recordServerLatencyMs, playbackTrackLatencyMs;
618 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
619 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
620 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
621 return OK;
622 }
623 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700624
Andy Hung30282562018-08-08 18:27:03 -0700625 // See if kernel latencies are available.
626 // If so, do a frame diff and time difference computation to estimate
627 // the total patch latency. This requires that frame counts are reported by the
628 // HAL are matched properly in the case of record overruns and playback underruns.
629 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
630 recordTrack->getKernelFrameTime(&recordFT);
631 playbackTrack->getKernelFrameTime(&playFT);
632 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
633 const int64_t frameDiff = recordFT.frames - playFT.frames;
634 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
635
636 // It is possible that the patch track and patch record have a large time disparity because
637 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
638 // time difference based on how often we expect the timestamps to update in normal operation
639 // (typical should be no more than 50 ms).
640 //
641 // If the timestamps aren't sampled close enough, the patch latency is not
642 // considered valid.
643 //
644 // TODO: change this based on more experiments.
645 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
646 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
647 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
648 - timeDiffNs * 1e-6;
649 return OK;
650 }
651 }
652
653 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700654}
655
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700656String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700657{
Andy Hungc3ab7732018-06-01 13:46:29 -0700658 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800659 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
660 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
661 mRecord.const_thread().get(), mPlayback.const_thread().get());
662
663 bool hasSinkDevice =
664 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
665 bool hasSourceDevice =
666 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
667 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
668 hasSinkDevice ? "num sinks" :
669 (hasSourceDevice ? "num sources" : "no devices"),
670 hasSinkDevice ? mAudioPatch.num_sinks :
671 (hasSourceDevice ? mAudioPatch.num_sources : 0),
672 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
673 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700674
675 // add latency if it exists
676 double latencyMs;
677 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700678 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700679 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700680 return result;
681}
682
Eric Laurent951f4552014-05-20 10:48:17 -0700683/* Disconnect a patch */
684status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
685{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700686 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700687 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700688
Mikhail Naganovdea53042018-04-26 13:10:21 -0700689 auto iter = mPatches.find(handle);
690 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700691 return BAD_VALUE;
692 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700693 Patch &removedPatch = iter->second;
694 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700695
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700696 const struct audio_port_config &src = patch.sources[0];
697 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700698 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700699 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
700 if (hwDevice == 0) {
701 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700702 status = BAD_VALUE;
703 break;
704 }
Eric Laurent83b88082014-06-20 18:31:16 -0700705
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700706 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700707 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700708 break;
709 }
710
Mikhail Naganovdea53042018-04-26 13:10:21 -0700711 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
712 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
713 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700714 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700715 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800716 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700717 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800718 status = BAD_VALUE;
719 break;
720 }
Eric Laurent951f4552014-05-20 10:48:17 -0700721 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700722 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700723 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700724 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700725 }
726 } break;
727 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700728 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
729 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700730 status = BAD_VALUE;
731 break;
732 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700733 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700734 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700735 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700736 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800737 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700738 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800739 status = BAD_VALUE;
740 break;
741 }
Eric Laurent951f4552014-05-20 10:48:17 -0700742 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700743 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700744 } break;
745 default:
746 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700747 }
748
Eric Laurentb82e6b72019-11-22 17:25:04 -0800749 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700750 return status;
751}
752
Eric Laurentb82e6b72019-11-22 17:25:04 -0800753void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
754 mPatches.erase(handle);
755 removeSoftwarePatchFromInsertedModules(handle);
756 mAudioFlinger.mDeviceEffectManager.releaseAudioPatch(handle);
757}
758
Eric Laurent951f4552014-05-20 10:48:17 -0700759/* List connected audio ports and they attributes */
760status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
761 struct audio_patch *patches __unused)
762{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700763 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700764 return NO_ERROR;
765}
766
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700767status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
768 audio_io_handle_t stream,
769 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
770{
771 for (const auto& module : mInsertedModules) {
772 if (module.second.streams.count(stream)) {
773 for (const auto& patchHandle : module.second.sw_patches) {
774 const auto& patch_iter = mPatches.find(patchHandle);
775 if (patch_iter != mPatches.end()) {
776 const Patch &patch = patch_iter->second;
777 patches->emplace_back(*this, patchHandle,
778 patch.mPlayback.const_thread()->id(),
779 patch.mRecord.const_thread()->id());
780 } else {
781 ALOGE("Stale patch handle in the cache: %d", patchHandle);
782 }
783 }
784 return OK;
785 }
786 }
787 // The stream is not associated with any of inserted modules.
788 return BAD_VALUE;
789}
790
791void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent029e33e2020-12-23 18:19:44 +0100792 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700793{
794 if (audioHwDevice->isInsert()) {
795 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent029e33e2020-12-23 18:19:44 +0100796 if (patch != nullptr) {
797 std::vector <SoftwarePatch> swPatches;
798 getDownstreamSoftwarePatches(stream, &swPatches);
799 if (swPatches.size() > 0) {
800 auto iter = mPatches.find(swPatches[0].getPatchHandle());
801 if (iter != mPatches.end()) {
802 *patch = iter->second.mAudioPatch;
803 }
804 }
805 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700806 }
807}
808
809void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
810{
811 for (auto& module : mInsertedModules) {
812 module.second.streams.erase(stream);
813 }
814}
815
816AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700817{
818 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
819 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
820 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700821 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700822 return nullptr;
823 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700824 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700825}
826
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700827sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700828{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700829 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
830 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
831}
832
833void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent029e33e2020-12-23 18:19:44 +0100834 audio_module_handle_t module, audio_patch_handle_t handle,
835 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700836{
837 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent029e33e2020-12-23 18:19:44 +0100838 if (!mInsertedModules[module].streams.empty()) {
839 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
840 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700841}
842
843void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
844 audio_patch_handle_t handle)
845{
846 for (auto& module : mInsertedModules) {
847 module.second.sw_patches.erase(handle);
848 }
849}
850
851void AudioFlinger::PatchPanel::dump(int fd) const
852{
853 String8 patchPanelDump;
854 const char *indent = " ";
855
Mikhail Naganov201369b2018-05-16 16:52:32 -0700856 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700857 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800858 if (!headerPrinted) {
859 patchPanelDump += "\nPatches:\n";
860 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700861 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800862 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700863 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700864
865 headerPrinted = false;
866 for (const auto& module : mInsertedModules) {
867 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
868 if (!headerPrinted) {
869 patchPanelDump += "\nTracked inserted modules:\n";
870 headerPrinted = true;
871 }
872 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
873 for (const auto& stream : module.second.streams) {
874 moduleDump.appendFormat("%d ", stream);
875 }
876 moduleDump.append("; SW Patches: ");
877 for (const auto& patch : module.second.sw_patches) {
878 moduleDump.appendFormat("%d ", patch);
879 }
880 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
881 }
882 }
883
884 if (!patchPanelDump.isEmpty()) {
885 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700886 }
887}
888
Glenn Kasten63238ef2015-03-02 15:50:29 -0800889} // namespace android