blob: 5b2b7b5ac33f24017051783cb65df9e0cf487496 [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);
Andy Hungd63e79d2023-07-13 16:52:46 -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);
Andy Hungd63e79d2023-07-13 16:52:46 -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);
Andy Hungd63e79d2023-07-13 16:52:46 -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);
Andy Hungd63e79d2023-07-13 16:52:46 -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);
Andy Hungd63e79d2023-07-13 16:52:46 -070094 return mPatchPanel->listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070095}
96
Andy Hungd63e79d2023-07-13 16:52:46 -070097/* static */
98sp<IAfPatchPanel> IAfPatchPanel::create(AudioFlinger* audioFlinger) {
99 return sp<AudioFlinger::PatchPanel>::make(audioFlinger);
100}
101
102status_t SoftwarePatch::getLatencyMs_l(double* latencyMs) const {
103 return mPatchPanel->getLatencyMs_l(mPatchHandle, latencyMs);
104}
105
106status_t AudioFlinger::PatchPanel::getLatencyMs_l(
107 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700108{
Andy Hungd63e79d2023-07-13 16:52:46 -0700109 const auto& iter = mPatches.find(patchHandle);
110 if (iter != mPatches.end()) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700111 return iter->second.getLatencyMs(latencyMs);
112 } else {
113 return BAD_VALUE;
114 }
115}
116
Andy Hungd63e79d2023-07-13 16:52:46 -0700117void AudioFlinger::PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
118{
119 if (const auto recordThread = thread->asIAfRecordThread();
120 recordThread) {
121 mAudioFlinger.closeThreadInternal_l(recordThread);
122 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
123 playbackThread) {
124 mAudioFlinger.closeThreadInternal_l(playbackThread);
125 } else {
126 LOG_ALWAYS_FATAL("%s: Endpoints only accept IAfPlayback and IAfRecord threads, "
127 "invalid thread, id: %d type: %d",
128 __func__, thread->id(), thread->type());
129 }
130}
131
Eric Laurent951f4552014-05-20 10:48:17 -0700132/* List connected audio ports and their attributes */
133status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
134 struct audio_port *ports __unused)
135{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700136 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700137 return NO_ERROR;
138}
139
140/* Get supported attributes for a given audio port */
jiabinb4fed192020-09-22 14:45:40 -0700141status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port)
Eric Laurent951f4552014-05-20 10:48:17 -0700142{
jiabinb4fed192020-09-22 14:45:40 -0700143 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
144 // Only query the HAL when the port is a device.
145 // TODO: implement getAudioPort for mix.
146 return INVALID_OPERATION;
147 }
148 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
149 if (hwDevice == nullptr) {
150 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
151 return BAD_VALUE;
152 }
153 if (!hwDevice->supportsAudioPatches()) {
154 return INVALID_OPERATION;
155 }
156 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700157}
158
Eric Laurent951f4552014-05-20 10:48:17 -0700159/* Connect a patch between several source and sink ports */
160status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200161 audio_patch_handle_t *handle,
162 bool endpointPatch)
Andy Hung44f27182023-07-06 20:56:16 -0700163 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendCreateAudioPatchConfigEvent
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200164 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
165 //before processing the create patch request.
166 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700167{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700168 if (handle == NULL || patch == NULL) {
169 return BAD_VALUE;
170 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700171 ALOGV("%s() num_sources %d num_sinks %d handle %d",
172 __func__, patch->num_sources, patch->num_sinks, *handle);
173 status_t status = NO_ERROR;
174 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700175
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700176 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700177 return BAD_VALUE;
178 }
Eric Laurent874c42872014-08-08 15:13:39 -0700179 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
180 // only the audio policy manager can request a patch creation with 2 sources.
181 if (patch->num_sources > 2) {
182 return INVALID_OPERATION;
183 }
Eric Laurent951f4552014-05-20 10:48:17 -0700184
Eric Laurent83b88082014-06-20 18:31:16 -0700185 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700186 auto iter = mPatches.find(*handle);
187 if (iter != mPatches.end()) {
188 ALOGV("%s() removing patch handle %d", __func__, *handle);
189 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700190 // free resources owned by the removed patch if applicable
191 // 1) if a software patch is present, release the playback and capture threads and
192 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700193 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700194 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700195 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700196 // 2) if the new patch and old patch source or sink are devices from different
197 // hw modules, clear the audio HAL patches now because they will not be updated
198 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700199 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700200 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
201 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
202 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
203 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
204 oldPatch.sources[0].ext.device.hw_module !=
205 patch->sources[0].ext.device.hw_module)) {
206 hwModule = oldPatch.sources[0].ext.device.hw_module;
207 } else if (patch->num_sinks == 0 ||
208 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
209 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
210 oldPatch.sinks[0].ext.device.hw_module !=
211 patch->sinks[0].ext.device.hw_module))) {
212 // Note on (patch->num_sinks == 0): this situation should not happen as
213 // these special patches are only created by the policy manager but just
214 // in case, systematically clear the HAL patch.
215 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700216 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700217 hwModule = oldPatch.sinks[0].ext.device.hw_module;
218 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700219 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
220 if (hwDevice != 0) {
221 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700222 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800223 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700224 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800225 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700226 }
227 }
228
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200229 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700230 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700231
Eric Laurent951f4552014-05-20 10:48:17 -0700232 switch (patch->sources[0].type) {
233 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700234 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700235 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
236 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700237 status = BAD_VALUE;
238 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700239 }
240 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700241 // support only one sink if connection to a mix or across HW modules
242 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700243 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
244 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700245 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700246 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700247 status = INVALID_OPERATION;
248 goto exit;
249 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700250 // reject connection to different sink types
251 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700252 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700253 status = BAD_VALUE;
254 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700255 }
Eric Laurent951f4552014-05-20 10:48:17 -0700256 }
257
Eric Laurent3bcf8592015-04-03 12:13:24 -0700258 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700259 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700260 // - Device to device AND
261 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700262 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700263 if ((patch->num_sources == 2) ||
264 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
265 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700266 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800267 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
268 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700269 if (patch->num_sources == 2) {
270 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700271 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
272 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700273 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700274 status = INVALID_OPERATION;
275 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700276 }
Andy Hung44f27182023-07-06 20:56:16 -0700277 const sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700278 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700279 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700280 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700281 status = INVALID_OPERATION;
282 goto exit;
283 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700284 // existing playback thread is reused, so it is not closed when patch is cleared
285 newPatch.mPlayback.setThread(
Andy Hung44f27182023-07-06 20:56:16 -0700286 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700287 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700288 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200289 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700290 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700291 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
292 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
293 config.sample_rate = patch->sinks[0].sample_rate;
294 }
295 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
296 config.channel_mask = patch->sinks[0].channel_mask;
297 }
298 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
299 config.format = patch->sinks[0].format;
300 }
301 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
302 flags = patch->sinks[0].flags.output;
303 }
Andy Hung44f27182023-07-06 20:56:16 -0700304 const sp<IAfThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800305 patch->sinks[0].ext.device.hw_module,
306 &output,
307 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200308 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800309 outputDevice,
310 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700311 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700312 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
313 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700314 status = NO_MEMORY;
315 goto exit;
316 }
Andy Hung44f27182023-07-06 20:56:16 -0700317 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700318 }
Eric Laurent83b88082014-06-20 18:31:16 -0700319 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700320 String8 address = String8(patch->sources[0].ext.device.address);
321 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700322 // open input stream with source device audio properties if provided or
323 // default to peer output stream properties otherwise.
324 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
325 config.sample_rate = patch->sources[0].sample_rate;
326 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700327 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700328 }
329 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
330 config.channel_mask = patch->sources[0].channel_mask;
331 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700332 config.channel_mask = audio_channel_in_mask_from_count(
333 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700334 }
335 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
336 config.format = patch->sources[0].format;
337 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700338 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700339 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700340 audio_input_flags_t flags =
341 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
342 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700343 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200344 audio_source_t source = AUDIO_SOURCE_MIC;
345 // For telephony patches, propagate voice communication use case to record side
346 if (patch->num_sources == 2
347 && patch->sources[1].ext.mix.usecase.stream
348 == AUDIO_STREAM_VOICE_CALL) {
349 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
350 }
Andy Hung44f27182023-07-06 20:56:16 -0700351 const sp<IAfThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700352 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700353 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700354 device,
355 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200356 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800357 flags,
358 outputDevice,
359 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700360 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700361 thread.get(), config.channel_mask);
362 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700363 status = NO_MEMORY;
364 goto exit;
365 }
Andy Hung44f27182023-07-06 20:56:16 -0700366 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700367 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700368 if (status != NO_ERROR) {
369 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700370 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700371 if (audioHwDevice->isInsert()) {
372 insertedModule = audioHwDevice->handle();
373 }
Eric Laurent951f4552014-05-20 10:48:17 -0700374 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700375 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung44f27182023-07-06 20:56:16 -0700376 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700377 patch->sinks[0].ext.mix.handle);
378 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700379 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800380 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700381 ALOGW("%s() bad capture I/O handle %d",
382 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800383 status = BAD_VALUE;
384 goto exit;
385 }
Eric Laurent83b88082014-06-20 18:31:16 -0700386 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200387 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700388 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200389 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800390 if (status == NO_ERROR) {
391 newPatch.setThread(thread);
392 }
Eric Laurent526aa572019-01-15 10:54:58 -0800393 // remove stale audio patch with same input as sink if any
394 for (auto& iter : mPatches) {
395 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800396 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800397 break;
398 }
399 }
Eric Laurent83b88082014-06-20 18:31:16 -0700400 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700401 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
402 status = hwDevice->createAudioPatch(patch->num_sources,
403 patch->sources,
404 patch->num_sinks,
405 patch->sinks,
406 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700407 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700408 }
Eric Laurent951f4552014-05-20 10:48:17 -0700409 }
410 } break;
411 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700412 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700413 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700414 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700415 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700416 status = BAD_VALUE;
417 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700418 }
419 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700420 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700421 for (unsigned int i = 0; i < patch->num_sinks; i++) {
422 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700423 ALOGW("%s() invalid sink type %d for mix source",
424 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700425 status = BAD_VALUE;
426 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700427 }
428 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700429 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700430 status = BAD_VALUE;
431 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700432 }
jiabinc52b1ff2019-10-31 17:20:42 -0700433 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
434 patch->sinks[i].ext.device.type);
435 device->setAddress(patch->sinks[i].ext.device.address);
436 device->applyAudioPortConfig(&patch->sinks[i]);
437 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700438 }
Andy Hung44f27182023-07-06 20:56:16 -0700439 sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700440 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700441 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700442 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800443 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700444 ALOGW("%s() bad playback I/O handle %d",
445 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800446 status = BAD_VALUE;
447 goto exit;
448 }
Eric Laurent951f4552014-05-20 10:48:17 -0700449 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700450 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700451 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700452 }
453
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200454 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700455 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200456 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800457 if (status == NO_ERROR) {
458 newPatch.setThread(thread);
459 }
Eric Laurent526aa572019-01-15 10:54:58 -0800460
461 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200462 // Prevent to remove endpoint patches (involved in a SwBridge)
463 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
464 if (!endpointPatch) {
465 for (auto& iter : mPatches) {
466 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
467 !iter.second.mIsEndpointPatch) {
468 erasePatch(iter.first);
469 break;
470 }
Eric Laurent526aa572019-01-15 10:54:58 -0800471 }
472 }
Eric Laurent951f4552014-05-20 10:48:17 -0700473 } break;
474 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700475 status = BAD_VALUE;
476 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700477 }
Eric Laurent83b88082014-06-20 18:31:16 -0700478exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700479 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700480 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700481 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
482 newPatch.mHalHandle = halHandle;
Vlad Popa5161f8a2022-10-10 16:17:20 +0200483 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700484 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100485 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700486 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100487 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700488 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700489 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700490 }
491 return status;
492}
493
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700494AudioFlinger::PatchPanel::Patch::~Patch()
495{
496 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
497 mRecord.handle(), mPlayback.handle());
498}
499
Andy Hungd63e79d2023-07-13 16:52:46 -0700500status_t AudioFlinger::PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700501{
502 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700503 status_t status = panel->createAudioPatch(
504 PatchBuilder().addSource(mAudioPatch.sources[0]).
505 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200506 mRecord.handlePtr(),
507 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700508 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700509 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700510 return status;
511 }
512
513 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700514 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700515 status = panel->createAudioPatch(
516 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200517 mPlayback.handlePtr(),
518 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700519 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700520 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700521 return status;
522 }
523 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700524 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700525 }
526
Eric Laurent83b88082014-06-20 18:31:16 -0700527 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700528 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700529 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700530 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
531 uint32_t sampleRate = mPlayback.thread()->sampleRate();
532 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700533
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700534 audio_format_t inputFormat = mRecord.thread()->format();
535 if (!audio_is_linear_pcm(inputFormat)) {
536 // The playbackThread format will say PCM for IEC61937 packetized stream.
537 // Use recordThread format.
538 format = inputFormat;
539 }
540 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
541 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700542 if (sampleRate == mRecord.thread()->sampleRate() &&
543 inChannelMask == mRecord.thread()->channelMask() &&
544 mRecord.thread()->fastTrackAvailable() &&
545 mRecord.thread()->hasFastCapture()) {
546 // Create a fast track if the record thread has fast capture to get better performance.
547 // Only enable fast mode when there is no resample needed.
548 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
549 } else {
550 // Fast mode is not available in this case.
551 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
552 }
Eric Laurent83b88082014-06-20 18:31:16 -0700553
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700554 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
555 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700556 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200557 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700558 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
559 // "reuse one existing output mix" case
560 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200561 // For telephony patches, propagate voice communication use case to record side
562 if (streamType == AUDIO_STREAM_VOICE_CALL) {
563 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
564 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700565 }
jiabin01c8f562018-07-19 17:47:28 -0700566 if (mPlayback.thread()->hasFastMixer()) {
567 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700568 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700569 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700570 } else {
571 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700572 }
573
Andy Hung3ff4b552023-06-26 19:20:57 -0700574 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800575 const bool usePassthruPatchRecord =
576 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100577 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
578 const size_t recordFrameCount = mRecord.thread()->frameCount();
579 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800580 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100581 // PassthruPatchRecord producesBufferOnDemand, so use
582 // maximum of playback and record thread framecounts
583 frameCount = std::max(playbackFrameCount, recordFrameCount);
584 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
585 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung3ff4b552023-06-26 19:20:57 -0700586 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700587 mRecord.thread().get(),
588 sampleRate,
589 inChannelMask,
590 format,
591 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200592 inputFlags,
593 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700594 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100595 // use a pseudo LCM between input and output framecount
596 int playbackShift = __builtin_ctz(playbackFrameCount);
597 int shift = __builtin_ctz(recordFrameCount);
598 if (playbackShift < shift) {
599 shift = playbackShift;
600 }
601 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
602 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
603 __func__, playbackFrameCount, recordFrameCount, frameCount);
604
Andy Hung3ff4b552023-06-26 19:20:57 -0700605 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700606 mRecord.thread().get(),
607 sampleRate,
608 inChannelMask,
609 format,
610 frameCount,
611 nullptr,
612 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200613 inputFlags,
614 {} /* timeout */,
615 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700616 }
617 status = mRecord.checkTrack(tempRecordTrack.get());
618 if (status != NO_ERROR) {
619 return status;
620 }
621
Eric Laurent83b88082014-06-20 18:31:16 -0700622 // create a special playback track to render to playback thread.
623 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200624
625 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
626 // it might glitch.
627 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
628 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
629 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung3ff4b552023-06-26 19:20:57 -0700630 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700631 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700632 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700633 sampleRate,
634 outChannelMask,
635 format,
636 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700637 tempRecordTrack->buffer(),
638 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200639 outputFlags,
640 {} /*timeout*/,
641 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700642 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700643 if (status != NO_ERROR) {
644 return status;
645 }
Eric Laurent83b88082014-06-20 18:31:16 -0700646
647 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800648 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
649 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
650 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
651 // is alive. There is no need to hold a reference, and there is no need
652 // to clear it. In fact, since playback stopping is asynchronous, there is
653 // no proper time when clearing could be done.
654 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
655 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700656
657 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700658 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
659 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700660
661 return status;
662}
663
Andy Hungd63e79d2023-07-13 16:52:46 -0700664void AudioFlinger::PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700665{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700666 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
667 __func__, mRecord.handle(), mPlayback.handle());
668 mRecord.stopTrack();
669 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700670 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700671 mRecord.closeConnections(panel);
672 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700673}
674
Andy Hungc3ab7732018-06-01 13:46:29 -0700675status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
676{
677 if (!isSoftware()) return INVALID_OPERATION;
678
679 auto recordTrack = mRecord.const_track();
680 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
681
682 auto playbackTrack = mPlayback.const_track();
683 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
684
685 // Latency information for tracks may be called without obtaining
686 // the underlying thread lock.
687 //
688 // We use record server latency + playback track latency (generally smaller than the
689 // reverse due to internal biases).
690 //
691 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700692
Andy Hung30282562018-08-08 18:27:03 -0700693 // For PCM tracks get server latency.
694 if (audio_is_linear_pcm(recordTrack->format())) {
695 double recordServerLatencyMs, playbackTrackLatencyMs;
696 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
697 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
698 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
699 return OK;
700 }
701 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700702
Andy Hung30282562018-08-08 18:27:03 -0700703 // See if kernel latencies are available.
704 // If so, do a frame diff and time difference computation to estimate
705 // the total patch latency. This requires that frame counts are reported by the
706 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hung02a6c4e2023-06-23 19:27:19 -0700707 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700708 recordTrack->getKernelFrameTime(&recordFT);
709 playbackTrack->getKernelFrameTime(&playFT);
710 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
711 const int64_t frameDiff = recordFT.frames - playFT.frames;
712 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
713
714 // It is possible that the patch track and patch record have a large time disparity because
715 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
716 // time difference based on how often we expect the timestamps to update in normal operation
717 // (typical should be no more than 50 ms).
718 //
719 // If the timestamps aren't sampled close enough, the patch latency is not
720 // considered valid.
721 //
722 // TODO: change this based on more experiments.
723 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
724 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
725 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
726 - timeDiffNs * 1e-6;
727 return OK;
728 }
729 }
730
731 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700732}
733
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700734String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700735{
Andy Hungc3ab7732018-06-01 13:46:29 -0700736 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800737 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
738 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
739 mRecord.const_thread().get(), mPlayback.const_thread().get());
740
741 bool hasSinkDevice =
742 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
743 bool hasSourceDevice =
744 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
745 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
746 hasSinkDevice ? "num sinks" :
747 (hasSourceDevice ? "num sources" : "no devices"),
748 hasSinkDevice ? mAudioPatch.num_sinks :
749 (hasSourceDevice ? mAudioPatch.num_sources : 0),
750 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
751 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700752
753 // add latency if it exists
754 double latencyMs;
755 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700756 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700757 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700758 return result;
759}
760
Eric Laurent951f4552014-05-20 10:48:17 -0700761/* Disconnect a patch */
762status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung44f27182023-07-06 20:56:16 -0700763 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200764 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
765 //before processing the release patch request.
766 NO_THREAD_SAFETY_ANALYSIS
767 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700768 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700769 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700770
Mikhail Naganovdea53042018-04-26 13:10:21 -0700771 auto iter = mPatches.find(handle);
772 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700773 return BAD_VALUE;
774 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700775 Patch &removedPatch = iter->second;
776 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700777
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700778 const struct audio_port_config &src = patch.sources[0];
779 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700780 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700781 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
782 if (hwDevice == 0) {
783 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700784 status = BAD_VALUE;
785 break;
786 }
Eric Laurent83b88082014-06-20 18:31:16 -0700787
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700788 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700789 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700790 break;
791 }
792
Mikhail Naganovdea53042018-04-26 13:10:21 -0700793 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
794 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung44f27182023-07-06 20:56:16 -0700795 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700796 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700797 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800798 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700799 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800800 status = BAD_VALUE;
801 break;
802 }
Eric Laurent951f4552014-05-20 10:48:17 -0700803 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200804 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700805 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200806 mAudioFlinger.lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700807 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700808 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700809 }
810 } break;
811 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700812 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
813 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700814 status = BAD_VALUE;
815 break;
816 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700817 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung44f27182023-07-06 20:56:16 -0700818 sp<IAfThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700819 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700820 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800821 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700822 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800823 status = BAD_VALUE;
824 break;
825 }
Eric Laurent951f4552014-05-20 10:48:17 -0700826 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200827 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700828 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200829 mAudioFlinger.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700830 } break;
831 default:
832 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700833 }
834
Eric Laurentb82e6b72019-11-22 17:25:04 -0800835 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700836 return status;
837}
838
Eric Laurentb82e6b72019-11-22 17:25:04 -0800839void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
840 mPatches.erase(handle);
841 removeSoftwarePatchFromInsertedModules(handle);
Vlad Popa5161f8a2022-10-10 16:17:20 +0200842 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800843}
844
Eric Laurent951f4552014-05-20 10:48:17 -0700845/* List connected audio ports and they attributes */
846status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
847 struct audio_patch *patches __unused)
848{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700849 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700850 return NO_ERROR;
851}
852
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700853status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
854 audio_io_handle_t stream,
Andy Hungd63e79d2023-07-13 16:52:46 -0700855 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700856{
857 for (const auto& module : mInsertedModules) {
858 if (module.second.streams.count(stream)) {
859 for (const auto& patchHandle : module.second.sw_patches) {
860 const auto& patch_iter = mPatches.find(patchHandle);
861 if (patch_iter != mPatches.end()) {
862 const Patch &patch = patch_iter->second;
Andy Hungd63e79d2023-07-13 16:52:46 -0700863 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
864 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700865 patch.mPlayback.const_thread()->id(),
866 patch.mRecord.const_thread()->id());
867 } else {
868 ALOGE("Stale patch handle in the cache: %d", patchHandle);
869 }
870 }
871 return OK;
872 }
873 }
874 // The stream is not associated with any of inserted modules.
875 return BAD_VALUE;
876}
877
878void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100879 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700880{
881 if (audioHwDevice->isInsert()) {
882 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100883 if (patch != nullptr) {
884 std::vector <SoftwarePatch> swPatches;
885 getDownstreamSoftwarePatches(stream, &swPatches);
886 if (swPatches.size() > 0) {
887 auto iter = mPatches.find(swPatches[0].getPatchHandle());
888 if (iter != mPatches.end()) {
889 *patch = iter->second.mAudioPatch;
890 }
891 }
892 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700893 }
894}
895
896void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
897{
898 for (auto& module : mInsertedModules) {
899 module.second.streams.erase(stream);
900 }
901}
902
903AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700904{
905 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
906 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
907 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700908 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700909 return nullptr;
910 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700911 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700912}
913
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700914sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700915{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700916 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
917 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
918}
919
920void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100921 audio_module_handle_t module, audio_patch_handle_t handle,
922 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700923{
924 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100925 if (!mInsertedModules[module].streams.empty()) {
926 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
927 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700928}
929
930void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
931 audio_patch_handle_t handle)
932{
933 for (auto& module : mInsertedModules) {
934 module.second.sw_patches.erase(handle);
935 }
936}
937
938void AudioFlinger::PatchPanel::dump(int fd) const
939{
940 String8 patchPanelDump;
941 const char *indent = " ";
942
Mikhail Naganov201369b2018-05-16 16:52:32 -0700943 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700944 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800945 if (!headerPrinted) {
946 patchPanelDump += "\nPatches:\n";
947 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700948 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000949 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).c_str());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700950 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700951
952 headerPrinted = false;
953 for (const auto& module : mInsertedModules) {
954 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
955 if (!headerPrinted) {
956 patchPanelDump += "\nTracked inserted modules:\n";
957 headerPrinted = true;
958 }
959 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
960 for (const auto& stream : module.second.streams) {
961 moduleDump.appendFormat("%d ", stream);
962 }
963 moduleDump.append("; SW Patches: ");
964 for (const auto& patch : module.second.sw_patches) {
965 moduleDump.appendFormat("%d ", patch);
966 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000967 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.c_str());
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700968 }
969 }
970
Tomasz Wasilczykfd9ffd12023-08-14 17:56:22 +0000971 if (!patchPanelDump.empty()) {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000972 write(fd, patchPanelDump.c_str(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700973 }
974}
975
Glenn Kasten63238ef2015-03-02 15:50:29 -0800976} // namespace android