blob: b84d32c7ef22794139feb2eb35a30af6e2abdb52 [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 Laurent1e28aaa2023-04-16 19:34:23 +0200138 //unlocks AudioFlinger::mLock when calling ThreadBase::sendCreateAudioPatchConfigEvent
139 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
140 //before processing the create patch request.
141 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700142{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700143 if (handle == NULL || patch == NULL) {
144 return BAD_VALUE;
145 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700146 ALOGV("%s() num_sources %d num_sinks %d handle %d",
147 __func__, patch->num_sources, patch->num_sinks, *handle);
148 status_t status = NO_ERROR;
149 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700150
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700151 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700152 return BAD_VALUE;
153 }
Eric Laurent874c42872014-08-08 15:13:39 -0700154 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
155 // only the audio policy manager can request a patch creation with 2 sources.
156 if (patch->num_sources > 2) {
157 return INVALID_OPERATION;
158 }
François Gaffie58e73af2023-02-15 11:47:24 +0100159 bool reuseExistingHalPatch = false;
160 audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700161 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700162 auto iter = mPatches.find(*handle);
163 if (iter != mPatches.end()) {
164 ALOGV("%s() removing patch handle %d", __func__, *handle);
165 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700166 // free resources owned by the removed patch if applicable
167 // 1) if a software patch is present, release the playback and capture threads and
168 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700169 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700170 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700171 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 // 2) if the new patch and old patch source or sink are devices from different
173 // hw modules, clear the audio HAL patches now because they will not be updated
174 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700175 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700176 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
177 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
François Gaffie58e73af2023-02-15 11:47:24 +0100178 oldhandle = *handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700179 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
180 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
181 oldPatch.sources[0].ext.device.hw_module !=
182 patch->sources[0].ext.device.hw_module)) {
183 hwModule = oldPatch.sources[0].ext.device.hw_module;
184 } else if (patch->num_sinks == 0 ||
185 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
186 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
187 oldPatch.sinks[0].ext.device.hw_module !=
188 patch->sinks[0].ext.device.hw_module))) {
189 // Note on (patch->num_sinks == 0): this situation should not happen as
190 // these special patches are only created by the policy manager but just
191 // in case, systematically clear the HAL patch.
192 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700193 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700194 hwModule = oldPatch.sinks[0].ext.device.hw_module;
195 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700196 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
197 if (hwDevice != 0) {
198 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700199 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800200 halHandle = removedPatch.mHalHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100201 // Prevent to remove/add device effect when mix / device did not change, and
202 // hal patch has not been released
203 // Note that no patch leak at hal layer as halHandle is reused.
204 reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700205 }
François Gaffie58e73af2023-02-15 11:47:24 +0100206 erasePatch(*handle, reuseExistingHalPatch);
Eric Laurent951f4552014-05-20 10:48:17 -0700207 }
208 }
209
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200210 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700211 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700212
Eric Laurent951f4552014-05-20 10:48:17 -0700213 switch (patch->sources[0].type) {
214 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700215 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700216 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
217 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700218 status = BAD_VALUE;
219 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700220 }
221 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700222 // support only one sink if connection to a mix or across HW modules
223 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700224 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
225 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700226 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700227 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700228 status = INVALID_OPERATION;
229 goto exit;
230 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700231 // reject connection to different sink types
232 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700233 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700234 status = BAD_VALUE;
235 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700236 }
Eric Laurent951f4552014-05-20 10:48:17 -0700237 }
238
Eric Laurent3bcf8592015-04-03 12:13:24 -0700239 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700240 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700241 // - Device to device AND
242 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700243 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700244 if ((patch->num_sources == 2) ||
245 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
246 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700247 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800248 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
249 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700250 if (patch->num_sources == 2) {
251 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700252 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
253 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700254 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700255 status = INVALID_OPERATION;
256 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700257 }
Eric Laurent83b88082014-06-20 18:31:16 -0700258 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700259 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700260 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700261 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700262 status = INVALID_OPERATION;
263 goto exit;
264 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700265 // existing playback thread is reused, so it is not closed when patch is cleared
266 newPatch.mPlayback.setThread(
267 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700268 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700269 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200270 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700271 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700272 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
273 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
274 config.sample_rate = patch->sinks[0].sample_rate;
275 }
276 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
277 config.channel_mask = patch->sinks[0].channel_mask;
278 }
279 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
280 config.format = patch->sinks[0].format;
281 }
282 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
283 flags = patch->sinks[0].flags.output;
284 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700285 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800286 patch->sinks[0].ext.device.hw_module,
287 &output,
288 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200289 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800290 outputDevice,
291 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700292 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700293 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
294 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700295 status = NO_MEMORY;
296 goto exit;
297 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700298 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700299 }
Eric Laurent83b88082014-06-20 18:31:16 -0700300 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700301 String8 address = String8(patch->sources[0].ext.device.address);
302 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700303 // open input stream with source device audio properties if provided or
304 // default to peer output stream properties otherwise.
305 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
306 config.sample_rate = patch->sources[0].sample_rate;
307 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700308 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700309 }
310 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
311 config.channel_mask = patch->sources[0].channel_mask;
312 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700313 config.channel_mask = audio_channel_in_mask_from_count(
314 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700315 }
316 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
317 config.format = patch->sources[0].format;
318 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700319 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700320 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700321 audio_input_flags_t flags =
322 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
323 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700324 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200325 audio_source_t source = AUDIO_SOURCE_MIC;
326 // For telephony patches, propagate voice communication use case to record side
327 if (patch->num_sources == 2
328 && patch->sources[1].ext.mix.usecase.stream
329 == AUDIO_STREAM_VOICE_CALL) {
330 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
331 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700332 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700333 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700334 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700335 device,
336 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200337 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800338 flags,
339 outputDevice,
340 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700341 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700342 thread.get(), config.channel_mask);
343 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700344 status = NO_MEMORY;
345 goto exit;
346 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700347 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700348 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700349 if (status != NO_ERROR) {
350 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700351 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700352 if (audioHwDevice->isInsert()) {
353 insertedModule = audioHwDevice->handle();
354 }
Eric Laurent951f4552014-05-20 10:48:17 -0700355 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700356 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700357 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700358 patch->sinks[0].ext.mix.handle);
359 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700360 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800361 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700362 ALOGW("%s() bad capture I/O handle %d",
363 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800364 status = BAD_VALUE;
365 goto exit;
366 }
Eric Laurent83b88082014-06-20 18:31:16 -0700367 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200368 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700369 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200370 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800371 if (status == NO_ERROR) {
372 newPatch.setThread(thread);
373 }
Eric Laurent526aa572019-01-15 10:54:58 -0800374 // remove stale audio patch with same input as sink if any
375 for (auto& iter : mPatches) {
376 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800377 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800378 break;
379 }
380 }
Eric Laurent83b88082014-06-20 18:31:16 -0700381 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700382 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
383 status = hwDevice->createAudioPatch(patch->num_sources,
384 patch->sources,
385 patch->num_sinks,
386 patch->sinks,
387 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700388 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700389 }
Eric Laurent951f4552014-05-20 10:48:17 -0700390 }
391 } break;
392 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700393 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700394 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700395 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700396 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700397 status = BAD_VALUE;
398 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700399 }
400 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700401 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700402 for (unsigned int i = 0; i < patch->num_sinks; i++) {
403 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700404 ALOGW("%s() invalid sink type %d for mix source",
405 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700406 status = BAD_VALUE;
407 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700408 }
409 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700410 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700411 status = BAD_VALUE;
412 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
jiabinc52b1ff2019-10-31 17:20:42 -0700414 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
415 patch->sinks[i].ext.device.type);
416 device->setAddress(patch->sinks[i].ext.device.address);
417 device->applyAudioPortConfig(&patch->sinks[i]);
418 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700419 }
Eric Laurent951f4552014-05-20 10:48:17 -0700420 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700421 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700422 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700423 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800424 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700425 ALOGW("%s() bad playback I/O handle %d",
426 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800427 status = BAD_VALUE;
428 goto exit;
429 }
Eric Laurent951f4552014-05-20 10:48:17 -0700430 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700431 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700432 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700433 }
434
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200435 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700436 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200437 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800438 if (status == NO_ERROR) {
439 newPatch.setThread(thread);
440 }
Eric Laurent526aa572019-01-15 10:54:58 -0800441
442 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200443 // Prevent to remove endpoint patches (involved in a SwBridge)
444 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
445 if (!endpointPatch) {
446 for (auto& iter : mPatches) {
447 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
448 !iter.second.mIsEndpointPatch) {
449 erasePatch(iter.first);
450 break;
451 }
Eric Laurent526aa572019-01-15 10:54:58 -0800452 }
453 }
Eric Laurent951f4552014-05-20 10:48:17 -0700454 } break;
455 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700456 status = BAD_VALUE;
457 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700458 }
Eric Laurent83b88082014-06-20 18:31:16 -0700459exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700460 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700461 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700462 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
463 newPatch.mHalHandle = halHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100464 if (reuseExistingHalPatch) {
465 mAudioFlinger.mPatchCommandThread->updateAudioPatch(oldhandle, *handle, newPatch);
466 } else {
467 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
468 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700469 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100470 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700471 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100472 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700473 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700474 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700475 }
476 return status;
477}
478
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700479AudioFlinger::PatchPanel::Patch::~Patch()
480{
481 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
482 mRecord.handle(), mPlayback.handle());
483}
484
Mikhail Naganovdea53042018-04-26 13:10:21 -0700485status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700486{
487 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700488 status_t status = panel->createAudioPatch(
489 PatchBuilder().addSource(mAudioPatch.sources[0]).
490 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200491 mRecord.handlePtr(),
492 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700493 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700494 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700495 return status;
496 }
497
498 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700499 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700500 status = panel->createAudioPatch(
501 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200502 mPlayback.handlePtr(),
503 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700504 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700505 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700506 return status;
507 }
508 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700509 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700510 }
511
Eric Laurent83b88082014-06-20 18:31:16 -0700512 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700513 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700514 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700515 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
516 uint32_t sampleRate = mPlayback.thread()->sampleRate();
517 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700518
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700519 audio_format_t inputFormat = mRecord.thread()->format();
520 if (!audio_is_linear_pcm(inputFormat)) {
521 // The playbackThread format will say PCM for IEC61937 packetized stream.
522 // Use recordThread format.
523 format = inputFormat;
524 }
525 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
526 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700527 if (sampleRate == mRecord.thread()->sampleRate() &&
528 inChannelMask == mRecord.thread()->channelMask() &&
529 mRecord.thread()->fastTrackAvailable() &&
530 mRecord.thread()->hasFastCapture()) {
531 // Create a fast track if the record thread has fast capture to get better performance.
532 // Only enable fast mode when there is no resample needed.
533 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
534 } else {
535 // Fast mode is not available in this case.
536 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
537 }
Eric Laurent83b88082014-06-20 18:31:16 -0700538
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700539 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
540 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700541 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200542 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700543 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
544 // "reuse one existing output mix" case
545 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200546 // For telephony patches, propagate voice communication use case to record side
547 if (streamType == AUDIO_STREAM_VOICE_CALL) {
548 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
549 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700550 }
jiabin01c8f562018-07-19 17:47:28 -0700551 if (mPlayback.thread()->hasFastMixer()) {
552 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700553 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700554 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700555 } else {
556 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700557 }
558
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700559 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800560 const bool usePassthruPatchRecord =
561 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100562 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
563 const size_t recordFrameCount = mRecord.thread()->frameCount();
564 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800565 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100566 // PassthruPatchRecord producesBufferOnDemand, so use
567 // maximum of playback and record thread framecounts
568 frameCount = std::max(playbackFrameCount, recordFrameCount);
569 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
570 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700571 tempRecordTrack = new RecordThread::PassthruPatchRecord(
572 mRecord.thread().get(),
573 sampleRate,
574 inChannelMask,
575 format,
576 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200577 inputFlags,
578 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700579 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100580 // use a pseudo LCM between input and output framecount
581 int playbackShift = __builtin_ctz(playbackFrameCount);
582 int shift = __builtin_ctz(recordFrameCount);
583 if (playbackShift < shift) {
584 shift = playbackShift;
585 }
586 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
587 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
588 __func__, playbackFrameCount, recordFrameCount, frameCount);
589
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700590 tempRecordTrack = new RecordThread::PatchRecord(
591 mRecord.thread().get(),
592 sampleRate,
593 inChannelMask,
594 format,
595 frameCount,
596 nullptr,
597 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200598 inputFlags,
599 {} /* timeout */,
600 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700601 }
602 status = mRecord.checkTrack(tempRecordTrack.get());
603 if (status != NO_ERROR) {
604 return status;
605 }
606
Eric Laurent83b88082014-06-20 18:31:16 -0700607 // create a special playback track to render to playback thread.
608 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200609
610 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
611 // it might glitch.
612 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
613 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
614 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700615 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700616 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700617 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700618 sampleRate,
619 outChannelMask,
620 format,
621 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700622 tempRecordTrack->buffer(),
623 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200624 outputFlags,
625 {} /*timeout*/,
626 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700627 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700628 if (status != NO_ERROR) {
629 return status;
630 }
Eric Laurent83b88082014-06-20 18:31:16 -0700631
632 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800633 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
634 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
635 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
636 // is alive. There is no need to hold a reference, and there is no need
637 // to clear it. In fact, since playback stopping is asynchronous, there is
638 // no proper time when clearing could be done.
639 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
640 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700641
642 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700643 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
644 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700645
646 return status;
647}
648
Mikhail Naganovdea53042018-04-26 13:10:21 -0700649void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700650{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700651 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
652 __func__, mRecord.handle(), mPlayback.handle());
653 mRecord.stopTrack();
654 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700655 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700656 mRecord.closeConnections(panel);
657 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700658}
659
Andy Hungc3ab7732018-06-01 13:46:29 -0700660status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
661{
662 if (!isSoftware()) return INVALID_OPERATION;
663
664 auto recordTrack = mRecord.const_track();
665 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
666
667 auto playbackTrack = mPlayback.const_track();
668 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
669
670 // Latency information for tracks may be called without obtaining
671 // the underlying thread lock.
672 //
673 // We use record server latency + playback track latency (generally smaller than the
674 // reverse due to internal biases).
675 //
676 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700677
Andy Hung30282562018-08-08 18:27:03 -0700678 // For PCM tracks get server latency.
679 if (audio_is_linear_pcm(recordTrack->format())) {
680 double recordServerLatencyMs, playbackTrackLatencyMs;
681 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
682 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
683 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
684 return OK;
685 }
686 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700687
Andy Hung30282562018-08-08 18:27:03 -0700688 // See if kernel latencies are available.
689 // If so, do a frame diff and time difference computation to estimate
690 // the total patch latency. This requires that frame counts are reported by the
691 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700692 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700693 recordTrack->getKernelFrameTime(&recordFT);
694 playbackTrack->getKernelFrameTime(&playFT);
695 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
696 const int64_t frameDiff = recordFT.frames - playFT.frames;
697 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
698
699 // It is possible that the patch track and patch record have a large time disparity because
700 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
701 // time difference based on how often we expect the timestamps to update in normal operation
702 // (typical should be no more than 50 ms).
703 //
704 // If the timestamps aren't sampled close enough, the patch latency is not
705 // considered valid.
706 //
707 // TODO: change this based on more experiments.
708 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
709 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
710 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
711 - timeDiffNs * 1e-6;
712 return OK;
713 }
714 }
715
716 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700717}
718
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700719String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700720{
Andy Hungc3ab7732018-06-01 13:46:29 -0700721 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800722 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
723 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
724 mRecord.const_thread().get(), mPlayback.const_thread().get());
725
726 bool hasSinkDevice =
727 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
728 bool hasSourceDevice =
729 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
730 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
731 hasSinkDevice ? "num sinks" :
732 (hasSourceDevice ? "num sources" : "no devices"),
733 hasSinkDevice ? mAudioPatch.num_sinks :
734 (hasSourceDevice ? mAudioPatch.num_sources : 0),
735 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
736 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700737
738 // add latency if it exists
739 double latencyMs;
740 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700741 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700742 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700743 return result;
744}
745
Eric Laurent951f4552014-05-20 10:48:17 -0700746/* Disconnect a patch */
747status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent34e55a42023-04-24 16:37:56 +0200748 //unlocks AudioFlinger::mLock when calling ThreadBase::sendReleaseAudioPatchConfigEvent
749 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
750 //before processing the release patch request.
751 NO_THREAD_SAFETY_ANALYSIS
752 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700753 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700754 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700755
Mikhail Naganovdea53042018-04-26 13:10:21 -0700756 auto iter = mPatches.find(handle);
757 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700758 return BAD_VALUE;
759 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700760 Patch &removedPatch = iter->second;
761 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700762
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700763 const struct audio_port_config &src = patch.sources[0];
764 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700765 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700766 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
767 if (hwDevice == 0) {
768 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700769 status = BAD_VALUE;
770 break;
771 }
Eric Laurent83b88082014-06-20 18:31:16 -0700772
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700773 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700774 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700775 break;
776 }
777
Mikhail Naganovdea53042018-04-26 13:10:21 -0700778 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
779 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
780 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700781 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700782 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800783 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700784 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800785 status = BAD_VALUE;
786 break;
787 }
Eric Laurent951f4552014-05-20 10:48:17 -0700788 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200789 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700790 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200791 mAudioFlinger.lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700792 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700793 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700794 }
795 } break;
796 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700797 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
798 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700799 status = BAD_VALUE;
800 break;
801 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700802 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700803 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700804 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700805 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800806 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700807 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800808 status = BAD_VALUE;
809 break;
810 }
Eric Laurent951f4552014-05-20 10:48:17 -0700811 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200812 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700813 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200814 mAudioFlinger.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700815 } break;
816 default:
817 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700818 }
819
Eric Laurentb82e6b72019-11-22 17:25:04 -0800820 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700821 return status;
822}
823
François Gaffie58e73af2023-02-15 11:47:24 +0100824void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800825 mPatches.erase(handle);
826 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100827 if (!reuseExistingHalPatch) {
828 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
829 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800830}
831
Eric Laurent951f4552014-05-20 10:48:17 -0700832/* List connected audio ports and they attributes */
833status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
834 struct audio_patch *patches __unused)
835{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700836 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700837 return NO_ERROR;
838}
839
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700840status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
841 audio_io_handle_t stream,
842 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
843{
844 for (const auto& module : mInsertedModules) {
845 if (module.second.streams.count(stream)) {
846 for (const auto& patchHandle : module.second.sw_patches) {
847 const auto& patch_iter = mPatches.find(patchHandle);
848 if (patch_iter != mPatches.end()) {
849 const Patch &patch = patch_iter->second;
850 patches->emplace_back(*this, patchHandle,
851 patch.mPlayback.const_thread()->id(),
852 patch.mRecord.const_thread()->id());
853 } else {
854 ALOGE("Stale patch handle in the cache: %d", patchHandle);
855 }
856 }
857 return OK;
858 }
859 }
860 // The stream is not associated with any of inserted modules.
861 return BAD_VALUE;
862}
863
864void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100865 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700866{
867 if (audioHwDevice->isInsert()) {
868 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100869 if (patch != nullptr) {
870 std::vector <SoftwarePatch> swPatches;
871 getDownstreamSoftwarePatches(stream, &swPatches);
872 if (swPatches.size() > 0) {
873 auto iter = mPatches.find(swPatches[0].getPatchHandle());
874 if (iter != mPatches.end()) {
875 *patch = iter->second.mAudioPatch;
876 }
877 }
878 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700879 }
880}
881
882void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
883{
884 for (auto& module : mInsertedModules) {
885 module.second.streams.erase(stream);
886 }
887}
888
889AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700890{
891 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
892 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
893 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700894 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700895 return nullptr;
896 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700897 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700898}
899
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700900sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700901{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700902 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
903 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
904}
905
906void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100907 audio_module_handle_t module, audio_patch_handle_t handle,
908 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700909{
910 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100911 if (!mInsertedModules[module].streams.empty()) {
912 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
913 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700914}
915
916void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
917 audio_patch_handle_t handle)
918{
919 for (auto& module : mInsertedModules) {
920 module.second.sw_patches.erase(handle);
921 }
922}
923
924void AudioFlinger::PatchPanel::dump(int fd) const
925{
926 String8 patchPanelDump;
927 const char *indent = " ";
928
Mikhail Naganov201369b2018-05-16 16:52:32 -0700929 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700930 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800931 if (!headerPrinted) {
932 patchPanelDump += "\nPatches:\n";
933 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700934 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800935 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700936 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700937
938 headerPrinted = false;
939 for (const auto& module : mInsertedModules) {
940 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
941 if (!headerPrinted) {
942 patchPanelDump += "\nTracked inserted modules:\n";
943 headerPrinted = true;
944 }
945 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
946 for (const auto& stream : module.second.streams) {
947 moduleDump.appendFormat("%d ", stream);
948 }
949 moduleDump.append("; SW Patches: ");
950 for (const auto& patch : module.second.sw_patches) {
951 moduleDump.appendFormat("%d ", patch);
952 }
953 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
954 }
955 }
956
957 if (!patchPanelDump.isEmpty()) {
958 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700959 }
960}
961
Glenn Kasten63238ef2015-03-02 15:50:29 -0800962} // namespace android