blob: 17591dd50eff84774b57dfe056f64365f0e883bc [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 Hung36346122023-08-31 15:24:24 -070086status_t PatchPanel::listAudioPorts_l(unsigned int* /* num_ports */,
Eric Laurent951f4552014-05-20 10:48:17 -070087 struct audio_port *ports __unused)
88{
Mikhail Naganovdea53042018-04-26 13:10:21 -070089 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -070090 return NO_ERROR;
91}
92
93/* Get supported attributes for a given audio port */
Andy Hung36346122023-08-31 15:24:24 -070094status_t PatchPanel::getAudioPort_l(struct audio_port_v7* port)
Eric Laurent951f4552014-05-20 10:48:17 -070095{
jiabinb4fed192020-09-22 14:45:40 -070096 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
97 // Only query the HAL when the port is a device.
98 // TODO: implement getAudioPort for mix.
99 return INVALID_OPERATION;
100 }
Andy Hung36346122023-08-31 15:24:24 -0700101 AudioHwDevice* hwDevice = findAudioHwDeviceByModule_l(port->ext.device.hw_module);
jiabinb4fed192020-09-22 14:45:40 -0700102 if (hwDevice == nullptr) {
103 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
104 return BAD_VALUE;
105 }
106 if (!hwDevice->supportsAudioPatches()) {
107 return INVALID_OPERATION;
108 }
109 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700110}
111
Eric Laurent951f4552014-05-20 10:48:17 -0700112/* Connect a patch between several source and sink ports */
Andy Hung36346122023-08-31 15:24:24 -0700113status_t PatchPanel::createAudioPatch_l(const struct audio_patch* patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200114 audio_patch_handle_t *handle,
115 bool endpointPatch)
Andy 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()) {
Andy Hung36346122023-08-31 15:24:24 -0700147 removedPatch.clearConnections_l(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 }
Andy Hung36346122023-08-31 15:24:24 -0700172 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule_l(hwModule);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700173 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;
Andy Hung36346122023-08-31 15:24:24 -0700188 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule_l(srcModule);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700189 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());
Andy Hung36346122023-08-31 15:24:24 -0700320 status = newPatch.createConnections_l(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) {
Andy Hung36346122023-08-31 15:24:24 -0700441 addSoftwarePatchToInsertedModules_l(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 {
Andy Hung36346122023-08-31 15:24:24 -0700445 newPatch.clearConnections_l(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700446 }
447 return status;
448}
449
jiabin12537fc2023-10-12 17:56:08 +0000450status_t PatchPanel::getAudioMixPort_l(const audio_port_v7 *devicePort,
451 audio_port_v7 *mixPort) {
452 if (devicePort->type != AUDIO_PORT_TYPE_DEVICE) {
453 ALOGE("%s the type of given device port is not DEVICE", __func__);
454 return INVALID_OPERATION;
455 }
456 if (mixPort->type != AUDIO_PORT_TYPE_MIX) {
457 ALOGE("%s the type of given mix port is not MIX", __func__);
458 return INVALID_OPERATION;
459 }
460 AudioHwDevice* hwDevice = findAudioHwDeviceByModule_l(devicePort->ext.device.hw_module);
461 if (hwDevice == nullptr) {
462 ALOGW("%s cannot find hw module %d", __func__, devicePort->ext.device.hw_module);
463 return BAD_VALUE;
464 }
465 return hwDevice->getAudioMixPort(devicePort, mixPort);
466}
467
Andy Hung07434ef2023-07-13 18:11:33 -0700468PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700469{
470 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
471 mRecord.handle(), mPlayback.handle());
472}
473
Andy Hung36346122023-08-31 15:24:24 -0700474status_t PatchPanel::Patch::createConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700475{
476 // create patch from source device to record thread input
Andy Hung36346122023-08-31 15:24:24 -0700477 status_t status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700478 PatchBuilder().addSource(mAudioPatch.sources[0]).
479 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200480 mRecord.handlePtr(),
481 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700482 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700483 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700484 return status;
485 }
486
487 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700488 if (mAudioPatch.num_sinks != 0) {
Andy Hung36346122023-08-31 15:24:24 -0700489 status = panel->createAudioPatch_l(
Mikhail Naganovdc769682018-05-04 15:34:08 -0700490 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200491 mPlayback.handlePtr(),
492 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700493 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700494 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700495 return status;
496 }
497 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700498 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700499 }
500
Eric Laurent83b88082014-06-20 18:31:16 -0700501 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700502 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700503 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700504 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
505 uint32_t sampleRate = mPlayback.thread()->sampleRate();
506 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700507
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700508 audio_format_t inputFormat = mRecord.thread()->format();
509 if (!audio_is_linear_pcm(inputFormat)) {
510 // The playbackThread format will say PCM for IEC61937 packetized stream.
511 // Use recordThread format.
512 format = inputFormat;
513 }
514 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
515 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700516 if (sampleRate == mRecord.thread()->sampleRate() &&
517 inChannelMask == mRecord.thread()->channelMask() &&
518 mRecord.thread()->fastTrackAvailable() &&
519 mRecord.thread()->hasFastCapture()) {
520 // Create a fast track if the record thread has fast capture to get better performance.
521 // Only enable fast mode when there is no resample needed.
522 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
523 } else {
524 // Fast mode is not available in this case.
525 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
526 }
Eric Laurent83b88082014-06-20 18:31:16 -0700527
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700528 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
529 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700530 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200531 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700532 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
533 // "reuse one existing output mix" case
534 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200535 // For telephony patches, propagate voice communication use case to record side
536 if (streamType == AUDIO_STREAM_VOICE_CALL) {
537 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
538 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700539 }
jiabin01c8f562018-07-19 17:47:28 -0700540 if (mPlayback.thread()->hasFastMixer()) {
541 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700542 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700543 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700544 } else {
545 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700546 }
547
Andy Hung3ff4b552023-06-26 19:20:57 -0700548 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800549 const bool usePassthruPatchRecord =
550 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100551 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
552 const size_t recordFrameCount = mRecord.thread()->frameCount();
553 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800554 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100555 // PassthruPatchRecord producesBufferOnDemand, so use
556 // maximum of playback and record thread framecounts
557 frameCount = std::max(playbackFrameCount, recordFrameCount);
558 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
559 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung3ff4b552023-06-26 19:20:57 -0700560 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700561 mRecord.thread().get(),
562 sampleRate,
563 inChannelMask,
564 format,
565 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200566 inputFlags,
567 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700568 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100569 // use a pseudo LCM between input and output framecount
570 int playbackShift = __builtin_ctz(playbackFrameCount);
571 int shift = __builtin_ctz(recordFrameCount);
572 if (playbackShift < shift) {
573 shift = playbackShift;
574 }
575 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
576 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
577 __func__, playbackFrameCount, recordFrameCount, frameCount);
578
Andy Hung3ff4b552023-06-26 19:20:57 -0700579 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700580 mRecord.thread().get(),
581 sampleRate,
582 inChannelMask,
583 format,
584 frameCount,
585 nullptr,
586 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200587 inputFlags,
588 {} /* timeout */,
589 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700590 }
591 status = mRecord.checkTrack(tempRecordTrack.get());
592 if (status != NO_ERROR) {
593 return status;
594 }
595
Eric Laurent83b88082014-06-20 18:31:16 -0700596 // create a special playback track to render to playback thread.
597 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200598
599 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
600 // it might glitch.
601 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
602 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
603 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung3ff4b552023-06-26 19:20:57 -0700604 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700605 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700606 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700607 sampleRate,
608 outChannelMask,
609 format,
610 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700611 tempRecordTrack->buffer(),
612 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200613 outputFlags,
614 {} /*timeout*/,
615 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700616 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700617 if (status != NO_ERROR) {
618 return status;
619 }
Eric Laurent83b88082014-06-20 18:31:16 -0700620
621 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800622 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
623 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
624 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
625 // is alive. There is no need to hold a reference, and there is no need
626 // to clear it. In fact, since playback stopping is asynchronous, there is
627 // no proper time when clearing could be done.
628 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
629 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700630
631 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700632 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
633 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700634
635 return status;
636}
637
Andy Hung36346122023-08-31 15:24:24 -0700638void PatchPanel::Patch::clearConnections_l(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700639{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700640 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
641 __func__, mRecord.handle(), mPlayback.handle());
642 mRecord.stopTrack();
643 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700644 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Andy Hung36346122023-08-31 15:24:24 -0700645 mRecord.closeConnections_l(panel);
646 mPlayback.closeConnections_l(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700647}
648
Andy Hung07434ef2023-07-13 18:11:33 -0700649status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700650{
651 if (!isSoftware()) return INVALID_OPERATION;
652
653 auto recordTrack = mRecord.const_track();
654 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
655
656 auto playbackTrack = mPlayback.const_track();
657 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
658
659 // Latency information for tracks may be called without obtaining
660 // the underlying thread lock.
661 //
662 // We use record server latency + playback track latency (generally smaller than the
663 // reverse due to internal biases).
664 //
665 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700666
Andy Hung30282562018-08-08 18:27:03 -0700667 // For PCM tracks get server latency.
668 if (audio_is_linear_pcm(recordTrack->format())) {
669 double recordServerLatencyMs, playbackTrackLatencyMs;
670 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
671 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
672 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
673 return OK;
674 }
675 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700676
Andy Hung30282562018-08-08 18:27:03 -0700677 // See if kernel latencies are available.
678 // If so, do a frame diff and time difference computation to estimate
679 // the total patch latency. This requires that frame counts are reported by the
680 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700681 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700682 recordTrack->getKernelFrameTime(&recordFT);
683 playbackTrack->getKernelFrameTime(&playFT);
684 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
685 const int64_t frameDiff = recordFT.frames - playFT.frames;
686 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
687
688 // It is possible that the patch track and patch record have a large time disparity because
689 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
690 // time difference based on how often we expect the timestamps to update in normal operation
691 // (typical should be no more than 50 ms).
692 //
693 // If the timestamps aren't sampled close enough, the patch latency is not
694 // considered valid.
695 //
696 // TODO: change this based on more experiments.
697 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
698 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
699 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
700 - timeDiffNs * 1e-6;
701 return OK;
702 }
703 }
704
705 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700706}
707
Andy Hung07434ef2023-07-13 18:11:33 -0700708String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700709{
Andy Hungc3ab7732018-06-01 13:46:29 -0700710 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800711 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
712 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
713 mRecord.const_thread().get(), mPlayback.const_thread().get());
714
715 bool hasSinkDevice =
716 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
717 bool hasSourceDevice =
718 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
719 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
720 hasSinkDevice ? "num sinks" :
721 (hasSourceDevice ? "num sources" : "no devices"),
722 hasSinkDevice ? mAudioPatch.num_sinks :
723 (hasSourceDevice ? mAudioPatch.num_sources : 0),
724 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
725 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700726
727 // add latency if it exists
728 double latencyMs;
729 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700730 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700731 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700732 return result;
733}
734
Eric Laurent951f4552014-05-20 10:48:17 -0700735/* Disconnect a patch */
Andy Hung36346122023-08-31 15:24:24 -0700736status_t PatchPanel::releaseAudioPatch_l(audio_patch_handle_t handle)
Andy Hung44f27182023-07-06 20:56:16 -0700737 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200738 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
739 //before processing the release patch request.
740 NO_THREAD_SAFETY_ANALYSIS
741 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700742 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700743 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700744
Mikhail Naganovdea53042018-04-26 13:10:21 -0700745 auto iter = mPatches.find(handle);
746 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700747 return BAD_VALUE;
748 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700749 Patch &removedPatch = iter->second;
750 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700751
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700752 const struct audio_port_config &src = patch.sources[0];
753 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700754 case AUDIO_PORT_TYPE_DEVICE: {
Andy Hung36346122023-08-31 15:24:24 -0700755 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule_l(src.ext.device.hw_module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700756 if (hwDevice == 0) {
757 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700758 status = BAD_VALUE;
759 break;
760 }
Eric Laurent83b88082014-06-20 18:31:16 -0700761
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700762 if (removedPatch.isSoftware()) {
Andy Hung36346122023-08-31 15:24:24 -0700763 removedPatch.clearConnections_l(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700764 break;
765 }
766
Mikhail Naganovdea53042018-04-26 13:10:21 -0700767 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
768 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700769 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700770 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700771 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800772 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700773 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800774 status = BAD_VALUE;
775 break;
776 }
Eric Laurent951f4552014-05-20 10:48:17 -0700777 }
Andy Hung2ac52f12023-08-28 18:36:53 -0700778 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700779 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700780 mAfPatchPanelCallback->mutex().lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700781 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700782 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700783 }
784 } break;
785 case AUDIO_PORT_TYPE_MIX: {
Andy Hung36346122023-08-31 15:24:24 -0700786 if (findHwDeviceByModule_l(src.ext.mix.hw_module) == 0) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700787 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700788 status = BAD_VALUE;
789 break;
790 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700791 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700792 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700793 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700794 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800795 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700796 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800797 status = BAD_VALUE;
798 break;
799 }
Eric Laurent951f4552014-05-20 10:48:17 -0700800 }
Andy Hung2ac52f12023-08-28 18:36:53 -0700801 mAfPatchPanelCallback->mutex().unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700802 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2ac52f12023-08-28 18:36:53 -0700803 mAfPatchPanelCallback->mutex().lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700804 } break;
805 default:
806 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700807 }
808
Eric Laurentb82e6b72019-11-22 17:25:04 -0800809 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700810 return status;
811}
812
Andy Hung07434ef2023-07-13 18:11:33 -0700813void PatchPanel::erasePatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800814 mPatches.erase(handle);
815 removeSoftwarePatchFromInsertedModules(handle);
Andy Hung68631eb2023-07-17 14:36:08 -0700816 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800817}
818
Eric Laurent951f4552014-05-20 10:48:17 -0700819/* List connected audio ports and they attributes */
Andy Hung36346122023-08-31 15:24:24 -0700820status_t PatchPanel::listAudioPatches_l(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700821 struct audio_patch *patches __unused)
822{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700823 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700824 return NO_ERROR;
825}
826
Andy Hung07434ef2023-07-13 18:11:33 -0700827status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700828 audio_io_handle_t stream,
Andy Hungd63e79d2023-07-13 16:52:46 -0700829 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700830{
831 for (const auto& module : mInsertedModules) {
832 if (module.second.streams.count(stream)) {
833 for (const auto& patchHandle : module.second.sw_patches) {
834 const auto& patch_iter = mPatches.find(patchHandle);
835 if (patch_iter != mPatches.end()) {
836 const Patch &patch = patch_iter->second;
Andy Hungd63e79d2023-07-13 16:52:46 -0700837 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
838 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700839 patch.mPlayback.const_thread()->id(),
840 patch.mRecord.const_thread()->id());
841 } else {
842 ALOGE("Stale patch handle in the cache: %d", patchHandle);
843 }
844 }
845 return OK;
846 }
847 }
848 // The stream is not associated with any of inserted modules.
849 return BAD_VALUE;
850}
851
Andy Hung07434ef2023-07-13 18:11:33 -0700852void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100853 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700854{
855 if (audioHwDevice->isInsert()) {
856 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100857 if (patch != nullptr) {
858 std::vector <SoftwarePatch> swPatches;
859 getDownstreamSoftwarePatches(stream, &swPatches);
860 if (swPatches.size() > 0) {
861 auto iter = mPatches.find(swPatches[0].getPatchHandle());
862 if (iter != mPatches.end()) {
863 *patch = iter->second.mAudioPatch;
864 }
865 }
866 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700867 }
868}
869
Andy Hung07434ef2023-07-13 18:11:33 -0700870void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700871{
872 for (auto& module : mInsertedModules) {
873 module.second.streams.erase(stream);
874 }
875}
876
Andy Hung36346122023-08-31 15:24:24 -0700877AudioHwDevice* PatchPanel::findAudioHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700878{
879 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung68631eb2023-07-17 14:36:08 -0700880 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700881 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700882 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700883 return nullptr;
884 }
Andy Hung68631eb2023-07-17 14:36:08 -0700885 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700886}
887
Andy Hung36346122023-08-31 15:24:24 -0700888sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule_l(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700889{
Andy Hung36346122023-08-31 15:24:24 -0700890 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule_l(module);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700891 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
892}
893
Andy Hung36346122023-08-31 15:24:24 -0700894void PatchPanel::addSoftwarePatchToInsertedModules_l(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100895 audio_module_handle_t module, audio_patch_handle_t handle,
896 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700897{
898 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100899 if (!mInsertedModules[module].streams.empty()) {
Andy Hung68631eb2023-07-17 14:36:08 -0700900 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100901 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700902}
903
Andy Hung07434ef2023-07-13 18:11:33 -0700904void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700905 audio_patch_handle_t handle)
906{
907 for (auto& module : mInsertedModules) {
908 module.second.sw_patches.erase(handle);
909 }
910}
911
Andy Hung07434ef2023-07-13 18:11:33 -0700912void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700913{
914 String8 patchPanelDump;
915 const char *indent = " ";
916
Mikhail Naganov201369b2018-05-16 16:52:32 -0700917 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700918 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800919 if (!headerPrinted) {
920 patchPanelDump += "\nPatches:\n";
921 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700922 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000923 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700924 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700925
926 headerPrinted = false;
927 for (const auto& module : mInsertedModules) {
928 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
929 if (!headerPrinted) {
930 patchPanelDump += "\nTracked inserted modules:\n";
931 headerPrinted = true;
932 }
933 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
934 for (const auto& stream : module.second.streams) {
935 moduleDump.appendFormat("%d ", stream);
936 }
937 moduleDump.append("; SW Patches: ");
938 for (const auto& patch : module.second.sw_patches) {
939 moduleDump.appendFormat("%d ", patch);
940 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000941 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700942 }
943 }
944
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000945 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000946 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700947 }
948}
949
Glenn Kasten63238ef2015-03-02 15:50:29 -0800950} // namespace android