blob: 93118b86b5da1ce283910d87b1c24f5e3b26b3cc [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>
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080028#include <media/AudioValidator.h>
jiabinc52b1ff2019-10-31 17:20:42 -070029#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070030#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070031#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070032
33// ----------------------------------------------------------------------------
34
35// Note: the following macro is used for extremely verbose logging message. In
36// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
37// 0; but one side effect of this is to turn all LOGV's as well. Some messages
38// are so verbose that we want to suppress them even when we have ALOG_ASSERT
39// turned on. Do not uncomment the #def below unless you really know what you
40// are doing and want to see all of the extremely verbose messages.
41//#define VERY_VERY_VERBOSE_LOGGING
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
47
48namespace android {
49
50/* List connected audio ports and their attributes */
51status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
52 struct audio_port *ports)
53{
54 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070055 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070056}
57
58/* Get supported attributes for a given audio port */
jiabinb4fed192020-09-22 14:45:40 -070059status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) {
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080060 status_t status = AudioValidator::validateAudioPort(*port);
61 if (status != NO_ERROR) {
62 return status;
63 }
64
Eric Laurent951f4552014-05-20 10:48:17 -070065 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070066 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070067}
68
Eric Laurent951f4552014-05-20 10:48:17 -070069/* Connect a patch between several source and sink ports */
70status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
71 audio_patch_handle_t *handle)
72{
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080073 status_t status = AudioValidator::validateAudioPatch(*patch);
74 if (status != NO_ERROR) {
75 return status;
76 }
77
Eric Laurent951f4552014-05-20 10:48:17 -070078 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070079 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070080}
81
82/* Disconnect a patch */
83status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
84{
85 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070086 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070087}
88
Eric Laurent951f4552014-05-20 10:48:17 -070089/* List connected audio ports and they attributes */
90status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
91 struct audio_patch *patches)
92{
93 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070094 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070095}
96
Mikhail Naganovadca70f2018-07-09 12:49:25 -070097status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const
98{
99 const auto& iter = mPatchPanel.mPatches.find(mPatchHandle);
100 if (iter != mPatchPanel.mPatches.end()) {
101 return iter->second.getLatencyMs(latencyMs);
102 } else {
103 return BAD_VALUE;
104 }
105}
106
Eric Laurent951f4552014-05-20 10:48:17 -0700107/* List connected audio ports and their attributes */
108status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
109 struct audio_port *ports __unused)
110{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700111 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700112 return NO_ERROR;
113}
114
115/* Get supported attributes for a given audio port */
jiabinb4fed192020-09-22 14:45:40 -0700116status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port)
Eric Laurent951f4552014-05-20 10:48:17 -0700117{
jiabinb4fed192020-09-22 14:45:40 -0700118 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
119 // Only query the HAL when the port is a device.
120 // TODO: implement getAudioPort for mix.
121 return INVALID_OPERATION;
122 }
123 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
124 if (hwDevice == nullptr) {
125 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
126 return BAD_VALUE;
127 }
128 if (!hwDevice->supportsAudioPatches()) {
129 return INVALID_OPERATION;
130 }
131 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700132}
133
Eric Laurent951f4552014-05-20 10:48:17 -0700134/* Connect a patch between several source and sink ports */
135status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200136 audio_patch_handle_t *handle,
137 bool endpointPatch)
Eric Laurent951f4552014-05-20 10:48:17 -0700138{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700139 if (handle == NULL || patch == NULL) {
140 return BAD_VALUE;
141 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700142 ALOGV("%s() num_sources %d num_sinks %d handle %d",
143 __func__, patch->num_sources, patch->num_sinks, *handle);
144 status_t status = NO_ERROR;
145 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700146
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700147 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700148 return BAD_VALUE;
149 }
Eric Laurent874c42872014-08-08 15:13:39 -0700150 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
151 // only the audio policy manager can request a patch creation with 2 sources.
152 if (patch->num_sources > 2) {
153 return INVALID_OPERATION;
154 }
Eric Laurent951f4552014-05-20 10:48:17 -0700155
Eric Laurent83b88082014-06-20 18:31:16 -0700156 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700157 auto iter = mPatches.find(*handle);
158 if (iter != mPatches.end()) {
159 ALOGV("%s() removing patch handle %d", __func__, *handle);
160 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700161 // free resources owned by the removed patch if applicable
162 // 1) if a software patch is present, release the playback and capture threads and
163 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700164 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700165 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700166 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700167 // 2) if the new patch and old patch source or sink are devices from different
168 // hw modules, clear the audio HAL patches now because they will not be updated
169 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700170 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
172 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
173 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
174 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
175 oldPatch.sources[0].ext.device.hw_module !=
176 patch->sources[0].ext.device.hw_module)) {
177 hwModule = oldPatch.sources[0].ext.device.hw_module;
178 } else if (patch->num_sinks == 0 ||
179 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
180 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
181 oldPatch.sinks[0].ext.device.hw_module !=
182 patch->sinks[0].ext.device.hw_module))) {
183 // Note on (patch->num_sinks == 0): this situation should not happen as
184 // these special patches are only created by the policy manager but just
185 // in case, systematically clear the HAL patch.
186 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700187 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700188 hwModule = oldPatch.sinks[0].ext.device.hw_module;
189 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700190 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
191 if (hwDevice != 0) {
192 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700193 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800194 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700195 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800196 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700197 }
198 }
199
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200200 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700201 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700202
Eric Laurent951f4552014-05-20 10:48:17 -0700203 switch (patch->sources[0].type) {
204 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700205 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700206 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
207 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700208 status = BAD_VALUE;
209 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700210 }
211 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700212 // support only one sink if connection to a mix or across HW modules
213 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700214 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
215 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700216 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700217 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700218 status = INVALID_OPERATION;
219 goto exit;
220 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700221 // reject connection to different sink types
222 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700223 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700224 status = BAD_VALUE;
225 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700226 }
Eric Laurent951f4552014-05-20 10:48:17 -0700227 }
228
Eric Laurent3bcf8592015-04-03 12:13:24 -0700229 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700230 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700231 // - Device to device AND
232 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700233 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700234 if ((patch->num_sources == 2) ||
235 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
236 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700237 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800238 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
239 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700240 if (patch->num_sources == 2) {
241 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700242 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
243 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700244 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700245 status = INVALID_OPERATION;
246 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700247 }
Eric Laurent83b88082014-06-20 18:31:16 -0700248
249 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700250 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700251 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700252 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700253 status = INVALID_OPERATION;
254 goto exit;
255 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700256 // existing playback thread is reused, so it is not closed when patch is cleared
257 newPatch.mPlayback.setThread(
258 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700259 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700260 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200261 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700262 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700263 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
264 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
265 config.sample_rate = patch->sinks[0].sample_rate;
266 }
267 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
268 config.channel_mask = patch->sinks[0].channel_mask;
269 }
270 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
271 config.format = patch->sinks[0].format;
272 }
273 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
274 flags = patch->sinks[0].flags.output;
275 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700276 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800277 patch->sinks[0].ext.device.hw_module,
278 &output,
279 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200280 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800281 outputDevice,
282 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700283 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700284 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
285 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700286 status = NO_MEMORY;
287 goto exit;
288 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700289 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700290 }
Eric Laurent83b88082014-06-20 18:31:16 -0700291 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700292 String8 address = String8(patch->sources[0].ext.device.address);
293 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700294 // open input stream with source device audio properties if provided or
295 // default to peer output stream properties otherwise.
296 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
297 config.sample_rate = patch->sources[0].sample_rate;
298 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700299 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700300 }
301 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
302 config.channel_mask = patch->sources[0].channel_mask;
303 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700304 config.channel_mask = audio_channel_in_mask_from_count(
305 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700306 }
307 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
308 config.format = patch->sources[0].format;
309 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700310 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700311 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700312 audio_input_flags_t flags =
313 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
314 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700315 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700316 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700317 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700318 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700319 device,
320 address,
321 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800322 flags,
323 outputDevice,
324 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700325 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700326 thread.get(), config.channel_mask);
327 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700328 status = NO_MEMORY;
329 goto exit;
330 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700331 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700332 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700333 if (status != NO_ERROR) {
334 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700335 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700336 if (audioHwDevice->isInsert()) {
337 insertedModule = audioHwDevice->handle();
338 }
Eric Laurent951f4552014-05-20 10:48:17 -0700339 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700340 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700341 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700342 patch->sinks[0].ext.mix.handle);
343 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700344 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800345 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700346 ALOGW("%s() bad capture I/O handle %d",
347 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800348 status = BAD_VALUE;
349 goto exit;
350 }
Eric Laurent83b88082014-06-20 18:31:16 -0700351 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700352 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800353 if (status == NO_ERROR) {
354 newPatch.setThread(thread);
355 }
356
Eric Laurent526aa572019-01-15 10:54:58 -0800357 // remove stale audio patch with same input as sink if any
358 for (auto& iter : mPatches) {
359 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800360 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800361 break;
362 }
363 }
Eric Laurent83b88082014-06-20 18:31:16 -0700364 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700365 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
366 status = hwDevice->createAudioPatch(patch->num_sources,
367 patch->sources,
368 patch->num_sinks,
369 patch->sinks,
370 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700371 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700372 }
Eric Laurent951f4552014-05-20 10:48:17 -0700373 }
374 } break;
375 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700376 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700377 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700378 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700379 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700380 status = BAD_VALUE;
381 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700382 }
383 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700384 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700385 for (unsigned int i = 0; i < patch->num_sinks; i++) {
386 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700387 ALOGW("%s() invalid sink type %d for mix source",
388 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700389 status = BAD_VALUE;
390 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700391 }
392 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700393 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700394 status = BAD_VALUE;
395 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700396 }
jiabinc52b1ff2019-10-31 17:20:42 -0700397 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
398 patch->sinks[i].ext.device.type);
399 device->setAddress(patch->sinks[i].ext.device.address);
400 device->applyAudioPortConfig(&patch->sinks[i]);
401 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700402 }
Eric Laurent951f4552014-05-20 10:48:17 -0700403 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700404 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700405 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700406 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800407 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700408 ALOGW("%s() bad playback I/O handle %d",
409 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800410 status = BAD_VALUE;
411 goto exit;
412 }
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700414 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700415 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700416 }
417
Eric Laurent054d9d32015-04-24 08:48:48 -0700418 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800419 if (status == NO_ERROR) {
420 newPatch.setThread(thread);
421 }
Eric Laurent526aa572019-01-15 10:54:58 -0800422
423 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200424 // Prevent to remove endpoint patches (involved in a SwBridge)
425 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
426 if (!endpointPatch) {
427 for (auto& iter : mPatches) {
428 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
429 !iter.second.mIsEndpointPatch) {
430 erasePatch(iter.first);
431 break;
432 }
Eric Laurent526aa572019-01-15 10:54:58 -0800433 }
434 }
Eric Laurent951f4552014-05-20 10:48:17 -0700435 } break;
436 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700437 status = BAD_VALUE;
438 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700439 }
Eric Laurent83b88082014-06-20 18:31:16 -0700440exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700441 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700442 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700443 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
444 newPatch.mHalHandle = halHandle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800445 mAudioFlinger.mDeviceEffectManager.createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700446 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100447 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700448 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100449 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700450 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700451 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700452 }
453 return status;
454}
455
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700456AudioFlinger::PatchPanel::Patch::~Patch()
457{
458 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
459 mRecord.handle(), mPlayback.handle());
460}
461
Mikhail Naganovdea53042018-04-26 13:10:21 -0700462status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700463{
464 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700465 status_t status = panel->createAudioPatch(
466 PatchBuilder().addSource(mAudioPatch.sources[0]).
467 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200468 mRecord.handlePtr(),
469 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700470 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700471 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700472 return status;
473 }
474
475 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700476 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700477 status = panel->createAudioPatch(
478 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200479 mPlayback.handlePtr(),
480 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700481 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700482 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700483 return status;
484 }
485 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700486 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700487 }
488
Eric Laurent83b88082014-06-20 18:31:16 -0700489 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700490 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700491 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700492 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
493 uint32_t sampleRate = mPlayback.thread()->sampleRate();
494 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700495
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700496 audio_format_t inputFormat = mRecord.thread()->format();
497 if (!audio_is_linear_pcm(inputFormat)) {
498 // The playbackThread format will say PCM for IEC61937 packetized stream.
499 // Use recordThread format.
500 format = inputFormat;
501 }
502 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
503 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700504 if (sampleRate == mRecord.thread()->sampleRate() &&
505 inChannelMask == mRecord.thread()->channelMask() &&
506 mRecord.thread()->fastTrackAvailable() &&
507 mRecord.thread()->hasFastCapture()) {
508 // Create a fast track if the record thread has fast capture to get better performance.
509 // Only enable fast mode when there is no resample needed.
510 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
511 } else {
512 // Fast mode is not available in this case.
513 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
514 }
Eric Laurent83b88082014-06-20 18:31:16 -0700515
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700516 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
517 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700518 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
519 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
520 // "reuse one existing output mix" case
521 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
522 }
jiabin01c8f562018-07-19 17:47:28 -0700523 if (mPlayback.thread()->hasFastMixer()) {
524 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700525 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700526 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700527 } else {
528 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700529 }
530
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700531 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800532 const bool usePassthruPatchRecord =
533 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100534 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
535 const size_t recordFrameCount = mRecord.thread()->frameCount();
536 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800537 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100538 // PassthruPatchRecord producesBufferOnDemand, so use
539 // maximum of playback and record thread framecounts
540 frameCount = std::max(playbackFrameCount, recordFrameCount);
541 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
542 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700543 tempRecordTrack = new RecordThread::PassthruPatchRecord(
544 mRecord.thread().get(),
545 sampleRate,
546 inChannelMask,
547 format,
548 frameCount,
549 inputFlags);
550 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100551 // use a pseudo LCM between input and output framecount
552 int playbackShift = __builtin_ctz(playbackFrameCount);
553 int shift = __builtin_ctz(recordFrameCount);
554 if (playbackShift < shift) {
555 shift = playbackShift;
556 }
557 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
558 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
559 __func__, playbackFrameCount, recordFrameCount, frameCount);
560
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700561 tempRecordTrack = new RecordThread::PatchRecord(
562 mRecord.thread().get(),
563 sampleRate,
564 inChannelMask,
565 format,
566 frameCount,
567 nullptr,
568 (size_t)0 /* bufferSize */,
569 inputFlags);
570 }
571 status = mRecord.checkTrack(tempRecordTrack.get());
572 if (status != NO_ERROR) {
573 return status;
574 }
575
Eric Laurent83b88082014-06-20 18:31:16 -0700576 // create a special playback track to render to playback thread.
577 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700578 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700579 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700580 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700581 sampleRate,
582 outChannelMask,
583 format,
584 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700585 tempRecordTrack->buffer(),
586 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700587 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700588 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700589 if (status != NO_ERROR) {
590 return status;
591 }
Eric Laurent83b88082014-06-20 18:31:16 -0700592
593 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800594 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
595 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
596 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
597 // is alive. There is no need to hold a reference, and there is no need
598 // to clear it. In fact, since playback stopping is asynchronous, there is
599 // no proper time when clearing could be done.
600 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
601 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700602
603 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700604 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
605 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700606
607 return status;
608}
609
Mikhail Naganovdea53042018-04-26 13:10:21 -0700610void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700611{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700612 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
613 __func__, mRecord.handle(), mPlayback.handle());
614 mRecord.stopTrack();
615 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700616 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700617 mRecord.closeConnections(panel);
618 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700619}
620
Andy Hungc3ab7732018-06-01 13:46:29 -0700621status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
622{
623 if (!isSoftware()) return INVALID_OPERATION;
624
625 auto recordTrack = mRecord.const_track();
626 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
627
628 auto playbackTrack = mPlayback.const_track();
629 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
630
631 // Latency information for tracks may be called without obtaining
632 // the underlying thread lock.
633 //
634 // We use record server latency + playback track latency (generally smaller than the
635 // reverse due to internal biases).
636 //
637 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700638
Andy Hung30282562018-08-08 18:27:03 -0700639 // For PCM tracks get server latency.
640 if (audio_is_linear_pcm(recordTrack->format())) {
641 double recordServerLatencyMs, playbackTrackLatencyMs;
642 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
643 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
644 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
645 return OK;
646 }
647 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700648
Andy Hung30282562018-08-08 18:27:03 -0700649 // See if kernel latencies are available.
650 // If so, do a frame diff and time difference computation to estimate
651 // the total patch latency. This requires that frame counts are reported by the
652 // HAL are matched properly in the case of record overruns and playback underruns.
653 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
654 recordTrack->getKernelFrameTime(&recordFT);
655 playbackTrack->getKernelFrameTime(&playFT);
656 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
657 const int64_t frameDiff = recordFT.frames - playFT.frames;
658 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
659
660 // It is possible that the patch track and patch record have a large time disparity because
661 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
662 // time difference based on how often we expect the timestamps to update in normal operation
663 // (typical should be no more than 50 ms).
664 //
665 // If the timestamps aren't sampled close enough, the patch latency is not
666 // considered valid.
667 //
668 // TODO: change this based on more experiments.
669 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
670 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
671 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
672 - timeDiffNs * 1e-6;
673 return OK;
674 }
675 }
676
677 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700678}
679
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700680String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700681{
Andy Hungc3ab7732018-06-01 13:46:29 -0700682 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800683 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
684 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
685 mRecord.const_thread().get(), mPlayback.const_thread().get());
686
687 bool hasSinkDevice =
688 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
689 bool hasSourceDevice =
690 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
691 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
692 hasSinkDevice ? "num sinks" :
693 (hasSourceDevice ? "num sources" : "no devices"),
694 hasSinkDevice ? mAudioPatch.num_sinks :
695 (hasSourceDevice ? mAudioPatch.num_sources : 0),
696 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
697 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700698
699 // add latency if it exists
700 double latencyMs;
701 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700702 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700703 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700704 return result;
705}
706
Eric Laurent951f4552014-05-20 10:48:17 -0700707/* Disconnect a patch */
708status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
709{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700710 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700711 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700712
Mikhail Naganovdea53042018-04-26 13:10:21 -0700713 auto iter = mPatches.find(handle);
714 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700715 return BAD_VALUE;
716 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700717 Patch &removedPatch = iter->second;
718 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700719
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700720 const struct audio_port_config &src = patch.sources[0];
721 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700722 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700723 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
724 if (hwDevice == 0) {
725 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700726 status = BAD_VALUE;
727 break;
728 }
Eric Laurent83b88082014-06-20 18:31:16 -0700729
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700730 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700731 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700732 break;
733 }
734
Mikhail Naganovdea53042018-04-26 13:10:21 -0700735 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
736 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
737 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700738 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700739 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800740 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700741 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800742 status = BAD_VALUE;
743 break;
744 }
Eric Laurent951f4552014-05-20 10:48:17 -0700745 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700746 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700747 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700748 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700749 }
750 } break;
751 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700752 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
753 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700754 status = BAD_VALUE;
755 break;
756 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700757 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700758 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700759 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700760 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800761 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700762 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800763 status = BAD_VALUE;
764 break;
765 }
Eric Laurent951f4552014-05-20 10:48:17 -0700766 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700767 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700768 } break;
769 default:
770 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700771 }
772
Eric Laurentb82e6b72019-11-22 17:25:04 -0800773 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700774 return status;
775}
776
Eric Laurentb82e6b72019-11-22 17:25:04 -0800777void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
778 mPatches.erase(handle);
779 removeSoftwarePatchFromInsertedModules(handle);
780 mAudioFlinger.mDeviceEffectManager.releaseAudioPatch(handle);
781}
782
Eric Laurent951f4552014-05-20 10:48:17 -0700783/* List connected audio ports and they attributes */
784status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
785 struct audio_patch *patches __unused)
786{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700787 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700788 return NO_ERROR;
789}
790
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700791status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
792 audio_io_handle_t stream,
793 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
794{
795 for (const auto& module : mInsertedModules) {
796 if (module.second.streams.count(stream)) {
797 for (const auto& patchHandle : module.second.sw_patches) {
798 const auto& patch_iter = mPatches.find(patchHandle);
799 if (patch_iter != mPatches.end()) {
800 const Patch &patch = patch_iter->second;
801 patches->emplace_back(*this, patchHandle,
802 patch.mPlayback.const_thread()->id(),
803 patch.mRecord.const_thread()->id());
804 } else {
805 ALOGE("Stale patch handle in the cache: %d", patchHandle);
806 }
807 }
808 return OK;
809 }
810 }
811 // The stream is not associated with any of inserted modules.
812 return BAD_VALUE;
813}
814
815void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100816 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700817{
818 if (audioHwDevice->isInsert()) {
819 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100820 if (patch != nullptr) {
821 std::vector <SoftwarePatch> swPatches;
822 getDownstreamSoftwarePatches(stream, &swPatches);
823 if (swPatches.size() > 0) {
824 auto iter = mPatches.find(swPatches[0].getPatchHandle());
825 if (iter != mPatches.end()) {
826 *patch = iter->second.mAudioPatch;
827 }
828 }
829 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700830 }
831}
832
833void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
834{
835 for (auto& module : mInsertedModules) {
836 module.second.streams.erase(stream);
837 }
838}
839
840AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700841{
842 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
843 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
844 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700845 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700846 return nullptr;
847 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700848 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700849}
850
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700851sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700852{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700853 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
854 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
855}
856
857void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100858 audio_module_handle_t module, audio_patch_handle_t handle,
859 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700860{
861 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100862 if (!mInsertedModules[module].streams.empty()) {
863 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
864 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700865}
866
867void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
868 audio_patch_handle_t handle)
869{
870 for (auto& module : mInsertedModules) {
871 module.second.sw_patches.erase(handle);
872 }
873}
874
875void AudioFlinger::PatchPanel::dump(int fd) const
876{
877 String8 patchPanelDump;
878 const char *indent = " ";
879
Mikhail Naganov201369b2018-05-16 16:52:32 -0700880 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700881 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800882 if (!headerPrinted) {
883 patchPanelDump += "\nPatches:\n";
884 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700885 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800886 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700887 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700888
889 headerPrinted = false;
890 for (const auto& module : mInsertedModules) {
891 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
892 if (!headerPrinted) {
893 patchPanelDump += "\nTracked inserted modules:\n";
894 headerPrinted = true;
895 }
896 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
897 for (const auto& stream : module.second.streams) {
898 moduleDump.appendFormat("%d ", stream);
899 }
900 moduleDump.append("; SW Patches: ");
901 for (const auto& patch : module.second.sw_patches) {
902 moduleDump.appendFormat("%d ", patch);
903 }
904 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
905 }
906 }
907
908 if (!patchPanelDump.isEmpty()) {
909 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700910 }
911}
912
Glenn Kasten63238ef2015-03-02 15:50:29 -0800913} // namespace android