blob: f6971fcc50d6b4884e71d1a110fa2157c328bd1a [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>
jiabinc52b1ff2019-10-31 17:20:42 -070028#include <media/DeviceDescriptorBase.h>
Mikhail Naganovdc769682018-05-04 15:34:08 -070029#include <media/PatchBuilder.h>
Andy Hungab7ef302018-05-15 19:35:29 -070030#include <mediautils/ServiceUtilities.h>
Eric Laurent951f4552014-05-20 10:48:17 -070031
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
47namespace android {
48
49/* List connected audio ports and their attributes */
50status_t AudioFlinger::listAudioPorts(unsigned int *num_ports,
51 struct audio_port *ports)
52{
53 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070054 return mPatchPanel.listAudioPorts(num_ports, ports);
Eric Laurent951f4552014-05-20 10:48:17 -070055}
56
57/* Get supported attributes for a given audio port */
jiabin3b4270e2020-09-22 14:45:40 -070058status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) {
Eric Laurent951f4552014-05-20 10:48:17 -070059 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070060 return mPatchPanel.getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -070061}
62
Eric Laurent951f4552014-05-20 10:48:17 -070063/* Connect a patch between several source and sink ports */
64status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch,
65 audio_patch_handle_t *handle)
66{
67 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070068 return mPatchPanel.createAudioPatch(patch, handle);
Eric Laurent951f4552014-05-20 10:48:17 -070069}
70
71/* Disconnect a patch */
72status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle)
73{
74 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070075 return mPatchPanel.releaseAudioPatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -070076}
77
Eric Laurent951f4552014-05-20 10:48:17 -070078/* List connected audio ports and they attributes */
79status_t AudioFlinger::listAudioPatches(unsigned int *num_patches,
80 struct audio_patch *patches)
81{
82 Mutex::Autolock _l(mLock);
Mikhail Naganovdea53042018-04-26 13:10:21 -070083 return mPatchPanel.listAudioPatches(num_patches, patches);
Eric Laurent951f4552014-05-20 10:48:17 -070084}
85
Mikhail Naganovadca70f2018-07-09 12:49:25 -070086status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const
87{
88 const auto& iter = mPatchPanel.mPatches.find(mPatchHandle);
89 if (iter != mPatchPanel.mPatches.end()) {
90 return iter->second.getLatencyMs(latencyMs);
91 } else {
92 return BAD_VALUE;
93 }
94}
95
Eric Laurent951f4552014-05-20 10:48:17 -070096/* List connected audio ports and their attributes */
97status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused,
98 struct audio_port *ports __unused)
99{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700100 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700101 return NO_ERROR;
102}
103
104/* Get supported attributes for a given audio port */
jiabin3b4270e2020-09-22 14:45:40 -0700105status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port)
Eric Laurent951f4552014-05-20 10:48:17 -0700106{
jiabin3b4270e2020-09-22 14:45:40 -0700107 if (port->type != AUDIO_PORT_TYPE_DEVICE) {
108 // Only query the HAL when the port is a device.
109 return NO_ERROR;
110 }
111 AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module);
112 if (hwDevice == nullptr) {
113 ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module);
114 return BAD_VALUE;
115 }
116 return hwDevice->getAudioPort(port);
Eric Laurent951f4552014-05-20 10:48:17 -0700117}
118
Eric Laurent951f4552014-05-20 10:48:17 -0700119/* Connect a patch between several source and sink ports */
120status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch,
121 audio_patch_handle_t *handle)
122{
Greg Kaiserf27ce402016-03-14 13:43:14 -0700123 if (handle == NULL || patch == NULL) {
124 return BAD_VALUE;
125 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700126 ALOGV("%s() num_sources %d num_sinks %d handle %d",
127 __func__, patch->num_sources, patch->num_sinks, *handle);
128 status_t status = NO_ERROR;
129 audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700130
Mikhail Naganovac9858b2018-06-15 13:12:37 -0700131 if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) {
Eric Laurent951f4552014-05-20 10:48:17 -0700132 return BAD_VALUE;
133 }
Eric Laurent874c42872014-08-08 15:13:39 -0700134 // limit number of sources to 1 for now or 2 sources for special cross hw module case.
135 // only the audio policy manager can request a patch creation with 2 sources.
136 if (patch->num_sources > 2) {
137 return INVALID_OPERATION;
138 }
Eric Laurent951f4552014-05-20 10:48:17 -0700139
Eric Laurent83b88082014-06-20 18:31:16 -0700140 if (*handle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700141 auto iter = mPatches.find(*handle);
142 if (iter != mPatches.end()) {
143 ALOGV("%s() removing patch handle %d", __func__, *handle);
144 Patch &removedPatch = iter->second;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700145 // free resources owned by the removed patch if applicable
146 // 1) if a software patch is present, release the playback and capture threads and
147 // tracks created. This will also release the corresponding audio HAL patches
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700148 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700149 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700150 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700151 // 2) if the new patch and old patch source or sink are devices from different
152 // hw modules, clear the audio HAL patches now because they will not be updated
153 // by call to create_audio_patch() below which will happen on a different HW module
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700154 if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700155 audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE;
156 const struct audio_patch &oldPatch = removedPatch.mAudioPatch;
157 if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
158 (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE ||
159 oldPatch.sources[0].ext.device.hw_module !=
160 patch->sources[0].ext.device.hw_module)) {
161 hwModule = oldPatch.sources[0].ext.device.hw_module;
162 } else if (patch->num_sinks == 0 ||
163 (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
164 (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE ||
165 oldPatch.sinks[0].ext.device.hw_module !=
166 patch->sinks[0].ext.device.hw_module))) {
167 // Note on (patch->num_sinks == 0): this situation should not happen as
168 // these special patches are only created by the policy manager but just
169 // in case, systematically clear the HAL patch.
170 // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700171 // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700172 hwModule = oldPatch.sinks[0].ext.device.hw_module;
173 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700174 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule);
175 if (hwDevice != 0) {
176 hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700177 }
Eric Laurent9bcfa7c2019-11-21 15:45:04 -0800178 halHandle = removedPatch.mHalHandle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700179 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800180 erasePatch(*handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700181 }
182 }
183
Mikhail Naganovdea53042018-04-26 13:10:21 -0700184 Patch newPatch{*patch};
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700185 audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700186
Eric Laurent951f4552014-05-20 10:48:17 -0700187 switch (patch->sources[0].type) {
188 case AUDIO_PORT_TYPE_DEVICE: {
Eric Laurent874c42872014-08-08 15:13:39 -0700189 audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700190 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule);
191 if (!audioHwDevice) {
Eric Laurent83b88082014-06-20 18:31:16 -0700192 status = BAD_VALUE;
193 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700194 }
195 for (unsigned int i = 0; i < patch->num_sinks; i++) {
Eric Laurent874c42872014-08-08 15:13:39 -0700196 // support only one sink if connection to a mix or across HW modules
197 if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX ||
Mikhail Naganovc589a492018-05-16 11:14:57 -0700198 (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE &&
199 patch->sinks[i].ext.device.hw_module != srcModule)) &&
Eric Laurent874c42872014-08-08 15:13:39 -0700200 patch->num_sinks > 1) {
Mikhail Naganovc589a492018-05-16 11:14:57 -0700201 ALOGW("%s() multiple sinks for mix or across modules not supported", __func__);
Eric Laurent874c42872014-08-08 15:13:39 -0700202 status = INVALID_OPERATION;
203 goto exit;
204 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700205 // reject connection to different sink types
206 if (patch->sinks[i].type != patch->sinks[0].type) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700207 ALOGW("%s() different sink types in same patch not supported", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700208 status = BAD_VALUE;
209 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700210 }
Eric Laurent951f4552014-05-20 10:48:17 -0700211 }
212
Eric Laurent3bcf8592015-04-03 12:13:24 -0700213 // manage patches requiring a software bridge
Eric Laurentd60560a2015-04-10 11:31:20 -0700214 // - special patch request with 2 sources (reuse one existing output mix) OR
Eric Laurent3bcf8592015-04-03 12:13:24 -0700215 // - Device to device AND
216 // - source HW module != destination HW module OR
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700217 // - audio HAL does not support audio patches creation
Eric Laurentd60560a2015-04-10 11:31:20 -0700218 if ((patch->num_sources == 2) ||
219 ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) &&
220 ((patch->sinks[0].ext.device.hw_module != srcModule) ||
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700221 !audioHwDevice->supportsAudioPatches()))) {
juyuchen2224c5a2019-01-21 12:00:58 +0800222 audio_devices_t outputDevice = patch->sinks[0].ext.device.type;
223 String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address);
Eric Laurent83b88082014-06-20 18:31:16 -0700224 if (patch->num_sources == 2) {
225 if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX ||
Eric Laurentd60560a2015-04-10 11:31:20 -0700226 (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module !=
227 patch->sources[1].ext.mix.hw_module)) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700228 ALOGW("%s() invalid source combination", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700229 status = INVALID_OPERATION;
230 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700231 }
Eric Laurent83b88082014-06-20 18:31:16 -0700232
233 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700234 mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle);
Eric Laurent83b88082014-06-20 18:31:16 -0700235 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700236 ALOGW("%s() cannot get playback thread", __func__);
Eric Laurent83b88082014-06-20 18:31:16 -0700237 status = INVALID_OPERATION;
238 goto exit;
239 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700240 // existing playback thread is reused, so it is not closed when patch is cleared
241 newPatch.mPlayback.setThread(
242 reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/);
Eric Laurent951f4552014-05-20 10:48:17 -0700243 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -0700244 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700245 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov67bae2c2018-07-16 15:44:35 -0700246 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE;
247 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
248 config.sample_rate = patch->sinks[0].sample_rate;
249 }
250 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
251 config.channel_mask = patch->sinks[0].channel_mask;
252 }
253 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
254 config.format = patch->sinks[0].format;
255 }
256 if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) {
257 flags = patch->sinks[0].flags.output;
258 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700259 sp<ThreadBase> thread = mAudioFlinger.openOutput_l(
Eric Laurent6acd1d42017-01-04 14:23:29 -0800260 patch->sinks[0].ext.device.hw_module,
261 &output,
262 &config,
juyuchen2224c5a2019-01-21 12:00:58 +0800263 outputDevice,
264 outputDeviceAddress,
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700265 flags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700266 ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get());
267 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700268 status = NO_MEMORY;
269 goto exit;
270 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700271 newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get()));
Eric Laurent83b88082014-06-20 18:31:16 -0700272 }
Eric Laurent83b88082014-06-20 18:31:16 -0700273 audio_devices_t device = patch->sources[0].ext.device.type;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700274 String8 address = String8(patch->sources[0].ext.device.address);
275 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
Eric Laurent8ae73122016-04-12 10:13:29 -0700276 // open input stream with source device audio properties if provided or
277 // default to peer output stream properties otherwise.
278 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) {
279 config.sample_rate = patch->sources[0].sample_rate;
280 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700281 config.sample_rate = newPatch.mPlayback.thread()->sampleRate();
Eric Laurent8ae73122016-04-12 10:13:29 -0700282 }
283 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) {
284 config.channel_mask = patch->sources[0].channel_mask;
285 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700286 config.channel_mask = audio_channel_in_mask_from_count(
287 newPatch.mPlayback.thread()->channelCount());
Eric Laurent8ae73122016-04-12 10:13:29 -0700288 }
289 if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) {
290 config.format = patch->sources[0].format;
291 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700292 config.format = newPatch.mPlayback.thread()->format();
Eric Laurent8ae73122016-04-12 10:13:29 -0700293 }
Mikhail Naganov32abc2b2018-05-24 12:57:11 -0700294 audio_input_flags_t flags =
295 patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
296 patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
Eric Laurentcf2c0212014-07-25 16:20:43 -0700297 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700298 sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700299 &input,
Eric Laurent83b88082014-06-20 18:31:16 -0700300 &config,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700301 device,
302 address,
303 AUDIO_SOURCE_MIC,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800304 flags,
305 outputDevice,
306 outputDeviceAddress);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700307 ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x",
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700308 thread.get(), config.channel_mask);
309 if (thread == 0) {
Eric Laurent83b88082014-06-20 18:31:16 -0700310 status = NO_MEMORY;
311 goto exit;
312 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700313 newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get()));
Mikhail Naganovdea53042018-04-26 13:10:21 -0700314 status = newPatch.createConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700315 if (status != NO_ERROR) {
316 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700317 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700318 if (audioHwDevice->isInsert()) {
319 insertedModule = audioHwDevice->handle();
320 }
Eric Laurent951f4552014-05-20 10:48:17 -0700321 } else {
Eric Laurent054d9d32015-04-24 08:48:48 -0700322 if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700323 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(
Eric Laurent054d9d32015-04-24 08:48:48 -0700324 patch->sinks[0].ext.mix.handle);
325 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700326 thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800327 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700328 ALOGW("%s() bad capture I/O handle %d",
329 __func__, patch->sinks[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800330 status = BAD_VALUE;
331 goto exit;
332 }
Eric Laurent83b88082014-06-20 18:31:16 -0700333 }
Eric Laurent054d9d32015-04-24 08:48:48 -0700334 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800335 if (status == NO_ERROR) {
336 newPatch.setThread(thread);
337 }
338
Eric Laurent526aa572019-01-15 10:54:58 -0800339 // remove stale audio patch with same input as sink if any
340 for (auto& iter : mPatches) {
341 if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800342 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800343 break;
344 }
345 }
Eric Laurent83b88082014-06-20 18:31:16 -0700346 } else {
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700347 sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice();
348 status = hwDevice->createAudioPatch(patch->num_sources,
349 patch->sources,
350 patch->num_sinks,
351 patch->sinks,
352 &halHandle);
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700353 if (status == INVALID_OPERATION) goto exit;
Eric Laurent83b88082014-06-20 18:31:16 -0700354 }
Eric Laurent951f4552014-05-20 10:48:17 -0700355 }
356 } break;
357 case AUDIO_PORT_TYPE_MIX: {
Eric Laurent874c42872014-08-08 15:13:39 -0700358 audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700359 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule);
Eric Laurent951f4552014-05-20 10:48:17 -0700360 if (index < 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700361 ALOGW("%s() bad src hw module %d", __func__, srcModule);
Eric Laurent83b88082014-06-20 18:31:16 -0700362 status = BAD_VALUE;
363 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700364 }
365 // limit to connections between devices and output streams
jiabinc52b1ff2019-10-31 17:20:42 -0700366 DeviceDescriptorBaseVector devices;
Eric Laurent951f4552014-05-20 10:48:17 -0700367 for (unsigned int i = 0; i < patch->num_sinks; i++) {
368 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700369 ALOGW("%s() invalid sink type %d for mix source",
370 __func__, patch->sinks[i].type);
Eric Laurent83b88082014-06-20 18:31:16 -0700371 status = BAD_VALUE;
372 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700373 }
374 // limit to connections between sinks and sources on same HW module
Eric Laurent874c42872014-08-08 15:13:39 -0700375 if (patch->sinks[i].ext.device.hw_module != srcModule) {
Eric Laurent83b88082014-06-20 18:31:16 -0700376 status = BAD_VALUE;
377 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700378 }
jiabinc52b1ff2019-10-31 17:20:42 -0700379 sp<DeviceDescriptorBase> device = new DeviceDescriptorBase(
380 patch->sinks[i].ext.device.type);
381 device->setAddress(patch->sinks[i].ext.device.address);
382 device->applyAudioPortConfig(&patch->sinks[i]);
383 devices.push_back(device);
Eric Laurent951f4552014-05-20 10:48:17 -0700384 }
Eric Laurent951f4552014-05-20 10:48:17 -0700385 sp<ThreadBase> thread =
Mikhail Naganovdea53042018-04-26 13:10:21 -0700386 mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700387 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700388 thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800389 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700390 ALOGW("%s() bad playback I/O handle %d",
391 __func__, patch->sources[0].ext.mix.handle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800392 status = BAD_VALUE;
393 goto exit;
394 }
Eric Laurent951f4552014-05-20 10:48:17 -0700395 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700396 if (thread == mAudioFlinger.primaryPlaybackThread_l()) {
jiabinc52b1ff2019-10-31 17:20:42 -0700397 mAudioFlinger.updateOutDevicesForRecordThreads_l(devices);
Eric Laurent951f4552014-05-20 10:48:17 -0700398 }
399
Eric Laurent054d9d32015-04-24 08:48:48 -0700400 status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800401 if (status == NO_ERROR) {
402 newPatch.setThread(thread);
403 }
Eric Laurent526aa572019-01-15 10:54:58 -0800404
405 // remove stale audio patch with same output as source if any
406 for (auto& iter : mPatches) {
407 if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800408 erasePatch(iter.first);
Eric Laurent526aa572019-01-15 10:54:58 -0800409 break;
410 }
411 }
Eric Laurent951f4552014-05-20 10:48:17 -0700412 } break;
413 default:
Eric Laurent83b88082014-06-20 18:31:16 -0700414 status = BAD_VALUE;
415 goto exit;
Eric Laurent951f4552014-05-20 10:48:17 -0700416 }
Eric Laurent83b88082014-06-20 18:31:16 -0700417exit:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700418 ALOGV("%s() status %d", __func__, status);
Eric Laurent951f4552014-05-20 10:48:17 -0700419 if (status == NO_ERROR) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700420 *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH);
421 newPatch.mHalHandle = halHandle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800422 mAudioFlinger.mDeviceEffectManager.createAudioPatch(*handle, newPatch);
Mikhail Naganovdea53042018-04-26 13:10:21 -0700423 mPatches.insert(std::make_pair(*handle, std::move(newPatch)));
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700424 if (insertedModule != AUDIO_MODULE_HANDLE_NONE) {
425 addSoftwarePatchToInsertedModules(insertedModule, *handle);
426 }
Eric Laurent83b88082014-06-20 18:31:16 -0700427 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700428 newPatch.clearConnections(this);
Eric Laurent951f4552014-05-20 10:48:17 -0700429 }
430 return status;
431}
432
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700433AudioFlinger::PatchPanel::Patch::~Patch()
434{
435 ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d",
436 mRecord.handle(), mPlayback.handle());
437}
438
Mikhail Naganovdea53042018-04-26 13:10:21 -0700439status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700440{
441 // create patch from source device to record thread input
Mikhail Naganovdc769682018-05-04 15:34:08 -0700442 status_t status = panel->createAudioPatch(
443 PatchBuilder().addSource(mAudioPatch.sources[0]).
444 addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(),
445 mRecord.handlePtr());
Eric Laurent83b88082014-06-20 18:31:16 -0700446 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700447 *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700448 return status;
449 }
450
451 // create patch from playback thread output to sink device
Mikhail Naganovdea53042018-04-26 13:10:21 -0700452 if (mAudioPatch.num_sinks != 0) {
Mikhail Naganovdc769682018-05-04 15:34:08 -0700453 status = panel->createAudioPatch(
454 PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(),
455 mPlayback.handlePtr());
Eric Laurentd60560a2015-04-10 11:31:20 -0700456 if (status != NO_ERROR) {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700457 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentd60560a2015-04-10 11:31:20 -0700458 return status;
459 }
460 } else {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700461 *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent83b88082014-06-20 18:31:16 -0700462 }
463
Eric Laurent83b88082014-06-20 18:31:16 -0700464 // create a special record track to capture from record thread
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700465 uint32_t channelCount = mPlayback.thread()->channelCount();
Eric Laurent83b88082014-06-20 18:31:16 -0700466 audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700467 audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask();
468 uint32_t sampleRate = mPlayback.thread()->sampleRate();
469 audio_format_t format = mPlayback.thread()->format();
Eric Laurent83b88082014-06-20 18:31:16 -0700470
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700471 audio_format_t inputFormat = mRecord.thread()->format();
472 if (!audio_is_linear_pcm(inputFormat)) {
473 // The playbackThread format will say PCM for IEC61937 packetized stream.
474 // Use recordThread format.
475 format = inputFormat;
476 }
477 audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
478 mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE;
jiabin01c8f562018-07-19 17:47:28 -0700479 if (sampleRate == mRecord.thread()->sampleRate() &&
480 inChannelMask == mRecord.thread()->channelMask() &&
481 mRecord.thread()->fastTrackAvailable() &&
482 mRecord.thread()->hasFastCapture()) {
483 // Create a fast track if the record thread has fast capture to get better performance.
484 // Only enable fast mode when there is no resample needed.
485 inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST);
486 } else {
487 // Fast mode is not available in this case.
488 inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST);
489 }
Eric Laurent83b88082014-06-20 18:31:16 -0700490
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700491 audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ?
492 mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE;
Mikhail Naganov776eb212018-07-19 15:14:11 -0700493 audio_stream_type_t streamType = AUDIO_STREAM_PATCH;
494 if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) {
495 // "reuse one existing output mix" case
496 streamType = mAudioPatch.sources[1].ext.mix.usecase.stream;
497 }
jiabin01c8f562018-07-19 17:47:28 -0700498 if (mPlayback.thread()->hasFastMixer()) {
499 // Create a fast track if the playback thread has fast mixer to get better performance.
Andy Hungae22b482019-05-09 15:38:55 -0700500 // Note: we should have matching channel mask, sample rate, and format by the logic above.
jiabin01c8f562018-07-19 17:47:28 -0700501 outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST);
Andy Hungae22b482019-05-09 15:38:55 -0700502 } else {
503 outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST);
jiabin01c8f562018-07-19 17:47:28 -0700504 }
505
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700506 sp<RecordThread::PatchRecord> tempRecordTrack;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800507 const bool usePassthruPatchRecord =
508 (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT);
Dean Wheatleyb3643892019-12-18 08:38:37 +1100509 const size_t playbackFrameCount = mPlayback.thread()->frameCount();
510 const size_t recordFrameCount = mRecord.thread()->frameCount();
511 size_t frameCount = 0;
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800512 if (usePassthruPatchRecord) {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100513 // PassthruPatchRecord producesBufferOnDemand, so use
514 // maximum of playback and record thread framecounts
515 frameCount = std::max(playbackFrameCount, recordFrameCount);
516 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
517 __func__, playbackFrameCount, recordFrameCount, frameCount);
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700518 tempRecordTrack = new RecordThread::PassthruPatchRecord(
519 mRecord.thread().get(),
520 sampleRate,
521 inChannelMask,
522 format,
523 frameCount,
524 inputFlags);
525 } else {
Dean Wheatleyb3643892019-12-18 08:38:37 +1100526 // use a pseudo LCM between input and output framecount
527 int playbackShift = __builtin_ctz(playbackFrameCount);
528 int shift = __builtin_ctz(recordFrameCount);
529 if (playbackShift < shift) {
530 shift = playbackShift;
531 }
532 frameCount = (playbackFrameCount * recordFrameCount) >> shift;
533 ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu",
534 __func__, playbackFrameCount, recordFrameCount, frameCount);
535
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700536 tempRecordTrack = new RecordThread::PatchRecord(
537 mRecord.thread().get(),
538 sampleRate,
539 inChannelMask,
540 format,
541 frameCount,
542 nullptr,
543 (size_t)0 /* bufferSize */,
544 inputFlags);
545 }
546 status = mRecord.checkTrack(tempRecordTrack.get());
547 if (status != NO_ERROR) {
548 return status;
549 }
550
Eric Laurent83b88082014-06-20 18:31:16 -0700551 // create a special playback track to render to playback thread.
552 // this track is given the same buffer as the PatchRecord buffer
Mikhail Naganov9515fc82019-10-01 16:52:14 -0700553 sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack(
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700554 mPlayback.thread().get(),
Mikhail Naganov776eb212018-07-19 15:14:11 -0700555 streamType,
Eric Laurent83b88082014-06-20 18:31:16 -0700556 sampleRate,
557 outChannelMask,
558 format,
559 frameCount,
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700560 tempRecordTrack->buffer(),
561 tempRecordTrack->bufferSize(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -0700562 outputFlags);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700563 status = mPlayback.checkTrack(tempPatchTrack.get());
Eric Laurent83b88082014-06-20 18:31:16 -0700564 if (status != NO_ERROR) {
565 return status;
566 }
Eric Laurent83b88082014-06-20 18:31:16 -0700567
568 // tie playback and record tracks together
Mikhail Naganovdd91ce22020-01-13 11:34:30 -0800569 // In the case of PassthruPatchRecord no I/O activity happens on RecordThread,
570 // everything is driven from PlaybackThread. Thus AudioBufferProvider methods
571 // of PassthruPatchRecord can only be called if the corresponding PatchTrack
572 // is alive. There is no need to hold a reference, and there is no need
573 // to clear it. In fact, since playback stopping is asynchronous, there is
574 // no proper time when clearing could be done.
575 mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord);
576 mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/);
Eric Laurent83b88082014-06-20 18:31:16 -0700577
578 // start capture and playback
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700579 mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE);
580 mPlayback.track()->start();
Eric Laurent83b88082014-06-20 18:31:16 -0700581
582 return status;
583}
584
Mikhail Naganovdea53042018-04-26 13:10:21 -0700585void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel)
Eric Laurent83b88082014-06-20 18:31:16 -0700586{
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700587 ALOGV("%s() mRecord.handle %d mPlayback.handle %d",
588 __func__, mRecord.handle(), mPlayback.handle());
589 mRecord.stopTrack();
590 mPlayback.stopTrack();
Mikhail Naganovd3f301c2019-03-11 08:58:03 -0700591 mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle.
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700592 mRecord.closeConnections(panel);
593 mPlayback.closeConnections(panel);
Eric Laurent83b88082014-06-20 18:31:16 -0700594}
595
Andy Hungc3ab7732018-06-01 13:46:29 -0700596status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const
597{
598 if (!isSoftware()) return INVALID_OPERATION;
599
600 auto recordTrack = mRecord.const_track();
601 if (recordTrack.get() == nullptr) return INVALID_OPERATION;
602
603 auto playbackTrack = mPlayback.const_track();
604 if (playbackTrack.get() == nullptr) return INVALID_OPERATION;
605
606 // Latency information for tracks may be called without obtaining
607 // the underlying thread lock.
608 //
609 // We use record server latency + playback track latency (generally smaller than the
610 // reverse due to internal biases).
611 //
612 // TODO: is this stable enough? Consider a PatchTrack synchronized version of this.
Andy Hungc3ab7732018-06-01 13:46:29 -0700613
Andy Hung30282562018-08-08 18:27:03 -0700614 // For PCM tracks get server latency.
615 if (audio_is_linear_pcm(recordTrack->format())) {
616 double recordServerLatencyMs, playbackTrackLatencyMs;
617 if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK
618 && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) {
619 *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs;
620 return OK;
621 }
622 }
Andy Hungc3ab7732018-06-01 13:46:29 -0700623
Andy Hung30282562018-08-08 18:27:03 -0700624 // See if kernel latencies are available.
625 // If so, do a frame diff and time difference computation to estimate
626 // the total patch latency. This requires that frame counts are reported by the
627 // HAL are matched properly in the case of record overruns and playback underruns.
628 ThreadBase::TrackBase::FrameTime recordFT{}, playFT{};
629 recordTrack->getKernelFrameTime(&recordFT);
630 playbackTrack->getKernelFrameTime(&playFT);
631 if (recordFT.timeNs > 0 && playFT.timeNs > 0) {
632 const int64_t frameDiff = recordFT.frames - playFT.frames;
633 const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs;
634
635 // It is possible that the patch track and patch record have a large time disparity because
636 // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp
637 // time difference based on how often we expect the timestamps to update in normal operation
638 // (typical should be no more than 50 ms).
639 //
640 // If the timestamps aren't sampled close enough, the patch latency is not
641 // considered valid.
642 //
643 // TODO: change this based on more experiments.
644 constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND;
645 if (std::abs(timeDiffNs) < maxValidTimeDiffNs) {
646 *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate()
647 - timeDiffNs * 1e-6;
648 return OK;
649 }
650 }
651
652 return INVALID_OPERATION;
Andy Hungc3ab7732018-06-01 13:46:29 -0700653}
654
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700655String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const
Mikhail Naganov201369b2018-05-16 16:52:32 -0700656{
Andy Hungc3ab7732018-06-01 13:46:29 -0700657 // TODO: Consider table dump form for patches, just like tracks.
Eric Laurentb82e6b72019-11-22 17:25:04 -0800658 String8 result = String8::format("Patch %d: %s (thread %p => thread %p)",
659 myHandle, isSoftware() ? "Software bridge between" : "No software bridge",
660 mRecord.const_thread().get(), mPlayback.const_thread().get());
661
662 bool hasSinkDevice =
663 mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE;
664 bool hasSourceDevice =
665 mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE;
666 result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(),
667 hasSinkDevice ? "num sinks" :
668 (hasSourceDevice ? "num sources" : "no devices"),
669 hasSinkDevice ? mAudioPatch.num_sinks :
670 (hasSourceDevice ? mAudioPatch.num_sources : 0),
671 hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type :
672 (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0));
Andy Hungc3ab7732018-06-01 13:46:29 -0700673
674 // add latency if it exists
675 double latencyMs;
676 if (getLatencyMs(&latencyMs) == OK) {
Mikhail Naganovb8b60972018-09-13 12:55:43 -0700677 result.appendFormat(" latency: %.2lf ms", latencyMs);
Andy Hungc3ab7732018-06-01 13:46:29 -0700678 }
Mikhail Naganov201369b2018-05-16 16:52:32 -0700679 return result;
680}
681
Eric Laurent951f4552014-05-20 10:48:17 -0700682/* Disconnect a patch */
683status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle)
684{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700685 ALOGV("%s handle %d", __func__, handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700686 status_t status = NO_ERROR;
Eric Laurent951f4552014-05-20 10:48:17 -0700687
Mikhail Naganovdea53042018-04-26 13:10:21 -0700688 auto iter = mPatches.find(handle);
689 if (iter == mPatches.end()) {
Eric Laurent951f4552014-05-20 10:48:17 -0700690 return BAD_VALUE;
691 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700692 Patch &removedPatch = iter->second;
693 const struct audio_patch &patch = removedPatch.mAudioPatch;
Eric Laurent951f4552014-05-20 10:48:17 -0700694
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700695 const struct audio_port_config &src = patch.sources[0];
696 switch (src.type) {
Eric Laurent951f4552014-05-20 10:48:17 -0700697 case AUDIO_PORT_TYPE_DEVICE: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700698 sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module);
699 if (hwDevice == 0) {
700 ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700701 status = BAD_VALUE;
702 break;
703 }
Eric Laurent83b88082014-06-20 18:31:16 -0700704
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700705 if (removedPatch.isSoftware()) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700706 removedPatch.clearConnections(this);
Eric Laurent83b88082014-06-20 18:31:16 -0700707 break;
708 }
709
Mikhail Naganovdea53042018-04-26 13:10:21 -0700710 if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) {
711 audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle;
712 sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700713 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700714 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800715 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700716 ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800717 status = BAD_VALUE;
718 break;
719 }
Eric Laurent951f4552014-05-20 10:48:17 -0700720 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700721 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent054d9d32015-04-24 08:48:48 -0700722 } else {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700723 status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700724 }
725 } break;
726 case AUDIO_PORT_TYPE_MIX: {
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700727 if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) {
728 ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module);
Eric Laurent951f4552014-05-20 10:48:17 -0700729 status = BAD_VALUE;
730 break;
731 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700732 audio_io_handle_t ioHandle = src.ext.mix.handle;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700733 sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700734 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700735 thread = mAudioFlinger.checkMmapThread_l(ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800736 if (thread == 0) {
Mikhail Naganovdea53042018-04-26 13:10:21 -0700737 ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle);
Eric Laurent6acd1d42017-01-04 14:23:29 -0800738 status = BAD_VALUE;
739 break;
740 }
Eric Laurent951f4552014-05-20 10:48:17 -0700741 }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700742 status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700743 } break;
744 default:
745 status = BAD_VALUE;
Eric Laurent951f4552014-05-20 10:48:17 -0700746 }
747
Eric Laurentb82e6b72019-11-22 17:25:04 -0800748 erasePatch(handle);
Eric Laurent951f4552014-05-20 10:48:17 -0700749 return status;
750}
751
Eric Laurentb82e6b72019-11-22 17:25:04 -0800752void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle) {
753 mPatches.erase(handle);
754 removeSoftwarePatchFromInsertedModules(handle);
755 mAudioFlinger.mDeviceEffectManager.releaseAudioPatch(handle);
756}
757
Eric Laurent951f4552014-05-20 10:48:17 -0700758/* List connected audio ports and they attributes */
759status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused,
760 struct audio_patch *patches __unused)
761{
Mikhail Naganovdea53042018-04-26 13:10:21 -0700762 ALOGV(__func__);
Eric Laurent951f4552014-05-20 10:48:17 -0700763 return NO_ERROR;
764}
765
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700766status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches(
767 audio_io_handle_t stream,
768 std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const
769{
770 for (const auto& module : mInsertedModules) {
771 if (module.second.streams.count(stream)) {
772 for (const auto& patchHandle : module.second.sw_patches) {
773 const auto& patch_iter = mPatches.find(patchHandle);
774 if (patch_iter != mPatches.end()) {
775 const Patch &patch = patch_iter->second;
776 patches->emplace_back(*this, patchHandle,
777 patch.mPlayback.const_thread()->id(),
778 patch.mRecord.const_thread()->id());
779 } else {
780 ALOGE("Stale patch handle in the cache: %d", patchHandle);
781 }
782 }
783 return OK;
784 }
785 }
786 // The stream is not associated with any of inserted modules.
787 return BAD_VALUE;
788}
789
790void AudioFlinger::PatchPanel::notifyStreamOpened(
791 AudioHwDevice *audioHwDevice, audio_io_handle_t stream)
792{
793 if (audioHwDevice->isInsert()) {
794 mInsertedModules[audioHwDevice->handle()].streams.insert(stream);
795 }
796}
797
798void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream)
799{
800 for (auto& module : mInsertedModules) {
801 module.second.streams.erase(stream);
802 }
803}
804
805AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700806{
807 if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr;
808 ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module);
809 if (index < 0) {
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700810 ALOGW("%s() bad hw module %d", __func__, module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700811 return nullptr;
812 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700813 return mAudioFlinger.mAudioHwDevs.valueAt(index);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700814}
815
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700816sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module)
Mikhail Naganov201369b2018-05-16 16:52:32 -0700817{
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700818 AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module);
819 return audioHwDevice ? audioHwDevice->hwDevice() : nullptr;
820}
821
822void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules(
823 audio_module_handle_t module, audio_patch_handle_t handle)
824{
825 mInsertedModules[module].sw_patches.insert(handle);
826}
827
828void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules(
829 audio_patch_handle_t handle)
830{
831 for (auto& module : mInsertedModules) {
832 module.second.sw_patches.erase(handle);
833 }
834}
835
836void AudioFlinger::PatchPanel::dump(int fd) const
837{
838 String8 patchPanelDump;
839 const char *indent = " ";
840
Mikhail Naganov201369b2018-05-16 16:52:32 -0700841 bool headerPrinted = false;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700842 for (const auto& iter : mPatches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800843 if (!headerPrinted) {
844 patchPanelDump += "\nPatches:\n";
845 headerPrinted = true;
Mikhail Naganov201369b2018-05-16 16:52:32 -0700846 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800847 patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700848 }
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700849
850 headerPrinted = false;
851 for (const auto& module : mInsertedModules) {
852 if (!module.second.streams.empty() || !module.second.sw_patches.empty()) {
853 if (!headerPrinted) {
854 patchPanelDump += "\nTracked inserted modules:\n";
855 headerPrinted = true;
856 }
857 String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first);
858 for (const auto& stream : module.second.streams) {
859 moduleDump.appendFormat("%d ", stream);
860 }
861 moduleDump.append("; SW Patches: ");
862 for (const auto& patch : module.second.sw_patches) {
863 moduleDump.appendFormat("%d ", patch);
864 }
865 patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string());
866 }
867 }
868
869 if (!patchPanelDump.isEmpty()) {
870 write(fd, patchPanelDump.string(), patchPanelDump.size());
Mikhail Naganov201369b2018-05-16 16:52:32 -0700871 }
872}
873
Glenn Kasten63238ef2015-03-02 15:50:29 -0800874} // namespace android