blob: d25d46f5c8684d8002f1ddd9de8f3a83e09d62e0 [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"
Eric Laurent951f4552014-05-20 10:48:17 -070027#include <media/AudioParameter.h>
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080028#include <media/AudioValidator.h>
jiabinc52b1ff2019-10-31 17:20:42 -070029#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070030#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070031#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070032
33// ----------------------------------------------------------------------------
34
35// Note: the following macro is used for extremely verbose logging message. In
36// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
37// 0; but one side effect of this is to turn all LOGV's as well. Some messages
38// are so verbose that we want to suppress them even when we have ALOG_ASSERT
39// turned on. Do not uncomment the #def below unless you really know what you
40// are doing and want to see all of the extremely verbose messages.
41//#define VERY_VERY_VERBOSE_LOGGING
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
47
48namespace android {
49
50/* List connected audio ports and their attributes */
51status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
52 struct audio_port *ports)
53{
54 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070055 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070056}
57
58/* Get supported attributes for a given audio port */
jiabinb4fed192020-09-22 14:45:40 -070059status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) {
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080060 status_t status = AudioValidator::validateAudioPort(*port);
61 if (status != NO_ERROR) {
62 return status;
63 }
64
Eric Laurent951f4552014-05-20 10:48:17 -070065 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070066 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070067}
68
Eric Laurent951f4552014-05-20 10:48:17 -070069/* Connect a patch between several source and sink ports */
70status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
71 audio_patch_handle_t *handle)
72{
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080073 status_t status = AudioValidator::validateAudioPatch(*patch);
74 if (status != NO_ERROR) {
75 return status;
76 }
77
Eric Laurent951f4552014-05-20 10:48:17 -070078 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070079 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070080}
81
82/* Disconnect a patch */
83status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
84{
85 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070086 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070087}
88
Eric Laurent951f4552014-05-20 10:48:17 -070089/* List connected audio ports and they attributes */
90status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
91 struct audio_patch *patches)
92{
93 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070094 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070095}
96
Mikhail Naganovadca70f2018-07-09 12:49:25 -070097status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const
98{
99 const auto& iter = mPatchPanel.mPatches.find(mPatchHandle);
100 if (iter != mPatchPanel.mPatches.end()) {
101 return iter->second.getLatencyMs(latencyMs);
102 } else {
103 return BAD_VALUE;
104 }
105}
106
Eric Laurent951f4552014-05-20 10:48:17 -0700107/* List connected audio ports and their attributes */
108status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
109 struct audio_port *ports __unused)
110{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700111 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700112 return NO_ERROR;
113}
114
115/* Get supported attributes for a given audio port */
jiabinb4fed192020-09-22 14:45:40 -0700116status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port)
Eric Laurent951f4552014-05-20 10:48:17 -0700117{
jiabinb4fed192020-09-22 14:45:40 -0700118 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
119 // Only query the HAL when the port is a device.
120 // TODO: implement getAudioPort for mix.
121 return INVALID_OPERATION;
122 }
123 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
124 if (hwDevice == nullptr) {
125 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
126 return BAD_VALUE;
127 }
128 if (!hwDevice->supportsAudioPatches()) {
129 return INVALID_OPERATION;
130 }
131 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700132}
133
Eric Laurent951f4552014-05-20 10:48:17 -0700134/* Connect a patch between several source and sink ports */
135status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200136 audio_patch_handle_t *handle,
137 bool endpointPatch)
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200138 //unlocks AudioFlinger::mLock when calling ThreadBase::sendCreateAudioPatchConfigEvent
139 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
140 //before processing the create patch request.
141 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700142{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700143 if (handle == NULL || patch == NULL) {
144 return BAD_VALUE;
145 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700146 ALOGV("%s() num_sources %d num_sinks %d handle %d",
147 __func__, patch->num_sources, patch->num_sinks, *handle);
148 status_t status = NO_ERROR;
149 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700150
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700151 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700152 return BAD_VALUE;
153 }
Eric Laurent874c42872014-08-08 15:13:39 -0700154 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
155 // only the audio policy manager can request a patch creation with 2 sources.
156 if (patch->num_sources > 2) {
157 return INVALID_OPERATION;
158 }
Eric Laurent951f4552014-05-20 10:48:17 -0700159
Eric Laurent83b88082014-06-20 18:31:16 -0700160 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700161 auto iter = mPatches.find(*handle);
162 if (iter != mPatches.end()) {
163 ALOGV("%s() removing patch handle %d", __func__, *handle);
164 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700165 // free resources owned by the removed patch if applicable
166 // 1) if a software patch is present, release the playback and capture threads and
167 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700168 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700169 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700170 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 // 2) if the new patch and old patch source or sink are devices from different
172 // hw modules, clear the audio HAL patches now because they will not be updated
173 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700174 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700175 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
176 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
177 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
178 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
179 oldPatch.sources[0].ext.device.hw_module !=
180 patch->sources[0].ext.device.hw_module)) {
181 hwModule = oldPatch.sources[0].ext.device.hw_module;
182 } else if (patch->num_sinks == 0 ||
183 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
184 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
185 oldPatch.sinks[0].ext.device.hw_module !=
186 patch->sinks[0].ext.device.hw_module))) {
187 // Note on (patch->num_sinks == 0): this situation should not happen as
188 // these special patches are only created by the policy manager but just
189 // in case, systematically clear the HAL patch.
190 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700191 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700192 hwModule = oldPatch.sinks[0].ext.device.hw_module;
193 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700194 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
195 if (hwDevice != 0) {
196 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700197 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800198 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700199 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800200 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700201 }
202 }
203
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200204 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700205 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700206
Eric Laurent951f4552014-05-20 10:48:17 -0700207 switch (patch->sources[0].type) {
208 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700209 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700210 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
211 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700212 status = BAD_VALUE;
213 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700214 }
215 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700216 // support only one sink if connection to a mix or across HW modules
217 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700218 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
219 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700220 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700221 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700222 status = INVALID_OPERATION;
223 goto exit;
224 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700225 // reject connection to different sink types
226 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700227 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700228 status = BAD_VALUE;
229 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700230 }
Eric Laurent951f4552014-05-20 10:48:17 -0700231 }
232
Eric Laurent3bcf8592015-04-03 12:13:24 -0700233 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700234 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700235 // - Device to device AND
236 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700237 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700238 if ((patch->num_sources == 2) ||
239 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
240 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700241 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800242 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
243 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700244 if (patch->num_sources == 2) {
245 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700246 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
247 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700248 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700249 status = INVALID_OPERATION;
250 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700251 }
Eric Laurent83b88082014-06-20 18:31:16 -0700252 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700253 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700254 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700255 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700256 status = INVALID_OPERATION;
257 goto exit;
258 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700259 // existing playback thread is reused, so it is not closed when patch is cleared
260 newPatch.mPlayback.setThread(
261 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700262 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700263 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200264 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700265 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700266 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
267 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
268 config.sample_rate = patch->sinks[0].sample_rate;
269 }
270 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
271 config.channel_mask = patch->sinks[0].channel_mask;
272 }
273 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
274 config.format = patch->sinks[0].format;
275 }
276 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
277 flags = patch->sinks[0].flags.output;
278 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700279 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800280 patch->sinks[0].ext.device.hw_module,
281 &output,
282 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200283 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800284 outputDevice,
285 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700286 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700287 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
288 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700289 status = NO_MEMORY;
290 goto exit;
291 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700292 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700293 }
Eric Laurent83b88082014-06-20 18:31:16 -0700294 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 String8 address = String8(patch->sources[0].ext.device.address);
296 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700297 // open input stream with source device audio properties if provided or
298 // default to peer output stream properties otherwise.
299 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
300 config.sample_rate = patch->sources[0].sample_rate;
301 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700302 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700303 }
304 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
305 config.channel_mask = patch->sources[0].channel_mask;
306 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700307 config.channel_mask = audio_channel_in_mask_from_count(
308 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700309 }
310 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
311 config.format = patch->sources[0].format;
312 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700313 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700314 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700315 audio_input_flags_t flags =
316 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
317 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700318 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200319 audio_source_t source = AUDIO_SOURCE_MIC;
320 // For telephony patches, propagate voice communication use case to record side
321 if (patch->num_sources == 2
322 && patch->sources[1].ext.mix.usecase.stream
323 == AUDIO_STREAM_VOICE_CALL) {
324 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
325 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700326 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700327 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700328 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700329 device,
330 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200331 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800332 flags,
333 outputDevice,
334 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700335 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700336 thread.get(), config.channel_mask);
337 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700338 status = NO_MEMORY;
339 goto exit;
340 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700341 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700342 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700343 if (status != NO_ERROR) {
344 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700345 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700346 if (audioHwDevice->isInsert()) {
347 insertedModule = audioHwDevice->handle();
348 }
Eric Laurent951f4552014-05-20 10:48:17 -0700349 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700350 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700351 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700352 patch->sinks[0].ext.mix.handle);
353 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700354 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800355 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700356 ALOGW("%s() bad capture I/O handle %d",
357 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800358 status = BAD_VALUE;
359 goto exit;
360 }
Eric Laurent83b88082014-06-20 18:31:16 -0700361 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200362 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700363 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200364 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800365 if (status == NO_ERROR) {
366 newPatch.setThread(thread);
367 }
Eric Laurent526aa572019-01-15 10:54:58 -0800368 // remove stale audio patch with same input as sink if any
369 for (auto& iter : mPatches) {
370 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800371 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800372 break;
373 }
374 }
Eric Laurent83b88082014-06-20 18:31:16 -0700375 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700376 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
377 status = hwDevice->createAudioPatch(patch->num_sources,
378 patch->sources,
379 patch->num_sinks,
380 patch->sinks,
381 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700382 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700383 }
Eric Laurent951f4552014-05-20 10:48:17 -0700384 }
385 } break;
386 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700387 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700388 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700389 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700390 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700391 status = BAD_VALUE;
392 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700393 }
394 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700395 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700396 for (unsigned int i = 0; i < patch->num_sinks; i++) {
397 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700398 ALOGW("%s() invalid sink type %d for mix source",
399 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700400 status = BAD_VALUE;
401 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700402 }
403 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700404 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700405 status = BAD_VALUE;
406 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700407 }
jiabinc52b1ff2019-10-31 17:20:42 -0700408 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
409 patch->sinks[i].ext.device.type);
410 device->setAddress(patch->sinks[i].ext.device.address);
411 device->applyAudioPortConfig(&patch->sinks[i]);
412 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700413 }
Eric Laurent951f4552014-05-20 10:48:17 -0700414 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700415 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700416 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700417 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800418 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700419 ALOGW("%s() bad playback I/O handle %d",
420 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800421 status = BAD_VALUE;
422 goto exit;
423 }
Eric Laurent951f4552014-05-20 10:48:17 -0700424 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700425 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700426 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700427 }
428
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200429 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700430 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200431 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800432 if (status == NO_ERROR) {
433 newPatch.setThread(thread);
434 }
Eric Laurent526aa572019-01-15 10:54:58 -0800435
436 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200437 // Prevent to remove endpoint patches (involved in a SwBridge)
438 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
439 if (!endpointPatch) {
440 for (auto& iter : mPatches) {
441 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
442 !iter.second.mIsEndpointPatch) {
443 erasePatch(iter.first);
444 break;
445 }
Eric Laurent526aa572019-01-15 10:54:58 -0800446 }
447 }
Eric Laurent951f4552014-05-20 10:48:17 -0700448 } break;
449 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700450 status = BAD_VALUE;
451 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700452 }
Eric Laurent83b88082014-06-20 18:31:16 -0700453exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700454 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700455 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700456 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
457 newPatch.mHalHandle = halHandle;
Vlad Popa5161f8a2022-10-10 16:17:20 +0200458 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700459 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100460 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700461 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100462 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700463 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700464 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700465 }
466 return status;
467}
468
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700469AudioFlinger::PatchPanel::Patch::~Patch()
470{
471 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
472 mRecord.handle(), mPlayback.handle());
473}
474
Mikhail Naganovdea53042018-04-26 13:10:21 -0700475status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700476{
477 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700478 status_t status = panel->createAudioPatch(
479 PatchBuilder().addSource(mAudioPatch.sources[0]).
480 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200481 mRecord.handlePtr(),
482 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700483 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700484 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700485 return status;
486 }
487
488 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700489 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700490 status = panel->createAudioPatch(
491 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200492 mPlayback.handlePtr(),
493 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700494 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700495 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700496 return status;
497 }
498 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700499 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700500 }
501
Eric Laurent83b88082014-06-20 18:31:16 -0700502 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700503 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700504 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700505 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
506 uint32_t sampleRate = mPlayback.thread()->sampleRate();
507 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700508
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700509 audio_format_t inputFormat = mRecord.thread()->format();
510 if (!audio_is_linear_pcm(inputFormat)) {
511 // The playbackThread format will say PCM for IEC61937 packetized stream.
512 // Use recordThread format.
513 format = inputFormat;
514 }
515 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
516 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700517 if (sampleRate == mRecord.thread()->sampleRate() &&
518 inChannelMask == mRecord.thread()->channelMask() &&
519 mRecord.thread()->fastTrackAvailable() &&
520 mRecord.thread()->hasFastCapture()) {
521 // Create a fast track if the record thread has fast capture to get better performance.
522 // Only enable fast mode when there is no resample needed.
523 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
524 } else {
525 // Fast mode is not available in this case.
526 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
527 }
Eric Laurent83b88082014-06-20 18:31:16 -0700528
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700529 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
530 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700531 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200532 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700533 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
534 // "reuse one existing output mix" case
535 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200536 // For telephony patches, propagate voice communication use case to record side
537 if (streamType == AUDIO_STREAM_VOICE_CALL) {
538 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
539 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700540 }
jiabin01c8f562018-07-19 17:47:28 -0700541 if (mPlayback.thread()->hasFastMixer()) {
542 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700543 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700544 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700545 } else {
546 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700547 }
548
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700549 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800550 const bool usePassthruPatchRecord =
551 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100552 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
553 const size_t recordFrameCount = mRecord.thread()->frameCount();
554 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800555 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100556 // PassthruPatchRecord producesBufferOnDemand, so use
557 // maximum of playback and record thread framecounts
558 frameCount = std::max(playbackFrameCount, recordFrameCount);
559 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
560 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700561 tempRecordTrack = new RecordThread::PassthruPatchRecord(
562 mRecord.thread().get(),
563 sampleRate,
564 inChannelMask,
565 format,
566 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200567 inputFlags,
568 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700569 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100570 // use a pseudo LCM between input and output framecount
571 int playbackShift = __builtin_ctz(playbackFrameCount);
572 int shift = __builtin_ctz(recordFrameCount);
573 if (playbackShift < shift) {
574 shift = playbackShift;
575 }
576 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
577 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
578 __func__, playbackFrameCount, recordFrameCount, frameCount);
579
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700580 tempRecordTrack = new RecordThread::PatchRecord(
581 mRecord.thread().get(),
582 sampleRate,
583 inChannelMask,
584 format,
585 frameCount,
586 nullptr,
587 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200588 inputFlags,
589 {} /* timeout */,
590 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700591 }
592 status = mRecord.checkTrack(tempRecordTrack.get());
593 if (status != NO_ERROR) {
594 return status;
595 }
596
Eric Laurent83b88082014-06-20 18:31:16 -0700597 // create a special playback track to render to playback thread.
598 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200599
600 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
601 // it might glitch.
602 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
603 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
604 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700605 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700606 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700607 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700608 sampleRate,
609 outChannelMask,
610 format,
611 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700612 tempRecordTrack->buffer(),
613 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200614 outputFlags,
615 {} /*timeout*/,
616 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700617 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700618 if (status != NO_ERROR) {
619 return status;
620 }
Eric Laurent83b88082014-06-20 18:31:16 -0700621
622 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800623 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
624 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
625 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
626 // is alive. There is no need to hold a reference, and there is no need
627 // to clear it. In fact, since playback stopping is asynchronous, there is
628 // no proper time when clearing could be done.
629 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
630 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700631
632 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700633 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
634 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700635
636 return status;
637}
638
Mikhail Naganovdea53042018-04-26 13:10:21 -0700639void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700640{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700641 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
642 __func__, mRecord.handle(), mPlayback.handle());
643 mRecord.stopTrack();
644 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700645 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700646 mRecord.closeConnections(panel);
647 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700648}
649
Andy Hungc3ab7732018-06-01 13:46:29 -0700650status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
651{
652 if (!isSoftware()) return INVALID_OPERATION;
653
654 auto recordTrack = mRecord.const_track();
655 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
656
657 auto playbackTrack = mPlayback.const_track();
658 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
659
660 // Latency information for tracks may be called without obtaining
661 // the underlying thread lock.
662 //
663 // We use record server latency + playback track latency (generally smaller than the
664 // reverse due to internal biases).
665 //
666 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700667
Andy Hung30282562018-08-08 18:27:03 -0700668 // For PCM tracks get server latency.
669 if (audio_is_linear_pcm(recordTrack->format())) {
670 double recordServerLatencyMs, playbackTrackLatencyMs;
671 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
672 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
673 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
674 return OK;
675 }
676 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700677
Andy Hung30282562018-08-08 18:27:03 -0700678 // See if kernel latencies are available.
679 // If so, do a frame diff and time difference computation to estimate
680 // the total patch latency. This requires that frame counts are reported by the
681 // HAL are matched properly in the case of record overruns and playback underruns.
682 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
683 recordTrack->getKernelFrameTime(&recordFT);
684 playbackTrack->getKernelFrameTime(&playFT);
685 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
686 const int64_t frameDiff = recordFT.frames - playFT.frames;
687 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
688
689 // It is possible that the patch track and patch record have a large time disparity because
690 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
691 // time difference based on how often we expect the timestamps to update in normal operation
692 // (typical should be no more than 50 ms).
693 //
694 // If the timestamps aren't sampled close enough, the patch latency is not
695 // considered valid.
696 //
697 // TODO: change this based on more experiments.
698 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
699 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
700 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
701 - timeDiffNs * 1e-6;
702 return OK;
703 }
704 }
705
706 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700707}
708
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700709String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700710{
Andy Hungc3ab7732018-06-01 13:46:29 -0700711 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800712 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
713 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
714 mRecord.const_thread().get(), mPlayback.const_thread().get());
715
716 bool hasSinkDevice =
717 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
718 bool hasSourceDevice =
719 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
720 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
721 hasSinkDevice ? "num sinks" :
722 (hasSourceDevice ? "num sources" : "no devices"),
723 hasSinkDevice ? mAudioPatch.num_sinks :
724 (hasSourceDevice ? mAudioPatch.num_sources : 0),
725 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
726 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700727
728 // add latency if it exists
729 double latencyMs;
730 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700731 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700732 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700733 return result;
734}
735
Eric Laurent951f4552014-05-20 10:48:17 -0700736/* Disconnect a patch */
737status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
738{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700739 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700740 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700741
Mikhail Naganovdea53042018-04-26 13:10:21 -0700742 auto iter = mPatches.find(handle);
743 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700744 return BAD_VALUE;
745 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700746 Patch &removedPatch = iter->second;
747 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700748
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700749 const struct audio_port_config &src = patch.sources[0];
750 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700751 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700752 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
753 if (hwDevice == 0) {
754 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700755 status = BAD_VALUE;
756 break;
757 }
Eric Laurent83b88082014-06-20 18:31:16 -0700758
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700759 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700760 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700761 break;
762 }
763
Mikhail Naganovdea53042018-04-26 13:10:21 -0700764 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
765 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
766 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700767 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700768 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800769 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700770 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800771 status = BAD_VALUE;
772 break;
773 }
Eric Laurent951f4552014-05-20 10:48:17 -0700774 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700775 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700776 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700777 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700778 }
779 } break;
780 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700781 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
782 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700783 status = BAD_VALUE;
784 break;
785 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700786 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700787 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700788 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700789 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800790 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700791 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800792 status = BAD_VALUE;
793 break;
794 }
Eric Laurent951f4552014-05-20 10:48:17 -0700795 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700796 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700797 } break;
798 default:
799 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700800 }
801
Eric Laurentb82e6b72019-11-22 17:25:04 -0800802 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700803 return status;
804}
805
Eric Laurentb82e6b72019-11-22 17:25:04 -0800806void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
807 mPatches.erase(handle);
808 removeSoftwarePatchFromInsertedModules(handle);
Vlad Popa5161f8a2022-10-10 16:17:20 +0200809 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800810}
811
Eric Laurent951f4552014-05-20 10:48:17 -0700812/* List connected audio ports and they attributes */
813status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
814 struct audio_patch *patches __unused)
815{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700816 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700817 return NO_ERROR;
818}
819
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700820status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
821 audio_io_handle_t stream,
822 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
823{
824 for (const auto& module : mInsertedModules) {
825 if (module.second.streams.count(stream)) {
826 for (const auto& patchHandle : module.second.sw_patches) {
827 const auto& patch_iter = mPatches.find(patchHandle);
828 if (patch_iter != mPatches.end()) {
829 const Patch &patch = patch_iter->second;
830 patches->emplace_back(*this, patchHandle,
831 patch.mPlayback.const_thread()->id(),
832 patch.mRecord.const_thread()->id());
833 } else {
834 ALOGE("Stale patch handle in the cache: %d", patchHandle);
835 }
836 }
837 return OK;
838 }
839 }
840 // The stream is not associated with any of inserted modules.
841 return BAD_VALUE;
842}
843
844void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100845 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700846{
847 if (audioHwDevice->isInsert()) {
848 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100849 if (patch != nullptr) {
850 std::vector <SoftwarePatch> swPatches;
851 getDownstreamSoftwarePatches(stream, &swPatches);
852 if (swPatches.size() > 0) {
853 auto iter = mPatches.find(swPatches[0].getPatchHandle());
854 if (iter != mPatches.end()) {
855 *patch = iter->second.mAudioPatch;
856 }
857 }
858 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700859 }
860}
861
862void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
863{
864 for (auto& module : mInsertedModules) {
865 module.second.streams.erase(stream);
866 }
867}
868
869AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700870{
871 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
872 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
873 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700874 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700875 return nullptr;
876 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700877 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700878}
879
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700880sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700881{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700882 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
883 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
884}
885
886void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100887 audio_module_handle_t module, audio_patch_handle_t handle,
888 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700889{
890 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100891 if (!mInsertedModules[module].streams.empty()) {
892 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
893 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700894}
895
896void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
897 audio_patch_handle_t handle)
898{
899 for (auto& module : mInsertedModules) {
900 module.second.sw_patches.erase(handle);
901 }
902}
903
904void AudioFlinger::PatchPanel::dump(int fd) const
905{
906 String8 patchPanelDump;
907 const char *indent = " ";
908
Mikhail Naganov201369b2018-05-16 16:52:32 -0700909 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700910 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800911 if (!headerPrinted) {
912 patchPanelDump += "\nPatches:\n";
913 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700914 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800915 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700916 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700917
918 headerPrinted = false;
919 for (const auto& module : mInsertedModules) {
920 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
921 if (!headerPrinted) {
922 patchPanelDump += "\nTracked inserted modules:\n";
923 headerPrinted = true;
924 }
925 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
926 for (const auto& stream : module.second.streams) {
927 moduleDump.appendFormat("%d ", stream);
928 }
929 moduleDump.append("; SW Patches: ");
930 for (const auto& patch : module.second.sw_patches) {
931 moduleDump.appendFormat("%d ", patch);
932 }
933 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
934 }
935 }
936
937 if (!patchPanelDump.isEmpty()) {
938 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700939 }
940}
941
Glenn Kasten63238ef2015-03-02 15:50:29 -0800942} // namespace android