blob: 12ac63c14b8ab0e61f720d9741cc2e1b0416a951 [file] [log] [blame]
Eric Laurent951f4552014-05-20 10:48:17 -07001/*
2**
3** Copyright 2014, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger::PatchPanel"
20//#define LOG_NDEBUG 0
21
22#include "Configuration.h"
23#include <utils/Log.h>
24#include <audio_utils/primitives.h>
25
26#include "AudioFlinger.h"
Andy Hung8e6b62a2023-07-13 18:11:33 -070027#include "PatchPanel.h"
Eric Laurent951f4552014-05-20 10:48:17 -070028#include <media/AudioParameter.h>
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080029#include <media/AudioValidator.h>
jiabinc52b1ff2019-10-31 17:20:42 -070030#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070031#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070032#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070033
34// ----------------------------------------------------------------------------
35
36// Note: the following macro is used for extremely verbose logging message. In
37// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
38// 0; but one side effect of this is to turn all LOGV's as well. Some messages
39// are so verbose that we want to suppress them even when we have ALOG_ASSERT
40// turned on. Do not uncomment the #def below unless you really know what you
41// are doing and want to see all of the extremely verbose messages.
42//#define VERY_VERY_VERBOSE_LOGGING
43#ifdef VERY_VERY_VERBOSE_LOGGING
44#define ALOGVV ALOGV
45#else
46#define ALOGVV(a...) do { } while(0)
47#endif
48
49namespace android {
50
51/* List connected audio ports and their attributes */
52status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
Andy Hungcdd80ef2023-07-17 11:37:26 -070053 struct audio_port* ports) const
Eric Laurent951f4552014-05-20 10:48:17 -070054{
55 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070056 return mPatchPanel->listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070057}
58
59/* Get supported attributes for a given audio port */
Andy Hungcdd80ef2023-07-17 11:37:26 -070060status_t AudioFlinger::getAudioPort(struct audio_port_v7* port) const {
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080061 status_t status = AudioValidator::validateAudioPort(*port);
62 if (status != NO_ERROR) {
63 return status;
64 }
65
Eric Laurent951f4552014-05-20 10:48:17 -070066 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070067 return mPatchPanel->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070068}
69
Eric Laurent951f4552014-05-20 10:48:17 -070070/* Connect a patch between several source and sink ports */
71status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
72 audio_patch_handle_t *handle)
73{
Ytai Ben-Tsvi53858472020-11-30 11:04:46 -080074 status_t status = AudioValidator::validateAudioPatch(*patch);
75 if (status != NO_ERROR) {
76 return status;
77 }
78
Eric Laurent951f4552014-05-20 10:48:17 -070079 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070080 return mPatchPanel->createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070081}
82
83/* Disconnect a patch */
84status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
85{
86 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070087 return mPatchPanel->releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070088}
89
Eric Laurent951f4552014-05-20 10:48:17 -070090/* List connected audio ports and they attributes */
Andy Hungcdd80ef2023-07-17 11:37:26 -070091status_t AudioFlinger::listAudioPatches(
92 unsigned int* num_patches, struct audio_patch* patches) const
Eric Laurent951f4552014-05-20 10:48:17 -070093{
94 Mutex::Autolock _l(mLock);
Andy Hungb6692eb2023-07-13 16:52:46 -070095 return mPatchPanel->listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070096}
97
Andy Hungb6692eb2023-07-13 16:52:46 -070098/* static */
99sp<IAfPatchPanel> IAfPatchPanel::create(AudioFlinger* audioFlinger) {
Andy Hung8e6b62a2023-07-13 18:11:33 -0700100 return sp<PatchPanel>::make(audioFlinger);
Andy Hungb6692eb2023-07-13 16:52:46 -0700101}
102
103status_t SoftwarePatch::getLatencyMs_l(double* latencyMs) const {
104 return mPatchPanel->getLatencyMs_l(mPatchHandle, latencyMs);
105}
106
Andy Hung8e6b62a2023-07-13 18:11:33 -0700107status_t PatchPanel::getLatencyMs_l(
Andy Hungb6692eb2023-07-13 16:52:46 -0700108 audio_patch_handle_t patchHandle, double* latencyMs) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700109{
Andy Hungb6692eb2023-07-13 16:52:46 -0700110 const auto& iter = mPatches.find(patchHandle);
111 if (iter != mPatches.end()) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700112 return iter->second.getLatencyMs(latencyMs);
113 } else {
114 return BAD_VALUE;
115 }
116}
117
Andy Hung8e6b62a2023-07-13 18:11:33 -0700118void PatchPanel::closeThreadInternal_l(const sp<IAfThreadBase>& thread) const
Andy Hungb6692eb2023-07-13 16:52:46 -0700119{
120 if (const auto recordThread = thread->asIAfRecordThread();
121 recordThread) {
122 mAudioFlinger.closeThreadInternal_l(recordThread);
123 } else if (const auto playbackThread = thread->asIAfPlaybackThread();
124 playbackThread) {
125 mAudioFlinger.closeThreadInternal_l(playbackThread);
126 } else {
127 LOG_ALWAYS_FATAL("%s: Endpoints only accept IAfPlayback and IAfRecord threads, "
128 "invalid thread, id: %d type: %d",
129 __func__, thread->id(), thread->type());
130 }
131}
132
Eric Laurent951f4552014-05-20 10:48:17 -0700133/* List connected audio ports and their attributes */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700134status_t PatchPanel::listAudioPorts(unsigned int* /* num_ports */,
Eric Laurent951f4552014-05-20 10:48:17 -0700135 struct audio_port *ports __unused)
136{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700137 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700138 return NO_ERROR;
139}
140
141/* Get supported attributes for a given audio port */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700142status_t PatchPanel::getAudioPort(struct audio_port_v7* port)
Eric Laurent951f4552014-05-20 10:48:17 -0700143{
jiabinb4fed192020-09-22 14:45:40 -0700144 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
145 // Only query the HAL when the port is a device.
146 // TODO: implement getAudioPort for mix.
147 return INVALID_OPERATION;
148 }
149 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
150 if (hwDevice == nullptr) {
151 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
152 return BAD_VALUE;
153 }
154 if (!hwDevice->supportsAudioPatches()) {
155 return INVALID_OPERATION;
156 }
157 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700158}
159
Eric Laurent951f4552014-05-20 10:48:17 -0700160/* Connect a patch between several source and sink ports */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700161status_t PatchPanel::createAudioPatch(const struct audio_patch* patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200162 audio_patch_handle_t *handle,
163 bool endpointPatch)
Andy Hung87c693c2023-07-06 20:56:16 -0700164 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendCreateAudioPatchConfigEvent
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200165 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
166 //before processing the create patch request.
167 NO_THREAD_SAFETY_ANALYSIS
Eric Laurent951f4552014-05-20 10:48:17 -0700168{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700169 if (handle == NULL || patch == NULL) {
170 return BAD_VALUE;
171 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 ALOGV("%s() num_sources %d num_sinks %d handle %d",
173 __func__, patch->num_sources, patch->num_sinks, *handle);
174 status_t status = NO_ERROR;
175 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700176
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700177 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700178 return BAD_VALUE;
179 }
Eric Laurent874c42872014-08-08 15:13:39 -0700180 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
181 // only the audio policy manager can request a patch creation with 2 sources.
182 if (patch->num_sources > 2) {
183 return INVALID_OPERATION;
184 }
François Gaffie58e73af2023-02-15 11:47:24 +0100185 bool reuseExistingHalPatch = false;
186 audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700187 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700188 auto iter = mPatches.find(*handle);
189 if (iter != mPatches.end()) {
190 ALOGV("%s() removing patch handle %d", __func__, *handle);
191 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700192 // free resources owned by the removed patch if applicable
193 // 1) if a software patch is present, release the playback and capture threads and
194 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700195 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700196 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700197 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700198 // 2) if the new patch and old patch source or sink are devices from different
199 // hw modules, clear the audio HAL patches now because they will not be updated
200 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700201 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700202 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
203 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
François Gaffie58e73af2023-02-15 11:47:24 +0100204 oldhandle = *handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700205 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
206 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
207 oldPatch.sources[0].ext.device.hw_module !=
208 patch->sources[0].ext.device.hw_module)) {
209 hwModule = oldPatch.sources[0].ext.device.hw_module;
210 } else if (patch->num_sinks == 0 ||
211 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
212 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
213 oldPatch.sinks[0].ext.device.hw_module !=
214 patch->sinks[0].ext.device.hw_module))) {
215 // Note on (patch->num_sinks == 0): this situation should not happen as
216 // these special patches are only created by the policy manager but just
217 // in case, systematically clear the HAL patch.
218 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700219 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700220 hwModule = oldPatch.sinks[0].ext.device.hw_module;
221 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700222 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
223 if (hwDevice != 0) {
224 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700225 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800226 halHandle = removedPatch.mHalHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100227 // Prevent to remove/add device effect when mix / device did not change, and
228 // hal patch has not been released
229 // Note that no patch leak at hal layer as halHandle is reused.
230 reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700231 }
François Gaffie58e73af2023-02-15 11:47:24 +0100232 erasePatch(*handle, reuseExistingHalPatch);
Eric Laurent951f4552014-05-20 10:48:17 -0700233 }
234 }
235
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200236 Patch newPatch{*patch, endpointPatch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700237 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700238
Eric Laurent951f4552014-05-20 10:48:17 -0700239 switch (patch->sources[0].type) {
240 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700241 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700242 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
243 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700244 status = BAD_VALUE;
245 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700246 }
247 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700248 // support only one sink if connection to a mix or across HW modules
249 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700250 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
251 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700252 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700253 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700254 status = INVALID_OPERATION;
255 goto exit;
256 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700257 // reject connection to different sink types
258 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700259 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700260 status = BAD_VALUE;
261 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700262 }
Eric Laurent951f4552014-05-20 10:48:17 -0700263 }
264
Eric Laurent3bcf8592015-04-03 12:13:24 -0700265 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700266 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700267 // - Device to device AND
268 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700269 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700270 if ((patch->num_sources == 2) ||
271 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
272 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700273 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800274 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
275 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700276 if (patch->num_sources == 2) {
277 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700278 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
279 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700280 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700281 status = INVALID_OPERATION;
282 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700283 }
Andy Hung87c693c2023-07-06 20:56:16 -0700284 const sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700285 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700286 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700287 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700288 status = INVALID_OPERATION;
289 goto exit;
290 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700291 // existing playback thread is reused, so it is not closed when patch is cleared
292 newPatch.mPlayback.setThread(
Andy Hung87c693c2023-07-06 20:56:16 -0700293 thread->asIAfPlaybackThread().get(), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700294 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700295 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200296 audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700297 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700298 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
299 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
300 config.sample_rate = patch->sinks[0].sample_rate;
301 }
302 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
303 config.channel_mask = patch->sinks[0].channel_mask;
304 }
305 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
306 config.format = patch->sinks[0].format;
307 }
308 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
309 flags = patch->sinks[0].flags.output;
310 }
Andy Hung87c693c2023-07-06 20:56:16 -0700311 const sp<IAfThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800312 patch->sinks[0].ext.device.hw_module,
313 &output,
314 &config,
Eric Laurentf1f22e72021-07-13 14:04:14 +0200315 &mixerConfig,
juyuchen2224c5a2019-01-21 12:00:58 +0800316 outputDevice,
317 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700318 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700319 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
320 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700321 status = NO_MEMORY;
322 goto exit;
323 }
Andy Hung87c693c2023-07-06 20:56:16 -0700324 newPatch.mPlayback.setThread(thread->asIAfPlaybackThread().get());
Eric Laurent83b88082014-06-20 18:31:16 -0700325 }
Eric Laurent83b88082014-06-20 18:31:16 -0700326 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700327 String8 address = String8(patch->sources[0].ext.device.address);
328 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700329 // open input stream with source device audio properties if provided or
330 // default to peer output stream properties otherwise.
331 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
332 config.sample_rate = patch->sources[0].sample_rate;
333 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700334 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700335 }
336 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
337 config.channel_mask = patch->sources[0].channel_mask;
338 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700339 config.channel_mask = audio_channel_in_mask_from_count(
340 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700341 }
342 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
343 config.format = patch->sources[0].format;
344 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700345 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700346 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700347 audio_input_flags_t flags =
348 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
349 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700350 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Eric Laurent78b07302022-10-07 16:20:34 +0200351 audio_source_t source = AUDIO_SOURCE_MIC;
352 // For telephony patches, propagate voice communication use case to record side
353 if (patch->num_sources == 2
354 && patch->sources[1].ext.mix.usecase.stream
355 == AUDIO_STREAM_VOICE_CALL) {
356 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
357 }
Andy Hung87c693c2023-07-06 20:56:16 -0700358 const sp<IAfThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700359 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700360 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700361 device,
362 address,
Eric Laurent78b07302022-10-07 16:20:34 +0200363 source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800364 flags,
365 outputDevice,
366 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700367 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700368 thread.get(), config.channel_mask);
369 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700370 status = NO_MEMORY;
371 goto exit;
372 }
Andy Hung87c693c2023-07-06 20:56:16 -0700373 newPatch.mRecord.setThread(thread->asIAfRecordThread().get());
Mikhail Naganovdea53042018-04-26 13:10:21 -0700374 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700375 if (status != NO_ERROR) {
376 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700377 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700378 if (audioHwDevice->isInsert()) {
379 insertedModule = audioHwDevice->handle();
380 }
Eric Laurent951f4552014-05-20 10:48:17 -0700381 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700382 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Andy Hung87c693c2023-07-06 20:56:16 -0700383 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700384 patch->sinks[0].ext.mix.handle);
385 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700386 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800387 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700388 ALOGW("%s() bad capture I/O handle %d",
389 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800390 status = BAD_VALUE;
391 goto exit;
392 }
Eric Laurent83b88082014-06-20 18:31:16 -0700393 }
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200394 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700395 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200396 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800397 if (status == NO_ERROR) {
398 newPatch.setThread(thread);
399 }
Eric Laurent526aa572019-01-15 10:54:58 -0800400 // remove stale audio patch with same input as sink if any
401 for (auto& iter : mPatches) {
402 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800403 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800404 break;
405 }
406 }
Eric Laurent83b88082014-06-20 18:31:16 -0700407 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700408 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
409 status = hwDevice->createAudioPatch(patch->num_sources,
410 patch->sources,
411 patch->num_sinks,
412 patch->sinks,
413 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700414 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700415 }
Eric Laurent951f4552014-05-20 10:48:17 -0700416 }
417 } break;
418 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700419 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700420 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700421 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700422 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700423 status = BAD_VALUE;
424 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700425 }
426 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700427 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700428 for (unsigned int i = 0; i < patch->num_sinks; i++) {
429 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700430 ALOGW("%s() invalid sink type %d for mix source",
431 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700432 status = BAD_VALUE;
433 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700434 }
435 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700436 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700437 status = BAD_VALUE;
438 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700439 }
jiabinc52b1ff2019-10-31 17:20:42 -0700440 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
441 patch->sinks[i].ext.device.type);
442 device->setAddress(patch->sinks[i].ext.device.address);
443 device->applyAudioPortConfig(&patch->sinks[i]);
444 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700445 }
Andy Hung87c693c2023-07-06 20:56:16 -0700446 sp<IAfThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700447 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700448 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700449 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800450 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700451 ALOGW("%s() bad playback I/O handle %d",
452 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800453 status = BAD_VALUE;
454 goto exit;
455 }
Eric Laurent951f4552014-05-20 10:48:17 -0700456 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700457 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700458 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700459 }
460
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200461 mAudioFlinger.unlock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700462 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurent1e28aaa2023-04-16 19:34:23 +0200463 mAudioFlinger.lock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800464 if (status == NO_ERROR) {
465 newPatch.setThread(thread);
466 }
Eric Laurent526aa572019-01-15 10:54:58 -0800467
468 // remove stale audio patch with same output as source if any
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200469 // Prevent to remove endpoint patches (involved in a SwBridge)
470 // Prevent to remove AudioPatch used to route an output involved in an endpoint.
471 if (!endpointPatch) {
472 for (auto& iter : mPatches) {
473 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() &&
474 !iter.second.mIsEndpointPatch) {
475 erasePatch(iter.first);
476 break;
477 }
Eric Laurent526aa572019-01-15 10:54:58 -0800478 }
479 }
Eric Laurent951f4552014-05-20 10:48:17 -0700480 } break;
481 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700482 status = BAD_VALUE;
483 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700484 }
Eric Laurent83b88082014-06-20 18:31:16 -0700485exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700486 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700487 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700488 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
489 newPatch.mHalHandle = halHandle;
François Gaffie58e73af2023-02-15 11:47:24 +0100490 if (reuseExistingHalPatch) {
491 mAudioFlinger.mPatchCommandThread->updateAudioPatch(oldhandle, *handle, newPatch);
492 } else {
493 mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch);
494 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700495 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
Eric Laurent74c38dc2020-12-23 18:19:44 +0100496 addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700497 }
Eric Laurentef03eef2021-01-05 16:30:04 +0100498 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Eric Laurent83b88082014-06-20 18:31:16 -0700499 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700500 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700501 }
502 return status;
503}
504
Andy Hung8e6b62a2023-07-13 18:11:33 -0700505PatchPanel::Patch::~Patch()
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700506{
507 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
508 mRecord.handle(), mPlayback.handle());
509}
510
Andy Hung8e6b62a2023-07-13 18:11:33 -0700511status_t PatchPanel::Patch::createConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700512{
513 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700514 status_t status = panel->createAudioPatch(
515 PatchBuilder().addSource(mAudioPatch.sources[0]).
516 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200517 mRecord.handlePtr(),
518 true /*endpointPatch*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700519 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700520 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700521 return status;
522 }
523
524 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700525 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700526 status = panel->createAudioPatch(
527 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
Francois Gaffiee0dc36d2021-04-01 16:01:00 +0200528 mPlayback.handlePtr(),
529 true /*endpointPatch*/);
Eric Laurentd60560a2015-04-10 11:31:20 -0700530 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700531 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700532 return status;
533 }
534 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700535 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700536 }
537
Eric Laurent83b88082014-06-20 18:31:16 -0700538 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700539 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700540 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700541 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
542 uint32_t sampleRate = mPlayback.thread()->sampleRate();
543 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700544
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700545 audio_format_t inputFormat = mRecord.thread()->format();
546 if (!audio_is_linear_pcm(inputFormat)) {
547 // The playbackThread format will say PCM for IEC61937 packetized stream.
548 // Use recordThread format.
549 format = inputFormat;
550 }
551 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
552 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700553 if (sampleRate == mRecord.thread()->sampleRate() &&
554 inChannelMask == mRecord.thread()->channelMask() &&
555 mRecord.thread()->fastTrackAvailable() &&
556 mRecord.thread()->hasFastCapture()) {
557 // Create a fast track if the record thread has fast capture to get better performance.
558 // Only enable fast mode when there is no resample needed.
559 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
560 } else {
561 // Fast mode is not available in this case.
562 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
563 }
Eric Laurent83b88082014-06-20 18:31:16 -0700564
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700565 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
566 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700567 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
Eric Laurent78b07302022-10-07 16:20:34 +0200568 audio_source_t source = AUDIO_SOURCE_DEFAULT;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700569 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
570 // "reuse one existing output mix" case
571 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
Eric Laurent78b07302022-10-07 16:20:34 +0200572 // For telephony patches, propagate voice communication use case to record side
573 if (streamType == AUDIO_STREAM_VOICE_CALL) {
574 source = AUDIO_SOURCE_VOICE_COMMUNICATION;
575 }
Mikhail Naganov776eb212018-07-19 15:14:11 -0700576 }
jiabin01c8f562018-07-19 17:47:28 -0700577 if (mPlayback.thread()->hasFastMixer()) {
578 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700579 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700580 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700581 } else {
582 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700583 }
584
Andy Hung8d31fd22023-06-26 19:20:57 -0700585 sp<IAfPatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800586 const bool usePassthruPatchRecord =
587 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100588 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
589 const size_t recordFrameCount = mRecord.thread()->frameCount();
590 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800591 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100592 // PassthruPatchRecord producesBufferOnDemand, so use
593 // maximum of playback and record thread framecounts
594 frameCount = std::max(playbackFrameCount, recordFrameCount);
595 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
596 __func__, playbackFrameCount, recordFrameCount, frameCount);
Andy Hung8d31fd22023-06-26 19:20:57 -0700597 tempRecordTrack = IAfPatchRecord::createPassThru(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700598 mRecord.thread().get(),
599 sampleRate,
600 inChannelMask,
601 format,
602 frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +0200603 inputFlags,
604 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700605 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100606 // use a pseudo LCM between input and output framecount
607 int playbackShift = __builtin_ctz(playbackFrameCount);
608 int shift = __builtin_ctz(recordFrameCount);
609 if (playbackShift < shift) {
610 shift = playbackShift;
611 }
612 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
613 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
614 __func__, playbackFrameCount, recordFrameCount, frameCount);
615
Andy Hung8d31fd22023-06-26 19:20:57 -0700616 tempRecordTrack = IAfPatchRecord::create(
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700617 mRecord.thread().get(),
618 sampleRate,
619 inChannelMask,
620 format,
621 frameCount,
622 nullptr,
623 (size_t)0 /* bufferSize */,
Eric Laurent78b07302022-10-07 16:20:34 +0200624 inputFlags,
625 {} /* timeout */,
626 source);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700627 }
628 status = mRecord.checkTrack(tempRecordTrack.get());
629 if (status != NO_ERROR) {
630 return status;
631 }
632
Eric Laurent83b88082014-06-20 18:31:16 -0700633 // create a special playback track to render to playback thread.
634 // this track is given the same buffer as the PatchRecord buffer
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200635
636 // Default behaviour is to start as soon as possible to have the lowest possible latency even if
637 // it might glitch.
638 // Disable this behavior for FM Tuner source if no fast capture/mixer available.
639 const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER;
640 const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1;
Andy Hung8d31fd22023-06-26 19:20:57 -0700641 sp<IAfPatchTrack> tempPatchTrack = IAfPatchTrack::create(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700642 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700643 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700644 sampleRate,
645 outChannelMask,
646 format,
647 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700648 tempRecordTrack->buffer(),
649 tempRecordTrack->bufferSize(),
Francois Gaffiea0fd4c72021-05-05 10:49:17 +0200650 outputFlags,
651 {} /*timeout*/,
652 frameCountToBeReady);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700653 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700654 if (status != NO_ERROR) {
655 return status;
656 }
Eric Laurent83b88082014-06-20 18:31:16 -0700657
658 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800659 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
660 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
661 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
662 // is alive. There is no need to hold a reference, and there is no need
663 // to clear it. In fact, since playback stopping is asynchronous, there is
664 // no proper time when clearing could be done.
665 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
666 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700667
668 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700669 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
670 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700671
672 return status;
673}
674
Andy Hung8e6b62a2023-07-13 18:11:33 -0700675void PatchPanel::Patch::clearConnections(const sp<IAfPatchPanel>& panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700676{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700677 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
678 __func__, mRecord.handle(), mPlayback.handle());
679 mRecord.stopTrack();
680 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700681 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700682 mRecord.closeConnections(panel);
683 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700684}
685
Andy Hung8e6b62a2023-07-13 18:11:33 -0700686status_t PatchPanel::Patch::getLatencyMs(double* latencyMs) const
Andy Hungc3ab7732018-06-01 13:46:29 -0700687{
688 if (!isSoftware()) return INVALID_OPERATION;
689
690 auto recordTrack = mRecord.const_track();
691 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
692
693 auto playbackTrack = mPlayback.const_track();
694 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
695
696 // Latency information for tracks may be called without obtaining
697 // the underlying thread lock.
698 //
699 // We use record server latency + playback track latency (generally smaller than the
700 // reverse due to internal biases).
701 //
702 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700703
Andy Hung30282562018-08-08 18:27:03 -0700704 // For PCM tracks get server latency.
705 if (audio_is_linear_pcm(recordTrack->format())) {
706 double recordServerLatencyMs, playbackTrackLatencyMs;
707 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
708 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
709 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
710 return OK;
711 }
712 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700713
Andy Hung30282562018-08-08 18:27:03 -0700714 // See if kernel latencies are available.
715 // If so, do a frame diff and time difference computation to estimate
716 // the total patch latency. This requires that frame counts are reported by the
717 // HAL are matched properly in the case of record overruns and playback underruns.
Andy Hungd29af632023-06-23 19:27:19 -0700718 IAfTrack::FrameTime recordFT{}, playFT{};
Andy Hung30282562018-08-08 18:27:03 -0700719 recordTrack->getKernelFrameTime(&recordFT);
720 playbackTrack->getKernelFrameTime(&playFT);
721 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
722 const int64_t frameDiff = recordFT.frames - playFT.frames;
723 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
724
725 // It is possible that the patch track and patch record have a large time disparity because
726 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
727 // time difference based on how often we expect the timestamps to update in normal operation
728 // (typical should be no more than 50 ms).
729 //
730 // If the timestamps aren't sampled close enough, the patch latency is not
731 // considered valid.
732 //
733 // TODO: change this based on more experiments.
734 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
735 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
736 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
737 - timeDiffNs * 1e-6;
738 return OK;
739 }
740 }
741
742 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700743}
744
Andy Hung8e6b62a2023-07-13 18:11:33 -0700745String8 PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700746{
Andy Hungc3ab7732018-06-01 13:46:29 -0700747 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800748 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
749 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
750 mRecord.const_thread().get(), mPlayback.const_thread().get());
751
752 bool hasSinkDevice =
753 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
754 bool hasSourceDevice =
755 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
756 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
757 hasSinkDevice ? "num sinks" :
758 (hasSourceDevice ? "num sources" : "no devices"),
759 hasSinkDevice ? mAudioPatch.num_sinks :
760 (hasSourceDevice ? mAudioPatch.num_sources : 0),
761 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
762 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700763
764 // add latency if it exists
765 double latencyMs;
766 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700767 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700768 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700769 return result;
770}
771
Eric Laurent951f4552014-05-20 10:48:17 -0700772/* Disconnect a patch */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700773status_t PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
Andy Hung87c693c2023-07-06 20:56:16 -0700774 //unlocks AudioFlinger::mLock when calling IAfThreadBase::sendReleaseAudioPatchConfigEvent
Eric Laurent34e55a42023-04-24 16:37:56 +0200775 //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock
776 //before processing the release patch request.
777 NO_THREAD_SAFETY_ANALYSIS
778 {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700779 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700780 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700781
Mikhail Naganovdea53042018-04-26 13:10:21 -0700782 auto iter = mPatches.find(handle);
783 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700784 return BAD_VALUE;
785 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700786 Patch &removedPatch = iter->second;
787 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700788
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700789 const struct audio_port_config &src = patch.sources[0];
790 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700791 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700792 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
793 if (hwDevice == 0) {
794 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700795 status = BAD_VALUE;
796 break;
797 }
Eric Laurent83b88082014-06-20 18:31:16 -0700798
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700799 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700800 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700801 break;
802 }
803
Mikhail Naganovdea53042018-04-26 13:10:21 -0700804 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
805 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
Andy Hung87c693c2023-07-06 20:56:16 -0700806 sp<IAfThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700807 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700808 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800809 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700810 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800811 status = BAD_VALUE;
812 break;
813 }
Eric Laurent951f4552014-05-20 10:48:17 -0700814 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200815 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700816 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200817 mAudioFlinger.lock();
Eric Laurent054d9d32015-04-24 08:48:48 -0700818 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700819 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700820 }
821 } break;
822 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700823 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
824 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700825 status = BAD_VALUE;
826 break;
827 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700828 audio_io_handle_t ioHandle = src.ext.mix.handle;
Andy Hung87c693c2023-07-06 20:56:16 -0700829 sp<IAfThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700830 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700831 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800832 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700833 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800834 status = BAD_VALUE;
835 break;
836 }
Eric Laurent951f4552014-05-20 10:48:17 -0700837 }
Eric Laurent34e55a42023-04-24 16:37:56 +0200838 mAudioFlinger.unlock();
Mikhail Naganovdea53042018-04-26 13:10:21 -0700839 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent34e55a42023-04-24 16:37:56 +0200840 mAudioFlinger.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700841 } break;
842 default:
843 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700844 }
845
Eric Laurentb82e6b72019-11-22 17:25:04 -0800846 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700847 return status;
848}
849
Andy Hung8e6b62a2023-07-13 18:11:33 -0700850void PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800851 mPatches.erase(handle);
852 removeSoftwarePatchFromInsertedModules(handle);
François Gaffie58e73af2023-02-15 11:47:24 +0100853 if (!reuseExistingHalPatch) {
854 mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle);
855 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800856}
857
Eric Laurent951f4552014-05-20 10:48:17 -0700858/* List connected audio ports and they attributes */
Andy Hung8e6b62a2023-07-13 18:11:33 -0700859status_t PatchPanel::listAudioPatches(unsigned int* /* num_patches */,
Eric Laurent951f4552014-05-20 10:48:17 -0700860 struct audio_patch *patches __unused)
861{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700862 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700863 return NO_ERROR;
864}
865
Andy Hung8e6b62a2023-07-13 18:11:33 -0700866status_t PatchPanel::getDownstreamSoftwarePatches(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700867 audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -0700868 std::vector<SoftwarePatch>* patches) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700869{
870 for (const auto& module : mInsertedModules) {
871 if (module.second.streams.count(stream)) {
872 for (const auto& patchHandle : module.second.sw_patches) {
873 const auto& patch_iter = mPatches.find(patchHandle);
874 if (patch_iter != mPatches.end()) {
875 const Patch &patch = patch_iter->second;
Andy Hungb6692eb2023-07-13 16:52:46 -0700876 patches->emplace_back(sp<const IAfPatchPanel>::fromExisting(this),
877 patchHandle,
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700878 patch.mPlayback.const_thread()->id(),
879 patch.mRecord.const_thread()->id());
880 } else {
881 ALOGE("Stale patch handle in the cache: %d", patchHandle);
882 }
883 }
884 return OK;
885 }
886 }
887 // The stream is not associated with any of inserted modules.
888 return BAD_VALUE;
889}
890
Andy Hung8e6b62a2023-07-13 18:11:33 -0700891void PatchPanel::notifyStreamOpened(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100892 AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700893{
894 if (audioHwDevice->isInsert()) {
895 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100896 if (patch != nullptr) {
897 std::vector <SoftwarePatch> swPatches;
898 getDownstreamSoftwarePatches(stream, &swPatches);
899 if (swPatches.size() > 0) {
900 auto iter = mPatches.find(swPatches[0].getPatchHandle());
901 if (iter != mPatches.end()) {
902 *patch = iter->second.mAudioPatch;
903 }
904 }
905 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700906 }
907}
908
Andy Hung8e6b62a2023-07-13 18:11:33 -0700909void PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700910{
911 for (auto& module : mInsertedModules) {
912 module.second.streams.erase(stream);
913 }
914}
915
Andy Hung8e6b62a2023-07-13 18:11:33 -0700916AudioHwDevice* PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700917{
918 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
919 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
920 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700921 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700922 return nullptr;
923 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700924 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700925}
926
Andy Hung8e6b62a2023-07-13 18:11:33 -0700927sp<DeviceHalInterface> PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700928{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700929 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
930 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
931}
932
Andy Hung8e6b62a2023-07-13 18:11:33 -0700933void PatchPanel::addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +0100934 audio_module_handle_t module, audio_patch_handle_t handle,
935 const struct audio_patch *patch)
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700936{
937 mInsertedModules[module].sw_patches.insert(handle);
Eric Laurent74c38dc2020-12-23 18:19:44 +0100938 if (!mInsertedModules[module].streams.empty()) {
939 mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams);
940 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700941}
942
Andy Hung8e6b62a2023-07-13 18:11:33 -0700943void PatchPanel::removeSoftwarePatchFromInsertedModules(
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700944 audio_patch_handle_t handle)
945{
946 for (auto& module : mInsertedModules) {
947 module.second.sw_patches.erase(handle);
948 }
949}
950
Andy Hung8e6b62a2023-07-13 18:11:33 -0700951void PatchPanel::dump(int fd) const
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700952{
953 String8 patchPanelDump;
954 const char *indent = " ";
955
Mikhail Naganov201369b2018-05-16 16:52:32 -0700956 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700957 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800958 if (!headerPrinted) {
959 patchPanelDump += "\nPatches:\n";
960 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700961 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800962 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700963 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700964
965 headerPrinted = false;
966 for (const auto& module : mInsertedModules) {
967 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
968 if (!headerPrinted) {
969 patchPanelDump += "\nTracked inserted modules:\n";
970 headerPrinted = true;
971 }
972 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
973 for (const auto& stream : module.second.streams) {
974 moduleDump.appendFormat("%d ", stream);
975 }
976 moduleDump.append("; SW Patches: ");
977 for (const auto& patch : module.second.sw_patches) {
978 moduleDump.appendFormat("%d ", patch);
979 }
980 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
981 }
982 }
983
984 if (!patchPanelDump.isEmpty()) {
985 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700986 }
987}
988
Glenn Kasten63238ef2015-03-02 15:50:29 -0800989} // namespace android