blob: 77ca0131011f91e225bf400071af301ff7ffe6d6 [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 Hung07434ef2023-07-13 18:11:33 -070022#include "PatchPanel.h"
Andy Hunga7926fd2023-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 Hunga7926fd2023-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 Hungd63e79d2023-07-13 16:52:46 -070050/* static */
Andy Hung68631eb2023-07-17 14:36:08 -070051sp<IAfPatchPanel> IAfPatchPanel::create(const sp<IAfPatchPanelCallback>& afPatchPanelCallback) {
52 return sp<PatchPanel>::make(afPatchPanelCallback);
Andy Hungd63e79d2023-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 Hung07434ef2023-07-13 18:11:33 -070059status_t PatchPanel::getLatencyMs_l(
Andy Hungd63e79d2023-07-13 16:52:46 -070060 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -070061{
Andy Hungd63e79d2023-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 Hung07434ef2023-07-13 18:11:33 -070070void PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
Andy Hungd63e79d2023-07-13 16:52:46 -070071{
72 if (const auto recordThread = thread->asIAfRecordThread();
73 recordThread) {
Andy Hung68631eb2023-07-17 14:36:08 -070074 mAfPatchPanelCallback->closeThreadInternal_l(recordThread);
Andy Hungd63e79d2023-07-13 16:52:46 -070075 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
76 playbackThread) {
Andy Hung68631eb2023-07-17 14:36:08 -070077 mAfPatchPanelCallback->closeThreadInternal_l(playbackThread);
Andy Hungd63e79d2023-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 Hung07434ef2023-07-13 18:11:33 -070086status_t PatchPanel::listAudioPorts(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 Hung07434ef2023-07-13 18:11:33 -070094status_t PatchPanel::getAudioPort(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 }
101 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
102 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 Hung07434ef2023-07-13 18:11:33 -0700113status_t PatchPanel::createAudioPatch(const struct audio_patch* patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200114 audio_patch_handle_t *handle,
115 bool endpointPatch)
Andy Hung44f27182023-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 }
Eric Laurent951f4552014-05-20 10:48:17 -0700137
Eric Laurent83b88082014-06-20 18:31:16 -0700138 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700139 auto iter = mPatches.find(*handle);
140 if (iter != mPatches.end()) {
141 ALOGV("%s() removing patch handle %d", __func__, *handle);
142 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700143 // free resources owned by the removed patch if applicable
144 // 1) if a software patch is present, release the playback and capture threads and
145 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700146 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700147 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700148 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700149 // 2) if the new patch and old patch source or sink are devices from different
150 // hw modules, clear the audio HAL patches now because they will not be updated
151 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700152 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700153 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
154 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
155 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
156 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
157 oldPatch.sources[0].ext.device.hw_module !=
158 patch->sources[0].ext.device.hw_module)) {
159 hwModule = oldPatch.sources[0].ext.device.hw_module;
160 } else if (patch->num_sinks == 0 ||
161 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
162 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
163 oldPatch.sinks[0].ext.device.hw_module !=
164 patch->sinks[0].ext.device.hw_module))) {
165 // Note on (patch->num_sinks == 0): this situation should not happen as
166 // these special patches are only created by the policy manager but just
167 // in case, systematically clear the HAL patch.
168 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700169 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700170 hwModule = oldPatch.sinks[0].ext.device.hw_module;
171 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700172 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
173 if (hwDevice != 0) {
174 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700175 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800176 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800178 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700179 }
180 }
181
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200182 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700183 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700184
Eric Laurent951f4552014-05-20 10:48:17 -0700185 switch (patch->sources[0].type) {
186 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700187 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700188 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
189 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700190 status = BAD_VALUE;
191 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700192 }
193 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700194 // support only one sink if connection to a mix or across HW modules
195 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700196 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
197 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700198 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700199 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700200 status = INVALID_OPERATION;
201 goto exit;
202 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700203 // reject connection to different sink types
204 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700205 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700206 status = BAD_VALUE;
207 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700208 }
Eric Laurent951f4552014-05-20 10:48:17 -0700209 }
210
Eric Laurent3bcf8592015-04-03 12:13:24 -0700211 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700212 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700213 // - Device to device AND
214 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700215 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700216 if ((patch->num_sources == 2) ||
217 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
218 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700219 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800220 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
221 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700222 if (patch->num_sources == 2) {
223 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700224 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
225 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700226 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700227 status = INVALID_OPERATION;
228 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700229 }
Andy Hung68631eb2023-07-17 14:36:08 -0700230 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
231 patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700232 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700233 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700234 status = INVALID_OPERATION;
235 goto exit;
236 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700237 // existing playback thread is reused, so it is not closed when patch is cleared
238 newPatch.mPlayback.setThread(
Andy Hung44f27182023-07-06 20:56:16 -0700239 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700240 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700241 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200242 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700243 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700244 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
245 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
246 config.sample_rate = patch->sinks[0].sample_rate;
247 }
248 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
249 config.channel_mask = patch->sinks[0].channel_mask;
250 }
251 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
252 config.format = patch->sinks[0].format;
253 }
254 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
255 flags = patch->sinks[0].flags.output;
256 }
Andy Hung68631eb2023-07-17 14:36:08 -0700257 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800258 patch->sinks[0].ext.device.hw_module,
259 &output,
260 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200261 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800262 outputDevice,
263 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700264 flags);
Andy Hung68631eb2023-07-17 14:36:08 -0700265 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700266 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700267 status = NO_MEMORY;
268 goto exit;
269 }
Andy Hung44f27182023-07-06 20:56:16 -0700270 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700271 }
Eric Laurent83b88082014-06-20 18:31:16 -0700272 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700273 String8 address = String8(patch->sources[0].ext.device.address);
274 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700275 // open input stream with source device audio properties if provided or
276 // default to peer output stream properties otherwise.
277 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
278 config.sample_rate = patch->sources[0].sample_rate;
279 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700280 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700281 }
282 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
283 config.channel_mask = patch->sources[0].channel_mask;
284 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700285 config.channel_mask = audio_channel_in_mask_from_count(
286 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700287 }
288 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
289 config.format = patch->sources[0].format;
290 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700291 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700292 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700293 audio_input_flags_t flags =
294 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
295 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700296 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200297 audio_source_t source = AUDIO_SOURCE_MIC;
298 // For telephony patches, propagate voice communication use case to record side
299 if (patch->num_sources == 2
300 && patch->sources[1].ext.mix.usecase.stream
301 == AUDIO_STREAM_VOICE_CALL) {
302 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
303 }
Andy Hung68631eb2023-07-17 14:36:08 -0700304 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700305 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700306 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700307 device,
308 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200309 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800310 flags,
311 outputDevice,
312 outputDeviceAddress);
Andy Hung68631eb2023-07-17 14:36:08 -0700313 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700314 thread.get(), config.channel_mask);
315 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700316 status = NO_MEMORY;
317 goto exit;
318 }
Andy Hung44f27182023-07-06 20:56:16 -0700319 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700320 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700321 if (status != NO_ERROR) {
322 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700323 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700324 if (audioHwDevice->isInsert()) {
325 insertedModule = audioHwDevice->handle();
326 }
Eric Laurent951f4552014-05-20 10:48:17 -0700327 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700328 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung68631eb2023-07-17 14:36:08 -0700329 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700330 patch->sinks[0].ext.mix.handle);
331 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700332 thread = mAfPatchPanelCallback->checkMmapThread_l(
333 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800334 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700335 ALOGW("%s() bad capture I/O handle %d",
336 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800337 status = BAD_VALUE;
338 goto exit;
339 }
Eric Laurent83b88082014-06-20 18:31:16 -0700340 }
Andy Hung2ac52f12023-08-28 18:36:53 -0700341 mAfPatchPanelCallback->mutex().unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700342 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700343 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800344 if (status == NO_ERROR) {
345 newPatch.setThread(thread);
346 }
Eric Laurent526aa572019-01-15 10:54:58 -0800347 // remove stale audio patch with same input as sink if any
348 for (auto& iter : mPatches) {
349 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800350 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800351 break;
352 }
353 }
Eric Laurent83b88082014-06-20 18:31:16 -0700354 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700355 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
356 status = hwDevice->createAudioPatch(patch->num_sources,
357 patch->sources,
358 patch->num_sinks,
359 patch->sinks,
360 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700361 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700362 }
Eric Laurent951f4552014-05-20 10:48:17 -0700363 }
364 } break;
365 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700366 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung68631eb2023-07-17 14:36:08 -0700367 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700368 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700369 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700370 status = BAD_VALUE;
371 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700372 }
373 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700374 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700375 for (unsigned int i = 0; i < patch->num_sinks; i++) {
376 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700377 ALOGW("%s() invalid sink type %d for mix source",
378 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700379 status = BAD_VALUE;
380 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700381 }
382 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700383 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700384 status = BAD_VALUE;
385 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700386 }
jiabinc52b1ff2019-10-31 17:20:42 -0700387 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
388 patch->sinks[i].ext.device.type);
389 device->setAddress(patch->sinks[i].ext.device.address);
390 device->applyAudioPortConfig(&patch->sinks[i]);
391 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700392 }
Andy Hung68631eb2023-07-17 14:36:08 -0700393 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
394 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700395 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700396 thread = mAfPatchPanelCallback->checkMmapThread_l(
397 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800398 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700399 ALOGW("%s() bad playback I/O handle %d",
400 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800401 status = BAD_VALUE;
402 goto exit;
403 }
Eric Laurent951f4552014-05-20 10:48:17 -0700404 }
Andy Hung68631eb2023-07-17 14:36:08 -0700405 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
406 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700407 }
408
Andy Hung2ac52f12023-08-28 18:36:53 -0700409 mAfPatchPanelCallback->mutex().unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700410 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700411 mAfPatchPanelCallback->mutex().lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800412 if (status == NO_ERROR) {
413 newPatch.setThread(thread);
414 }
Eric Laurent526aa572019-01-15 10:54:58 -0800415
416 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200417 // Prevent to remove endpoint patches (involved in a SwBridge)
418 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
419 if (!endpointPatch) {
420 for (auto& iter : mPatches) {
421 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
422 !iter.second.mIsEndpointPatch) {
423 erasePatch(iter.first);
424 break;
425 }
Eric Laurent526aa572019-01-15 10:54:58 -0800426 }
427 }
Eric Laurent951f4552014-05-20 10:48:17 -0700428 } break;
429 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700430 status = BAD_VALUE;
431 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700432 }
Eric Laurent83b88082014-06-20 18:31:16 -0700433exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700434 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700435 if (status == NO_ERROR) {
Andy Hung68631eb2023-07-17 14:36:08 -0700436 *handle = static_cast<audio_patch_handle_t>(
437 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700438 newPatch.mHalHandle = halHandle;
Andy Hung68631eb2023-07-17 14:36:08 -0700439 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700440 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100441 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700442 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100443 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700444 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700445 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700446 }
447 return status;
448}
449
Andy Hung07434ef2023-07-13 18:11:33 -0700450PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700451{
452 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
453 mRecord.handle(), mPlayback.handle());
454}
455
Andy Hung07434ef2023-07-13 18:11:33 -0700456status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700457{
458 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700459 status_t status = panel->createAudioPatch(
460 PatchBuilder().addSource(mAudioPatch.sources[0]).
461 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200462 mRecord.handlePtr(),
463 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700464 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700465 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700466 return status;
467 }
468
469 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700470 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700471 status = panel->createAudioPatch(
472 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200473 mPlayback.handlePtr(),
474 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700475 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700476 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700477 return status;
478 }
479 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700480 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700481 }
482
Eric Laurent83b88082014-06-20 18:31:16 -0700483 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700484 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700485 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700486 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
487 uint32_t sampleRate = mPlayback.thread()->sampleRate();
488 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700489
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700490 audio_format_t inputFormat = mRecord.thread()->format();
491 if (!audio_is_linear_pcm(inputFormat)) {
492 // The playbackThread format will say PCM for IEC61937 packetized stream.
493 // Use recordThread format.
494 format = inputFormat;
495 }
496 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
497 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700498 if (sampleRate == mRecord.thread()->sampleRate() &&
499 inChannelMask == mRecord.thread()->channelMask() &&
500 mRecord.thread()->fastTrackAvailable() &&
501 mRecord.thread()->hasFastCapture()) {
502 // Create a fast track if the record thread has fast capture to get better performance.
503 // Only enable fast mode when there is no resample needed.
504 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
505 } else {
506 // Fast mode is not available in this case.
507 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
508 }
Eric Laurent83b88082014-06-20 18:31:16 -0700509
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700510 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
511 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700512 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200513 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700514 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
515 // "reuse one existing output mix" case
516 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200517 // For telephony patches, propagate voice communication use case to record side
518 if (streamType == AUDIO_STREAM_VOICE_CALL) {
519 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
520 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700521 }
jiabin01c8f562018-07-19 17:47:28 -0700522 if (mPlayback.thread()->hasFastMixer()) {
523 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700524 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700525 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700526 } else {
527 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700528 }
529
Andy Hung3ff4b552023-06-26 19:20:57 -0700530 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800531 const bool usePassthruPatchRecord =
532 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100533 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
534 const size_t recordFrameCount = mRecord.thread()->frameCount();
535 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800536 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100537 // PassthruPatchRecord producesBufferOnDemand, so use
538 // maximum of playback and record thread framecounts
539 frameCount = std::max(playbackFrameCount, recordFrameCount);
540 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
541 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung3ff4b552023-06-26 19:20:57 -0700542 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700543 mRecord.thread().get(),
544 sampleRate,
545 inChannelMask,
546 format,
547 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200548 inputFlags,
549 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700550 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100551 // use a pseudo LCM between input and output framecount
552 int playbackShift = __builtin_ctz(playbackFrameCount);
553 int shift = __builtin_ctz(recordFrameCount);
554 if (playbackShift < shift) {
555 shift = playbackShift;
556 }
557 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
558 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
559 __func__, playbackFrameCount, recordFrameCount, frameCount);
560
Andy Hung3ff4b552023-06-26 19:20:57 -0700561 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700562 mRecord.thread().get(),
563 sampleRate,
564 inChannelMask,
565 format,
566 frameCount,
567 nullptr,
568 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200569 inputFlags,
570 {} /* timeout */,
571 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700572 }
573 status = mRecord.checkTrack(tempRecordTrack.get());
574 if (status != NO_ERROR) {
575 return status;
576 }
577
Eric Laurent83b88082014-06-20 18:31:16 -0700578 // create a special playback track to render to playback thread.
579 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200580
581 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
582 // it might glitch.
583 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
584 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
585 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung3ff4b552023-06-26 19:20:57 -0700586 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700587 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700588 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700589 sampleRate,
590 outChannelMask,
591 format,
592 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700593 tempRecordTrack->buffer(),
594 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200595 outputFlags,
596 {} /*timeout*/,
597 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700598 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700599 if (status != NO_ERROR) {
600 return status;
601 }
Eric Laurent83b88082014-06-20 18:31:16 -0700602
603 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800604 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
605 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
606 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
607 // is alive. There is no need to hold a reference, and there is no need
608 // to clear it. In fact, since playback stopping is asynchronous, there is
609 // no proper time when clearing could be done.
610 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
611 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700612
613 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700614 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
615 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700616
617 return status;
618}
619
Andy Hung07434ef2023-07-13 18:11:33 -0700620void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700621{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700622 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
623 __func__, mRecord.handle(), mPlayback.handle());
624 mRecord.stopTrack();
625 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700626 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700627 mRecord.closeConnections(panel);
628 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700629}
630
Andy Hung07434ef2023-07-13 18:11:33 -0700631status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700632{
633 if (!isSoftware()) return INVALID_OPERATION;
634
635 auto recordTrack = mRecord.const_track();
636 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
637
638 auto playbackTrack = mPlayback.const_track();
639 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
640
641 // Latency information for tracks may be called without obtaining
642 // the underlying thread lock.
643 //
644 // We use record server latency + playback track latency (generally smaller than the
645 // reverse due to internal biases).
646 //
647 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700648
Andy Hung30282562018-08-08 18:27:03 -0700649 // For PCM tracks get server latency.
650 if (audio_is_linear_pcm(recordTrack->format())) {
651 double recordServerLatencyMs, playbackTrackLatencyMs;
652 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
653 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
654 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
655 return OK;
656 }
657 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700658
Andy Hung30282562018-08-08 18:27:03 -0700659 // See if kernel latencies are available.
660 // If so, do a frame diff and time difference computation to estimate
661 // the total patch latency. This requires that frame counts are reported by the
662 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700663 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700664 recordTrack->getKernelFrameTime(&recordFT);
665 playbackTrack->getKernelFrameTime(&playFT);
666 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
667 const int64_t frameDiff = recordFT.frames - playFT.frames;
668 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
669
670 // It is possible that the patch track and patch record have a large time disparity because
671 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
672 // time difference based on how often we expect the timestamps to update in normal operation
673 // (typical should be no more than 50 ms).
674 //
675 // If the timestamps aren't sampled close enough, the patch latency is not
676 // considered valid.
677 //
678 // TODO: change this based on more experiments.
679 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
680 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
681 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
682 - timeDiffNs * 1e-6;
683 return OK;
684 }
685 }
686
687 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700688}
689
Andy Hung07434ef2023-07-13 18:11:33 -0700690String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700691{
Andy Hungc3ab7732018-06-01 13:46:29 -0700692 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800693 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
694 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
695 mRecord.const_thread().get(), mPlayback.const_thread().get());
696
697 bool hasSinkDevice =
698 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
699 bool hasSourceDevice =
700 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
701 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
702 hasSinkDevice ? "num sinks" :
703 (hasSourceDevice ? "num sources" : "no devices"),
704 hasSinkDevice ? mAudioPatch.num_sinks :
705 (hasSourceDevice ? mAudioPatch.num_sources : 0),
706 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
707 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700708
709 // add latency if it exists
710 double latencyMs;
711 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700712 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700713 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700714 return result;
715}
716
Eric Laurent951f4552014-05-20 10:48:17 -0700717/* Disconnect a patch */
Andy Hung07434ef2023-07-13 18:11:33 -0700718status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung44f27182023-07-06 20:56:16 -0700719 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200720 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
721 //before processing the release patch request.
722 NO_THREAD_SAFETY_ANALYSIS
723 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700724 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700725 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700726
Mikhail Naganovdea53042018-04-26 13:10:21 -0700727 auto iter = mPatches.find(handle);
728 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700729 return BAD_VALUE;
730 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700731 Patch &removedPatch = iter->second;
732 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700733
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700734 const struct audio_port_config &src = patch.sources[0];
735 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700736 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700737 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
738 if (hwDevice == 0) {
739 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700740 status = BAD_VALUE;
741 break;
742 }
Eric Laurent83b88082014-06-20 18:31:16 -0700743
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700744 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700745 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700746 break;
747 }
748
Mikhail Naganovdea53042018-04-26 13:10:21 -0700749 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
750 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700751 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700752 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700753 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800754 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700755 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800756 status = BAD_VALUE;
757 break;
758 }
Eric Laurent951f4552014-05-20 10:48:17 -0700759 }
Andy Hung2ac52f12023-08-28 18:36:53 -0700760 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700761 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700762 mAfPatchPanelCallback->mutex().lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700763 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700764 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700765 }
766 } break;
767 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700768 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
769 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700770 status = BAD_VALUE;
771 break;
772 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700773 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700774 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700775 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700776 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800777 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700778 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800779 status = BAD_VALUE;
780 break;
781 }
Eric Laurent951f4552014-05-20 10:48:17 -0700782 }
Andy Hung2ac52f12023-08-28 18:36:53 -0700783 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700784 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700785 mAfPatchPanelCallback->mutex().lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700786 } break;
787 default:
788 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700789 }
790
Eric Laurentb82e6b72019-11-22 17:25:04 -0800791 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700792 return status;
793}
794
Andy Hung07434ef2023-07-13 18:11:33 -0700795void PatchPanel::erasePatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800796 mPatches.erase(handle);
797 removeSoftwarePatchFromInsertedModules(handle);
Andy Hung68631eb2023-07-17 14:36:08 -0700798 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800799}
800
Eric Laurent951f4552014-05-20 10:48:17 -0700801/* List connected audio ports and they attributes */
Andy Hung07434ef2023-07-13 18:11:33 -0700802status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700803 struct audio_patch *patches __unused)
804{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700805 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700806 return NO_ERROR;
807}
808
Andy Hung07434ef2023-07-13 18:11:33 -0700809status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700810 audio_io_handle_t stream,
Andy Hungd63e79d2023-07-13 16:52:46 -0700811 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700812{
813 for (const auto& module : mInsertedModules) {
814 if (module.second.streams.count(stream)) {
815 for (const auto& patchHandle : module.second.sw_patches) {
816 const auto& patch_iter = mPatches.find(patchHandle);
817 if (patch_iter != mPatches.end()) {
818 const Patch &patch = patch_iter->second;
Andy Hungd63e79d2023-07-13 16:52:46 -0700819 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
820 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700821 patch.mPlayback.const_thread()->id(),
822 patch.mRecord.const_thread()->id());
823 } else {
824 ALOGE("Stale patch handle in the cache: %d", patchHandle);
825 }
826 }
827 return OK;
828 }
829 }
830 // The stream is not associated with any of inserted modules.
831 return BAD_VALUE;
832}
833
Andy Hung07434ef2023-07-13 18:11:33 -0700834void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100835 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700836{
837 if (audioHwDevice->isInsert()) {
838 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100839 if (patch != nullptr) {
840 std::vector <SoftwarePatch> swPatches;
841 getDownstreamSoftwarePatches(stream, &swPatches);
842 if (swPatches.size() > 0) {
843 auto iter = mPatches.find(swPatches[0].getPatchHandle());
844 if (iter != mPatches.end()) {
845 *patch = iter->second.mAudioPatch;
846 }
847 }
848 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700849 }
850}
851
Andy Hung07434ef2023-07-13 18:11:33 -0700852void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700853{
854 for (auto& module : mInsertedModules) {
855 module.second.streams.erase(stream);
856 }
857}
858
Andy Hung07434ef2023-07-13 18:11:33 -0700859AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700860{
861 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung68631eb2023-07-17 14:36:08 -0700862 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700863 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700864 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700865 return nullptr;
866 }
Andy Hung68631eb2023-07-17 14:36:08 -0700867 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700868}
869
Andy Hung07434ef2023-07-13 18:11:33 -0700870sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700871{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700872 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
873 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
874}
875
Andy Hung07434ef2023-07-13 18:11:33 -0700876void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100877 audio_module_handle_t module, audio_patch_handle_t handle,
878 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700879{
880 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100881 if (!mInsertedModules[module].streams.empty()) {
Andy Hung68631eb2023-07-17 14:36:08 -0700882 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100883 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700884}
885
Andy Hung07434ef2023-07-13 18:11:33 -0700886void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700887 audio_patch_handle_t handle)
888{
889 for (auto& module : mInsertedModules) {
890 module.second.sw_patches.erase(handle);
891 }
892}
893
Andy Hung07434ef2023-07-13 18:11:33 -0700894void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700895{
896 String8 patchPanelDump;
897 const char *indent = " ";
898
Mikhail Naganov201369b2018-05-16 16:52:32 -0700899 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700900 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800901 if (!headerPrinted) {
902 patchPanelDump += "\nPatches:\n";
903 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700904 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000905 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700906 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700907
908 headerPrinted = false;
909 for (const auto& module : mInsertedModules) {
910 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
911 if (!headerPrinted) {
912 patchPanelDump += "\nTracked inserted modules:\n";
913 headerPrinted = true;
914 }
915 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
916 for (const auto& stream : module.second.streams) {
917 moduleDump.appendFormat("%d ", stream);
918 }
919 moduleDump.append("; SW Patches: ");
920 for (const auto& patch : module.second.sw_patches) {
921 moduleDump.appendFormat("%d ", patch);
922 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000923 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700924 }
925 }
926
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000927 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000928 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700929 }
930}
931
Glenn Kasten63238ef2015-03-02 15:50:29 -0800932} // namespace android