blob: ac1455c361c2bce0668a673142f5e110ffc9c719 [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 Hungb6692eb2023-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 Hungb6692eb2023-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 Hungb6692eb2023-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 Hungb6692eb2023-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 Hungb6692eb2023-07-13 16:52:46 -070094 return mPatchPanel->listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070095}
96
Andy Hungb6692eb2023-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 Hungb6692eb2023-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 Hungb6692eb2023-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 Hung87c693c2023-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 }
François Gaffie58e73af2023-02-15 11:47:24 +0100184 bool reuseExistingHalPatch = false;
185 audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE;
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;
François Gaffie58e73af2023-02-15 11:47:24 +0100203 oldhandle = *handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700204 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
205 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
206 oldPatch.sources[0].ext.device.hw_module !=
207 patch->sources[0].ext.device.hw_module)) {
208 hwModule = oldPatch.sources[0].ext.device.hw_module;
209 } else if (patch->num_sinks == 0 ||
210 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
211 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
212 oldPatch.sinks[0].ext.device.hw_module !=
213 patch->sinks[0].ext.device.hw_module))) {
214 // Note on (patch->num_sinks == 0): this situation should not happen as
215 // these special patches are only created by the policy manager but just
216 // in case, systematically clear the HAL patch.
217 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700218 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700219 hwModule = oldPatch.sinks[0].ext.device.hw_module;
220 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700221 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
222 if (hwDevice != 0) {
223 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700224 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800225 halHandle = removedPatch.mHalHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100226 // Prevent to remove/add device effect when mix / device did not change, and
227 // hal patch has not been released
228 // Note that no patch leak at hal layer as halHandle is reused.
229 reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700230 }
François Gaffie58e73af2023-02-15 11:47:24 +0100231 erasePatch(*handle, reuseExistingHalPatch);
Eric Laurent951f4552014-05-20 10:48:17 -0700232 }
233 }
234
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200235 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700236 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700237
Eric Laurent951f4552014-05-20 10:48:17 -0700238 switch (patch->sources[0].type) {
239 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700240 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700241 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
242 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700243 status = BAD_VALUE;
244 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700245 }
246 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700247 // support only one sink if connection to a mix or across HW modules
248 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700249 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
250 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700251 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700252 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700253 status = INVALID_OPERATION;
254 goto exit;
255 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700256 // reject connection to different sink types
257 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700258 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700259 status = BAD_VALUE;
260 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700261 }
Eric Laurent951f4552014-05-20 10:48:17 -0700262 }
263
Eric Laurent3bcf8592015-04-03 12:13:24 -0700264 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700265 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700266 // - Device to device AND
267 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700268 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700269 if ((patch->num_sources == 2) ||
270 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
271 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700272 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800273 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
274 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700275 if (patch->num_sources == 2) {
276 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700277 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
278 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700279 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700280 status = INVALID_OPERATION;
281 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700282 }
Andy Hung87c693c2023-07-06 20:56:16 -0700283 const sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700284 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700285 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700286 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700287 status = INVALID_OPERATION;
288 goto exit;
289 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700290 // existing playback thread is reused, so it is not closed when patch is cleared
291 newPatch.mPlayback.setThread(
Andy Hung87c693c2023-07-06 20:56:16 -0700292 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700293 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700294 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200295 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700296 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700297 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
298 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
299 config.sample_rate = patch->sinks[0].sample_rate;
300 }
301 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
302 config.channel_mask = patch->sinks[0].channel_mask;
303 }
304 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
305 config.format = patch->sinks[0].format;
306 }
307 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
308 flags = patch->sinks[0].flags.output;
309 }
Andy Hung87c693c2023-07-06 20:56:16 -0700310 const sp<IAfThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800311 patch->sinks[0].ext.device.hw_module,
312 &output,
313 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200314 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800315 outputDevice,
316 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700317 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700318 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
319 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700320 status = NO_MEMORY;
321 goto exit;
322 }
Andy Hung87c693c2023-07-06 20:56:16 -0700323 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700324 }
Eric Laurent83b88082014-06-20 18:31:16 -0700325 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700326 String8 address = String8(patch->sources[0].ext.device.address);
327 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700328 // open input stream with source device audio properties if provided or
329 // default to peer output stream properties otherwise.
330 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
331 config.sample_rate = patch->sources[0].sample_rate;
332 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700333 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700334 }
335 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
336 config.channel_mask = patch->sources[0].channel_mask;
337 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700338 config.channel_mask = audio_channel_in_mask_from_count(
339 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700340 }
341 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
342 config.format = patch->sources[0].format;
343 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700344 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700345 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700346 audio_input_flags_t flags =
347 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
348 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700349 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200350 audio_source_t source = AUDIO_SOURCE_MIC;
351 // For telephony patches, propagate voice communication use case to record side
352 if (patch->num_sources == 2
353 && patch->sources[1].ext.mix.usecase.stream
354 == AUDIO_STREAM_VOICE_CALL) {
355 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
356 }
Andy Hung87c693c2023-07-06 20:56:16 -0700357 const sp<IAfThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700358 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700359 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700360 device,
361 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200362 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800363 flags,
364 outputDevice,
365 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700366 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700367 thread.get(), config.channel_mask);
368 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700369 status = NO_MEMORY;
370 goto exit;
371 }
Andy Hung87c693c2023-07-06 20:56:16 -0700372 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700373 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700374 if (status != NO_ERROR) {
375 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700376 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700377 if (audioHwDevice->isInsert()) {
378 insertedModule = audioHwDevice->handle();
379 }
Eric Laurent951f4552014-05-20 10:48:17 -0700380 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700381 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung87c693c2023-07-06 20:56:16 -0700382 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700383 patch->sinks[0].ext.mix.handle);
384 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700385 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800386 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700387 ALOGW("%s() bad capture I/O handle %d",
388 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800389 status = BAD_VALUE;
390 goto exit;
391 }
Eric Laurent83b88082014-06-20 18:31:16 -0700392 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200393 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700394 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200395 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800396 if (status == NO_ERROR) {
397 newPatch.setThread(thread);
398 }
Eric Laurent526aa572019-01-15 10:54:58 -0800399 // remove stale audio patch with same input as sink if any
400 for (auto& iter : mPatches) {
401 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800402 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800403 break;
404 }
405 }
Eric Laurent83b88082014-06-20 18:31:16 -0700406 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700407 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
408 status = hwDevice->createAudioPatch(patch->num_sources,
409 patch->sources,
410 patch->num_sinks,
411 patch->sinks,
412 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700413 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700414 }
Eric Laurent951f4552014-05-20 10:48:17 -0700415 }
416 } break;
417 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700418 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700419 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700420 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700421 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700422 status = BAD_VALUE;
423 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700424 }
425 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700426 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700427 for (unsigned int i = 0; i < patch->num_sinks; i++) {
428 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700429 ALOGW("%s() invalid sink type %d for mix source",
430 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700431 status = BAD_VALUE;
432 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700433 }
434 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700435 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700436 status = BAD_VALUE;
437 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700438 }
jiabinc52b1ff2019-10-31 17:20:42 -0700439 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
440 patch->sinks[i].ext.device.type);
441 device->setAddress(patch->sinks[i].ext.device.address);
442 device->applyAudioPortConfig(&patch->sinks[i]);
443 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700444 }
Andy Hung87c693c2023-07-06 20:56:16 -0700445 sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700446 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700447 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700448 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800449 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700450 ALOGW("%s() bad playback I/O handle %d",
451 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800452 status = BAD_VALUE;
453 goto exit;
454 }
Eric Laurent951f4552014-05-20 10:48:17 -0700455 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700456 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700457 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700458 }
459
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200460 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700461 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200462 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800463 if (status == NO_ERROR) {
464 newPatch.setThread(thread);
465 }
Eric Laurent526aa572019-01-15 10:54:58 -0800466
467 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200468 // Prevent to remove endpoint patches (involved in a SwBridge)
469 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
470 if (!endpointPatch) {
471 for (auto& iter : mPatches) {
472 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
473 !iter.second.mIsEndpointPatch) {
474 erasePatch(iter.first);
475 break;
476 }
Eric Laurent526aa572019-01-15 10:54:58 -0800477 }
478 }
Eric Laurent951f4552014-05-20 10:48:17 -0700479 } break;
480 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700481 status = BAD_VALUE;
482 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700483 }
Eric Laurent83b88082014-06-20 18:31:16 -0700484exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700485 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700486 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700487 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
488 newPatch.mHalHandle = halHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100489 if (reuseExistingHalPatch) {
490 mAudioFlinger.mPatchCommandThread->updateAudioPatch(oldhandle, *handle, newPatch);
491 } else {
492 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
493 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700494 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100495 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700496 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100497 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700498 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700499 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700500 }
501 return status;
502}
503
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700504AudioFlinger::PatchPanel::Patch::~Patch()
505{
506 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
507 mRecord.handle(), mPlayback.handle());
508}
509
Andy Hungb6692eb2023-07-13 16:52:46 -0700510status_t AudioFlinger::PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700511{
512 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700513 status_t status = panel->createAudioPatch(
514 PatchBuilder().addSource(mAudioPatch.sources[0]).
515 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200516 mRecord.handlePtr(),
517 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700518 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700519 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700520 return status;
521 }
522
523 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700524 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700525 status = panel->createAudioPatch(
526 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200527 mPlayback.handlePtr(),
528 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700529 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700530 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700531 return status;
532 }
533 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700534 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700535 }
536
Eric Laurent83b88082014-06-20 18:31:16 -0700537 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700538 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700539 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700540 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
541 uint32_t sampleRate = mPlayback.thread()->sampleRate();
542 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700543
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700544 audio_format_t inputFormat = mRecord.thread()->format();
545 if (!audio_is_linear_pcm(inputFormat)) {
546 // The playbackThread format will say PCM for IEC61937 packetized stream.
547 // Use recordThread format.
548 format = inputFormat;
549 }
550 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
551 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700552 if (sampleRate == mRecord.thread()->sampleRate() &&
553 inChannelMask == mRecord.thread()->channelMask() &&
554 mRecord.thread()->fastTrackAvailable() &&
555 mRecord.thread()->hasFastCapture()) {
556 // Create a fast track if the record thread has fast capture to get better performance.
557 // Only enable fast mode when there is no resample needed.
558 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
559 } else {
560 // Fast mode is not available in this case.
561 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
562 }
Eric Laurent83b88082014-06-20 18:31:16 -0700563
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700564 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
565 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700566 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200567 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700568 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
569 // "reuse one existing output mix" case
570 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200571 // For telephony patches, propagate voice communication use case to record side
572 if (streamType == AUDIO_STREAM_VOICE_CALL) {
573 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
574 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700575 }
jiabin01c8f562018-07-19 17:47:28 -0700576 if (mPlayback.thread()->hasFastMixer()) {
577 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700578 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700579 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700580 } else {
581 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700582 }
583
Andy Hung8d31fd22023-06-26 19:20:57 -0700584 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800585 const bool usePassthruPatchRecord =
586 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100587 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
588 const size_t recordFrameCount = mRecord.thread()->frameCount();
589 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800590 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100591 // PassthruPatchRecord producesBufferOnDemand, so use
592 // maximum of playback and record thread framecounts
593 frameCount = std::max(playbackFrameCount, recordFrameCount);
594 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
595 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700596 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700597 mRecord.thread().get(),
598 sampleRate,
599 inChannelMask,
600 format,
601 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200602 inputFlags,
603 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700604 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100605 // use a pseudo LCM between input and output framecount
606 int playbackShift = __builtin_ctz(playbackFrameCount);
607 int shift = __builtin_ctz(recordFrameCount);
608 if (playbackShift < shift) {
609 shift = playbackShift;
610 }
611 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
612 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
613 __func__, playbackFrameCount, recordFrameCount, frameCount);
614
Andy Hung8d31fd22023-06-26 19:20:57 -0700615 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700616 mRecord.thread().get(),
617 sampleRate,
618 inChannelMask,
619 format,
620 frameCount,
621 nullptr,
622 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200623 inputFlags,
624 {} /* timeout */,
625 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700626 }
627 status = mRecord.checkTrack(tempRecordTrack.get());
628 if (status != NO_ERROR) {
629 return status;
630 }
631
Eric Laurent83b88082014-06-20 18:31:16 -0700632 // create a special playback track to render to playback thread.
633 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200634
635 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
636 // it might glitch.
637 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
638 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
639 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700640 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700641 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700642 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700643 sampleRate,
644 outChannelMask,
645 format,
646 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700647 tempRecordTrack->buffer(),
648 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200649 outputFlags,
650 {} /*timeout*/,
651 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700652 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700653 if (status != NO_ERROR) {
654 return status;
655 }
Eric Laurent83b88082014-06-20 18:31:16 -0700656
657 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800658 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
659 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
660 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
661 // is alive. There is no need to hold a reference, and there is no need
662 // to clear it. In fact, since playback stopping is asynchronous, there is
663 // no proper time when clearing could be done.
664 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
665 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700666
667 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700668 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
669 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700670
671 return status;
672}
673
Andy Hungb6692eb2023-07-13 16:52:46 -0700674void AudioFlinger::PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700675{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700676 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
677 __func__, mRecord.handle(), mPlayback.handle());
678 mRecord.stopTrack();
679 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700680 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700681 mRecord.closeConnections(panel);
682 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700683}
684
Andy Hungc3ab7732018-06-01 13:46:29 -0700685status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
686{
687 if (!isSoftware()) return INVALID_OPERATION;
688
689 auto recordTrack = mRecord.const_track();
690 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
691
692 auto playbackTrack = mPlayback.const_track();
693 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
694
695 // Latency information for tracks may be called without obtaining
696 // the underlying thread lock.
697 //
698 // We use record server latency + playback track latency (generally smaller than the
699 // reverse due to internal biases).
700 //
701 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700702
Andy Hung30282562018-08-08 18:27:03 -0700703 // For PCM tracks get server latency.
704 if (audio_is_linear_pcm(recordTrack->format())) {
705 double recordServerLatencyMs, playbackTrackLatencyMs;
706 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
707 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
708 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
709 return OK;
710 }
711 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700712
Andy Hung30282562018-08-08 18:27:03 -0700713 // See if kernel latencies are available.
714 // If so, do a frame diff and time difference computation to estimate
715 // the total patch latency. This requires that frame counts are reported by the
716 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700717 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700718 recordTrack->getKernelFrameTime(&recordFT);
719 playbackTrack->getKernelFrameTime(&playFT);
720 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
721 const int64_t frameDiff = recordFT.frames - playFT.frames;
722 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
723
724 // It is possible that the patch track and patch record have a large time disparity because
725 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
726 // time difference based on how often we expect the timestamps to update in normal operation
727 // (typical should be no more than 50 ms).
728 //
729 // If the timestamps aren't sampled close enough, the patch latency is not
730 // considered valid.
731 //
732 // TODO: change this based on more experiments.
733 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
734 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
735 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
736 - timeDiffNs * 1e-6;
737 return OK;
738 }
739 }
740
741 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700742}
743
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700744String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700745{
Andy Hungc3ab7732018-06-01 13:46:29 -0700746 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800747 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
748 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
749 mRecord.const_thread().get(), mPlayback.const_thread().get());
750
751 bool hasSinkDevice =
752 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
753 bool hasSourceDevice =
754 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
755 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
756 hasSinkDevice ? "num sinks" :
757 (hasSourceDevice ? "num sources" : "no devices"),
758 hasSinkDevice ? mAudioPatch.num_sinks :
759 (hasSourceDevice ? mAudioPatch.num_sources : 0),
760 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
761 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700762
763 // add latency if it exists
764 double latencyMs;
765 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700766 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700767 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700768 return result;
769}
770
Eric Laurent951f4552014-05-20 10:48:17 -0700771/* Disconnect a patch */
772status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700773 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200774 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
775 //before processing the release patch request.
776 NO_THREAD_SAFETY_ANALYSIS
777 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700778 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700779 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700780
Mikhail Naganovdea53042018-04-26 13:10:21 -0700781 auto iter = mPatches.find(handle);
782 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700783 return BAD_VALUE;
784 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700785 Patch &removedPatch = iter->second;
786 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700787
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700788 const struct audio_port_config &src = patch.sources[0];
789 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700790 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700791 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
792 if (hwDevice == 0) {
793 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700794 status = BAD_VALUE;
795 break;
796 }
Eric Laurent83b88082014-06-20 18:31:16 -0700797
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700798 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700799 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700800 break;
801 }
802
Mikhail Naganovdea53042018-04-26 13:10:21 -0700803 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
804 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung87c693c2023-07-06 20:56:16 -0700805 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700806 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700807 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800808 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700809 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800810 status = BAD_VALUE;
811 break;
812 }
Eric Laurent951f4552014-05-20 10:48:17 -0700813 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200814 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700815 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200816 mAudioFlinger.lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700817 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700818 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700819 }
820 } break;
821 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700822 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
823 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700824 status = BAD_VALUE;
825 break;
826 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700827 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung87c693c2023-07-06 20:56:16 -0700828 sp<IAfThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700829 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700830 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800831 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700832 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800833 status = BAD_VALUE;
834 break;
835 }
Eric Laurent951f4552014-05-20 10:48:17 -0700836 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200837 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700838 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200839 mAudioFlinger.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700840 } break;
841 default:
842 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700843 }
844
Eric Laurentb82e6b72019-11-22 17:25:04 -0800845 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700846 return status;
847}
848
François Gaffie58e73af2023-02-15 11:47:24 +0100849void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800850 mPatches.erase(handle);
851 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100852 if (!reuseExistingHalPatch) {
853 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
854 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800855}
856
Eric Laurent951f4552014-05-20 10:48:17 -0700857/* List connected audio ports and they attributes */
858status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
859 struct audio_patch *patches __unused)
860{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700861 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700862 return NO_ERROR;
863}
864
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700865status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
866 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700867 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700868{
869 for (const auto& module : mInsertedModules) {
870 if (module.second.streams.count(stream)) {
871 for (const auto& patchHandle : module.second.sw_patches) {
872 const auto& patch_iter = mPatches.find(patchHandle);
873 if (patch_iter != mPatches.end()) {
874 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700875 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
876 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700877 patch.mPlayback.const_thread()->id(),
878 patch.mRecord.const_thread()->id());
879 } else {
880 ALOGE("Stale patch handle in the cache: %d", patchHandle);
881 }
882 }
883 return OK;
884 }
885 }
886 // The stream is not associated with any of inserted modules.
887 return BAD_VALUE;
888}
889
890void AudioFlinger::PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100891 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700892{
893 if (audioHwDevice->isInsert()) {
894 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100895 if (patch != nullptr) {
896 std::vector <SoftwarePatch> swPatches;
897 getDownstreamSoftwarePatches(stream, &swPatches);
898 if (swPatches.size() > 0) {
899 auto iter = mPatches.find(swPatches[0].getPatchHandle());
900 if (iter != mPatches.end()) {
901 *patch = iter->second.mAudioPatch;
902 }
903 }
904 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700905 }
906}
907
908void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
909{
910 for (auto& module : mInsertedModules) {
911 module.second.streams.erase(stream);
912 }
913}
914
915AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700916{
917 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
918 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
919 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700920 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700921 return nullptr;
922 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700923 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700924}
925
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700926sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700927{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700928 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
929 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
930}
931
932void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100933 audio_module_handle_t module, audio_patch_handle_t handle,
934 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700935{
936 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100937 if (!mInsertedModules[module].streams.empty()) {
938 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
939 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700940}
941
942void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
943 audio_patch_handle_t handle)
944{
945 for (auto& module : mInsertedModules) {
946 module.second.sw_patches.erase(handle);
947 }
948}
949
950void AudioFlinger::PatchPanel::dump(int fd) const
951{
952 String8 patchPanelDump;
953 const char *indent = " ";
954
Mikhail Naganov201369b2018-05-16 16:52:32 -0700955 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700956 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800957 if (!headerPrinted) {
958 patchPanelDump += "\nPatches:\n";
959 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700960 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800961 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700962 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700963
964 headerPrinted = false;
965 for (const auto& module : mInsertedModules) {
966 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
967 if (!headerPrinted) {
968 patchPanelDump += "\nTracked inserted modules:\n";
969 headerPrinted = true;
970 }
971 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
972 for (const auto& stream : module.second.streams) {
973 moduleDump.appendFormat("%d ", stream);
974 }
975 moduleDump.append("; SW Patches: ");
976 for (const auto& patch : module.second.sw_patches) {
977 moduleDump.appendFormat("%d ", patch);
978 }
979 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
980 }
981 }
982
983 if (!patchPanelDump.isEmpty()) {
984 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700985 }
986}
987
Glenn Kasten63238ef2015-03-02 15:50:29 -0800988} // namespace android