blob: f11a5307beb14ff153dea41a4b337f1a49f74015 [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 Hung8e6b62a2023-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 Hung8e6b62a2023-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 Hung8e6b62a2023-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 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()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700148 removedPatch.clearConnections(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 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700174 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
175 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;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700194 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
195 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700196 status = BAD_VALUE;
197 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700198 }
199 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700200 // support only one sink if connection to a mix or across HW modules
201 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700202 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
203 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700204 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700205 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700206 status = INVALID_OPERATION;
207 goto exit;
208 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700209 // reject connection to different sink types
210 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700211 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700212 status = BAD_VALUE;
213 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700214 }
Eric Laurent951f4552014-05-20 10:48:17 -0700215 }
216
Eric Laurent3bcf8592015-04-03 12:13:24 -0700217 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700218 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700219 // - Device to device AND
220 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700221 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700222 if ((patch->num_sources == 2) ||
223 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
224 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700225 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800226 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
227 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700228 if (patch->num_sources == 2) {
229 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700230 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
231 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700232 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700233 status = INVALID_OPERATION;
234 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700235 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700236 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
237 patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700238 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700239 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700240 status = INVALID_OPERATION;
241 goto exit;
242 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700243 // existing playback thread is reused, so it is not closed when patch is cleared
244 newPatch.mPlayback.setThread(
Andy Hung87c693c2023-07-06 20:56:16 -0700245 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700246 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700247 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200248 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700249 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700250 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
251 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
252 config.sample_rate = patch->sinks[0].sample_rate;
253 }
254 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
255 config.channel_mask = patch->sinks[0].channel_mask;
256 }
257 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
258 config.format = patch->sinks[0].format;
259 }
260 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
261 flags = patch->sinks[0].flags.output;
262 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700263 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800264 patch->sinks[0].ext.device.hw_module,
265 &output,
266 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200267 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800268 outputDevice,
269 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700270 flags);
Andy Hung2dc61c42023-07-17 14:36:08 -0700271 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700272 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700273 status = NO_MEMORY;
274 goto exit;
275 }
Andy Hung87c693c2023-07-06 20:56:16 -0700276 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700277 }
Eric Laurent83b88082014-06-20 18:31:16 -0700278 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700279 String8 address = String8(patch->sources[0].ext.device.address);
280 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700281 // open input stream with source device audio properties if provided or
282 // default to peer output stream properties otherwise.
283 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
284 config.sample_rate = patch->sources[0].sample_rate;
285 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700286 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700287 }
288 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
289 config.channel_mask = patch->sources[0].channel_mask;
290 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700291 config.channel_mask = audio_channel_in_mask_from_count(
292 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700293 }
294 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
295 config.format = patch->sources[0].format;
296 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700297 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700298 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700299 audio_input_flags_t flags =
300 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
301 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700302 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200303 audio_source_t source = AUDIO_SOURCE_MIC;
304 // For telephony patches, propagate voice communication use case to record side
305 if (patch->num_sources == 2
306 && patch->sources[1].ext.mix.usecase.stream
307 == AUDIO_STREAM_VOICE_CALL) {
308 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
309 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700310 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700311 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700312 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700313 device,
314 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200315 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800316 flags,
317 outputDevice,
318 outputDeviceAddress);
Andy Hung2dc61c42023-07-17 14:36:08 -0700319 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700320 thread.get(), config.channel_mask);
321 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700322 status = NO_MEMORY;
323 goto exit;
324 }
Andy Hung87c693c2023-07-06 20:56:16 -0700325 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700326 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700327 if (status != NO_ERROR) {
328 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700329 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700330 if (audioHwDevice->isInsert()) {
331 insertedModule = audioHwDevice->handle();
332 }
Eric Laurent951f4552014-05-20 10:48:17 -0700333 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700334 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700335 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700336 patch->sinks[0].ext.mix.handle);
337 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700338 thread = mAfPatchPanelCallback->checkMmapThread_l(
339 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800340 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700341 ALOGW("%s() bad capture I/O handle %d",
342 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800343 status = BAD_VALUE;
344 goto exit;
345 }
Eric Laurent83b88082014-06-20 18:31:16 -0700346 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700347 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700348 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700349 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800350 if (status == NO_ERROR) {
351 newPatch.setThread(thread);
352 }
Eric Laurent526aa572019-01-15 10:54:58 -0800353 // remove stale audio patch with same input as sink if any
354 for (auto& iter : mPatches) {
355 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800356 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800357 break;
358 }
359 }
Eric Laurent83b88082014-06-20 18:31:16 -0700360 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700361 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
362 status = hwDevice->createAudioPatch(patch->num_sources,
363 patch->sources,
364 patch->num_sinks,
365 patch->sinks,
366 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700367 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700368 }
Eric Laurent951f4552014-05-20 10:48:17 -0700369 }
370 } break;
371 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700372 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung2dc61c42023-07-17 14:36:08 -0700373 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700374 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700375 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700376 status = BAD_VALUE;
377 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700378 }
379 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700380 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700381 for (unsigned int i = 0; i < patch->num_sinks; i++) {
382 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700383 ALOGW("%s() invalid sink type %d for mix source",
384 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700385 status = BAD_VALUE;
386 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700387 }
388 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700389 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700390 status = BAD_VALUE;
391 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700392 }
jiabinc52b1ff2019-10-31 17:20:42 -0700393 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
394 patch->sinks[i].ext.device.type);
395 device->setAddress(patch->sinks[i].ext.device.address);
396 device->applyAudioPortConfig(&patch->sinks[i]);
397 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700398 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700399 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
400 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700401 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700402 thread = mAfPatchPanelCallback->checkMmapThread_l(
403 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800404 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700405 ALOGW("%s() bad playback I/O handle %d",
406 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800407 status = BAD_VALUE;
408 goto exit;
409 }
Eric Laurent951f4552014-05-20 10:48:17 -0700410 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700411 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
412 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
414
Andy Hung2dc61c42023-07-17 14:36:08 -0700415 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700416 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700417 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800418 if (status == NO_ERROR) {
419 newPatch.setThread(thread);
420 }
Eric Laurent526aa572019-01-15 10:54:58 -0800421
422 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200423 // Prevent to remove endpoint patches (involved in a SwBridge)
424 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
425 if (!endpointPatch) {
426 for (auto& iter : mPatches) {
427 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
428 !iter.second.mIsEndpointPatch) {
429 erasePatch(iter.first);
430 break;
431 }
Eric Laurent526aa572019-01-15 10:54:58 -0800432 }
433 }
Eric Laurent951f4552014-05-20 10:48:17 -0700434 } break;
435 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700436 status = BAD_VALUE;
437 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700438 }
Eric Laurent83b88082014-06-20 18:31:16 -0700439exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700440 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700441 if (status == NO_ERROR) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700442 *handle = static_cast<audio_patch_handle_t>(
443 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700444 newPatch.mHalHandle = halHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100445 if (reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700446 mAfPatchPanelCallback->getPatchCommandThread()->updateAudioPatch(
447 oldhandle, *handle, newPatch);
François Gaffie58e73af2023-02-15 11:47:24 +0100448 } else {
Andy Hung2dc61c42023-07-17 14:36:08 -0700449 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(*handle, newPatch);
François Gaffie58e73af2023-02-15 11:47:24 +0100450 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700451 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100452 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700453 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100454 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700455 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700456 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700457 }
458 return status;
459}
460
Andy Hung8e6b62a2023-07-13 18:11:33 -0700461PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700462{
463 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
464 mRecord.handle(), mPlayback.handle());
465}
466
Andy Hung8e6b62a2023-07-13 18:11:33 -0700467status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700468{
469 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700470 status_t status = panel->createAudioPatch(
471 PatchBuilder().addSource(mAudioPatch.sources[0]).
472 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200473 mRecord.handlePtr(),
474 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700475 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700476 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700477 return status;
478 }
479
480 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700481 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700482 status = panel->createAudioPatch(
483 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200484 mPlayback.handlePtr(),
485 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700486 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700487 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700488 return status;
489 }
490 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700491 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700492 }
493
Eric Laurent83b88082014-06-20 18:31:16 -0700494 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700495 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700496 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700497 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
498 uint32_t sampleRate = mPlayback.thread()->sampleRate();
499 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700500
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700501 audio_format_t inputFormat = mRecord.thread()->format();
502 if (!audio_is_linear_pcm(inputFormat)) {
503 // The playbackThread format will say PCM for IEC61937 packetized stream.
504 // Use recordThread format.
505 format = inputFormat;
506 }
507 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
508 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700509 if (sampleRate == mRecord.thread()->sampleRate() &&
510 inChannelMask == mRecord.thread()->channelMask() &&
511 mRecord.thread()->fastTrackAvailable() &&
512 mRecord.thread()->hasFastCapture()) {
513 // Create a fast track if the record thread has fast capture to get better performance.
514 // Only enable fast mode when there is no resample needed.
515 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
516 } else {
517 // Fast mode is not available in this case.
518 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
519 }
Eric Laurent83b88082014-06-20 18:31:16 -0700520
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700521 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
522 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700523 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200524 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700525 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
526 // "reuse one existing output mix" case
527 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200528 // For telephony patches, propagate voice communication use case to record side
529 if (streamType == AUDIO_STREAM_VOICE_CALL) {
530 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
531 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700532 }
jiabin01c8f562018-07-19 17:47:28 -0700533 if (mPlayback.thread()->hasFastMixer()) {
534 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700535 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700536 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700537 } else {
538 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700539 }
540
Andy Hung8d31fd22023-06-26 19:20:57 -0700541 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800542 const bool usePassthruPatchRecord =
543 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100544 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
545 const size_t recordFrameCount = mRecord.thread()->frameCount();
546 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800547 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100548 // PassthruPatchRecord producesBufferOnDemand, so use
549 // maximum of playback and record thread framecounts
550 frameCount = std::max(playbackFrameCount, recordFrameCount);
551 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
552 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700553 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700554 mRecord.thread().get(),
555 sampleRate,
556 inChannelMask,
557 format,
558 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200559 inputFlags,
560 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700561 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100562 // use a pseudo LCM between input and output framecount
563 int playbackShift = __builtin_ctz(playbackFrameCount);
564 int shift = __builtin_ctz(recordFrameCount);
565 if (playbackShift < shift) {
566 shift = playbackShift;
567 }
568 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
569 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
570 __func__, playbackFrameCount, recordFrameCount, frameCount);
571
Andy Hung8d31fd22023-06-26 19:20:57 -0700572 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700573 mRecord.thread().get(),
574 sampleRate,
575 inChannelMask,
576 format,
577 frameCount,
578 nullptr,
579 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200580 inputFlags,
581 {} /* timeout */,
582 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700583 }
584 status = mRecord.checkTrack(tempRecordTrack.get());
585 if (status != NO_ERROR) {
586 return status;
587 }
588
Eric Laurent83b88082014-06-20 18:31:16 -0700589 // create a special playback track to render to playback thread.
590 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200591
592 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
593 // it might glitch.
594 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
595 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
596 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700597 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700598 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700599 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700600 sampleRate,
601 outChannelMask,
602 format,
603 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700604 tempRecordTrack->buffer(),
605 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200606 outputFlags,
607 {} /*timeout*/,
608 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700609 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700610 if (status != NO_ERROR) {
611 return status;
612 }
Eric Laurent83b88082014-06-20 18:31:16 -0700613
614 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800615 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
616 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
617 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
618 // is alive. There is no need to hold a reference, and there is no need
619 // to clear it. In fact, since playback stopping is asynchronous, there is
620 // no proper time when clearing could be done.
621 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
622 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700623
624 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700625 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
626 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700627
628 return status;
629}
630
Andy Hung8e6b62a2023-07-13 18:11:33 -0700631void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700632{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700633 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
634 __func__, mRecord.handle(), mPlayback.handle());
635 mRecord.stopTrack();
636 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700637 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700638 mRecord.closeConnections(panel);
639 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700640}
641
Andy Hung8e6b62a2023-07-13 18:11:33 -0700642status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700643{
644 if (!isSoftware()) return INVALID_OPERATION;
645
646 auto recordTrack = mRecord.const_track();
647 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
648
649 auto playbackTrack = mPlayback.const_track();
650 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
651
652 // Latency information for tracks may be called without obtaining
653 // the underlying thread lock.
654 //
655 // We use record server latency + playback track latency (generally smaller than the
656 // reverse due to internal biases).
657 //
658 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700659
Andy Hung30282562018-08-08 18:27:03 -0700660 // For PCM tracks get server latency.
661 if (audio_is_linear_pcm(recordTrack->format())) {
662 double recordServerLatencyMs, playbackTrackLatencyMs;
663 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
664 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
665 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
666 return OK;
667 }
668 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700669
Andy Hung30282562018-08-08 18:27:03 -0700670 // See if kernel latencies are available.
671 // If so, do a frame diff and time difference computation to estimate
672 // the total patch latency. This requires that frame counts are reported by the
673 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700674 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700675 recordTrack->getKernelFrameTime(&recordFT);
676 playbackTrack->getKernelFrameTime(&playFT);
677 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
678 const int64_t frameDiff = recordFT.frames - playFT.frames;
679 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
680
681 // It is possible that the patch track and patch record have a large time disparity because
682 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
683 // time difference based on how often we expect the timestamps to update in normal operation
684 // (typical should be no more than 50 ms).
685 //
686 // If the timestamps aren't sampled close enough, the patch latency is not
687 // considered valid.
688 //
689 // TODO: change this based on more experiments.
690 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
691 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
692 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
693 - timeDiffNs * 1e-6;
694 return OK;
695 }
696 }
697
698 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700699}
700
Andy Hung8e6b62a2023-07-13 18:11:33 -0700701String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700702{
Andy Hungc3ab7732018-06-01 13:46:29 -0700703 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800704 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
705 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
706 mRecord.const_thread().get(), mPlayback.const_thread().get());
707
708 bool hasSinkDevice =
709 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
710 bool hasSourceDevice =
711 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
712 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
713 hasSinkDevice ? "num sinks" :
714 (hasSourceDevice ? "num sources" : "no devices"),
715 hasSinkDevice ? mAudioPatch.num_sinks :
716 (hasSourceDevice ? mAudioPatch.num_sources : 0),
717 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
718 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700719
720 // add latency if it exists
721 double latencyMs;
722 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700723 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700724 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700725 return result;
726}
727
Eric Laurent951f4552014-05-20 10:48:17 -0700728/* Disconnect a patch */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700729status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700730 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200731 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
732 //before processing the release patch request.
733 NO_THREAD_SAFETY_ANALYSIS
734 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700735 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700736 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700737
Mikhail Naganovdea53042018-04-26 13:10:21 -0700738 auto iter = mPatches.find(handle);
739 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700740 return BAD_VALUE;
741 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700742 Patch &removedPatch = iter->second;
743 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700744
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700745 const struct audio_port_config &src = patch.sources[0];
746 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700747 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700748 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
749 if (hwDevice == 0) {
750 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700751 status = BAD_VALUE;
752 break;
753 }
Eric Laurent83b88082014-06-20 18:31:16 -0700754
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700755 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700756 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700757 break;
758 }
759
Mikhail Naganovdea53042018-04-26 13:10:21 -0700760 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
761 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700762 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700763 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700764 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800765 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700766 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800767 status = BAD_VALUE;
768 break;
769 }
Eric Laurent951f4552014-05-20 10:48:17 -0700770 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700771 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700772 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700773 mAfPatchPanelCallback->lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700774 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700775 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700776 }
777 } break;
778 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700779 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
780 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700781 status = BAD_VALUE;
782 break;
783 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700784 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700785 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700786 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700787 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800788 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700789 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800790 status = BAD_VALUE;
791 break;
792 }
Eric Laurent951f4552014-05-20 10:48:17 -0700793 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700794 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700795 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700796 mAfPatchPanelCallback->lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700797 } break;
798 default:
799 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700800 }
801
Eric Laurentb82e6b72019-11-22 17:25:04 -0800802 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700803 return status;
804}
805
Andy Hung8e6b62a2023-07-13 18:11:33 -0700806void PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800807 mPatches.erase(handle);
808 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100809 if (!reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700810 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100811 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800812}
813
Eric Laurent951f4552014-05-20 10:48:17 -0700814/* List connected audio ports and they attributes */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700815status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700816 struct audio_patch *patches __unused)
817{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700818 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700819 return NO_ERROR;
820}
821
Andy Hung8e6b62a2023-07-13 18:11:33 -0700822status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700823 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700824 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700825{
826 for (const auto& module : mInsertedModules) {
827 if (module.second.streams.count(stream)) {
828 for (const auto& patchHandle : module.second.sw_patches) {
829 const auto& patch_iter = mPatches.find(patchHandle);
830 if (patch_iter != mPatches.end()) {
831 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700832 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
833 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700834 patch.mPlayback.const_thread()->id(),
835 patch.mRecord.const_thread()->id());
836 } else {
837 ALOGE("Stale patch handle in the cache: %d", patchHandle);
838 }
839 }
840 return OK;
841 }
842 }
843 // The stream is not associated with any of inserted modules.
844 return BAD_VALUE;
845}
846
Andy Hung8e6b62a2023-07-13 18:11:33 -0700847void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100848 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700849{
850 if (audioHwDevice->isInsert()) {
851 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100852 if (patch != nullptr) {
853 std::vector <SoftwarePatch> swPatches;
854 getDownstreamSoftwarePatches(stream, &swPatches);
855 if (swPatches.size() > 0) {
856 auto iter = mPatches.find(swPatches[0].getPatchHandle());
857 if (iter != mPatches.end()) {
858 *patch = iter->second.mAudioPatch;
859 }
860 }
861 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700862 }
863}
864
Andy Hung8e6b62a2023-07-13 18:11:33 -0700865void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700866{
867 for (auto& module : mInsertedModules) {
868 module.second.streams.erase(stream);
869 }
870}
871
Andy Hung8e6b62a2023-07-13 18:11:33 -0700872AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700873{
874 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung2dc61c42023-07-17 14:36:08 -0700875 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700876 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700877 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700878 return nullptr;
879 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700880 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700881}
882
Andy Hung8e6b62a2023-07-13 18:11:33 -0700883sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700884{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700885 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
886 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
887}
888
Andy Hung8e6b62a2023-07-13 18:11:33 -0700889void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100890 audio_module_handle_t module, audio_patch_handle_t handle,
891 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700892{
893 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100894 if (!mInsertedModules[module].streams.empty()) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700895 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100896 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700897}
898
Andy Hung8e6b62a2023-07-13 18:11:33 -0700899void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700900 audio_patch_handle_t handle)
901{
902 for (auto& module : mInsertedModules) {
903 module.second.sw_patches.erase(handle);
904 }
905}
906
Andy Hung8e6b62a2023-07-13 18:11:33 -0700907void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700908{
909 String8 patchPanelDump;
910 const char *indent = " ";
911
Mikhail Naganov201369b2018-05-16 16:52:32 -0700912 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700913 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800914 if (!headerPrinted) {
915 patchPanelDump += "\nPatches:\n";
916 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700917 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800918 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700919 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700920
921 headerPrinted = false;
922 for (const auto& module : mInsertedModules) {
923 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
924 if (!headerPrinted) {
925 patchPanelDump += "\nTracked inserted modules:\n";
926 headerPrinted = true;
927 }
928 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
929 for (const auto& stream : module.second.streams) {
930 moduleDump.appendFormat("%d ", stream);
931 }
932 moduleDump.append("; SW Patches: ");
933 for (const auto& patch : module.second.sw_patches) {
934 moduleDump.appendFormat("%d ", patch);
935 }
936 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
937 }
938 }
939
940 if (!patchPanelDump.isEmpty()) {
941 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700942 }
943}
944
Glenn Kasten63238ef2015-03-02 15:50:29 -0800945} // namespace android