blob: a88a27fc779b80d8768ad604a4fe4e7f94b995bf [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 Hung07434ef2023-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 Hung8b99ea32023-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 Hungd63e79d2023-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 Hung8b99ea32023-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 Hungd63e79d2023-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 Hungd63e79d2023-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 Hungd63e79d2023-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 Hung8b99ea32023-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 Hungd63e79d2023-07-13 16:52:46 -070095 return mPatchPanel->listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070096}
97
Andy Hungd63e79d2023-07-13 16:52:46 -070098/* static */
Andy Hung68631eb2023-07-17 14:36:08 -070099sp<IAfPatchPanel> IAfPatchPanel::create(const sp<IAfPatchPanelCallback>& afPatchPanelCallback) {
100 return sp<PatchPanel>::make(afPatchPanelCallback);
Andy Hungd63e79d2023-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 Hung07434ef2023-07-13 18:11:33 -0700107status_t PatchPanel::getLatencyMs_l(
Andy Hungd63e79d2023-07-13 16:52:46 -0700108 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700109{
Andy Hungd63e79d2023-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 Hung07434ef2023-07-13 18:11:33 -0700118void PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
Andy Hungd63e79d2023-07-13 16:52:46 -0700119{
120 if (const auto recordThread = thread->asIAfRecordThread();
121 recordThread) {
Andy Hung68631eb2023-07-17 14:36:08 -0700122 mAfPatchPanelCallback->closeThreadInternal_l(recordThread);
Andy Hungd63e79d2023-07-13 16:52:46 -0700123 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
124 playbackThread) {
Andy Hung68631eb2023-07-17 14:36:08 -0700125 mAfPatchPanelCallback->closeThreadInternal_l(playbackThread);
Andy Hungd63e79d2023-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 Hung07434ef2023-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 Hung07434ef2023-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 Hung07434ef2023-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 Hung44f27182023-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 }
Eric Laurent951f4552014-05-20 10:48:17 -0700185
Eric Laurent83b88082014-06-20 18:31:16 -0700186 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700187 auto iter = mPatches.find(*handle);
188 if (iter != mPatches.end()) {
189 ALOGV("%s() removing patch handle %d", __func__, *handle);
190 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700191 // free resources owned by the removed patch if applicable
192 // 1) if a software patch is present, release the playback and capture threads and
193 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700194 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700195 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700196 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700197 // 2) if the new patch and old patch source or sink are devices from different
198 // hw modules, clear the audio HAL patches now because they will not be updated
199 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700200 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700201 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
202 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
203 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
204 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
205 oldPatch.sources[0].ext.device.hw_module !=
206 patch->sources[0].ext.device.hw_module)) {
207 hwModule = oldPatch.sources[0].ext.device.hw_module;
208 } else if (patch->num_sinks == 0 ||
209 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
210 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
211 oldPatch.sinks[0].ext.device.hw_module !=
212 patch->sinks[0].ext.device.hw_module))) {
213 // Note on (patch->num_sinks == 0): this situation should not happen as
214 // these special patches are only created by the policy manager but just
215 // in case, systematically clear the HAL patch.
216 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700217 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700218 hwModule = oldPatch.sinks[0].ext.device.hw_module;
219 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700220 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
221 if (hwDevice != 0) {
222 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700223 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800224 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700225 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800226 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700227 }
228 }
229
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200230 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700231 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700232
Eric Laurent951f4552014-05-20 10:48:17 -0700233 switch (patch->sources[0].type) {
234 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700235 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700236 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
237 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700238 status = BAD_VALUE;
239 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700240 }
241 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700242 // support only one sink if connection to a mix or across HW modules
243 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700244 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
245 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700246 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700247 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700248 status = INVALID_OPERATION;
249 goto exit;
250 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700251 // reject connection to different sink types
252 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700253 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700254 status = BAD_VALUE;
255 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700256 }
Eric Laurent951f4552014-05-20 10:48:17 -0700257 }
258
Eric Laurent3bcf8592015-04-03 12:13:24 -0700259 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700260 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700261 // - Device to device AND
262 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700263 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700264 if ((patch->num_sources == 2) ||
265 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
266 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700267 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800268 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
269 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700270 if (patch->num_sources == 2) {
271 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700272 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
273 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700274 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700275 status = INVALID_OPERATION;
276 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700277 }
Andy Hung68631eb2023-07-17 14:36:08 -0700278 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
279 patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700280 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700281 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700282 status = INVALID_OPERATION;
283 goto exit;
284 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700285 // existing playback thread is reused, so it is not closed when patch is cleared
286 newPatch.mPlayback.setThread(
Andy Hung44f27182023-07-06 20:56:16 -0700287 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700288 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700289 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200290 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700291 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700292 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
293 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
294 config.sample_rate = patch->sinks[0].sample_rate;
295 }
296 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
297 config.channel_mask = patch->sinks[0].channel_mask;
298 }
299 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
300 config.format = patch->sinks[0].format;
301 }
302 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
303 flags = patch->sinks[0].flags.output;
304 }
Andy Hung68631eb2023-07-17 14:36:08 -0700305 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800306 patch->sinks[0].ext.device.hw_module,
307 &output,
308 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200309 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800310 outputDevice,
311 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700312 flags);
Andy Hung68631eb2023-07-17 14:36:08 -0700313 ALOGV("mAfPatchPanelCallback->openOutput_l() returned %p", thread.get());
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700314 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700315 status = NO_MEMORY;
316 goto exit;
317 }
Andy Hung44f27182023-07-06 20:56:16 -0700318 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700319 }
Eric Laurent83b88082014-06-20 18:31:16 -0700320 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700321 String8 address = String8(patch->sources[0].ext.device.address);
322 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700323 // open input stream with source device audio properties if provided or
324 // default to peer output stream properties otherwise.
325 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
326 config.sample_rate = patch->sources[0].sample_rate;
327 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700328 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700329 }
330 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
331 config.channel_mask = patch->sources[0].channel_mask;
332 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700333 config.channel_mask = audio_channel_in_mask_from_count(
334 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700335 }
336 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
337 config.format = patch->sources[0].format;
338 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700339 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700340 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700341 audio_input_flags_t flags =
342 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
343 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700344 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200345 audio_source_t source = AUDIO_SOURCE_MIC;
346 // For telephony patches, propagate voice communication use case to record side
347 if (patch->num_sources == 2
348 && patch->sources[1].ext.mix.usecase.stream
349 == AUDIO_STREAM_VOICE_CALL) {
350 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
351 }
Andy Hung68631eb2023-07-17 14:36:08 -0700352 const sp<IAfThreadBase> thread = mAfPatchPanelCallback->openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700353 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700354 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700355 device,
356 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200357 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800358 flags,
359 outputDevice,
360 outputDeviceAddress);
Andy Hung68631eb2023-07-17 14:36:08 -0700361 ALOGV("mAfPatchPanelCallback->openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700362 thread.get(), config.channel_mask);
363 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700364 status = NO_MEMORY;
365 goto exit;
366 }
Andy Hung44f27182023-07-06 20:56:16 -0700367 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700368 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700369 if (status != NO_ERROR) {
370 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700371 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700372 if (audioHwDevice->isInsert()) {
373 insertedModule = audioHwDevice->handle();
374 }
Eric Laurent951f4552014-05-20 10:48:17 -0700375 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700376 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung68631eb2023-07-17 14:36:08 -0700377 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700378 patch->sinks[0].ext.mix.handle);
379 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700380 thread = mAfPatchPanelCallback->checkMmapThread_l(
381 patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800382 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700383 ALOGW("%s() bad capture I/O handle %d",
384 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800385 status = BAD_VALUE;
386 goto exit;
387 }
Eric Laurent83b88082014-06-20 18:31:16 -0700388 }
Andy Hung68631eb2023-07-17 14:36:08 -0700389 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700390 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung68631eb2023-07-17 14:36:08 -0700391 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800392 if (status == NO_ERROR) {
393 newPatch.setThread(thread);
394 }
Eric Laurent526aa572019-01-15 10:54:58 -0800395 // remove stale audio patch with same input as sink if any
396 for (auto& iter : mPatches) {
397 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800398 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800399 break;
400 }
401 }
Eric Laurent83b88082014-06-20 18:31:16 -0700402 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700403 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
404 status = hwDevice->createAudioPatch(patch->num_sources,
405 patch->sources,
406 patch->num_sinks,
407 patch->sinks,
408 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700409 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700410 }
Eric Laurent951f4552014-05-20 10:48:17 -0700411 }
412 } break;
413 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700414 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Andy Hung68631eb2023-07-17 14:36:08 -0700415 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700416 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700417 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700418 status = BAD_VALUE;
419 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700420 }
421 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700422 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700423 for (unsigned int i = 0; i < patch->num_sinks; i++) {
424 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700425 ALOGW("%s() invalid sink type %d for mix source",
426 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700427 status = BAD_VALUE;
428 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700429 }
430 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700431 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700432 status = BAD_VALUE;
433 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700434 }
jiabinc52b1ff2019-10-31 17:20:42 -0700435 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
436 patch->sinks[i].ext.device.type);
437 device->setAddress(patch->sinks[i].ext.device.address);
438 device->applyAudioPortConfig(&patch->sinks[i]);
439 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700440 }
Andy Hung68631eb2023-07-17 14:36:08 -0700441 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(
442 patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700443 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700444 thread = mAfPatchPanelCallback->checkMmapThread_l(
445 patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800446 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700447 ALOGW("%s() bad playback I/O handle %d",
448 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800449 status = BAD_VALUE;
450 goto exit;
451 }
Eric Laurent951f4552014-05-20 10:48:17 -0700452 }
Andy Hung68631eb2023-07-17 14:36:08 -0700453 if (thread == mAfPatchPanelCallback->primaryPlaybackThread_l()) {
454 mAfPatchPanelCallback->updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700455 }
456
Andy Hung68631eb2023-07-17 14:36:08 -0700457 mAfPatchPanelCallback->unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700458 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Andy Hung68631eb2023-07-17 14:36:08 -0700459 mAfPatchPanelCallback->lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800460 if (status == NO_ERROR) {
461 newPatch.setThread(thread);
462 }
Eric Laurent526aa572019-01-15 10:54:58 -0800463
464 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200465 // Prevent to remove endpoint patches (involved in a SwBridge)
466 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
467 if (!endpointPatch) {
468 for (auto& iter : mPatches) {
469 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
470 !iter.second.mIsEndpointPatch) {
471 erasePatch(iter.first);
472 break;
473 }
Eric Laurent526aa572019-01-15 10:54:58 -0800474 }
475 }
Eric Laurent951f4552014-05-20 10:48:17 -0700476 } break;
477 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700478 status = BAD_VALUE;
479 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700480 }
Eric Laurent83b88082014-06-20 18:31:16 -0700481exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700482 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700483 if (status == NO_ERROR) {
Andy Hung68631eb2023-07-17 14:36:08 -0700484 *handle = static_cast<audio_patch_handle_t>(
485 mAfPatchPanelCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700486 newPatch.mHalHandle = halHandle;
Andy Hung68631eb2023-07-17 14:36:08 -0700487 mAfPatchPanelCallback->getPatchCommandThread()->createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700488 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100489 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700490 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100491 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700492 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700493 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700494 }
495 return status;
496}
497
Andy Hung07434ef2023-07-13 18:11:33 -0700498PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700499{
500 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
501 mRecord.handle(), mPlayback.handle());
502}
503
Andy Hung07434ef2023-07-13 18:11:33 -0700504status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700505{
506 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700507 status_t status = panel->createAudioPatch(
508 PatchBuilder().addSource(mAudioPatch.sources[0]).
509 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200510 mRecord.handlePtr(),
511 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700512 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700513 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700514 return status;
515 }
516
517 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700518 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700519 status = panel->createAudioPatch(
520 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200521 mPlayback.handlePtr(),
522 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700523 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700524 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700525 return status;
526 }
527 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700528 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700529 }
530
Eric Laurent83b88082014-06-20 18:31:16 -0700531 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700532 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700533 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700534 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
535 uint32_t sampleRate = mPlayback.thread()->sampleRate();
536 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700537
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700538 audio_format_t inputFormat = mRecord.thread()->format();
539 if (!audio_is_linear_pcm(inputFormat)) {
540 // The playbackThread format will say PCM for IEC61937 packetized stream.
541 // Use recordThread format.
542 format = inputFormat;
543 }
544 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
545 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700546 if (sampleRate == mRecord.thread()->sampleRate() &&
547 inChannelMask == mRecord.thread()->channelMask() &&
548 mRecord.thread()->fastTrackAvailable() &&
549 mRecord.thread()->hasFastCapture()) {
550 // Create a fast track if the record thread has fast capture to get better performance.
551 // Only enable fast mode when there is no resample needed.
552 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
553 } else {
554 // Fast mode is not available in this case.
555 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
556 }
Eric Laurent83b88082014-06-20 18:31:16 -0700557
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700558 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
559 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700560 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200561 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700562 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
563 // "reuse one existing output mix" case
564 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200565 // For telephony patches, propagate voice communication use case to record side
566 if (streamType == AUDIO_STREAM_VOICE_CALL) {
567 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
568 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700569 }
jiabin01c8f562018-07-19 17:47:28 -0700570 if (mPlayback.thread()->hasFastMixer()) {
571 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700572 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700573 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700574 } else {
575 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700576 }
577
Andy Hung3ff4b552023-06-26 19:20:57 -0700578 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800579 const bool usePassthruPatchRecord =
580 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100581 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
582 const size_t recordFrameCount = mRecord.thread()->frameCount();
583 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800584 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100585 // PassthruPatchRecord producesBufferOnDemand, so use
586 // maximum of playback and record thread framecounts
587 frameCount = std::max(playbackFrameCount, recordFrameCount);
588 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
589 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung3ff4b552023-06-26 19:20:57 -0700590 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700591 mRecord.thread().get(),
592 sampleRate,
593 inChannelMask,
594 format,
595 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200596 inputFlags,
597 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700598 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100599 // use a pseudo LCM between input and output framecount
600 int playbackShift = __builtin_ctz(playbackFrameCount);
601 int shift = __builtin_ctz(recordFrameCount);
602 if (playbackShift < shift) {
603 shift = playbackShift;
604 }
605 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
606 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
607 __func__, playbackFrameCount, recordFrameCount, frameCount);
608
Andy Hung3ff4b552023-06-26 19:20:57 -0700609 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700610 mRecord.thread().get(),
611 sampleRate,
612 inChannelMask,
613 format,
614 frameCount,
615 nullptr,
616 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200617 inputFlags,
618 {} /* timeout */,
619 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700620 }
621 status = mRecord.checkTrack(tempRecordTrack.get());
622 if (status != NO_ERROR) {
623 return status;
624 }
625
Eric Laurent83b88082014-06-20 18:31:16 -0700626 // create a special playback track to render to playback thread.
627 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200628
629 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
630 // it might glitch.
631 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
632 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
633 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung3ff4b552023-06-26 19:20:57 -0700634 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700635 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700636 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700637 sampleRate,
638 outChannelMask,
639 format,
640 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700641 tempRecordTrack->buffer(),
642 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200643 outputFlags,
644 {} /*timeout*/,
645 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700646 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700647 if (status != NO_ERROR) {
648 return status;
649 }
Eric Laurent83b88082014-06-20 18:31:16 -0700650
651 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800652 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
653 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
654 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
655 // is alive. There is no need to hold a reference, and there is no need
656 // to clear it. In fact, since playback stopping is asynchronous, there is
657 // no proper time when clearing could be done.
658 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
659 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700660
661 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700662 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
663 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700664
665 return status;
666}
667
Andy Hung07434ef2023-07-13 18:11:33 -0700668void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700669{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700670 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
671 __func__, mRecord.handle(), mPlayback.handle());
672 mRecord.stopTrack();
673 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700674 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700675 mRecord.closeConnections(panel);
676 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700677}
678
Andy Hung07434ef2023-07-13 18:11:33 -0700679status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700680{
681 if (!isSoftware()) return INVALID_OPERATION;
682
683 auto recordTrack = mRecord.const_track();
684 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
685
686 auto playbackTrack = mPlayback.const_track();
687 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
688
689 // Latency information for tracks may be called without obtaining
690 // the underlying thread lock.
691 //
692 // We use record server latency + playback track latency (generally smaller than the
693 // reverse due to internal biases).
694 //
695 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700696
Andy Hung30282562018-08-08 18:27:03 -0700697 // For PCM tracks get server latency.
698 if (audio_is_linear_pcm(recordTrack->format())) {
699 double recordServerLatencyMs, playbackTrackLatencyMs;
700 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
701 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
702 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
703 return OK;
704 }
705 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700706
Andy Hung30282562018-08-08 18:27:03 -0700707 // See if kernel latencies are available.
708 // If so, do a frame diff and time difference computation to estimate
709 // the total patch latency. This requires that frame counts are reported by the
710 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700711 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700712 recordTrack->getKernelFrameTime(&recordFT);
713 playbackTrack->getKernelFrameTime(&playFT);
714 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
715 const int64_t frameDiff = recordFT.frames - playFT.frames;
716 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
717
718 // It is possible that the patch track and patch record have a large time disparity because
719 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
720 // time difference based on how often we expect the timestamps to update in normal operation
721 // (typical should be no more than 50 ms).
722 //
723 // If the timestamps aren't sampled close enough, the patch latency is not
724 // considered valid.
725 //
726 // TODO: change this based on more experiments.
727 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
728 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
729 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
730 - timeDiffNs * 1e-6;
731 return OK;
732 }
733 }
734
735 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700736}
737
Andy Hung07434ef2023-07-13 18:11:33 -0700738String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700739{
Andy Hungc3ab7732018-06-01 13:46:29 -0700740 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800741 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
742 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
743 mRecord.const_thread().get(), mPlayback.const_thread().get());
744
745 bool hasSinkDevice =
746 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
747 bool hasSourceDevice =
748 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
749 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
750 hasSinkDevice ? "num sinks" :
751 (hasSourceDevice ? "num sources" : "no devices"),
752 hasSinkDevice ? mAudioPatch.num_sinks :
753 (hasSourceDevice ? mAudioPatch.num_sources : 0),
754 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
755 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700756
757 // add latency if it exists
758 double latencyMs;
759 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700760 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700761 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700762 return result;
763}
764
Eric Laurent951f4552014-05-20 10:48:17 -0700765/* Disconnect a patch */
Andy Hung07434ef2023-07-13 18:11:33 -0700766status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung44f27182023-07-06 20:56:16 -0700767 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200768 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
769 //before processing the release patch request.
770 NO_THREAD_SAFETY_ANALYSIS
771 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700772 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700773 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700774
Mikhail Naganovdea53042018-04-26 13:10:21 -0700775 auto iter = mPatches.find(handle);
776 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700777 return BAD_VALUE;
778 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700779 Patch &removedPatch = iter->second;
780 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700781
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700782 const struct audio_port_config &src = patch.sources[0];
783 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700784 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700785 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
786 if (hwDevice == 0) {
787 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700788 status = BAD_VALUE;
789 break;
790 }
Eric Laurent83b88082014-06-20 18:31:16 -0700791
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700792 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700793 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700794 break;
795 }
796
Mikhail Naganovdea53042018-04-26 13:10:21 -0700797 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
798 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700799 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700800 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700801 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800802 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700803 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800804 status = BAD_VALUE;
805 break;
806 }
Eric Laurent951f4552014-05-20 10:48:17 -0700807 }
Andy Hung68631eb2023-07-17 14:36:08 -0700808 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700809 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung68631eb2023-07-17 14:36:08 -0700810 mAfPatchPanelCallback->lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700811 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700812 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700813 }
814 } break;
815 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700816 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
817 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700818 status = BAD_VALUE;
819 break;
820 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700821 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung68631eb2023-07-17 14:36:08 -0700822 sp<IAfThreadBase> thread = mAfPatchPanelCallback->checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700823 if (thread == 0) {
Andy Hung68631eb2023-07-17 14:36:08 -0700824 thread = mAfPatchPanelCallback->checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800825 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700826 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800827 status = BAD_VALUE;
828 break;
829 }
Eric Laurent951f4552014-05-20 10:48:17 -0700830 }
Andy Hung68631eb2023-07-17 14:36:08 -0700831 mAfPatchPanelCallback->unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700832 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Andy Hung68631eb2023-07-17 14:36:08 -0700833 mAfPatchPanelCallback->lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700834 } break;
835 default:
836 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700837 }
838
Eric Laurentb82e6b72019-11-22 17:25:04 -0800839 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700840 return status;
841}
842
Andy Hung07434ef2023-07-13 18:11:33 -0700843void PatchPanel::erasePatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800844 mPatches.erase(handle);
845 removeSoftwarePatchFromInsertedModules(handle);
Andy Hung68631eb2023-07-17 14:36:08 -0700846 mAfPatchPanelCallback->getPatchCommandThread()->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800847}
848
Eric Laurent951f4552014-05-20 10:48:17 -0700849/* List connected audio ports and they attributes */
Andy Hung07434ef2023-07-13 18:11:33 -0700850status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700851 struct audio_patch *patches __unused)
852{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700853 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700854 return NO_ERROR;
855}
856
Andy Hung07434ef2023-07-13 18:11:33 -0700857status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700858 audio_io_handle_t stream,
Andy Hungd63e79d2023-07-13 16:52:46 -0700859 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700860{
861 for (const auto& module : mInsertedModules) {
862 if (module.second.streams.count(stream)) {
863 for (const auto& patchHandle : module.second.sw_patches) {
864 const auto& patch_iter = mPatches.find(patchHandle);
865 if (patch_iter != mPatches.end()) {
866 const Patch &patch = patch_iter->second;
Andy Hungd63e79d2023-07-13 16:52:46 -0700867 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
868 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700869 patch.mPlayback.const_thread()->id(),
870 patch.mRecord.const_thread()->id());
871 } else {
872 ALOGE("Stale patch handle in the cache: %d", patchHandle);
873 }
874 }
875 return OK;
876 }
877 }
878 // The stream is not associated with any of inserted modules.
879 return BAD_VALUE;
880}
881
Andy Hung07434ef2023-07-13 18:11:33 -0700882void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100883 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700884{
885 if (audioHwDevice->isInsert()) {
886 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100887 if (patch != nullptr) {
888 std::vector <SoftwarePatch> swPatches;
889 getDownstreamSoftwarePatches(stream, &swPatches);
890 if (swPatches.size() > 0) {
891 auto iter = mPatches.find(swPatches[0].getPatchHandle());
892 if (iter != mPatches.end()) {
893 *patch = iter->second.mAudioPatch;
894 }
895 }
896 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700897 }
898}
899
Andy Hung07434ef2023-07-13 18:11:33 -0700900void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700901{
902 for (auto& module : mInsertedModules) {
903 module.second.streams.erase(stream);
904 }
905}
906
Andy Hung07434ef2023-07-13 18:11:33 -0700907AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700908{
909 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
Andy Hung68631eb2023-07-17 14:36:08 -0700910 ssize_t index = mAfPatchPanelCallback->getAudioHwDevs_l().indexOfKey(module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700911 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700912 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700913 return nullptr;
914 }
Andy Hung68631eb2023-07-17 14:36:08 -0700915 return mAfPatchPanelCallback->getAudioHwDevs_l().valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700916}
917
Andy Hung07434ef2023-07-13 18:11:33 -0700918sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700919{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700920 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
921 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
922}
923
Andy Hung07434ef2023-07-13 18:11:33 -0700924void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100925 audio_module_handle_t module, audio_patch_handle_t handle,
926 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700927{
928 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100929 if (!mInsertedModules[module].streams.empty()) {
Andy Hung68631eb2023-07-17 14:36:08 -0700930 mAfPatchPanelCallback->updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100931 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700932}
933
Andy Hung07434ef2023-07-13 18:11:33 -0700934void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700935 audio_patch_handle_t handle)
936{
937 for (auto& module : mInsertedModules) {
938 module.second.sw_patches.erase(handle);
939 }
940}
941
Andy Hung07434ef2023-07-13 18:11:33 -0700942void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700943{
944 String8 patchPanelDump;
945 const char *indent = " ";
946
Mikhail Naganov201369b2018-05-16 16:52:32 -0700947 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700948 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800949 if (!headerPrinted) {
950 patchPanelDump += "\nPatches:\n";
951 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700952 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000953 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700954 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700955
956 headerPrinted = false;
957 for (const auto& module : mInsertedModules) {
958 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
959 if (!headerPrinted) {
960 patchPanelDump += "\nTracked inserted modules:\n";
961 headerPrinted = true;
962 }
963 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
964 for (const auto& stream : module.second.streams) {
965 moduleDump.appendFormat("%d ", stream);
966 }
967 moduleDump.append("; SW Patches: ");
968 for (const auto& patch : module.second.sw_patches) {
969 moduleDump.appendFormat("%d ", patch);
970 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000971 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700972 }
973 }
974
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000975 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000976 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700977 }
978}
979
Glenn Kasten63238ef2015-03-02 15:50:29 -0800980} // namespace android