blob: 32513456c7b21215042c87ac9190197507c93d92 [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
22#include "Configuration.h"
23#include <utils/Log.h>
24#include <audio_utils/primitives.h>
25
26#include "AudioFlinger.h"
Andy Hung8e6b62a2023-07-13 18:11:33 -070027#include "PatchPanel.h"
Eric Laurent951f4552014-05-20 10:48:17 -070028#include <media/AudioParameter.h>
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080029#include <media/AudioValidator.h>
jiabinc52b1ff2019-10-31 17:20:42 -070030#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070031#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070032#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070033
34// ----------------------------------------------------------------------------
35
36// Note: the following macro is used for extremely verbose logging message. In
37// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
38// 0; but one side effect of this is to turn all LOGV's as well. Some messages
39// are so verbose that we want to suppress them even when we have ALOG_ASSERT
40// turned on. Do not uncomment the #def below unless you really know what you
41// are doing and want to see all of the extremely verbose messages.
42//#define VERY_VERY_VERBOSE_LOGGING
43#ifdef VERY_VERY_VERBOSE_LOGGING
44#define ALOGVV ALOGV
45#else
46#define ALOGVV(a...) do { } while(0)
47#endif
48
49namespace android {
50
51/* List connected audio ports and their attributes */
52status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
Andy Hungcdd80ef2023-07-17 11:37:26 -070053 struct audio_port* ports) const
Eric Laurent951f4552014-05-20 10:48:17 -070054{
55 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070056 return mPatchPanel->listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070057}
58
59/* Get supported attributes for a given audio port */
Andy Hungcdd80ef2023-07-17 11:37:26 -070060status_t AudioFlinger::getAudioPort(struct audio_port_v7* port) const {
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080061 status_t status = AudioValidator::validateAudioPort(*port);
62 if (status != NO_ERROR) {
63 return status;
64 }
65
Eric Laurent951f4552014-05-20 10:48:17 -070066 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070067 return mPatchPanel->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070068}
69
Eric Laurent951f4552014-05-20 10:48:17 -070070/* Connect a patch between several source and sink ports */
71status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
72 audio_patch_handle_t *handle)
73{
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080074 status_t status = AudioValidator::validateAudioPatch(*patch);
75 if (status != NO_ERROR) {
76 return status;
77 }
78
Eric Laurent951f4552014-05-20 10:48:17 -070079 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070080 return mPatchPanel->createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070081}
82
83/* Disconnect a patch */
84status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
85{
86 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070087 return mPatchPanel->releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070088}
89
Eric Laurent951f4552014-05-20 10:48:17 -070090/* List connected audio ports and they attributes */
Andy Hungcdd80ef2023-07-17 11:37:26 -070091status_t AudioFlinger::listAudioPatches(
92 unsigned int* num_patches, struct audio_patch* patches) const
Eric Laurent951f4552014-05-20 10:48:17 -070093{
94 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070095 return mPatchPanel->listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070096}
97
Andy Hungb6692eb2023-07-13 16:52:46 -070098/* static */
Andy Hung2dc61c42023-07-17 14:36:08 -070099sp<IAfPatchPanel> IAfPatchPanel::create(const sp<IAfPatchPanelCallback>& afPatchPanelCallback) {
100 return sp<PatchPanel>::make(afPatchPanelCallback);
Andy Hungb6692eb2023-07-13 16:52:46 -0700101}
102
103status_t SoftwarePatch::getLatencyMs_l(double* latencyMs) const {
104 return mPatchPanel->getLatencyMs_l(mPatchHandle, latencyMs);
105}
106
Andy Hung8e6b62a2023-07-13 18:11:33 -0700107status_t PatchPanel::getLatencyMs_l(
Andy Hungb6692eb2023-07-13 16:52:46 -0700108 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700109{
Andy Hungb6692eb2023-07-13 16:52:46 -0700110 const auto& iter = mPatches.find(patchHandle);
111 if (iter != mPatches.end()) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700112 return iter->second.getLatencyMs(latencyMs);
113 } else {
114 return BAD_VALUE;
115 }
116}
117
Andy Hung8e6b62a2023-07-13 18:11:33 -0700118void PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
Andy Hungb6692eb2023-07-13 16:52:46 -0700119{
120 if (const auto recordThread = thread->asIAfRecordThread();
121 recordThread) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700122 mAfPatchPanelCallback->closeThreadInternal_l(recordThread);
Andy Hungb6692eb2023-07-13 16:52:46 -0700123 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
124 playbackThread) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700125 mAfPatchPanelCallback->closeThreadInternal_l(playbackThread);
Andy Hungb6692eb2023-07-13 16:52:46 -0700126 } else {
127 LOG_ALWAYS_FATAL("%s: Endpoints only accept IAfPlayback and IAfRecord threads, "
128 "invalid thread, id: %d type: %d",
129 __func__, thread->id(), thread->type());
130 }
131}
132
Eric Laurent951f4552014-05-20 10:48:17 -0700133/* List connected audio ports and their attributes */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700134status_t PatchPanel::listAudioPorts(unsigned int* /* num_ports */,
Eric Laurent951f4552014-05-20 10:48:17 -0700135 struct audio_port *ports __unused)
136{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700137 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700138 return NO_ERROR;
139}
140
141/* Get supported attributes for a given audio port */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700142status_t PatchPanel::getAudioPort(struct audio_port_v7* port)
Eric Laurent951f4552014-05-20 10:48:17 -0700143{
jiabinb4fed192020-09-22 14:45:40 -0700144 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
145 // Only query the HAL when the port is a device.
146 // TODO: implement getAudioPort for mix.
147 return INVALID_OPERATION;
148 }
149 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
150 if (hwDevice == nullptr) {
151 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
152 return BAD_VALUE;
153 }
154 if (!hwDevice->supportsAudioPatches()) {
155 return INVALID_OPERATION;
156 }
157 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700158}
159
Eric Laurent951f4552014-05-20 10:48:17 -0700160/* Connect a patch between several source and sink ports */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700161status_t PatchPanel::createAudioPatch(const struct audio_patch* patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200162 audio_patch_handle_t *handle,
163 bool endpointPatch)
Andy Hung87c693c2023-07-06 20:56:16 -0700164 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendCreateAudioPatchConfigEvent
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200165 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
166 //before processing the create patch request.
167 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700168{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700169 if (handle == NULL || patch == NULL) {
170 return BAD_VALUE;
171 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 ALOGV("%s() num_sources %d num_sinks %d handle %d",
173 __func__, patch->num_sources, patch->num_sinks, *handle);
174 status_t status = NO_ERROR;
175 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700176
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700177 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700178 return BAD_VALUE;
179 }
Eric Laurent874c42872014-08-08 15:13:39 -0700180 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
181 // only the audio policy manager can request a patch creation with 2 sources.
182 if (patch->num_sources > 2) {
183 return INVALID_OPERATION;
184 }
François Gaffie58e73af2023-02-15 11:47:24 +0100185 bool reuseExistingHalPatch = false;
186 audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700187 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700188 auto iter = mPatches.find(*handle);
189 if (iter != mPatches.end()) {
190 ALOGV("%s() removing patch handle %d", __func__, *handle);
191 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700192 // free resources owned by the removed patch if applicable
193 // 1) if a software patch is present, release the playback and capture threads and
194 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700195 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700196 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700197 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700198 // 2) if the new patch and old patch source or sink are devices from different
199 // hw modules, clear the audio HAL patches now because they will not be updated
200 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700201 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700202 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
203 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
François Gaffie58e73af2023-02-15 11:47:24 +0100204 oldhandle = *handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700205 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
206 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
207 oldPatch.sources[0].ext.device.hw_module !=
208 patch->sources[0].ext.device.hw_module)) {
209 hwModule = oldPatch.sources[0].ext.device.hw_module;
210 } else if (patch->num_sinks == 0 ||
211 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
212 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
213 oldPatch.sinks[0].ext.device.hw_module !=
214 patch->sinks[0].ext.device.hw_module))) {
215 // Note on (patch->num_sinks == 0): this situation should not happen as
216 // these special patches are only created by the policy manager but just
217 // in case, systematically clear the HAL patch.
218 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700219 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700220 hwModule = oldPatch.sinks[0].ext.device.hw_module;
221 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700222 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
223 if (hwDevice != 0) {
224 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700225 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800226 halHandle = removedPatch.mHalHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100227 // Prevent to remove/add device effect when mix / device did not change, and
228 // hal patch has not been released
229 // Note that no patch leak at hal layer as halHandle is reused.
230 reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700231 }
François Gaffie58e73af2023-02-15 11:47:24 +0100232 erasePatch(*handle, reuseExistingHalPatch);
Eric Laurent951f4552014-05-20 10:48:17 -0700233 }
234 }
235
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200236 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700237 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700238
Eric Laurent951f4552014-05-20 10:48:17 -0700239 switch (patch->sources[0].type) {
240 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700241 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700242 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
243 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700244 status = BAD_VALUE;
245 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700246 }
247 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700248 // support only one sink if connection to a mix or across HW modules
249 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700250 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
251 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700252 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700253 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700254 status = INVALID_OPERATION;
255 goto exit;
256 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700257 // reject connection to different sink types
258 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700259 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700260 status = BAD_VALUE;
261 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700262 }
Eric Laurent951f4552014-05-20 10:48:17 -0700263 }
264
Eric Laurent3bcf8592015-04-03 12:13:24 -0700265 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700266 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700267 // - Device to device AND
268 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700269 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700270 if ((patch->num_sources == 2) ||
271 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
272 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700273 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800274 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
275 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700276 if (patch->num_sources == 2) {
277 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700278 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
279 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700280 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700281 status = INVALID_OPERATION;
282 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700283 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700284 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
285 patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700286 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700287 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700288 status = INVALID_OPERATION;
289 goto exit;
290 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700291 // existing playback thread is reused, so it is not closed when patch is cleared
292 newPatch.mPlayback.setThread(
Andy Hung87c693c2023-07-06 20:56:16 -0700293 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700294 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200296 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700297 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700298 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
299 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
300 config.sample_rate = patch->sinks[0].sample_rate;
301 }
302 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
303 config.channel_mask = patch->sinks[0].channel_mask;
304 }
305 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
306 config.format = patch->sinks[0].format;
307 }
308 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
309 flags = patch->sinks[0].flags.output;
310 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700311 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800312 patch->sinks[0].ext.device.hw_module,
313 &output,
314 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200315 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800316 outputDevice,
317 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700318 flags);
Andy Hung2dc61c42023-07-17 14:36:08 -0700319 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700320 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700321 status = NO_MEMORY;
322 goto exit;
323 }
Andy Hung87c693c2023-07-06 20:56:16 -0700324 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700325 }
Eric Laurent83b88082014-06-20 18:31:16 -0700326 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700327 String8 address = String8(patch->sources[0].ext.device.address);
328 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700329 // open input stream with source device audio properties if provided or
330 // default to peer output stream properties otherwise.
331 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
332 config.sample_rate = patch->sources[0].sample_rate;
333 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700334 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700335 }
336 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
337 config.channel_mask = patch->sources[0].channel_mask;
338 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700339 config.channel_mask = audio_channel_in_mask_from_count(
340 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700341 }
342 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
343 config.format = patch->sources[0].format;
344 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700345 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700346 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700347 audio_input_flags_t flags =
348 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
349 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700350 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200351 audio_source_t source = AUDIO_SOURCE_MIC;
352 // For telephony patches, propagate voice communication use case to record side
353 if (patch->num_sources == 2
354 && patch->sources[1].ext.mix.usecase.stream
355 == AUDIO_STREAM_VOICE_CALL) {
356 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
357 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700358 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700359 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700360 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700361 device,
362 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200363 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800364 flags,
365 outputDevice,
366 outputDeviceAddress);
Andy Hung2dc61c42023-07-17 14:36:08 -0700367 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700368 thread.get(), config.channel_mask);
369 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700370 status = NO_MEMORY;
371 goto exit;
372 }
Andy Hung87c693c2023-07-06 20:56:16 -0700373 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700374 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700375 if (status != NO_ERROR) {
376 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700377 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700378 if (audioHwDevice->isInsert()) {
379 insertedModule = audioHwDevice->handle();
380 }
Eric Laurent951f4552014-05-20 10:48:17 -0700381 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700382 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700383 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700384 patch->sinks[0].ext.mix.handle);
385 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700386 thread = mAfPatchPanelCallback->checkMmapThread_l(
387 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800388 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700389 ALOGW("%s() bad capture I/O handle %d",
390 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800391 status = BAD_VALUE;
392 goto exit;
393 }
Eric Laurent83b88082014-06-20 18:31:16 -0700394 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700395 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700396 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700397 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800398 if (status == NO_ERROR) {
399 newPatch.setThread(thread);
400 }
Eric Laurent526aa572019-01-15 10:54:58 -0800401 // remove stale audio patch with same input as sink if any
402 for (auto& iter : mPatches) {
403 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800404 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800405 break;
406 }
407 }
Eric Laurent83b88082014-06-20 18:31:16 -0700408 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700409 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
410 status = hwDevice->createAudioPatch(patch->num_sources,
411 patch->sources,
412 patch->num_sinks,
413 patch->sinks,
414 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700415 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700416 }
Eric Laurent951f4552014-05-20 10:48:17 -0700417 }
418 } break;
419 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700420 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung2dc61c42023-07-17 14:36:08 -0700421 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700422 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700423 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700424 status = BAD_VALUE;
425 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700426 }
427 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700428 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700429 for (unsigned int i = 0; i < patch->num_sinks; i++) {
430 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700431 ALOGW("%s() invalid sink type %d for mix source",
432 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700433 status = BAD_VALUE;
434 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700435 }
436 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700437 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700438 status = BAD_VALUE;
439 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700440 }
jiabinc52b1ff2019-10-31 17:20:42 -0700441 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
442 patch->sinks[i].ext.device.type);
443 device->setAddress(patch->sinks[i].ext.device.address);
444 device->applyAudioPortConfig(&patch->sinks[i]);
445 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700446 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700447 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
448 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700449 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700450 thread = mAfPatchPanelCallback->checkMmapThread_l(
451 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800452 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700453 ALOGW("%s() bad playback I/O handle %d",
454 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800455 status = BAD_VALUE;
456 goto exit;
457 }
Eric Laurent951f4552014-05-20 10:48:17 -0700458 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700459 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
460 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700461 }
462
Andy Hung2dc61c42023-07-17 14:36:08 -0700463 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700464 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700465 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800466 if (status == NO_ERROR) {
467 newPatch.setThread(thread);
468 }
Eric Laurent526aa572019-01-15 10:54:58 -0800469
470 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200471 // Prevent to remove endpoint patches (involved in a SwBridge)
472 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
473 if (!endpointPatch) {
474 for (auto& iter : mPatches) {
475 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
476 !iter.second.mIsEndpointPatch) {
477 erasePatch(iter.first);
478 break;
479 }
Eric Laurent526aa572019-01-15 10:54:58 -0800480 }
481 }
Eric Laurent951f4552014-05-20 10:48:17 -0700482 } break;
483 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700484 status = BAD_VALUE;
485 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700486 }
Eric Laurent83b88082014-06-20 18:31:16 -0700487exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700488 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700489 if (status == NO_ERROR) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700490 *handle = static_cast<audio_patch_handle_t>(
491 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700492 newPatch.mHalHandle = halHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100493 if (reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700494 mAfPatchPanelCallback->getPatchCommandThread()->updateAudioPatch(
495 oldhandle, *handle, newPatch);
François Gaffie58e73af2023-02-15 11:47:24 +0100496 } else {
Andy Hung2dc61c42023-07-17 14:36:08 -0700497 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(*handle, newPatch);
François Gaffie58e73af2023-02-15 11:47:24 +0100498 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700499 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100500 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700501 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100502 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700503 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700504 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700505 }
506 return status;
507}
508
Andy Hung8e6b62a2023-07-13 18:11:33 -0700509PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700510{
511 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
512 mRecord.handle(), mPlayback.handle());
513}
514
Andy Hung8e6b62a2023-07-13 18:11:33 -0700515status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700516{
517 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700518 status_t status = panel->createAudioPatch(
519 PatchBuilder().addSource(mAudioPatch.sources[0]).
520 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200521 mRecord.handlePtr(),
522 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700523 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700524 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700525 return status;
526 }
527
528 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700529 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700530 status = panel->createAudioPatch(
531 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200532 mPlayback.handlePtr(),
533 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700534 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700535 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700536 return status;
537 }
538 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700539 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700540 }
541
Eric Laurent83b88082014-06-20 18:31:16 -0700542 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700543 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700544 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700545 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
546 uint32_t sampleRate = mPlayback.thread()->sampleRate();
547 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700548
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700549 audio_format_t inputFormat = mRecord.thread()->format();
550 if (!audio_is_linear_pcm(inputFormat)) {
551 // The playbackThread format will say PCM for IEC61937 packetized stream.
552 // Use recordThread format.
553 format = inputFormat;
554 }
555 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
556 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700557 if (sampleRate == mRecord.thread()->sampleRate() &&
558 inChannelMask == mRecord.thread()->channelMask() &&
559 mRecord.thread()->fastTrackAvailable() &&
560 mRecord.thread()->hasFastCapture()) {
561 // Create a fast track if the record thread has fast capture to get better performance.
562 // Only enable fast mode when there is no resample needed.
563 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
564 } else {
565 // Fast mode is not available in this case.
566 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
567 }
Eric Laurent83b88082014-06-20 18:31:16 -0700568
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700569 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
570 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700571 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200572 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700573 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
574 // "reuse one existing output mix" case
575 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200576 // For telephony patches, propagate voice communication use case to record side
577 if (streamType == AUDIO_STREAM_VOICE_CALL) {
578 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
579 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700580 }
jiabin01c8f562018-07-19 17:47:28 -0700581 if (mPlayback.thread()->hasFastMixer()) {
582 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700583 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700584 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700585 } else {
586 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700587 }
588
Andy Hung8d31fd22023-06-26 19:20:57 -0700589 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800590 const bool usePassthruPatchRecord =
591 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100592 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
593 const size_t recordFrameCount = mRecord.thread()->frameCount();
594 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800595 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100596 // PassthruPatchRecord producesBufferOnDemand, so use
597 // maximum of playback and record thread framecounts
598 frameCount = std::max(playbackFrameCount, recordFrameCount);
599 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
600 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700601 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700602 mRecord.thread().get(),
603 sampleRate,
604 inChannelMask,
605 format,
606 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200607 inputFlags,
608 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700609 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100610 // use a pseudo LCM between input and output framecount
611 int playbackShift = __builtin_ctz(playbackFrameCount);
612 int shift = __builtin_ctz(recordFrameCount);
613 if (playbackShift < shift) {
614 shift = playbackShift;
615 }
616 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
617 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
618 __func__, playbackFrameCount, recordFrameCount, frameCount);
619
Andy Hung8d31fd22023-06-26 19:20:57 -0700620 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700621 mRecord.thread().get(),
622 sampleRate,
623 inChannelMask,
624 format,
625 frameCount,
626 nullptr,
627 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200628 inputFlags,
629 {} /* timeout */,
630 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700631 }
632 status = mRecord.checkTrack(tempRecordTrack.get());
633 if (status != NO_ERROR) {
634 return status;
635 }
636
Eric Laurent83b88082014-06-20 18:31:16 -0700637 // create a special playback track to render to playback thread.
638 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200639
640 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
641 // it might glitch.
642 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
643 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
644 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700645 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700646 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700647 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700648 sampleRate,
649 outChannelMask,
650 format,
651 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700652 tempRecordTrack->buffer(),
653 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200654 outputFlags,
655 {} /*timeout*/,
656 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700657 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700658 if (status != NO_ERROR) {
659 return status;
660 }
Eric Laurent83b88082014-06-20 18:31:16 -0700661
662 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800663 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
664 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
665 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
666 // is alive. There is no need to hold a reference, and there is no need
667 // to clear it. In fact, since playback stopping is asynchronous, there is
668 // no proper time when clearing could be done.
669 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
670 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700671
672 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700673 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
674 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700675
676 return status;
677}
678
Andy Hung8e6b62a2023-07-13 18:11:33 -0700679void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700680{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700681 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
682 __func__, mRecord.handle(), mPlayback.handle());
683 mRecord.stopTrack();
684 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700685 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700686 mRecord.closeConnections(panel);
687 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700688}
689
Andy Hung8e6b62a2023-07-13 18:11:33 -0700690status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700691{
692 if (!isSoftware()) return INVALID_OPERATION;
693
694 auto recordTrack = mRecord.const_track();
695 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
696
697 auto playbackTrack = mPlayback.const_track();
698 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
699
700 // Latency information for tracks may be called without obtaining
701 // the underlying thread lock.
702 //
703 // We use record server latency + playback track latency (generally smaller than the
704 // reverse due to internal biases).
705 //
706 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700707
Andy Hung30282562018-08-08 18:27:03 -0700708 // For PCM tracks get server latency.
709 if (audio_is_linear_pcm(recordTrack->format())) {
710 double recordServerLatencyMs, playbackTrackLatencyMs;
711 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
712 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
713 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
714 return OK;
715 }
716 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700717
Andy Hung30282562018-08-08 18:27:03 -0700718 // See if kernel latencies are available.
719 // If so, do a frame diff and time difference computation to estimate
720 // the total patch latency. This requires that frame counts are reported by the
721 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700722 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700723 recordTrack->getKernelFrameTime(&recordFT);
724 playbackTrack->getKernelFrameTime(&playFT);
725 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
726 const int64_t frameDiff = recordFT.frames - playFT.frames;
727 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
728
729 // It is possible that the patch track and patch record have a large time disparity because
730 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
731 // time difference based on how often we expect the timestamps to update in normal operation
732 // (typical should be no more than 50 ms).
733 //
734 // If the timestamps aren't sampled close enough, the patch latency is not
735 // considered valid.
736 //
737 // TODO: change this based on more experiments.
738 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
739 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
740 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
741 - timeDiffNs * 1e-6;
742 return OK;
743 }
744 }
745
746 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700747}
748
Andy Hung8e6b62a2023-07-13 18:11:33 -0700749String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700750{
Andy Hungc3ab7732018-06-01 13:46:29 -0700751 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800752 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
753 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
754 mRecord.const_thread().get(), mPlayback.const_thread().get());
755
756 bool hasSinkDevice =
757 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
758 bool hasSourceDevice =
759 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
760 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
761 hasSinkDevice ? "num sinks" :
762 (hasSourceDevice ? "num sources" : "no devices"),
763 hasSinkDevice ? mAudioPatch.num_sinks :
764 (hasSourceDevice ? mAudioPatch.num_sources : 0),
765 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
766 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700767
768 // add latency if it exists
769 double latencyMs;
770 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700771 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700772 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700773 return result;
774}
775
Eric Laurent951f4552014-05-20 10:48:17 -0700776/* Disconnect a patch */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700777status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700778 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200779 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
780 //before processing the release patch request.
781 NO_THREAD_SAFETY_ANALYSIS
782 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700783 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700784 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700785
Mikhail Naganovdea53042018-04-26 13:10:21 -0700786 auto iter = mPatches.find(handle);
787 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700788 return BAD_VALUE;
789 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700790 Patch &removedPatch = iter->second;
791 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700792
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700793 const struct audio_port_config &src = patch.sources[0];
794 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700795 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700796 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
797 if (hwDevice == 0) {
798 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700799 status = BAD_VALUE;
800 break;
801 }
Eric Laurent83b88082014-06-20 18:31:16 -0700802
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700803 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700804 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700805 break;
806 }
807
Mikhail Naganovdea53042018-04-26 13:10:21 -0700808 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
809 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700810 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700811 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700812 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800813 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700814 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800815 status = BAD_VALUE;
816 break;
817 }
Eric Laurent951f4552014-05-20 10:48:17 -0700818 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700819 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700820 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700821 mAfPatchPanelCallback->lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700822 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700823 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700824 }
825 } break;
826 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700827 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
828 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700829 status = BAD_VALUE;
830 break;
831 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700832 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung2dc61c42023-07-17 14:36:08 -0700833 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700834 if (thread == 0) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700835 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800836 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700837 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800838 status = BAD_VALUE;
839 break;
840 }
Eric Laurent951f4552014-05-20 10:48:17 -0700841 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700842 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700843 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung2dc61c42023-07-17 14:36:08 -0700844 mAfPatchPanelCallback->lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700845 } break;
846 default:
847 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700848 }
849
Eric Laurentb82e6b72019-11-22 17:25:04 -0800850 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700851 return status;
852}
853
Andy Hung8e6b62a2023-07-13 18:11:33 -0700854void PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800855 mPatches.erase(handle);
856 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100857 if (!reuseExistingHalPatch) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700858 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100859 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800860}
861
Eric Laurent951f4552014-05-20 10:48:17 -0700862/* List connected audio ports and they attributes */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700863status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700864 struct audio_patch *patches __unused)
865{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700866 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700867 return NO_ERROR;
868}
869
Andy Hung8e6b62a2023-07-13 18:11:33 -0700870status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700871 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700872 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700873{
874 for (const auto& module : mInsertedModules) {
875 if (module.second.streams.count(stream)) {
876 for (const auto& patchHandle : module.second.sw_patches) {
877 const auto& patch_iter = mPatches.find(patchHandle);
878 if (patch_iter != mPatches.end()) {
879 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700880 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
881 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700882 patch.mPlayback.const_thread()->id(),
883 patch.mRecord.const_thread()->id());
884 } else {
885 ALOGE("Stale patch handle in the cache: %d", patchHandle);
886 }
887 }
888 return OK;
889 }
890 }
891 // The stream is not associated with any of inserted modules.
892 return BAD_VALUE;
893}
894
Andy Hung8e6b62a2023-07-13 18:11:33 -0700895void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100896 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700897{
898 if (audioHwDevice->isInsert()) {
899 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100900 if (patch != nullptr) {
901 std::vector <SoftwarePatch> swPatches;
902 getDownstreamSoftwarePatches(stream, &swPatches);
903 if (swPatches.size() > 0) {
904 auto iter = mPatches.find(swPatches[0].getPatchHandle());
905 if (iter != mPatches.end()) {
906 *patch = iter->second.mAudioPatch;
907 }
908 }
909 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700910 }
911}
912
Andy Hung8e6b62a2023-07-13 18:11:33 -0700913void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700914{
915 for (auto& module : mInsertedModules) {
916 module.second.streams.erase(stream);
917 }
918}
919
Andy Hung8e6b62a2023-07-13 18:11:33 -0700920AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700921{
922 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung2dc61c42023-07-17 14:36:08 -0700923 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700924 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700925 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700926 return nullptr;
927 }
Andy Hung2dc61c42023-07-17 14:36:08 -0700928 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700929}
930
Andy Hung8e6b62a2023-07-13 18:11:33 -0700931sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700932{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700933 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
934 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
935}
936
Andy Hung8e6b62a2023-07-13 18:11:33 -0700937void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100938 audio_module_handle_t module, audio_patch_handle_t handle,
939 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700940{
941 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100942 if (!mInsertedModules[module].streams.empty()) {
Andy Hung2dc61c42023-07-17 14:36:08 -0700943 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100944 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700945}
946
Andy Hung8e6b62a2023-07-13 18:11:33 -0700947void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700948 audio_patch_handle_t handle)
949{
950 for (auto& module : mInsertedModules) {
951 module.second.sw_patches.erase(handle);
952 }
953}
954
Andy Hung8e6b62a2023-07-13 18:11:33 -0700955void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700956{
957 String8 patchPanelDump;
958 const char *indent = " ";
959
Mikhail Naganov201369b2018-05-16 16:52:32 -0700960 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700961 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800962 if (!headerPrinted) {
963 patchPanelDump += "\nPatches:\n";
964 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700965 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800966 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700967 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700968
969 headerPrinted = false;
970 for (const auto& module : mInsertedModules) {
971 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
972 if (!headerPrinted) {
973 patchPanelDump += "\nTracked inserted modules:\n";
974 headerPrinted = true;
975 }
976 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
977 for (const auto& stream : module.second.streams) {
978 moduleDump.appendFormat("%d ", stream);
979 }
980 moduleDump.append("; SW Patches: ");
981 for (const auto& patch : module.second.sw_patches) {
982 moduleDump.appendFormat("%d ", patch);
983 }
984 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
985 }
986 }
987
988 if (!patchPanelDump.isEmpty()) {
989 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700990 }
991}
992
Glenn Kasten63238ef2015-03-02 15:50:29 -0800993} // namespace android