blob: 994dd473b4d82706d16e5fc65e54bb282d6abb72 [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 }
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700263 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
Andy Hung2dc61c42023-07-17 14:36:08 -0700264 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800265 patch->sinks[0].ext.device.hw_module,
266 &output,
267 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200268 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800269 outputDevice,
270 outputDeviceAddress,
Dean Wheatleydfb67b82024-01-23 09:36:29 +1100271 &flags,
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700272 attributes);
Andy Hung2dc61c42023-07-17 14:36:08 -0700273 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700274 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700275 status = NO_MEMORY;
276 goto exit;
277 }
Andy Hung87c693c2023-07-06 20:56:16 -0700278 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700279 }
Eric Laurent83b88082014-06-20 18:31:16 -0700280 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700281 String8 address = String8(patch->sources[0].ext.device.address);
282 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700283 // open input stream with source device audio properties if provided or
284 // default to peer output stream properties otherwise.
285 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
286 config.sample_rate = patch->sources[0].sample_rate;
287 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700288 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700289 }
290 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
291 config.channel_mask = patch->sources[0].channel_mask;
292 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700293 config.channel_mask = audio_channel_in_mask_from_count(
294 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700295 }
296 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
297 config.format = patch->sources[0].format;
298 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700299 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700300 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700301 audio_input_flags_t flags =
302 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
303 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700304 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200305 audio_source_t source = AUDIO_SOURCE_MIC;
306 // For telephony patches, propagate voice communication use case to record side
307 if (patch->num_sources == 2
308 && patch->sources[1].ext.mix.usecase.stream
309 == AUDIO_STREAM_VOICE_CALL) {
310 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
311 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700312 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700313 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700314 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700315 device,
316 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200317 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800318 flags,
319 outputDevice,
320 outputDeviceAddress);
Andy Hung2dc61c42023-07-17 14:36:08 -0700321 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700322 thread.get(), config.channel_mask);
323 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700324 status = NO_MEMORY;
325 goto exit;
326 }
Andy Hung87c693c2023-07-06 20:56:16 -0700327 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Andy Hung37006372023-08-31 15:24:24 -0700328 status = newPatch.createConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700329 if (status != NO_ERROR) {
330 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700331 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700332 if (audioHwDevice->isInsert()) {
333 insertedModule = audioHwDevice->handle();
334 }
Eric Laurent951f4552014-05-20 10:48:17 -0700335 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700336 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700337 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700338 patch->sinks[0].ext.mix.handle);
339 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700340 thread = mAfPatchPanelCallback->checkMmapThread_l(
341 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800342 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700343 ALOGW("%s() bad capture I/O handle %d",
344 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800345 status = BAD_VALUE;
346 goto exit;
347 }
Eric Laurent83b88082014-06-20 18:31:16 -0700348 }
Andy Hung954b9712023-08-28 18:36:53 -0700349 mAfPatchPanelCallback->mutex().unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700350 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700351 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800352 if (status == NO_ERROR) {
353 newPatch.setThread(thread);
354 }
Eric Laurent526aa572019-01-15 10:54:58 -0800355 // remove stale audio patch with same input as sink if any
356 for (auto& iter : mPatches) {
357 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800358 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800359 break;
360 }
361 }
Eric Laurent83b88082014-06-20 18:31:16 -0700362 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700363 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
364 status = hwDevice->createAudioPatch(patch->num_sources,
365 patch->sources,
366 patch->num_sinks,
367 patch->sinks,
368 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700369 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700370 }
Eric Laurent951f4552014-05-20 10:48:17 -0700371 }
372 } break;
373 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700374 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung2dc61c42023-07-17 14:36:08 -0700375 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700376 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700377 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700378 status = BAD_VALUE;
379 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700380 }
381 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700382 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700383 for (unsigned int i = 0; i < patch->num_sinks; i++) {
384 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700385 ALOGW("%s() invalid sink type %d for mix source",
386 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700387 status = BAD_VALUE;
388 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700389 }
390 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700391 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700392 status = BAD_VALUE;
393 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700394 }
jiabinc52b1ff2019-10-31 17:20:42 -0700395 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
396 patch->sinks[i].ext.device.type);
397 device->setAddress(patch->sinks[i].ext.device.address);
398 device->applyAudioPortConfig(&patch->sinks[i]);
399 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700400 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700401 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
402 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700403 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700404 thread = mAfPatchPanelCallback->checkMmapThread_l(
405 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800406 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700407 ALOGW("%s() bad playback I/O handle %d",
408 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800409 status = BAD_VALUE;
410 goto exit;
411 }
Eric Laurent951f4552014-05-20 10:48:17 -0700412 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700413 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
414 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700415 }
416
François Gaffie150fcc62023-09-15 11:02:39 +0200417 // For endpoint patches, we do not need to re-evaluate the device effect state
418 // if the same HAL patch is reused (see calls to mAfPatchPanelCallback below)
419 if (endpointPatch) {
420 for (auto& p : mPatches) {
421 // end point patches are skipped so we do not compare against this patch
422 if (!p.second.mIsEndpointPatch && patchesHaveSameRoute(
423 newPatch.mAudioPatch, p.second.mAudioPatch)) {
424 ALOGV("%s() Sw Bridge endpoint reusing halHandle=%d", __func__,
425 p.second.mHalHandle);
426 halHandle = p.second.mHalHandle;
427 reuseExistingHalPatch = true;
428 break;
429 }
430 }
431 }
Andy Hung954b9712023-08-28 18:36:53 -0700432 mAfPatchPanelCallback->mutex().unlock();
François Gaffie150fcc62023-09-15 11:02:39 +0200433
Eric Laurent054d9d32015-04-24 08:48:48 -0700434 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700435 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800436 if (status == NO_ERROR) {
437 newPatch.setThread(thread);
438 }
Eric Laurent526aa572019-01-15 10:54:58 -0800439
440 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200441 // Prevent to remove endpoint patches (involved in a SwBridge)
442 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
443 if (!endpointPatch) {
444 for (auto& iter : mPatches) {
445 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
446 !iter.second.mIsEndpointPatch) {
447 erasePatch(iter.first);
448 break;
449 }
Eric Laurent526aa572019-01-15 10:54:58 -0800450 }
451 }
Eric Laurent951f4552014-05-20 10:48:17 -0700452 } break;
453 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700454 status = BAD_VALUE;
455 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700456 }
Eric Laurent83b88082014-06-20 18:31:16 -0700457exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700458 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700459 if (status == NO_ERROR) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700460 *handle = static_cast<audio_patch_handle_t>(
461 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700462 newPatch.mHalHandle = halHandle;
François Gaffie150fcc62023-09-15 11:02:39 +0200463 // Skip device effect:
464 // -for sw bridge as effect are likely held by endpoint patches
465 // -for endpoint reusing a HalPatch handle
466 if (!(newPatch.isSoftware()
467 || (endpointPatch && reuseExistingHalPatch))) {
468 if (reuseExistingHalPatch) {
469 mAfPatchPanelCallback->getPatchCommandThread()->updateAudioPatch(
470 oldhandle, *handle, newPatch);
471 } else {
472 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(
473 *handle, newPatch);
474 }
François Gaffie58e73af2023-02-15 11:47:24 +0100475 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700476 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Andy Hung37006372023-08-31 15:24:24 -0700477 addSoftwarePatchToInsertedModules_l(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700478 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100479 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700480 } else {
Andy Hung37006372023-08-31 15:24:24 -0700481 newPatch.clearConnections_l(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700482 }
483 return status;
484}
485
jiabin12537fc2023-10-12 17:56:08 +0000486status_t PatchPanel::getAudioMixPort_l(const audio_port_v7 *devicePort,
487 audio_port_v7 *mixPort) {
488 if (devicePort->type != AUDIO_PORT_TYPE_DEVICE) {
489 ALOGE("%s the type of given device port is not DEVICE", __func__);
490 return INVALID_OPERATION;
491 }
492 if (mixPort->type != AUDIO_PORT_TYPE_MIX) {
493 ALOGE("%s the type of given mix port is not MIX", __func__);
494 return INVALID_OPERATION;
495 }
496 AudioHwDevice* hwDevice = findAudioHwDeviceByModule_l(devicePort->ext.device.hw_module);
497 if (hwDevice == nullptr) {
498 ALOGW("%s cannot find hw module %d", __func__, devicePort->ext.device.hw_module);
499 return BAD_VALUE;
500 }
501 return hwDevice->getAudioMixPort(devicePort, mixPort);
502}
503
Andy Hung8e6b62a2023-07-13 18:11:33 -0700504PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700505{
506 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
507 mRecord.handle(), mPlayback.handle());
508}
509
Andy Hung37006372023-08-31 15:24:24 -0700510status_t PatchPanel::Patch::createConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700511{
512 // create patch from source device to record thread input
Andy Hung37006372023-08-31 15:24:24 -0700513 status_t status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700514 PatchBuilder().addSource(mAudioPatch.sources[0]).
515 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200516 mRecord.handlePtr(),
517 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700518 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700519 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700520 return status;
521 }
522
523 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700524 if (mAudioPatch.num_sinks != 0) {
Andy Hung37006372023-08-31 15:24:24 -0700525 status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700526 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200527 mPlayback.handlePtr(),
528 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700529 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700530 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700531 return status;
532 }
533 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700534 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700535 }
536
Eric Laurent83b88082014-06-20 18:31:16 -0700537 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700538 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700539 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700540 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
541 uint32_t sampleRate = mPlayback.thread()->sampleRate();
542 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700543
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700544 audio_format_t inputFormat = mRecord.thread()->format();
545 if (!audio_is_linear_pcm(inputFormat)) {
546 // The playbackThread format will say PCM for IEC61937 packetized stream.
547 // Use recordThread format.
548 format = inputFormat;
549 }
550 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
551 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700552 if (sampleRate == mRecord.thread()->sampleRate() &&
553 inChannelMask == mRecord.thread()->channelMask() &&
554 mRecord.thread()->fastTrackAvailable() &&
555 mRecord.thread()->hasFastCapture()) {
556 // Create a fast track if the record thread has fast capture to get better performance.
557 // Only enable fast mode when there is no resample needed.
558 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
559 } else {
560 // Fast mode is not available in this case.
561 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
562 }
Eric Laurent83b88082014-06-20 18:31:16 -0700563
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700564 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
565 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700566 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200567 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700568 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
569 // "reuse one existing output mix" case
570 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200571 // For telephony patches, propagate voice communication use case to record side
572 if (streamType == AUDIO_STREAM_VOICE_CALL) {
573 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
574 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700575 }
jiabin01c8f562018-07-19 17:47:28 -0700576 if (mPlayback.thread()->hasFastMixer()) {
577 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700578 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700579 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700580 } else {
581 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700582 }
583
Andy Hung8d31fd22023-06-26 19:20:57 -0700584 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800585 const bool usePassthruPatchRecord =
586 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100587 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
588 const size_t recordFrameCount = mRecord.thread()->frameCount();
589 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800590 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100591 // PassthruPatchRecord producesBufferOnDemand, so use
592 // maximum of playback and record thread framecounts
593 frameCount = std::max(playbackFrameCount, recordFrameCount);
594 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
595 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700596 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700597 mRecord.thread().get(),
598 sampleRate,
599 inChannelMask,
600 format,
601 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200602 inputFlags,
603 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700604 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100605 // use a pseudo LCM between input and output framecount
606 int playbackShift = __builtin_ctz(playbackFrameCount);
607 int shift = __builtin_ctz(recordFrameCount);
608 if (playbackShift < shift) {
609 shift = playbackShift;
610 }
611 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
612 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
613 __func__, playbackFrameCount, recordFrameCount, frameCount);
614
Andy Hung8d31fd22023-06-26 19:20:57 -0700615 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700616 mRecord.thread().get(),
617 sampleRate,
618 inChannelMask,
619 format,
620 frameCount,
621 nullptr,
622 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200623 inputFlags,
624 {} /* timeout */,
625 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700626 }
627 status = mRecord.checkTrack(tempRecordTrack.get());
628 if (status != NO_ERROR) {
629 return status;
630 }
631
Eric Laurent83b88082014-06-20 18:31:16 -0700632 // create a special playback track to render to playback thread.
633 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200634
635 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
636 // it might glitch.
637 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
638 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
639 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700640 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700641 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700642 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700643 sampleRate,
644 outChannelMask,
645 format,
646 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700647 tempRecordTrack->buffer(),
648 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200649 outputFlags,
650 {} /*timeout*/,
guonaichao3acc9b12024-06-07 09:27:21 +0800651 frameCountToBeReady,
652 1.0f);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700653 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700654 if (status != NO_ERROR) {
655 return status;
656 }
Eric Laurent83b88082014-06-20 18:31:16 -0700657
658 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800659 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
660 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
661 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
662 // is alive. There is no need to hold a reference, and there is no need
663 // to clear it. In fact, since playback stopping is asynchronous, there is
664 // no proper time when clearing could be done.
665 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
666 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700667
668 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700669 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
670 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700671
672 return status;
673}
674
Andy Hung37006372023-08-31 15:24:24 -0700675void PatchPanel::Patch::clearConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700676{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700677 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
678 __func__, mRecord.handle(), mPlayback.handle());
679 mRecord.stopTrack();
680 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700681 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Andy Hung37006372023-08-31 15:24:24 -0700682 mRecord.closeConnections_l(panel);
683 mPlayback.closeConnections_l(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700684}
685
Andy Hung8e6b62a2023-07-13 18:11:33 -0700686status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700687{
688 if (!isSoftware()) return INVALID_OPERATION;
689
690 auto recordTrack = mRecord.const_track();
691 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
692
693 auto playbackTrack = mPlayback.const_track();
694 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
695
696 // Latency information for tracks may be called without obtaining
697 // the underlying thread lock.
698 //
699 // We use record server latency + playback track latency (generally smaller than the
700 // reverse due to internal biases).
701 //
702 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700703
Andy Hung30282562018-08-08 18:27:03 -0700704 // For PCM tracks get server latency.
705 if (audio_is_linear_pcm(recordTrack->format())) {
706 double recordServerLatencyMs, playbackTrackLatencyMs;
707 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
708 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
709 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
710 return OK;
711 }
712 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700713
Andy Hung30282562018-08-08 18:27:03 -0700714 // See if kernel latencies are available.
715 // If so, do a frame diff and time difference computation to estimate
716 // the total patch latency. This requires that frame counts are reported by the
717 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700718 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700719 recordTrack->getKernelFrameTime(&recordFT);
720 playbackTrack->getKernelFrameTime(&playFT);
721 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
722 const int64_t frameDiff = recordFT.frames - playFT.frames;
723 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
724
725 // It is possible that the patch track and patch record have a large time disparity because
726 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
727 // time difference based on how often we expect the timestamps to update in normal operation
728 // (typical should be no more than 50 ms).
729 //
730 // If the timestamps aren't sampled close enough, the patch latency is not
731 // considered valid.
732 //
733 // TODO: change this based on more experiments.
734 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
735 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
736 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
737 - timeDiffNs * 1e-6;
738 return OK;
739 }
740 }
741
742 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700743}
744
Andy Hung8e6b62a2023-07-13 18:11:33 -0700745String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700746{
Andy Hungc3ab7732018-06-01 13:46:29 -0700747 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800748 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
749 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
750 mRecord.const_thread().get(), mPlayback.const_thread().get());
751
752 bool hasSinkDevice =
753 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
754 bool hasSourceDevice =
755 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
756 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
757 hasSinkDevice ? "num sinks" :
758 (hasSourceDevice ? "num sources" : "no devices"),
759 hasSinkDevice ? mAudioPatch.num_sinks :
760 (hasSourceDevice ? mAudioPatch.num_sources : 0),
761 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
762 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700763
764 // add latency if it exists
765 double latencyMs;
766 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700767 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700768 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700769 return result;
770}
771
Eric Laurent951f4552014-05-20 10:48:17 -0700772/* Disconnect a patch */
Andy Hung37006372023-08-31 15:24:24 -0700773status_t PatchPanel::releaseAudioPatch_l(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700774 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200775 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
776 //before processing the release patch request.
777 NO_THREAD_SAFETY_ANALYSIS
778 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700779 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700780 status_t status = NO_ERROR;
François Gaffie150fcc62023-09-15 11:02:39 +0200781 bool doReleasePatch = true;
Eric Laurent951f4552014-05-20 10:48:17 -0700782
Mikhail Naganovdea53042018-04-26 13:10:21 -0700783 auto iter = mPatches.find(handle);
784 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700785 return BAD_VALUE;
786 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700787 Patch &removedPatch = iter->second;
François Gaffie150fcc62023-09-15 11:02:39 +0200788 const bool isSwBridge = removedPatch.isSoftware();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700789 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700790
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700791 const struct audio_port_config &src = patch.sources[0];
792 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700793 case AUDIO_PORT_TYPE_DEVICE: {
Andy Hung37006372023-08-31 15:24:24 -0700794 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule_l(src.ext.device.hw_module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700795 if (hwDevice == 0) {
796 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700797 status = BAD_VALUE;
798 break;
799 }
Eric Laurent83b88082014-06-20 18:31:16 -0700800
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700801 if (removedPatch.isSoftware()) {
Andy Hung37006372023-08-31 15:24:24 -0700802 removedPatch.clearConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700803 break;
804 }
805
Mikhail Naganovdea53042018-04-26 13:10:21 -0700806 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
807 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700808 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700809 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700810 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800811 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700812 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800813 status = BAD_VALUE;
814 break;
815 }
Eric Laurent951f4552014-05-20 10:48:17 -0700816 }
Andy Hung954b9712023-08-28 18:36:53 -0700817 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700818 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung954b9712023-08-28 18:36:53 -0700819 mAfPatchPanelCallback->mutex().lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700820 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700821 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700822 }
823 } break;
824 case AUDIO_PORT_TYPE_MIX: {
Andy Hung37006372023-08-31 15:24:24 -0700825 if (findHwDeviceByModule_l(src.ext.mix.hw_module) == 0) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700826 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700827 status = BAD_VALUE;
828 break;
829 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700830 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700831 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700832 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700833 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800834 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700835 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800836 status = BAD_VALUE;
837 break;
838 }
Eric Laurent951f4552014-05-20 10:48:17 -0700839 }
François Gaffie150fcc62023-09-15 11:02:39 +0200840 // Check whether the removed patch Hal Handle is used in another non-Endpoint patch.
841 // Since this is a non-Endpoint patch, the removed patch is not considered (it is
842 // removed later from mPatches).
843 if (removedPatch.mIsEndpointPatch) {
844 for (auto& p: mPatches) {
845 if (!p.second.mIsEndpointPatch
846 && p.second.mHalHandle == removedPatch.mHalHandle) {
847 ALOGV("%s() Sw Bridge endpoint used existing halHandle=%d, do not release",
848 __func__, p.second.mHalHandle);
849 doReleasePatch = false;
850 break;
851 }
852 }
853 }
854 if (doReleasePatch) {
855 mAfPatchPanelCallback->mutex().unlock();
856 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
857 mAfPatchPanelCallback->mutex().lock();
858 }
Eric Laurent951f4552014-05-20 10:48:17 -0700859 } break;
860 default:
861 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700862 }
863
François Gaffie150fcc62023-09-15 11:02:39 +0200864 erasePatch(handle, /* reuseExistingHalPatch= */ !doReleasePatch || isSwBridge);
Eric Laurent951f4552014-05-20 10:48:17 -0700865 return status;
866}
867
Andy Hung8e6b62a2023-07-13 18:11:33 -0700868void PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800869 mPatches.erase(handle);
870 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100871 if (!reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700872 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100873 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800874}
875
Eric Laurent951f4552014-05-20 10:48:17 -0700876/* List connected audio ports and they attributes */
Andy Hung37006372023-08-31 15:24:24 -0700877status_t PatchPanel::listAudioPatches_l(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700878 struct audio_patch *patches __unused)
879{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700880 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700881 return NO_ERROR;
882}
883
Andy Hung8e6b62a2023-07-13 18:11:33 -0700884status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700885 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700886 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700887{
888 for (const auto& module : mInsertedModules) {
889 if (module.second.streams.count(stream)) {
890 for (const auto& patchHandle : module.second.sw_patches) {
891 const auto& patch_iter = mPatches.find(patchHandle);
892 if (patch_iter != mPatches.end()) {
893 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700894 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
895 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700896 patch.mPlayback.const_thread()->id(),
897 patch.mRecord.const_thread()->id());
898 } else {
899 ALOGE("Stale patch handle in the cache: %d", patchHandle);
900 }
901 }
902 return OK;
903 }
904 }
905 // The stream is not associated with any of inserted modules.
906 return BAD_VALUE;
907}
908
Andy Hung8e6b62a2023-07-13 18:11:33 -0700909void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100910 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700911{
912 if (audioHwDevice->isInsert()) {
913 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100914 if (patch != nullptr) {
915 std::vector <SoftwarePatch> swPatches;
916 getDownstreamSoftwarePatches(stream, &swPatches);
917 if (swPatches.size() > 0) {
918 auto iter = mPatches.find(swPatches[0].getPatchHandle());
919 if (iter != mPatches.end()) {
920 *patch = iter->second.mAudioPatch;
921 }
922 }
923 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700924 }
925}
926
Andy Hung8e6b62a2023-07-13 18:11:33 -0700927void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700928{
929 for (auto& module : mInsertedModules) {
930 module.second.streams.erase(stream);
931 }
932}
933
Andy Hung37006372023-08-31 15:24:24 -0700934AudioHwDevice* PatchPanel::findAudioHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700935{
936 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung2dc61c42023-07-17 14:36:08 -0700937 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700938 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700939 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700940 return nullptr;
941 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700942 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700943}
944
Andy Hung37006372023-08-31 15:24:24 -0700945sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700946{
Andy Hung37006372023-08-31 15:24:24 -0700947 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule_l(module);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700948 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
949}
950
Andy Hung37006372023-08-31 15:24:24 -0700951void PatchPanel::addSoftwarePatchToInsertedModules_l(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100952 audio_module_handle_t module, audio_patch_handle_t handle,
953 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700954{
955 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100956 if (!mInsertedModules[module].streams.empty()) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700957 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100958 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700959}
960
Andy Hung8e6b62a2023-07-13 18:11:33 -0700961void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700962 audio_patch_handle_t handle)
963{
964 for (auto& module : mInsertedModules) {
965 module.second.sw_patches.erase(handle);
966 }
967}
968
Andy Hung8e6b62a2023-07-13 18:11:33 -0700969void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700970{
971 String8 patchPanelDump;
972 const char *indent = " ";
973
Mikhail Naganov201369b2018-05-16 16:52:32 -0700974 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700975 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800976 if (!headerPrinted) {
977 patchPanelDump += "\nPatches:\n";
978 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700979 }
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000980 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700981 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700982
983 headerPrinted = false;
984 for (const auto& module : mInsertedModules) {
985 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
986 if (!headerPrinted) {
987 patchPanelDump += "\nTracked inserted modules:\n";
988 headerPrinted = true;
989 }
990 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
991 for (const auto& stream : module.second.streams) {
992 moduleDump.appendFormat("%d ", stream);
993 }
994 moduleDump.append("; SW Patches: ");
995 for (const auto& patch : module.second.sw_patches) {
996 moduleDump.appendFormat("%d ", patch);
997 }
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000998 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700999 }
1000 }
1001
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +00001002 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +00001003 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -07001004 }
1005}
1006
Glenn Kasten63238ef2015-03-02 15:50:29 -08001007} // namespace android