blob: 5f238fba7daa2678491ab97860c8baf713c608a2 [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
Andy Hung8e6b62a2023-07-13 18:11:33 -070022#include "PatchPanel.h"
Andy Hung00b9aea2023-07-18 18:43:08 -070023#include "PatchCommandThread.h"
24
25#include <audio_utils/primitives.h>
Eric Laurent951f4552014-05-20 10:48:17 -070026#include <media/AudioParameter.h>
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080027#include <media/AudioValidator.h>
jiabinc52b1ff2019-10-31 17:20:42 -070028#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070029#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070030#include <mediautils/ServiceUtilities.h>
Andy Hung00b9aea2023-07-18 18:43:08 -070031#include <utils/Log.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
Andy Hungb6692eb2023-07-13 16:52:46 -070050/* static */
Andy Hung2dc61c42023-07-17 14:36:08 -070051sp<IAfPatchPanel> IAfPatchPanel::create(const sp<IAfPatchPanelCallback>& afPatchPanelCallback) {
52 return sp<PatchPanel>::make(afPatchPanelCallback);
Andy Hungb6692eb2023-07-13 16:52:46 -070053}
54
55status_t SoftwarePatch::getLatencyMs_l(double* latencyMs) const {
56 return mPatchPanel->getLatencyMs_l(mPatchHandle, latencyMs);
57}
58
Andy Hung8e6b62a2023-07-13 18:11:33 -070059status_t PatchPanel::getLatencyMs_l(
Andy Hungb6692eb2023-07-13 16:52:46 -070060 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -070061{
Andy Hungb6692eb2023-07-13 16:52:46 -070062 const auto& iter = mPatches.find(patchHandle);
63 if (iter != mPatches.end()) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -070064 return iter->second.getLatencyMs(latencyMs);
65 } else {
66 return BAD_VALUE;
67 }
68}
69
Andy Hung8e6b62a2023-07-13 18:11:33 -070070void PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
Andy Hungb6692eb2023-07-13 16:52:46 -070071{
72 if (const auto recordThread = thread->asIAfRecordThread();
73 recordThread) {
Andy Hung2dc61c42023-07-17 14:36:08 -070074 mAfPatchPanelCallback->closeThreadInternal_l(recordThread);
Andy Hungb6692eb2023-07-13 16:52:46 -070075 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
76 playbackThread) {
Andy Hung2dc61c42023-07-17 14:36:08 -070077 mAfPatchPanelCallback->closeThreadInternal_l(playbackThread);
Andy Hungb6692eb2023-07-13 16:52:46 -070078 } else {
79 LOG_ALWAYS_FATAL("%s: Endpoints only accept IAfPlayback and IAfRecord threads, "
80 "invalid thread, id: %d type: %d",
81 __func__, thread->id(), thread->type());
82 }
83}
84
Eric Laurent951f4552014-05-20 10:48:17 -070085/* List connected audio ports and their attributes */
Andy Hung37006372023-08-31 15:24:24 -070086status_t PatchPanel::listAudioPorts_l(unsigned int* /* num_ports */,
Eric Laurent951f4552014-05-20 10:48:17 -070087 struct audio_port *ports __unused)
88{
Mikhail Naganovdea53042018-04-26 13:10:21 -070089 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070090 return NO_ERROR;
91}
92
93/* Get supported attributes for a given audio port */
Andy Hung37006372023-08-31 15:24:24 -070094status_t PatchPanel::getAudioPort_l(struct audio_port_v7* port)
Eric Laurent951f4552014-05-20 10:48:17 -070095{
jiabinb4fed192020-09-22 14:45:40 -070096 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
97 // Only query the HAL when the port is a device.
98 // TODO: implement getAudioPort for mix.
99 return INVALID_OPERATION;
100 }
Andy Hung37006372023-08-31 15:24:24 -0700101 AudioHwDevice* hwDevice = findAudioHwDeviceByModule_l(port->ext.device.hw_module);
jiabinb4fed192020-09-22 14:45:40 -0700102 if (hwDevice == nullptr) {
103 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
104 return BAD_VALUE;
105 }
106 if (!hwDevice->supportsAudioPatches()) {
107 return INVALID_OPERATION;
108 }
109 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700110}
111
Eric Laurent951f4552014-05-20 10:48:17 -0700112/* Connect a patch between several source and sink ports */
Andy Hung37006372023-08-31 15:24:24 -0700113status_t PatchPanel::createAudioPatch_l(const struct audio_patch* patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200114 audio_patch_handle_t *handle,
115 bool endpointPatch)
Andy Hung87c693c2023-07-06 20:56:16 -0700116 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendCreateAudioPatchConfigEvent
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200117 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
118 //before processing the create patch request.
119 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700120{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700121 if (handle == NULL || patch == NULL) {
122 return BAD_VALUE;
123 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700124 ALOGV("%s() num_sources %d num_sinks %d handle %d",
125 __func__, patch->num_sources, patch->num_sinks, *handle);
126 status_t status = NO_ERROR;
127 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700128
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700129 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700130 return BAD_VALUE;
131 }
Eric Laurent874c42872014-08-08 15:13:39 -0700132 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
133 // only the audio policy manager can request a patch creation with 2 sources.
134 if (patch->num_sources > 2) {
135 return INVALID_OPERATION;
136 }
François Gaffie58e73af2023-02-15 11:47:24 +0100137 bool reuseExistingHalPatch = false;
138 audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700139 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700140 auto iter = mPatches.find(*handle);
141 if (iter != mPatches.end()) {
142 ALOGV("%s() removing patch handle %d", __func__, *handle);
143 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700144 // free resources owned by the removed patch if applicable
145 // 1) if a software patch is present, release the playback and capture threads and
146 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700147 if (removedPatch.isSoftware()) {
Andy Hung37006372023-08-31 15:24:24 -0700148 removedPatch.clearConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700149 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700150 // 2) if the new patch and old patch source or sink are devices from different
151 // hw modules, clear the audio HAL patches now because they will not be updated
152 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700153 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700154 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
155 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
François Gaffie58e73af2023-02-15 11:47:24 +0100156 oldhandle = *handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700157 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
158 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
159 oldPatch.sources[0].ext.device.hw_module !=
160 patch->sources[0].ext.device.hw_module)) {
161 hwModule = oldPatch.sources[0].ext.device.hw_module;
162 } else if (patch->num_sinks == 0 ||
163 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
164 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
165 oldPatch.sinks[0].ext.device.hw_module !=
166 patch->sinks[0].ext.device.hw_module))) {
167 // Note on (patch->num_sinks == 0): this situation should not happen as
168 // these special patches are only created by the policy manager but just
169 // in case, systematically clear the HAL patch.
170 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700171 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 hwModule = oldPatch.sinks[0].ext.device.hw_module;
173 }
Andy Hung37006372023-08-31 15:24:24 -0700174 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule_l(hwModule);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700175 if (hwDevice != 0) {
176 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800178 halHandle = removedPatch.mHalHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100179 // Prevent to remove/add device effect when mix / device did not change, and
180 // hal patch has not been released
181 // Note that no patch leak at hal layer as halHandle is reused.
182 reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700183 }
François Gaffie58e73af2023-02-15 11:47:24 +0100184 erasePatch(*handle, reuseExistingHalPatch);
Eric Laurent951f4552014-05-20 10:48:17 -0700185 }
186 }
187
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200188 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700189 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700190
Eric Laurent951f4552014-05-20 10:48:17 -0700191 switch (patch->sources[0].type) {
192 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700193 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Andy Hung37006372023-08-31 15:24:24 -0700194 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule_l(srcModule);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700195 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700196 status = BAD_VALUE;
197 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700198 }
199 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700200 // support only one sink if connection to a mix or across HW modules
201 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700202 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
203 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700204 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700205 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700206 status = INVALID_OPERATION;
207 goto exit;
208 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700209 // reject connection to different sink types
210 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700211 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700212 status = BAD_VALUE;
213 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700214 }
Eric Laurent951f4552014-05-20 10:48:17 -0700215 }
216
Eric Laurent3bcf8592015-04-03 12:13:24 -0700217 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700218 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700219 // - Device to device AND
220 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700221 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700222 if ((patch->num_sources == 2) ||
223 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
224 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700225 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800226 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
227 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700228 if (patch->num_sources == 2) {
229 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700230 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
231 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700232 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700233 status = INVALID_OPERATION;
234 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700235 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700236 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
237 patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700238 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700239 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700240 status = INVALID_OPERATION;
241 goto exit;
242 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700243 // existing playback thread is reused, so it is not closed when patch is cleared
244 newPatch.mPlayback.setThread(
Andy Hung87c693c2023-07-06 20:56:16 -0700245 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700246 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700247 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200248 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700249 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700250 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
251 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
252 config.sample_rate = patch->sinks[0].sample_rate;
253 }
254 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
255 config.channel_mask = patch->sinks[0].channel_mask;
256 }
257 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
258 config.format = patch->sinks[0].format;
259 }
260 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
261 flags = patch->sinks[0].flags.output;
262 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700263 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800264 patch->sinks[0].ext.device.hw_module,
265 &output,
266 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200267 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800268 outputDevice,
269 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700270 flags);
Andy Hung2dc61c42023-07-17 14:36:08 -0700271 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700272 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700273 status = NO_MEMORY;
274 goto exit;
275 }
Andy Hung87c693c2023-07-06 20:56:16 -0700276 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700277 }
Eric Laurent83b88082014-06-20 18:31:16 -0700278 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700279 String8 address = String8(patch->sources[0].ext.device.address);
280 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700281 // open input stream with source device audio properties if provided or
282 // default to peer output stream properties otherwise.
283 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
284 config.sample_rate = patch->sources[0].sample_rate;
285 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700286 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700287 }
288 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
289 config.channel_mask = patch->sources[0].channel_mask;
290 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700291 config.channel_mask = audio_channel_in_mask_from_count(
292 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700293 }
294 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
295 config.format = patch->sources[0].format;
296 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700297 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700298 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700299 audio_input_flags_t flags =
300 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
301 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700302 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200303 audio_source_t source = AUDIO_SOURCE_MIC;
304 // For telephony patches, propagate voice communication use case to record side
305 if (patch->num_sources == 2
306 && patch->sources[1].ext.mix.usecase.stream
307 == AUDIO_STREAM_VOICE_CALL) {
308 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
309 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700310 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700311 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700312 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700313 device,
314 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200315 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800316 flags,
317 outputDevice,
318 outputDeviceAddress);
Andy Hung2dc61c42023-07-17 14:36:08 -0700319 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700320 thread.get(), config.channel_mask);
321 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700322 status = NO_MEMORY;
323 goto exit;
324 }
Andy Hung87c693c2023-07-06 20:56:16 -0700325 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Andy Hung37006372023-08-31 15:24:24 -0700326 status = newPatch.createConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700327 if (status != NO_ERROR) {
328 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700329 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700330 if (audioHwDevice->isInsert()) {
331 insertedModule = audioHwDevice->handle();
332 }
Eric Laurent951f4552014-05-20 10:48:17 -0700333 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700334 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700335 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700336 patch->sinks[0].ext.mix.handle);
337 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700338 thread = mAfPatchPanelCallback->checkMmapThread_l(
339 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800340 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700341 ALOGW("%s() bad capture I/O handle %d",
342 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800343 status = BAD_VALUE;
344 goto exit;
345 }
Eric Laurent83b88082014-06-20 18:31:16 -0700346 }
Andy Hung954b9712023-08-28 18:36:53 -0700347 mAfPatchPanelCallback->mutex().unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700348 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700349 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800350 if (status == NO_ERROR) {
351 newPatch.setThread(thread);
352 }
Eric Laurent526aa572019-01-15 10:54:58 -0800353 // remove stale audio patch with same input as sink if any
354 for (auto& iter : mPatches) {
355 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800356 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800357 break;
358 }
359 }
Eric Laurent83b88082014-06-20 18:31:16 -0700360 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700361 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
362 status = hwDevice->createAudioPatch(patch->num_sources,
363 patch->sources,
364 patch->num_sinks,
365 patch->sinks,
366 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700367 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700368 }
Eric Laurent951f4552014-05-20 10:48:17 -0700369 }
370 } break;
371 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700372 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung2dc61c42023-07-17 14:36:08 -0700373 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700374 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700375 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700376 status = BAD_VALUE;
377 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700378 }
379 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700380 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700381 for (unsigned int i = 0; i < patch->num_sinks; i++) {
382 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700383 ALOGW("%s() invalid sink type %d for mix source",
384 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700385 status = BAD_VALUE;
386 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700387 }
388 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700389 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700390 status = BAD_VALUE;
391 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700392 }
jiabinc52b1ff2019-10-31 17:20:42 -0700393 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
394 patch->sinks[i].ext.device.type);
395 device->setAddress(patch->sinks[i].ext.device.address);
396 device->applyAudioPortConfig(&patch->sinks[i]);
397 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700398 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700399 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
400 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700401 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700402 thread = mAfPatchPanelCallback->checkMmapThread_l(
403 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800404 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700405 ALOGW("%s() bad playback I/O handle %d",
406 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800407 status = BAD_VALUE;
408 goto exit;
409 }
Eric Laurent951f4552014-05-20 10:48:17 -0700410 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700411 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
412 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
414
François Gaffie150fcc62023-09-15 11:02:39 +0200415 // For endpoint patches, we do not need to re-evaluate the device effect state
416 // if the same HAL patch is reused (see calls to mAfPatchPanelCallback below)
417 if (endpointPatch) {
418 for (auto& p : mPatches) {
419 // end point patches are skipped so we do not compare against this patch
420 if (!p.second.mIsEndpointPatch && patchesHaveSameRoute(
421 newPatch.mAudioPatch, p.second.mAudioPatch)) {
422 ALOGV("%s() Sw Bridge endpoint reusing halHandle=%d", __func__,
423 p.second.mHalHandle);
424 halHandle = p.second.mHalHandle;
425 reuseExistingHalPatch = true;
426 break;
427 }
428 }
429 }
Andy Hung954b9712023-08-28 18:36:53 -0700430 mAfPatchPanelCallback->mutex().unlock();
François Gaffie150fcc62023-09-15 11:02:39 +0200431
Eric Laurent054d9d32015-04-24 08:48:48 -0700432 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700433 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800434 if (status == NO_ERROR) {
435 newPatch.setThread(thread);
436 }
Eric Laurent526aa572019-01-15 10:54:58 -0800437
438 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200439 // Prevent to remove endpoint patches (involved in a SwBridge)
440 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
441 if (!endpointPatch) {
442 for (auto& iter : mPatches) {
443 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
444 !iter.second.mIsEndpointPatch) {
445 erasePatch(iter.first);
446 break;
447 }
Eric Laurent526aa572019-01-15 10:54:58 -0800448 }
449 }
Eric Laurent951f4552014-05-20 10:48:17 -0700450 } break;
451 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700452 status = BAD_VALUE;
453 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700454 }
Eric Laurent83b88082014-06-20 18:31:16 -0700455exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700456 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700457 if (status == NO_ERROR) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700458 *handle = static_cast<audio_patch_handle_t>(
459 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700460 newPatch.mHalHandle = halHandle;
François Gaffie150fcc62023-09-15 11:02:39 +0200461 // Skip device effect:
462 // -for sw bridge as effect are likely held by endpoint patches
463 // -for endpoint reusing a HalPatch handle
464 if (!(newPatch.isSoftware()
465 || (endpointPatch && reuseExistingHalPatch))) {
466 if (reuseExistingHalPatch) {
467 mAfPatchPanelCallback->getPatchCommandThread()->updateAudioPatch(
468 oldhandle, *handle, newPatch);
469 } else {
470 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(
471 *handle, newPatch);
472 }
François Gaffie58e73af2023-02-15 11:47:24 +0100473 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700474 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Andy Hung37006372023-08-31 15:24:24 -0700475 addSoftwarePatchToInsertedModules_l(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700476 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100477 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700478 } else {
Andy Hung37006372023-08-31 15:24:24 -0700479 newPatch.clearConnections_l(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700480 }
481 return status;
482}
483
Andy Hung8e6b62a2023-07-13 18:11:33 -0700484PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700485{
486 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
487 mRecord.handle(), mPlayback.handle());
488}
489
Andy Hung37006372023-08-31 15:24:24 -0700490status_t PatchPanel::Patch::createConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700491{
492 // create patch from source device to record thread input
Andy Hung37006372023-08-31 15:24:24 -0700493 status_t status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700494 PatchBuilder().addSource(mAudioPatch.sources[0]).
495 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200496 mRecord.handlePtr(),
497 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700498 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700499 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700500 return status;
501 }
502
503 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700504 if (mAudioPatch.num_sinks != 0) {
Andy Hung37006372023-08-31 15:24:24 -0700505 status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700506 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200507 mPlayback.handlePtr(),
508 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700509 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700510 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700511 return status;
512 }
513 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700514 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700515 }
516
Eric Laurent83b88082014-06-20 18:31:16 -0700517 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700518 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700519 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700520 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
521 uint32_t sampleRate = mPlayback.thread()->sampleRate();
522 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700523
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700524 audio_format_t inputFormat = mRecord.thread()->format();
525 if (!audio_is_linear_pcm(inputFormat)) {
526 // The playbackThread format will say PCM for IEC61937 packetized stream.
527 // Use recordThread format.
528 format = inputFormat;
529 }
530 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
531 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700532 if (sampleRate == mRecord.thread()->sampleRate() &&
533 inChannelMask == mRecord.thread()->channelMask() &&
534 mRecord.thread()->fastTrackAvailable() &&
535 mRecord.thread()->hasFastCapture()) {
536 // Create a fast track if the record thread has fast capture to get better performance.
537 // Only enable fast mode when there is no resample needed.
538 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
539 } else {
540 // Fast mode is not available in this case.
541 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
542 }
Eric Laurent83b88082014-06-20 18:31:16 -0700543
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700544 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
545 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700546 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200547 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700548 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
549 // "reuse one existing output mix" case
550 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200551 // For telephony patches, propagate voice communication use case to record side
552 if (streamType == AUDIO_STREAM_VOICE_CALL) {
553 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
554 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700555 }
jiabin01c8f562018-07-19 17:47:28 -0700556 if (mPlayback.thread()->hasFastMixer()) {
557 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700558 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700559 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700560 } else {
561 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700562 }
563
Andy Hung8d31fd22023-06-26 19:20:57 -0700564 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800565 const bool usePassthruPatchRecord =
566 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100567 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
568 const size_t recordFrameCount = mRecord.thread()->frameCount();
569 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800570 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100571 // PassthruPatchRecord producesBufferOnDemand, so use
572 // maximum of playback and record thread framecounts
573 frameCount = std::max(playbackFrameCount, recordFrameCount);
574 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
575 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700576 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700577 mRecord.thread().get(),
578 sampleRate,
579 inChannelMask,
580 format,
581 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200582 inputFlags,
583 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700584 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100585 // use a pseudo LCM between input and output framecount
586 int playbackShift = __builtin_ctz(playbackFrameCount);
587 int shift = __builtin_ctz(recordFrameCount);
588 if (playbackShift < shift) {
589 shift = playbackShift;
590 }
591 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
592 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
593 __func__, playbackFrameCount, recordFrameCount, frameCount);
594
Andy Hung8d31fd22023-06-26 19:20:57 -0700595 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700596 mRecord.thread().get(),
597 sampleRate,
598 inChannelMask,
599 format,
600 frameCount,
601 nullptr,
602 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200603 inputFlags,
604 {} /* timeout */,
605 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700606 }
607 status = mRecord.checkTrack(tempRecordTrack.get());
608 if (status != NO_ERROR) {
609 return status;
610 }
611
Eric Laurent83b88082014-06-20 18:31:16 -0700612 // create a special playback track to render to playback thread.
613 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200614
615 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
616 // it might glitch.
617 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
618 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
619 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700620 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700621 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700622 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700623 sampleRate,
624 outChannelMask,
625 format,
626 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700627 tempRecordTrack->buffer(),
628 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200629 outputFlags,
630 {} /*timeout*/,
631 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700632 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700633 if (status != NO_ERROR) {
634 return status;
635 }
Eric Laurent83b88082014-06-20 18:31:16 -0700636
637 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800638 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
639 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
640 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
641 // is alive. There is no need to hold a reference, and there is no need
642 // to clear it. In fact, since playback stopping is asynchronous, there is
643 // no proper time when clearing could be done.
644 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
645 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700646
647 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700648 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
649 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700650
651 return status;
652}
653
Andy Hung37006372023-08-31 15:24:24 -0700654void PatchPanel::Patch::clearConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700655{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700656 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
657 __func__, mRecord.handle(), mPlayback.handle());
658 mRecord.stopTrack();
659 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700660 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Andy Hung37006372023-08-31 15:24:24 -0700661 mRecord.closeConnections_l(panel);
662 mPlayback.closeConnections_l(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700663}
664
Andy Hung8e6b62a2023-07-13 18:11:33 -0700665status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700666{
667 if (!isSoftware()) return INVALID_OPERATION;
668
669 auto recordTrack = mRecord.const_track();
670 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
671
672 auto playbackTrack = mPlayback.const_track();
673 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
674
675 // Latency information for tracks may be called without obtaining
676 // the underlying thread lock.
677 //
678 // We use record server latency + playback track latency (generally smaller than the
679 // reverse due to internal biases).
680 //
681 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700682
Andy Hung30282562018-08-08 18:27:03 -0700683 // For PCM tracks get server latency.
684 if (audio_is_linear_pcm(recordTrack->format())) {
685 double recordServerLatencyMs, playbackTrackLatencyMs;
686 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
687 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
688 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
689 return OK;
690 }
691 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700692
Andy Hung30282562018-08-08 18:27:03 -0700693 // See if kernel latencies are available.
694 // If so, do a frame diff and time difference computation to estimate
695 // the total patch latency. This requires that frame counts are reported by the
696 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700697 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700698 recordTrack->getKernelFrameTime(&recordFT);
699 playbackTrack->getKernelFrameTime(&playFT);
700 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
701 const int64_t frameDiff = recordFT.frames - playFT.frames;
702 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
703
704 // It is possible that the patch track and patch record have a large time disparity because
705 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
706 // time difference based on how often we expect the timestamps to update in normal operation
707 // (typical should be no more than 50 ms).
708 //
709 // If the timestamps aren't sampled close enough, the patch latency is not
710 // considered valid.
711 //
712 // TODO: change this based on more experiments.
713 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
714 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
715 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
716 - timeDiffNs * 1e-6;
717 return OK;
718 }
719 }
720
721 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700722}
723
Andy Hung8e6b62a2023-07-13 18:11:33 -0700724String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700725{
Andy Hungc3ab7732018-06-01 13:46:29 -0700726 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800727 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
728 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
729 mRecord.const_thread().get(), mPlayback.const_thread().get());
730
731 bool hasSinkDevice =
732 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
733 bool hasSourceDevice =
734 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
735 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
736 hasSinkDevice ? "num sinks" :
737 (hasSourceDevice ? "num sources" : "no devices"),
738 hasSinkDevice ? mAudioPatch.num_sinks :
739 (hasSourceDevice ? mAudioPatch.num_sources : 0),
740 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
741 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700742
743 // add latency if it exists
744 double latencyMs;
745 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700746 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700747 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700748 return result;
749}
750
Eric Laurent951f4552014-05-20 10:48:17 -0700751/* Disconnect a patch */
Andy Hung37006372023-08-31 15:24:24 -0700752status_t PatchPanel::releaseAudioPatch_l(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700753 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200754 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
755 //before processing the release patch request.
756 NO_THREAD_SAFETY_ANALYSIS
757 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700758 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700759 status_t status = NO_ERROR;
François Gaffie150fcc62023-09-15 11:02:39 +0200760 bool doReleasePatch = true;
Eric Laurent951f4552014-05-20 10:48:17 -0700761
Mikhail Naganovdea53042018-04-26 13:10:21 -0700762 auto iter = mPatches.find(handle);
763 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700764 return BAD_VALUE;
765 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700766 Patch &removedPatch = iter->second;
François Gaffie150fcc62023-09-15 11:02:39 +0200767 const bool isSwBridge = removedPatch.isSoftware();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700768 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700769
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700770 const struct audio_port_config &src = patch.sources[0];
771 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700772 case AUDIO_PORT_TYPE_DEVICE: {
Andy Hung37006372023-08-31 15:24:24 -0700773 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule_l(src.ext.device.hw_module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700774 if (hwDevice == 0) {
775 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700776 status = BAD_VALUE;
777 break;
778 }
Eric Laurent83b88082014-06-20 18:31:16 -0700779
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700780 if (removedPatch.isSoftware()) {
Andy Hung37006372023-08-31 15:24:24 -0700781 removedPatch.clearConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700782 break;
783 }
784
Mikhail Naganovdea53042018-04-26 13:10:21 -0700785 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
786 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700787 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700788 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700789 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800790 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700791 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800792 status = BAD_VALUE;
793 break;
794 }
Eric Laurent951f4552014-05-20 10:48:17 -0700795 }
Andy Hung954b9712023-08-28 18:36:53 -0700796 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700797 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700798 mAfPatchPanelCallback->mutex().lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700799 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700800 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700801 }
802 } break;
803 case AUDIO_PORT_TYPE_MIX: {
Andy Hung37006372023-08-31 15:24:24 -0700804 if (findHwDeviceByModule_l(src.ext.mix.hw_module) == 0) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700805 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700806 status = BAD_VALUE;
807 break;
808 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700809 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700810 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700811 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700812 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800813 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700814 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800815 status = BAD_VALUE;
816 break;
817 }
Eric Laurent951f4552014-05-20 10:48:17 -0700818 }
François Gaffie150fcc62023-09-15 11:02:39 +0200819 // Check whether the removed patch Hal Handle is used in another non-Endpoint patch.
820 // Since this is a non-Endpoint patch, the removed patch is not considered (it is
821 // removed later from mPatches).
822 if (removedPatch.mIsEndpointPatch) {
823 for (auto& p: mPatches) {
824 if (!p.second.mIsEndpointPatch
825 && p.second.mHalHandle == removedPatch.mHalHandle) {
826 ALOGV("%s() Sw Bridge endpoint used existing halHandle=%d, do not release",
827 __func__, p.second.mHalHandle);
828 doReleasePatch = false;
829 break;
830 }
831 }
832 }
833 if (doReleasePatch) {
834 mAfPatchPanelCallback->mutex().unlock();
835 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
836 mAfPatchPanelCallback->mutex().lock();
837 }
Eric Laurent951f4552014-05-20 10:48:17 -0700838 } break;
839 default:
840 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700841 }
842
François Gaffie150fcc62023-09-15 11:02:39 +0200843 erasePatch(handle, /* reuseExistingHalPatch= */ !doReleasePatch || isSwBridge);
Eric Laurent951f4552014-05-20 10:48:17 -0700844 return status;
845}
846
Andy Hung8e6b62a2023-07-13 18:11:33 -0700847void PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800848 mPatches.erase(handle);
849 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100850 if (!reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700851 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100852 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800853}
854
Eric Laurent951f4552014-05-20 10:48:17 -0700855/* List connected audio ports and they attributes */
Andy Hung37006372023-08-31 15:24:24 -0700856status_t PatchPanel::listAudioPatches_l(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700857 struct audio_patch *patches __unused)
858{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700859 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700860 return NO_ERROR;
861}
862
Andy Hung8e6b62a2023-07-13 18:11:33 -0700863status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700864 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700865 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700866{
867 for (const auto& module : mInsertedModules) {
868 if (module.second.streams.count(stream)) {
869 for (const auto& patchHandle : module.second.sw_patches) {
870 const auto& patch_iter = mPatches.find(patchHandle);
871 if (patch_iter != mPatches.end()) {
872 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700873 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
874 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700875 patch.mPlayback.const_thread()->id(),
876 patch.mRecord.const_thread()->id());
877 } else {
878 ALOGE("Stale patch handle in the cache: %d", patchHandle);
879 }
880 }
881 return OK;
882 }
883 }
884 // The stream is not associated with any of inserted modules.
885 return BAD_VALUE;
886}
887
Andy Hung8e6b62a2023-07-13 18:11:33 -0700888void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100889 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700890{
891 if (audioHwDevice->isInsert()) {
892 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100893 if (patch != nullptr) {
894 std::vector <SoftwarePatch> swPatches;
895 getDownstreamSoftwarePatches(stream, &swPatches);
896 if (swPatches.size() > 0) {
897 auto iter = mPatches.find(swPatches[0].getPatchHandle());
898 if (iter != mPatches.end()) {
899 *patch = iter->second.mAudioPatch;
900 }
901 }
902 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700903 }
904}
905
Andy Hung8e6b62a2023-07-13 18:11:33 -0700906void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700907{
908 for (auto& module : mInsertedModules) {
909 module.second.streams.erase(stream);
910 }
911}
912
Andy Hung37006372023-08-31 15:24:24 -0700913AudioHwDevice* PatchPanel::findAudioHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700914{
915 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung2dc61c42023-07-17 14:36:08 -0700916 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700917 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700918 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700919 return nullptr;
920 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700921 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700922}
923
Andy Hung37006372023-08-31 15:24:24 -0700924sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700925{
Andy Hung37006372023-08-31 15:24:24 -0700926 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule_l(module);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700927 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
928}
929
Andy Hung37006372023-08-31 15:24:24 -0700930void PatchPanel::addSoftwarePatchToInsertedModules_l(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100931 audio_module_handle_t module, audio_patch_handle_t handle,
932 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700933{
934 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100935 if (!mInsertedModules[module].streams.empty()) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700936 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100937 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700938}
939
Andy Hung8e6b62a2023-07-13 18:11:33 -0700940void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700941 audio_patch_handle_t handle)
942{
943 for (auto& module : mInsertedModules) {
944 module.second.sw_patches.erase(handle);
945 }
946}
947
Andy Hung8e6b62a2023-07-13 18:11:33 -0700948void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700949{
950 String8 patchPanelDump;
951 const char *indent = " ";
952
Mikhail Naganov201369b2018-05-16 16:52:32 -0700953 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700954 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800955 if (!headerPrinted) {
956 patchPanelDump += "\nPatches:\n";
957 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700958 }
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000959 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700960 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700961
962 headerPrinted = false;
963 for (const auto& module : mInsertedModules) {
964 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
965 if (!headerPrinted) {
966 patchPanelDump += "\nTracked inserted modules:\n";
967 headerPrinted = true;
968 }
969 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
970 for (const auto& stream : module.second.streams) {
971 moduleDump.appendFormat("%d ", stream);
972 }
973 moduleDump.append("; SW Patches: ");
974 for (const auto& patch : module.second.sw_patches) {
975 moduleDump.appendFormat("%d ", patch);
976 }
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000977 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700978 }
979 }
980
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000981 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000982 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700983 }
984}
985
Glenn Kasten63238ef2015-03-02 15:50:29 -0800986} // namespace android