blob: 2302bd84a02d0994b0909e04c80835189e6d02af [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,
53 struct audio_port *ports)
54{
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 */
jiabinb4fed192020-09-22 14:45:40 -070060status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) {
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 */
91status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
92 struct audio_patch *patches)
93{
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 */
99sp<IAfPatchPanel> IAfPatchPanel::create(AudioFlinger* audioFlinger) {
Andy Hung07434ef2023-07-13 18:11:33 -0700100 return sp<PatchPanel>::make(audioFlinger);
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) {
122 mAudioFlinger.closeThreadInternal_l(recordThread);
123 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
124 playbackThread) {
125 mAudioFlinger.closeThreadInternal_l(playbackThread);
126 } 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 Hung44f27182023-07-06 20:56:16 -0700278 const sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700279 mAudioFlinger.checkPlaybackThread_l(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 Hung44f27182023-07-06 20:56:16 -0700305 const sp<IAfThreadBase> thread = mAudioFlinger.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);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700313 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
314 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 Hung44f27182023-07-06 20:56:16 -0700352 const sp<IAfThreadBase> thread = mAudioFlinger.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);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700361 ALOGV("mAudioFlinger.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 Hung44f27182023-07-06 20:56:16 -0700377 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700378 patch->sinks[0].ext.mix.handle);
379 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700380 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800381 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700382 ALOGW("%s() bad capture I/O handle %d",
383 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800384 status = BAD_VALUE;
385 goto exit;
386 }
Eric Laurent83b88082014-06-20 18:31:16 -0700387 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200388 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700389 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200390 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800391 if (status == NO_ERROR) {
392 newPatch.setThread(thread);
393 }
Eric Laurent526aa572019-01-15 10:54:58 -0800394 // remove stale audio patch with same input as sink if any
395 for (auto& iter : mPatches) {
396 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800397 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800398 break;
399 }
400 }
Eric Laurent83b88082014-06-20 18:31:16 -0700401 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700402 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
403 status = hwDevice->createAudioPatch(patch->num_sources,
404 patch->sources,
405 patch->num_sinks,
406 patch->sinks,
407 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700408 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700409 }
Eric Laurent951f4552014-05-20 10:48:17 -0700410 }
411 } break;
412 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700413 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700414 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700415 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700416 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700417 status = BAD_VALUE;
418 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700419 }
420 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700421 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700422 for (unsigned int i = 0; i < patch->num_sinks; i++) {
423 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700424 ALOGW("%s() invalid sink type %d for mix source",
425 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700426 status = BAD_VALUE;
427 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700428 }
429 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700430 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700431 status = BAD_VALUE;
432 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700433 }
jiabinc52b1ff2019-10-31 17:20:42 -0700434 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
435 patch->sinks[i].ext.device.type);
436 device->setAddress(patch->sinks[i].ext.device.address);
437 device->applyAudioPortConfig(&patch->sinks[i]);
438 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700439 }
Andy Hung44f27182023-07-06 20:56:16 -0700440 sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700441 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700442 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700443 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800444 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700445 ALOGW("%s() bad playback I/O handle %d",
446 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800447 status = BAD_VALUE;
448 goto exit;
449 }
Eric Laurent951f4552014-05-20 10:48:17 -0700450 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700451 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700452 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700453 }
454
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200455 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700456 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200457 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800458 if (status == NO_ERROR) {
459 newPatch.setThread(thread);
460 }
Eric Laurent526aa572019-01-15 10:54:58 -0800461
462 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200463 // Prevent to remove endpoint patches (involved in a SwBridge)
464 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
465 if (!endpointPatch) {
466 for (auto& iter : mPatches) {
467 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
468 !iter.second.mIsEndpointPatch) {
469 erasePatch(iter.first);
470 break;
471 }
Eric Laurent526aa572019-01-15 10:54:58 -0800472 }
473 }
Eric Laurent951f4552014-05-20 10:48:17 -0700474 } break;
475 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700476 status = BAD_VALUE;
477 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700478 }
Eric Laurent83b88082014-06-20 18:31:16 -0700479exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700480 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700481 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700482 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
483 newPatch.mHalHandle = halHandle;
Vlad Popa5161f8a2022-10-10 16:17:20 +0200484 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700485 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100486 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700487 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100488 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700489 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700490 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700491 }
492 return status;
493}
494
Andy Hung07434ef2023-07-13 18:11:33 -0700495PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700496{
497 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
498 mRecord.handle(), mPlayback.handle());
499}
500
Andy Hung07434ef2023-07-13 18:11:33 -0700501status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700502{
503 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700504 status_t status = panel->createAudioPatch(
505 PatchBuilder().addSource(mAudioPatch.sources[0]).
506 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200507 mRecord.handlePtr(),
508 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700509 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700510 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700511 return status;
512 }
513
514 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700515 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700516 status = panel->createAudioPatch(
517 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200518 mPlayback.handlePtr(),
519 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700520 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700521 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700522 return status;
523 }
524 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700525 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700526 }
527
Eric Laurent83b88082014-06-20 18:31:16 -0700528 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700529 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700530 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700531 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
532 uint32_t sampleRate = mPlayback.thread()->sampleRate();
533 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700534
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700535 audio_format_t inputFormat = mRecord.thread()->format();
536 if (!audio_is_linear_pcm(inputFormat)) {
537 // The playbackThread format will say PCM for IEC61937 packetized stream.
538 // Use recordThread format.
539 format = inputFormat;
540 }
541 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
542 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700543 if (sampleRate == mRecord.thread()->sampleRate() &&
544 inChannelMask == mRecord.thread()->channelMask() &&
545 mRecord.thread()->fastTrackAvailable() &&
546 mRecord.thread()->hasFastCapture()) {
547 // Create a fast track if the record thread has fast capture to get better performance.
548 // Only enable fast mode when there is no resample needed.
549 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
550 } else {
551 // Fast mode is not available in this case.
552 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
553 }
Eric Laurent83b88082014-06-20 18:31:16 -0700554
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700555 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
556 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700557 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200558 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700559 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
560 // "reuse one existing output mix" case
561 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200562 // For telephony patches, propagate voice communication use case to record side
563 if (streamType == AUDIO_STREAM_VOICE_CALL) {
564 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
565 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700566 }
jiabin01c8f562018-07-19 17:47:28 -0700567 if (mPlayback.thread()->hasFastMixer()) {
568 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700569 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700570 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700571 } else {
572 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700573 }
574
Andy Hung3ff4b552023-06-26 19:20:57 -0700575 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800576 const bool usePassthruPatchRecord =
577 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100578 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
579 const size_t recordFrameCount = mRecord.thread()->frameCount();
580 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800581 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100582 // PassthruPatchRecord producesBufferOnDemand, so use
583 // maximum of playback and record thread framecounts
584 frameCount = std::max(playbackFrameCount, recordFrameCount);
585 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
586 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung3ff4b552023-06-26 19:20:57 -0700587 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700588 mRecord.thread().get(),
589 sampleRate,
590 inChannelMask,
591 format,
592 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200593 inputFlags,
594 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700595 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100596 // use a pseudo LCM between input and output framecount
597 int playbackShift = __builtin_ctz(playbackFrameCount);
598 int shift = __builtin_ctz(recordFrameCount);
599 if (playbackShift < shift) {
600 shift = playbackShift;
601 }
602 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
603 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
604 __func__, playbackFrameCount, recordFrameCount, frameCount);
605
Andy Hung3ff4b552023-06-26 19:20:57 -0700606 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700607 mRecord.thread().get(),
608 sampleRate,
609 inChannelMask,
610 format,
611 frameCount,
612 nullptr,
613 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200614 inputFlags,
615 {} /* timeout */,
616 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700617 }
618 status = mRecord.checkTrack(tempRecordTrack.get());
619 if (status != NO_ERROR) {
620 return status;
621 }
622
Eric Laurent83b88082014-06-20 18:31:16 -0700623 // create a special playback track to render to playback thread.
624 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200625
626 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
627 // it might glitch.
628 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
629 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
630 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung3ff4b552023-06-26 19:20:57 -0700631 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700632 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700633 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700634 sampleRate,
635 outChannelMask,
636 format,
637 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700638 tempRecordTrack->buffer(),
639 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200640 outputFlags,
641 {} /*timeout*/,
642 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700643 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700644 if (status != NO_ERROR) {
645 return status;
646 }
Eric Laurent83b88082014-06-20 18:31:16 -0700647
648 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800649 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
650 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
651 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
652 // is alive. There is no need to hold a reference, and there is no need
653 // to clear it. In fact, since playback stopping is asynchronous, there is
654 // no proper time when clearing could be done.
655 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
656 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700657
658 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700659 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
660 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700661
662 return status;
663}
664
Andy Hung07434ef2023-07-13 18:11:33 -0700665void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700666{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700667 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
668 __func__, mRecord.handle(), mPlayback.handle());
669 mRecord.stopTrack();
670 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700671 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700672 mRecord.closeConnections(panel);
673 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700674}
675
Andy Hung07434ef2023-07-13 18:11:33 -0700676status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700677{
678 if (!isSoftware()) return INVALID_OPERATION;
679
680 auto recordTrack = mRecord.const_track();
681 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
682
683 auto playbackTrack = mPlayback.const_track();
684 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
685
686 // Latency information for tracks may be called without obtaining
687 // the underlying thread lock.
688 //
689 // We use record server latency + playback track latency (generally smaller than the
690 // reverse due to internal biases).
691 //
692 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700693
Andy Hung30282562018-08-08 18:27:03 -0700694 // For PCM tracks get server latency.
695 if (audio_is_linear_pcm(recordTrack->format())) {
696 double recordServerLatencyMs, playbackTrackLatencyMs;
697 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
698 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
699 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
700 return OK;
701 }
702 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700703
Andy Hung30282562018-08-08 18:27:03 -0700704 // See if kernel latencies are available.
705 // If so, do a frame diff and time difference computation to estimate
706 // the total patch latency. This requires that frame counts are reported by the
707 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700708 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700709 recordTrack->getKernelFrameTime(&recordFT);
710 playbackTrack->getKernelFrameTime(&playFT);
711 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
712 const int64_t frameDiff = recordFT.frames - playFT.frames;
713 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
714
715 // It is possible that the patch track and patch record have a large time disparity because
716 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
717 // time difference based on how often we expect the timestamps to update in normal operation
718 // (typical should be no more than 50 ms).
719 //
720 // If the timestamps aren't sampled close enough, the patch latency is not
721 // considered valid.
722 //
723 // TODO: change this based on more experiments.
724 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
725 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
726 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
727 - timeDiffNs * 1e-6;
728 return OK;
729 }
730 }
731
732 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700733}
734
Andy Hung07434ef2023-07-13 18:11:33 -0700735String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700736{
Andy Hungc3ab7732018-06-01 13:46:29 -0700737 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800738 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
739 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
740 mRecord.const_thread().get(), mPlayback.const_thread().get());
741
742 bool hasSinkDevice =
743 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
744 bool hasSourceDevice =
745 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
746 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
747 hasSinkDevice ? "num sinks" :
748 (hasSourceDevice ? "num sources" : "no devices"),
749 hasSinkDevice ? mAudioPatch.num_sinks :
750 (hasSourceDevice ? mAudioPatch.num_sources : 0),
751 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
752 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700753
754 // add latency if it exists
755 double latencyMs;
756 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700757 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700758 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700759 return result;
760}
761
Eric Laurent951f4552014-05-20 10:48:17 -0700762/* Disconnect a patch */
Andy Hung07434ef2023-07-13 18:11:33 -0700763status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung44f27182023-07-06 20:56:16 -0700764 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200765 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
766 //before processing the release patch request.
767 NO_THREAD_SAFETY_ANALYSIS
768 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700769 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700770 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700771
Mikhail Naganovdea53042018-04-26 13:10:21 -0700772 auto iter = mPatches.find(handle);
773 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700774 return BAD_VALUE;
775 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700776 Patch &removedPatch = iter->second;
777 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700778
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700779 const struct audio_port_config &src = patch.sources[0];
780 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700781 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700782 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
783 if (hwDevice == 0) {
784 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700785 status = BAD_VALUE;
786 break;
787 }
Eric Laurent83b88082014-06-20 18:31:16 -0700788
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700789 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700790 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700791 break;
792 }
793
Mikhail Naganovdea53042018-04-26 13:10:21 -0700794 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
795 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung44f27182023-07-06 20:56:16 -0700796 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700797 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700798 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800799 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700800 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800801 status = BAD_VALUE;
802 break;
803 }
Eric Laurent951f4552014-05-20 10:48:17 -0700804 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200805 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700806 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200807 mAudioFlinger.lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700808 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700809 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700810 }
811 } break;
812 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700813 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
814 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700815 status = BAD_VALUE;
816 break;
817 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700818 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung44f27182023-07-06 20:56:16 -0700819 sp<IAfThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700820 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700821 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800822 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700823 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800824 status = BAD_VALUE;
825 break;
826 }
Eric Laurent951f4552014-05-20 10:48:17 -0700827 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200828 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700829 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200830 mAudioFlinger.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700831 } break;
832 default:
833 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700834 }
835
Eric Laurentb82e6b72019-11-22 17:25:04 -0800836 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700837 return status;
838}
839
Andy Hung07434ef2023-07-13 18:11:33 -0700840void PatchPanel::erasePatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800841 mPatches.erase(handle);
842 removeSoftwarePatchFromInsertedModules(handle);
Vlad Popa5161f8a2022-10-10 16:17:20 +0200843 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800844}
845
Eric Laurent951f4552014-05-20 10:48:17 -0700846/* List connected audio ports and they attributes */
Andy Hung07434ef2023-07-13 18:11:33 -0700847status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700848 struct audio_patch *patches __unused)
849{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700850 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700851 return NO_ERROR;
852}
853
Andy Hung07434ef2023-07-13 18:11:33 -0700854status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700855 audio_io_handle_t stream,
Andy Hungd63e79d2023-07-13 16:52:46 -0700856 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700857{
858 for (const auto& module : mInsertedModules) {
859 if (module.second.streams.count(stream)) {
860 for (const auto& patchHandle : module.second.sw_patches) {
861 const auto& patch_iter = mPatches.find(patchHandle);
862 if (patch_iter != mPatches.end()) {
863 const Patch &patch = patch_iter->second;
Andy Hungd63e79d2023-07-13 16:52:46 -0700864 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
865 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700866 patch.mPlayback.const_thread()->id(),
867 patch.mRecord.const_thread()->id());
868 } else {
869 ALOGE("Stale patch handle in the cache: %d", patchHandle);
870 }
871 }
872 return OK;
873 }
874 }
875 // The stream is not associated with any of inserted modules.
876 return BAD_VALUE;
877}
878
Andy Hung07434ef2023-07-13 18:11:33 -0700879void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100880 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700881{
882 if (audioHwDevice->isInsert()) {
883 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100884 if (patch != nullptr) {
885 std::vector <SoftwarePatch> swPatches;
886 getDownstreamSoftwarePatches(stream, &swPatches);
887 if (swPatches.size() > 0) {
888 auto iter = mPatches.find(swPatches[0].getPatchHandle());
889 if (iter != mPatches.end()) {
890 *patch = iter->second.mAudioPatch;
891 }
892 }
893 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700894 }
895}
896
Andy Hung07434ef2023-07-13 18:11:33 -0700897void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700898{
899 for (auto& module : mInsertedModules) {
900 module.second.streams.erase(stream);
901 }
902}
903
Andy Hung07434ef2023-07-13 18:11:33 -0700904AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700905{
906 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
907 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
908 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700909 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700910 return nullptr;
911 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700912 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700913}
914
Andy Hung07434ef2023-07-13 18:11:33 -0700915sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700916{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700917 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
918 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
919}
920
Andy Hung07434ef2023-07-13 18:11:33 -0700921void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100922 audio_module_handle_t module, audio_patch_handle_t handle,
923 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700924{
925 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100926 if (!mInsertedModules[module].streams.empty()) {
927 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
928 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700929}
930
Andy Hung07434ef2023-07-13 18:11:33 -0700931void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700932 audio_patch_handle_t handle)
933{
934 for (auto& module : mInsertedModules) {
935 module.second.sw_patches.erase(handle);
936 }
937}
938
Andy Hung07434ef2023-07-13 18:11:33 -0700939void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700940{
941 String8 patchPanelDump;
942 const char *indent = " ";
943
Mikhail Naganov201369b2018-05-16 16:52:32 -0700944 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700945 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800946 if (!headerPrinted) {
947 patchPanelDump += "\nPatches:\n";
948 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700949 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000950 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700951 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700952
953 headerPrinted = false;
954 for (const auto& module : mInsertedModules) {
955 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
956 if (!headerPrinted) {
957 patchPanelDump += "\nTracked inserted modules:\n";
958 headerPrinted = true;
959 }
960 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
961 for (const auto& stream : module.second.streams) {
962 moduleDump.appendFormat("%d ", stream);
963 }
964 moduleDump.append("; SW Patches: ");
965 for (const auto& patch : module.second.sw_patches) {
966 moduleDump.appendFormat("%d ", patch);
967 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000968 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700969 }
970 }
971
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000972 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000973 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700974 }
975}
976
Glenn Kasten63238ef2015-03-02 15:50:29 -0800977} // namespace android